From 323dc514961e5629e2720e0b4af9c68ecf6b91f6 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Sat, 19 Oct 2013 22:55:29 +0200 Subject: [PATCH] Add readNullableStartTag method. --- src/fr/devinsy/util/xml/XMLReader.java | 41 +++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/src/fr/devinsy/util/xml/XMLReader.java b/src/fr/devinsy/util/xml/XMLReader.java index e850f50..3258252 100644 --- a/src/fr/devinsy/util/xml/XMLReader.java +++ b/src/fr/devinsy/util/xml/XMLReader.java @@ -121,9 +121,9 @@ public class XMLReader // Load next event. if (this.nextEvent == null) { - if (in.hasNext()) + if (this.in.hasNext()) { - this.nextEvent = in.nextEvent(); + this.nextEvent = this.in.nextEvent(); } } @@ -275,6 +275,39 @@ public class XMLReader return result; } + /** + * + * @param label + * @return + * @throws XMLStreamException + * @throws XMLBadFormatException + * @throws Exception + */ + public XMLTag readNullableStartTag(final String label) throws XMLStreamException, XMLBadFormatException + { + XMLTag result; + + // + result = readTag(); + + // + if (result == null) + { + throw new XMLBadFormatException("XML file ends prematurely, start tag [" + label + "] is expected."); + } + else if ((result.getType() != TagType.START) && (result.getType() != TagType.EMPTY)) + { + throw new XMLBadFormatException("Start tag [" + label + "] is missing."); + } + else if (!StringUtils.equals(result.getLabel(), label)) + { + throw new XMLBadFormatException("Tag with label [" + label + "] is missing."); + } + + // + return result; + } + /** * * @param label @@ -345,9 +378,9 @@ public class XMLReader event = this.nextEvent; this.nextEvent = null; } - else if (in.hasNext()) + else if (this.in.hasNext()) { - event = in.nextEvent(); + event = this.in.nextEvent(); } else {