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.FileReader;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
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
|
||||
* or any later version.
|
||||
*/
|
||||
public class XMLReader {
|
||||
public class XMLReader
|
||||
{
|
||||
|
||||
static private final Logger logger = LoggerFactory.getLogger(XMLReader.class);
|
||||
|
||||
|
@ -37,7 +39,8 @@ public class XMLReader {
|
|||
/**
|
||||
*
|
||||
*/
|
||||
protected XMLReader() {
|
||||
protected XMLReader()
|
||||
{
|
||||
this.in = null;
|
||||
this.nextEvent = null;
|
||||
}
|
||||
|
@ -49,7 +52,8 @@ public class XMLReader {
|
|||
* @throws FileNotFoundException
|
||||
* @throws UnsupportedEncodingException
|
||||
*/
|
||||
public XMLReader(final File file) throws FileNotFoundException, XMLStreamException {
|
||||
public XMLReader(final File file) throws FileNotFoundException, XMLStreamException
|
||||
{
|
||||
|
||||
this.nextEvent = null;
|
||||
XMLInputFactory factory = XMLInputFactory.newInstance();
|
||||
|
@ -62,7 +66,22 @@ public class XMLReader {
|
|||
* @throws XMLStreamException
|
||||
* @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;
|
||||
XMLInputFactory factory = XMLInputFactory.newInstance();
|
||||
|
@ -73,11 +92,16 @@ public class XMLReader {
|
|||
* @throws XMLStreamException
|
||||
*
|
||||
*/
|
||||
public void close() {
|
||||
if (this.in != null) {
|
||||
try {
|
||||
public void close()
|
||||
{
|
||||
if (this.in != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
this.in.close();
|
||||
} catch (XMLStreamException exception) {
|
||||
}
|
||||
catch (XMLStreamException exception)
|
||||
{
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
@ -90,22 +114,30 @@ public class XMLReader {
|
|||
* @return
|
||||
* @throws XMLStreamException
|
||||
*/
|
||||
public boolean hasNextStartTag(final String label) throws XMLStreamException {
|
||||
public boolean hasNextStartTag(final String label) throws XMLStreamException
|
||||
{
|
||||
boolean result;
|
||||
|
||||
// Load next event.
|
||||
if (this.nextEvent == null) {
|
||||
if (in.hasNext()) {
|
||||
if (this.nextEvent == null)
|
||||
{
|
||||
if (in.hasNext())
|
||||
{
|
||||
this.nextEvent = in.nextEvent();
|
||||
}
|
||||
}
|
||||
|
||||
// Analyze next event.
|
||||
if (this.nextEvent == null) {
|
||||
if (this.nextEvent == null)
|
||||
{
|
||||
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;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
|
||||
|
@ -120,18 +152,24 @@ public class XMLReader {
|
|||
* @throws XMLBadFormatException
|
||||
* @throws XMLStreamException
|
||||
*/
|
||||
public XMLTag readContentTag(final String label) throws XMLBadFormatException, XMLStreamException {
|
||||
public XMLTag readContentTag(final String label) throws XMLBadFormatException, XMLStreamException
|
||||
{
|
||||
XMLTag result;
|
||||
|
||||
//
|
||||
result = readTag();
|
||||
|
||||
//
|
||||
if (result == null) {
|
||||
if (result == null)
|
||||
{
|
||||
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.");
|
||||
} else if (!StringUtils.equals(label, result.getLabel())) {
|
||||
}
|
||||
else if (!StringUtils.equals(label, result.getLabel()))
|
||||
{
|
||||
throw new XMLBadFormatException("Tag with label [" + label + "] is missing.");
|
||||
}
|
||||
|
||||
|
@ -146,18 +184,24 @@ public class XMLReader {
|
|||
* @throws XMLStreamException
|
||||
* @throws XMLBadFormatException
|
||||
*/
|
||||
public XMLTag readEndTag(final String label) throws XMLStreamException, XMLBadFormatException {
|
||||
public XMLTag readEndTag(final String label) throws XMLStreamException, XMLBadFormatException
|
||||
{
|
||||
XMLTag result;
|
||||
|
||||
//
|
||||
result = readTag();
|
||||
|
||||
//
|
||||
if (result == null) {
|
||||
if (result == null)
|
||||
{
|
||||
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.");
|
||||
} else if (!StringUtils.equals(result.getLabel(), label)) {
|
||||
}
|
||||
else if (!StringUtils.equals(result.getLabel(), label))
|
||||
{
|
||||
throw new XMLBadFormatException("Tag with label [" + label + "] is missing.");
|
||||
}
|
||||
|
||||
|
@ -173,18 +217,24 @@ public class XMLReader {
|
|||
* @throws XMLBadFormatException
|
||||
* @throws Exception
|
||||
*/
|
||||
public XMLTag readListTag(final String label) throws XMLStreamException, XMLBadFormatException {
|
||||
public XMLTag readListTag(final String label) throws XMLStreamException, XMLBadFormatException
|
||||
{
|
||||
XMLTag result;
|
||||
|
||||
//
|
||||
result = readTag();
|
||||
|
||||
//
|
||||
if (result == null) {
|
||||
if (result == null)
|
||||
{
|
||||
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.");
|
||||
} else if (!StringUtils.equals(label, result.getLabel())) {
|
||||
}
|
||||
else if (!StringUtils.equals(label, result.getLabel()))
|
||||
{
|
||||
throw new XMLBadFormatException("Tag with label [" + label + "] is missing.");
|
||||
}
|
||||
|
||||
|
@ -200,18 +250,24 @@ public class XMLReader {
|
|||
* @throws XMLBadFormatException
|
||||
* @throws Exception
|
||||
*/
|
||||
public XMLTag readNullableContentTag(final String label) throws XMLStreamException, XMLBadFormatException {
|
||||
public XMLTag readNullableContentTag(final String label) throws XMLStreamException, XMLBadFormatException
|
||||
{
|
||||
XMLTag result;
|
||||
|
||||
//
|
||||
result = readTag();
|
||||
|
||||
//
|
||||
if (result == null) {
|
||||
if (result == null)
|
||||
{
|
||||
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.");
|
||||
} 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.");
|
||||
}
|
||||
|
||||
|
@ -227,18 +283,24 @@ public class XMLReader {
|
|||
* @throws XMLBadFormatException
|
||||
* @throws Exception
|
||||
*/
|
||||
public XMLTag readStartTag(final String label) throws XMLStreamException, XMLBadFormatException {
|
||||
public XMLTag readStartTag(final String label) throws XMLStreamException, XMLBadFormatException
|
||||
{
|
||||
XMLTag result;
|
||||
|
||||
//
|
||||
result = readTag();
|
||||
|
||||
//
|
||||
if (result == null) {
|
||||
if (result == null)
|
||||
{
|
||||
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.");
|
||||
} else if (!StringUtils.equals(result.getLabel(), label)) {
|
||||
}
|
||||
else if (!StringUtils.equals(result.getLabel(), label))
|
||||
{
|
||||
throw new XMLBadFormatException("Tag with label [" + label + "] is missing.");
|
||||
}
|
||||
|
||||
|
@ -264,7 +326,8 @@ public class XMLReader {
|
|||
* @throws XMLBadFormatException
|
||||
*
|
||||
*/
|
||||
public XMLTag readTag() throws XMLStreamException, XMLBadFormatException {
|
||||
public XMLTag readTag() throws XMLStreamException, XMLBadFormatException
|
||||
{
|
||||
XMLTag result;
|
||||
|
||||
int level = 1;
|
||||
|
@ -273,25 +336,36 @@ public class XMLReader {
|
|||
XMLAttributes attributesBuffer = null;
|
||||
QName nameBuffer = null;
|
||||
String contentBuffer = null;
|
||||
while (!ended) {
|
||||
while (!ended)
|
||||
{
|
||||
//
|
||||
XMLEvent event;
|
||||
if (this.nextEvent != null) {
|
||||
if (this.nextEvent != null)
|
||||
{
|
||||
event = this.nextEvent;
|
||||
this.nextEvent = null;
|
||||
} else if (in.hasNext()) {
|
||||
}
|
||||
else if (in.hasNext())
|
||||
{
|
||||
event = in.nextEvent();
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
event = null;
|
||||
}
|
||||
|
||||
if (event == null) {
|
||||
if (event == null)
|
||||
{
|
||||
result = null;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.debug("eventType=" + XMLTools.toString(event));
|
||||
switch (level) {
|
||||
switch (level)
|
||||
{
|
||||
case 1:
|
||||
switch (event.getEventType()) {
|
||||
switch (event.getEventType())
|
||||
{
|
||||
case XMLEvent.START_DOCUMENT:
|
||||
// START_DOCUMENT => START DOCUMENT TAG
|
||||
ended = true;
|
||||
|
@ -316,7 +390,8 @@ public class XMLReader {
|
|||
}
|
||||
break;
|
||||
case 2:
|
||||
switch (event.getEventType()) {
|
||||
switch (event.getEventType())
|
||||
{
|
||||
case XMLEvent.START_ELEMENT:
|
||||
// START_ELEMENT(X) + START_ELEMENT(Y) => <X><Y>
|
||||
// => START TAG
|
||||
|
@ -340,7 +415,8 @@ public class XMLReader {
|
|||
}
|
||||
break;
|
||||
case 3:
|
||||
switch (event.getEventType()) {
|
||||
switch (event.getEventType())
|
||||
{
|
||||
case XMLEvent.START_ELEMENT:
|
||||
// START_ELEMENT(X) + CHARACTERS(C) +
|
||||
// START_ELEMENT(Y) =>
|
||||
|
@ -385,16 +461,20 @@ public class XMLReader {
|
|||
* @throws XMLStreamException
|
||||
* @throws XMLBadFormatException
|
||||
*/
|
||||
public XMLTag readXMLFooter() throws XMLStreamException, XMLBadFormatException {
|
||||
public XMLTag readXMLFooter() throws XMLStreamException, XMLBadFormatException
|
||||
{
|
||||
XMLTag result;
|
||||
|
||||
//
|
||||
result = readTag();
|
||||
|
||||
//
|
||||
if (result == null) {
|
||||
if (result == null)
|
||||
{
|
||||
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.");
|
||||
}
|
||||
|
||||
|
@ -409,7 +489,8 @@ public class XMLReader {
|
|||
* @throws XMLStreamException
|
||||
* @throws XMLBadFormatException
|
||||
*/
|
||||
public XMLTag readXMLHeader() throws XMLStreamException, XMLBadFormatException {
|
||||
public XMLTag readXMLHeader() throws XMLStreamException, XMLBadFormatException
|
||||
{
|
||||
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.");
|
||||
} else if (result.getType() != TagType.HEADER) {
|
||||
}
|
||||
else if (result.getType() != TagType.HEADER)
|
||||
{
|
||||
throw new XMLBadFormatException("XML header is missing.");
|
||||
}
|
||||
|
||||
|
@ -432,16 +516,19 @@ public class XMLReader {
|
|||
* @param args
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void main(final String args[]) throws Exception {
|
||||
public static void main(final String args[]) throws Exception
|
||||
{
|
||||
|
||||
XMLInputFactory factory = XMLInputFactory.newInstance();
|
||||
XMLEventReader in = factory.createXMLEventReader(new FileReader("/home/cpm/C/Puck/Dev/Puck/test/TT/t3.puc"));
|
||||
|
||||
XMLEvent event;
|
||||
while (in.hasNext()) {
|
||||
while (in.hasNext())
|
||||
{
|
||||
event = in.nextEvent();
|
||||
|
||||
switch (event.getEventType()) {
|
||||
switch (event.getEventType())
|
||||
{
|
||||
case XMLEvent.ATTRIBUTE:
|
||||
System.out.println("ATTRIBUTE ");
|
||||
break;
|
||||
|
@ -485,9 +572,8 @@ public class XMLReader {
|
|||
System.out.println("START_DOCUMENT");
|
||||
break;
|
||||
case XMLEvent.START_ELEMENT:
|
||||
System.out.println("START_ELEMENT [name=" + event.asStartElement().getName() + "][namespaceURI="
|
||||
+ event.asStartElement().getName().getNamespaceURI() + "][prefix=" + event.asStartElement().getName().getPrefix() + "][localPart="
|
||||
+ event.asStartElement().getName().getLocalPart() + "]");
|
||||
System.out.println("START_ELEMENT [name=" + event.asStartElement().getName() + "][namespaceURI=" + event.asStartElement().getName().getNamespaceURI() + "][prefix="
|
||||
+ event.asStartElement().getName().getPrefix() + "][localPart=" + event.asStartElement().getName().getLocalPart() + "]");
|
||||
break;
|
||||
default:
|
||||
System.out.println("DEFAULT");
|
||||
|
|
Loading…
Reference in a new issue