Indent.
This commit is contained in:
parent
8fc245b626
commit
19bc64bedc
1 changed files with 143 additions and 57 deletions
|
@ -5,6 +5,7 @@ import java.io.FileInputStream;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.io.Reader;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
|
||||||
import javax.xml.namespace.QName;
|
import javax.xml.namespace.QName;
|
||||||
|
@ -27,7 +28,8 @@ import fr.devinsy.util.xml.XMLTag.TagType;
|
||||||
* Public License as published by the Free Software Foundation version 3
|
* Public License as published by the Free Software Foundation version 3
|
||||||
* or any later version.
|
* or any later version.
|
||||||
*/
|
*/
|
||||||
public class XMLReader {
|
public class XMLReader
|
||||||
|
{
|
||||||
|
|
||||||
static private final Logger logger = LoggerFactory.getLogger(XMLReader.class);
|
static private final Logger logger = LoggerFactory.getLogger(XMLReader.class);
|
||||||
|
|
||||||
|
@ -37,7 +39,8 @@ public class XMLReader {
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
protected XMLReader() {
|
protected XMLReader()
|
||||||
|
{
|
||||||
this.in = null;
|
this.in = null;
|
||||||
this.nextEvent = null;
|
this.nextEvent = null;
|
||||||
}
|
}
|
||||||
|
@ -49,7 +52,8 @@ public class XMLReader {
|
||||||
* @throws FileNotFoundException
|
* @throws FileNotFoundException
|
||||||
* @throws UnsupportedEncodingException
|
* @throws UnsupportedEncodingException
|
||||||
*/
|
*/
|
||||||
public XMLReader(final File file) throws FileNotFoundException, XMLStreamException {
|
public XMLReader(final File file) throws FileNotFoundException, XMLStreamException
|
||||||
|
{
|
||||||
|
|
||||||
this.nextEvent = null;
|
this.nextEvent = null;
|
||||||
XMLInputFactory factory = XMLInputFactory.newInstance();
|
XMLInputFactory factory = XMLInputFactory.newInstance();
|
||||||
|
@ -62,7 +66,22 @@ public class XMLReader {
|
||||||
* @throws XMLStreamException
|
* @throws XMLStreamException
|
||||||
* @throws UnsupportedEncodingException
|
* @throws UnsupportedEncodingException
|
||||||
*/
|
*/
|
||||||
public XMLReader(final InputStream source) throws XMLStreamException {
|
public XMLReader(final InputStream source) throws XMLStreamException
|
||||||
|
{
|
||||||
|
|
||||||
|
this.nextEvent = null;
|
||||||
|
XMLInputFactory factory = XMLInputFactory.newInstance();
|
||||||
|
this.in = factory.createXMLEventReader(source);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param target
|
||||||
|
* @throws XMLStreamException
|
||||||
|
* @throws UnsupportedEncodingException
|
||||||
|
*/
|
||||||
|
public XMLReader(final Reader source) throws XMLStreamException
|
||||||
|
{
|
||||||
|
|
||||||
this.nextEvent = null;
|
this.nextEvent = null;
|
||||||
XMLInputFactory factory = XMLInputFactory.newInstance();
|
XMLInputFactory factory = XMLInputFactory.newInstance();
|
||||||
|
@ -73,11 +92,16 @@ public class XMLReader {
|
||||||
* @throws XMLStreamException
|
* @throws XMLStreamException
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public void close() {
|
public void close()
|
||||||
if (this.in != null) {
|
{
|
||||||
try {
|
if (this.in != null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
this.in.close();
|
this.in.close();
|
||||||
} catch (XMLStreamException exception) {
|
}
|
||||||
|
catch (XMLStreamException exception)
|
||||||
|
{
|
||||||
exception.printStackTrace();
|
exception.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -90,22 +114,30 @@ public class XMLReader {
|
||||||
* @return
|
* @return
|
||||||
* @throws XMLStreamException
|
* @throws XMLStreamException
|
||||||
*/
|
*/
|
||||||
public boolean hasNextStartTag(final String label) throws XMLStreamException {
|
public boolean hasNextStartTag(final String label) throws XMLStreamException
|
||||||
|
{
|
||||||
boolean result;
|
boolean result;
|
||||||
|
|
||||||
// Load next event.
|
// Load next event.
|
||||||
if (this.nextEvent == null) {
|
if (this.nextEvent == null)
|
||||||
if (in.hasNext()) {
|
{
|
||||||
|
if (in.hasNext())
|
||||||
|
{
|
||||||
this.nextEvent = in.nextEvent();
|
this.nextEvent = in.nextEvent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Analyze next event.
|
// Analyze next event.
|
||||||
if (this.nextEvent == null) {
|
if (this.nextEvent == null)
|
||||||
|
{
|
||||||
result = false;
|
result = false;
|
||||||
} else if ((this.nextEvent.isStartElement()) && (StringUtils.equals(this.nextEvent.asStartElement().getName().getLocalPart(), label))) {
|
}
|
||||||
|
else if ((this.nextEvent.isStartElement()) && (StringUtils.equals(this.nextEvent.asStartElement().getName().getLocalPart(), label)))
|
||||||
|
{
|
||||||
result = true;
|
result = true;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
result = false;
|
result = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,18 +152,24 @@ public class XMLReader {
|
||||||
* @throws XMLBadFormatException
|
* @throws XMLBadFormatException
|
||||||
* @throws XMLStreamException
|
* @throws XMLStreamException
|
||||||
*/
|
*/
|
||||||
public XMLTag readContentTag(final String label) throws XMLBadFormatException, XMLStreamException {
|
public XMLTag readContentTag(final String label) throws XMLBadFormatException, XMLStreamException
|
||||||
|
{
|
||||||
XMLTag result;
|
XMLTag result;
|
||||||
|
|
||||||
//
|
//
|
||||||
result = readTag();
|
result = readTag();
|
||||||
|
|
||||||
//
|
//
|
||||||
if (result == null) {
|
if (result == null)
|
||||||
|
{
|
||||||
throw new XMLBadFormatException("XML file ends prematurely, content tag [" + label + "] is expected.");
|
throw new XMLBadFormatException("XML file ends prematurely, content tag [" + label + "] is expected.");
|
||||||
} else if (result.getType() != TagType.CONTENT) {
|
}
|
||||||
|
else if (result.getType() != TagType.CONTENT)
|
||||||
|
{
|
||||||
throw new XMLBadFormatException("Content tag [" + label + "] is missing.");
|
throw new XMLBadFormatException("Content tag [" + label + "] is missing.");
|
||||||
} else if (!StringUtils.equals(label, result.getLabel())) {
|
}
|
||||||
|
else if (!StringUtils.equals(label, result.getLabel()))
|
||||||
|
{
|
||||||
throw new XMLBadFormatException("Tag with label [" + label + "] is missing.");
|
throw new XMLBadFormatException("Tag with label [" + label + "] is missing.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,18 +184,24 @@ public class XMLReader {
|
||||||
* @throws XMLStreamException
|
* @throws XMLStreamException
|
||||||
* @throws XMLBadFormatException
|
* @throws XMLBadFormatException
|
||||||
*/
|
*/
|
||||||
public XMLTag readEndTag(final String label) throws XMLStreamException, XMLBadFormatException {
|
public XMLTag readEndTag(final String label) throws XMLStreamException, XMLBadFormatException
|
||||||
|
{
|
||||||
XMLTag result;
|
XMLTag result;
|
||||||
|
|
||||||
//
|
//
|
||||||
result = readTag();
|
result = readTag();
|
||||||
|
|
||||||
//
|
//
|
||||||
if (result == null) {
|
if (result == null)
|
||||||
|
{
|
||||||
throw new XMLBadFormatException("XML file ends prematurely, end tag [" + label + "] is expected.");
|
throw new XMLBadFormatException("XML file ends prematurely, end tag [" + label + "] is expected.");
|
||||||
} else if (result.getType() != TagType.END) {
|
}
|
||||||
|
else if (result.getType() != TagType.END)
|
||||||
|
{
|
||||||
throw new XMLBadFormatException("End tag [" + label + "] is missing.");
|
throw new XMLBadFormatException("End tag [" + label + "] is missing.");
|
||||||
} else if (!StringUtils.equals(result.getLabel(), label)) {
|
}
|
||||||
|
else if (!StringUtils.equals(result.getLabel(), label))
|
||||||
|
{
|
||||||
throw new XMLBadFormatException("Tag with label [" + label + "] is missing.");
|
throw new XMLBadFormatException("Tag with label [" + label + "] is missing.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,18 +217,24 @@ public class XMLReader {
|
||||||
* @throws XMLBadFormatException
|
* @throws XMLBadFormatException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public XMLTag readListTag(final String label) throws XMLStreamException, XMLBadFormatException {
|
public XMLTag readListTag(final String label) throws XMLStreamException, XMLBadFormatException
|
||||||
|
{
|
||||||
XMLTag result;
|
XMLTag result;
|
||||||
|
|
||||||
//
|
//
|
||||||
result = readTag();
|
result = readTag();
|
||||||
|
|
||||||
//
|
//
|
||||||
if (result == null) {
|
if (result == null)
|
||||||
|
{
|
||||||
throw new XMLBadFormatException("XML file ends prematurely, tag [" + label + "] is expected.");
|
throw new XMLBadFormatException("XML file ends prematurely, tag [" + label + "] is expected.");
|
||||||
} else if ((result.getType() != TagType.START) && (result.getType() != TagType.EMPTY)) {
|
}
|
||||||
|
else if ((result.getType() != TagType.START) && (result.getType() != TagType.EMPTY))
|
||||||
|
{
|
||||||
throw new XMLBadFormatException("List tag [" + label + "] is missing.");
|
throw new XMLBadFormatException("List tag [" + label + "] is missing.");
|
||||||
} else if (!StringUtils.equals(label, result.getLabel())) {
|
}
|
||||||
|
else if (!StringUtils.equals(label, result.getLabel()))
|
||||||
|
{
|
||||||
throw new XMLBadFormatException("Tag with label [" + label + "] is missing.");
|
throw new XMLBadFormatException("Tag with label [" + label + "] is missing.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -200,18 +250,24 @@ public class XMLReader {
|
||||||
* @throws XMLBadFormatException
|
* @throws XMLBadFormatException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public XMLTag readNullableContentTag(final String label) throws XMLStreamException, XMLBadFormatException {
|
public XMLTag readNullableContentTag(final String label) throws XMLStreamException, XMLBadFormatException
|
||||||
|
{
|
||||||
XMLTag result;
|
XMLTag result;
|
||||||
|
|
||||||
//
|
//
|
||||||
result = readTag();
|
result = readTag();
|
||||||
|
|
||||||
//
|
//
|
||||||
if (result == null) {
|
if (result == null)
|
||||||
|
{
|
||||||
throw new XMLBadFormatException("XML file ends prematurely, tag [" + label + "] is expected.");
|
throw new XMLBadFormatException("XML file ends prematurely, tag [" + label + "] is expected.");
|
||||||
} else if (!StringUtils.equals(label, result.getLabel())) {
|
}
|
||||||
|
else if (!StringUtils.equals(label, result.getLabel()))
|
||||||
|
{
|
||||||
throw new XMLBadFormatException("Nullable content tag [" + label + "] is missing.");
|
throw new XMLBadFormatException("Nullable content tag [" + label + "] is missing.");
|
||||||
} else if ((result.getType() != TagType.EMPTY) && (result.getType() != TagType.CONTENT)) {
|
}
|
||||||
|
else if ((result.getType() != TagType.EMPTY) && (result.getType() != TagType.CONTENT))
|
||||||
|
{
|
||||||
throw new XMLBadFormatException("Nullable content tag [" + label + "] is missing.");
|
throw new XMLBadFormatException("Nullable content tag [" + label + "] is missing.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -227,18 +283,24 @@ public class XMLReader {
|
||||||
* @throws XMLBadFormatException
|
* @throws XMLBadFormatException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public XMLTag readStartTag(final String label) throws XMLStreamException, XMLBadFormatException {
|
public XMLTag readStartTag(final String label) throws XMLStreamException, XMLBadFormatException
|
||||||
|
{
|
||||||
XMLTag result;
|
XMLTag result;
|
||||||
|
|
||||||
//
|
//
|
||||||
result = readTag();
|
result = readTag();
|
||||||
|
|
||||||
//
|
//
|
||||||
if (result == null) {
|
if (result == null)
|
||||||
|
{
|
||||||
throw new XMLBadFormatException("XML file ends prematurely, start tag [" + label + "] is expected.");
|
throw new XMLBadFormatException("XML file ends prematurely, start tag [" + label + "] is expected.");
|
||||||
} else if (result.getType() != TagType.START) {
|
}
|
||||||
|
else if (result.getType() != TagType.START)
|
||||||
|
{
|
||||||
throw new XMLBadFormatException("Start tag [" + label + "] is missing.");
|
throw new XMLBadFormatException("Start tag [" + label + "] is missing.");
|
||||||
} else if (!StringUtils.equals(result.getLabel(), label)) {
|
}
|
||||||
|
else if (!StringUtils.equals(result.getLabel(), label))
|
||||||
|
{
|
||||||
throw new XMLBadFormatException("Tag with label [" + label + "] is missing.");
|
throw new XMLBadFormatException("Tag with label [" + label + "] is missing.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -264,7 +326,8 @@ public class XMLReader {
|
||||||
* @throws XMLBadFormatException
|
* @throws XMLBadFormatException
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public XMLTag readTag() throws XMLStreamException, XMLBadFormatException {
|
public XMLTag readTag() throws XMLStreamException, XMLBadFormatException
|
||||||
|
{
|
||||||
XMLTag result;
|
XMLTag result;
|
||||||
|
|
||||||
int level = 1;
|
int level = 1;
|
||||||
|
@ -273,25 +336,36 @@ public class XMLReader {
|
||||||
XMLAttributes attributesBuffer = null;
|
XMLAttributes attributesBuffer = null;
|
||||||
QName nameBuffer = null;
|
QName nameBuffer = null;
|
||||||
String contentBuffer = null;
|
String contentBuffer = null;
|
||||||
while (!ended) {
|
while (!ended)
|
||||||
|
{
|
||||||
//
|
//
|
||||||
XMLEvent event;
|
XMLEvent event;
|
||||||
if (this.nextEvent != null) {
|
if (this.nextEvent != null)
|
||||||
|
{
|
||||||
event = this.nextEvent;
|
event = this.nextEvent;
|
||||||
this.nextEvent = null;
|
this.nextEvent = null;
|
||||||
} else if (in.hasNext()) {
|
}
|
||||||
|
else if (in.hasNext())
|
||||||
|
{
|
||||||
event = in.nextEvent();
|
event = in.nextEvent();
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
event = null;
|
event = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event == null) {
|
if (event == null)
|
||||||
|
{
|
||||||
result = null;
|
result = null;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
logger.debug("eventType=" + XMLTools.toString(event));
|
logger.debug("eventType=" + XMLTools.toString(event));
|
||||||
switch (level) {
|
switch (level)
|
||||||
|
{
|
||||||
case 1:
|
case 1:
|
||||||
switch (event.getEventType()) {
|
switch (event.getEventType())
|
||||||
|
{
|
||||||
case XMLEvent.START_DOCUMENT:
|
case XMLEvent.START_DOCUMENT:
|
||||||
// START_DOCUMENT => START DOCUMENT TAG
|
// START_DOCUMENT => START DOCUMENT TAG
|
||||||
ended = true;
|
ended = true;
|
||||||
|
@ -316,7 +390,8 @@ public class XMLReader {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
switch (event.getEventType()) {
|
switch (event.getEventType())
|
||||||
|
{
|
||||||
case XMLEvent.START_ELEMENT:
|
case XMLEvent.START_ELEMENT:
|
||||||
// START_ELEMENT(X) + START_ELEMENT(Y) => <X><Y>
|
// START_ELEMENT(X) + START_ELEMENT(Y) => <X><Y>
|
||||||
// => START TAG
|
// => START TAG
|
||||||
|
@ -340,7 +415,8 @@ public class XMLReader {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
switch (event.getEventType()) {
|
switch (event.getEventType())
|
||||||
|
{
|
||||||
case XMLEvent.START_ELEMENT:
|
case XMLEvent.START_ELEMENT:
|
||||||
// START_ELEMENT(X) + CHARACTERS(C) +
|
// START_ELEMENT(X) + CHARACTERS(C) +
|
||||||
// START_ELEMENT(Y) =>
|
// START_ELEMENT(Y) =>
|
||||||
|
@ -385,16 +461,20 @@ public class XMLReader {
|
||||||
* @throws XMLStreamException
|
* @throws XMLStreamException
|
||||||
* @throws XMLBadFormatException
|
* @throws XMLBadFormatException
|
||||||
*/
|
*/
|
||||||
public XMLTag readXMLFooter() throws XMLStreamException, XMLBadFormatException {
|
public XMLTag readXMLFooter() throws XMLStreamException, XMLBadFormatException
|
||||||
|
{
|
||||||
XMLTag result;
|
XMLTag result;
|
||||||
|
|
||||||
//
|
//
|
||||||
result = readTag();
|
result = readTag();
|
||||||
|
|
||||||
//
|
//
|
||||||
if (result == null) {
|
if (result == null)
|
||||||
|
{
|
||||||
throw new XMLBadFormatException("XML file ends prematurely, end document event is expected.");
|
throw new XMLBadFormatException("XML file ends prematurely, end document event is expected.");
|
||||||
} else if (result.getType() != TagType.FOOTER) {
|
}
|
||||||
|
else if (result.getType() != TagType.FOOTER)
|
||||||
|
{
|
||||||
throw new XMLBadFormatException("End document tag is missing.");
|
throw new XMLBadFormatException("End document tag is missing.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -409,7 +489,8 @@ public class XMLReader {
|
||||||
* @throws XMLStreamException
|
* @throws XMLStreamException
|
||||||
* @throws XMLBadFormatException
|
* @throws XMLBadFormatException
|
||||||
*/
|
*/
|
||||||
public XMLTag readXMLHeader() throws XMLStreamException, XMLBadFormatException {
|
public XMLTag readXMLHeader() throws XMLStreamException, XMLBadFormatException
|
||||||
|
{
|
||||||
XMLTag result;
|
XMLTag result;
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -417,9 +498,12 @@ public class XMLReader {
|
||||||
|
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
if (result == null) {
|
if (result == null)
|
||||||
|
{
|
||||||
throw new XMLBadFormatException("XML file ends prematurely, start document event is expected.");
|
throw new XMLBadFormatException("XML file ends prematurely, start document event is expected.");
|
||||||
} else if (result.getType() != TagType.HEADER) {
|
}
|
||||||
|
else if (result.getType() != TagType.HEADER)
|
||||||
|
{
|
||||||
throw new XMLBadFormatException("XML header is missing.");
|
throw new XMLBadFormatException("XML header is missing.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -432,16 +516,19 @@ public class XMLReader {
|
||||||
* @param args
|
* @param args
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static void main(final String args[]) throws Exception {
|
public static void main(final String args[]) throws Exception
|
||||||
|
{
|
||||||
|
|
||||||
XMLInputFactory factory = XMLInputFactory.newInstance();
|
XMLInputFactory factory = XMLInputFactory.newInstance();
|
||||||
XMLEventReader in = factory.createXMLEventReader(new FileReader("/home/cpm/C/Puck/Dev/Puck/test/TT/t3.puc"));
|
XMLEventReader in = factory.createXMLEventReader(new FileReader("/home/cpm/C/Puck/Dev/Puck/test/TT/t3.puc"));
|
||||||
|
|
||||||
XMLEvent event;
|
XMLEvent event;
|
||||||
while (in.hasNext()) {
|
while (in.hasNext())
|
||||||
|
{
|
||||||
event = in.nextEvent();
|
event = in.nextEvent();
|
||||||
|
|
||||||
switch (event.getEventType()) {
|
switch (event.getEventType())
|
||||||
|
{
|
||||||
case XMLEvent.ATTRIBUTE:
|
case XMLEvent.ATTRIBUTE:
|
||||||
System.out.println("ATTRIBUTE ");
|
System.out.println("ATTRIBUTE ");
|
||||||
break;
|
break;
|
||||||
|
@ -485,9 +572,8 @@ public class XMLReader {
|
||||||
System.out.println("START_DOCUMENT");
|
System.out.println("START_DOCUMENT");
|
||||||
break;
|
break;
|
||||||
case XMLEvent.START_ELEMENT:
|
case XMLEvent.START_ELEMENT:
|
||||||
System.out.println("START_ELEMENT [name=" + event.asStartElement().getName() + "][namespaceURI="
|
System.out.println("START_ELEMENT [name=" + event.asStartElement().getName() + "][namespaceURI=" + event.asStartElement().getName().getNamespaceURI() + "][prefix="
|
||||||
+ event.asStartElement().getName().getNamespaceURI() + "][prefix=" + event.asStartElement().getName().getPrefix() + "][localPart="
|
+ event.asStartElement().getName().getPrefix() + "][localPart=" + event.asStartElement().getName().getLocalPart() + "]");
|
||||||
+ event.asStartElement().getName().getLocalPart() + "]");
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
System.out.println("DEFAULT");
|
System.out.println("DEFAULT");
|
||||||
|
|
Loading…
Reference in a new issue