diff --git a/src/fr/devinsy/util/xml/XMLReader.java b/src/fr/devinsy/util/xml/XMLReader.java index 1fc277d..57dd535 100644 --- a/src/fr/devinsy/util/xml/XMLReader.java +++ b/src/fr/devinsy/util/xml/XMLReader.java @@ -1,5 +1,6 @@ /** * Copyright (C) 2013-2014 Christian Pierre MOMON + * Copyright (C) 2017 Christian Pierre MOMON * * This file is part of Devinsy-utils. * @@ -44,581 +45,573 @@ import fr.devinsy.util.xml.XMLTag.TagType; */ public class XMLReader { - private static final Logger logger = LoggerFactory.getLogger(XMLReader.class); + private static final Logger logger = LoggerFactory.getLogger(XMLReader.class); - protected XMLEventReader in; - private XMLEvent nextEvent; + protected XMLEventReader in; + private XMLEvent nextEvent; - /** + /** * */ - protected XMLReader() - { - this.in = null; - this.nextEvent = null; - } + protected XMLReader() + { + this.in = null; + this.nextEvent = null; + } - /** - * - * @param file - * @throws XMLStreamException - * @throws FileNotFoundException - */ - public XMLReader(final File file) throws FileNotFoundException, XMLStreamException - { - this.nextEvent = null; - XMLInputFactory factory = XMLInputFactory.newInstance(); - this.in = factory.createXMLEventReader(new FileInputStream(file), "UTF-8"); - } + /** + * + * @param file + * @throws XMLStreamException + * @throws FileNotFoundException + */ + public XMLReader(final File file) throws FileNotFoundException, XMLStreamException + { + this.nextEvent = null; + XMLInputFactory factory = XMLInputFactory.newInstance(); + this.in = factory.createXMLEventReader(new FileInputStream(file), "UTF-8"); + } - /** - * - * @param target - * @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 + */ + public XMLReader(final InputStream source) throws XMLStreamException + { + this.nextEvent = null; + XMLInputFactory factory = XMLInputFactory.newInstance(); + this.in = factory.createXMLEventReader(source); + } - /** - * - * @param target - * @throws XMLStreamException - */ - public XMLReader(final Reader source) throws XMLStreamException - { - this.nextEvent = null; - XMLInputFactory factory = XMLInputFactory.newInstance(); - this.in = factory.createXMLEventReader(source); - } + /** + * + * @param target + * @throws XMLStreamException + */ + public XMLReader(final Reader source) throws XMLStreamException + { + this.nextEvent = null; + XMLInputFactory factory = XMLInputFactory.newInstance(); + this.in = factory.createXMLEventReader(source); + } - /** - * @throws XMLStreamException - * - */ - public void close() - { - if (this.in != null) - { - try - { - this.in.close(); - } - catch (XMLStreamException exception) - { - exception.printStackTrace(); - } - } - } + /** + * @throws XMLStreamException + */ + public void close() throws XMLStreamException + { + if (this.in != null) + { + this.in.close(); + } + } - /** - * This methods does a premonition act. Useful to detect end of a list. - * - * @param label - * @return - * @throws XMLStreamException - */ - public boolean hasNextStartTag(final String label) throws XMLStreamException - { - boolean result; + /** + * This methods does a premonition act. Useful to detect end of a list. + * + * @param label + * @return + * @throws XMLStreamException + */ + public boolean hasNextStartTag(final String label) throws XMLStreamException + { + boolean result; - // Load next event. - if (this.nextEvent == null) - { - if (this.in.hasNext()) - { - this.nextEvent = this.in.nextEvent(); - } - } + // Load next event. + if (this.nextEvent == null) + { + if (this.in.hasNext()) + { + this.nextEvent = this.in.nextEvent(); + } + } - // Analyze next event. - if (this.nextEvent == null) - { - result = false; - } - else if ((this.nextEvent.isStartElement()) && (StringUtils.equals(this.nextEvent.asStartElement().getName().getLocalPart(), label))) - { - result = true; - } - else - { - result = false; - } + // Analyze next event. + if (this.nextEvent == null) + { + result = false; + } + else if ((this.nextEvent.isStartElement()) && (StringUtils.equals(this.nextEvent.asStartElement().getName().getLocalPart(), label))) + { + result = true; + } + else + { + result = false; + } - // - return result; - } + // + return result; + } - /** - * - * @param label - * @return - * @throws XMLBadFormatException - * @throws XMLStreamException - */ - public XMLTag readContentTag(final String label) throws XMLBadFormatException, XMLStreamException - { - XMLTag result; + /** + * + * @param label + * @return + * @throws XMLBadFormatException + * @throws XMLStreamException + */ + public XMLTag readContentTag(final String label) throws XMLBadFormatException, XMLStreamException + { + XMLTag result; - // - result = readTag(); + // + result = readTag(); - // - if (result == null) - { - throw new XMLBadFormatException("XML file ends prematurely, content tag [" + label + "] is expected."); - } - else if (result.getType() != TagType.CONTENT) - { - throw new XMLBadFormatException("Content tag [" + label + "] is missing."); - } - else if (!StringUtils.equals(label, result.getLabel())) - { - throw new XMLBadFormatException("Tag with label [" + label + "] is missing."); - } + // + if (result == null) + { + throw new XMLBadFormatException("XML file ends prematurely, content tag [" + label + "] is expected."); + } + else if (result.getType() != TagType.CONTENT) + { + throw new XMLBadFormatException("Content tag [" + label + "] is missing."); + } + else if (!StringUtils.equals(label, result.getLabel())) + { + throw new XMLBadFormatException("Tag with label [" + label + "] is missing."); + } - // - return result; - } + // + return result; + } - /** - * - * @param label - * @return - * @throws XMLStreamException - * @throws XMLBadFormatException - */ - public XMLTag readEndTag(final String label) throws XMLStreamException, XMLBadFormatException - { - XMLTag result; + /** + * + * @param label + * @return + * @throws XMLStreamException + * @throws XMLBadFormatException + */ + public XMLTag readEndTag(final String label) throws XMLStreamException, XMLBadFormatException + { + XMLTag result; - // - result = readTag(); + // + result = readTag(); - // - if (result == null) - { - throw new XMLBadFormatException("XML file ends prematurely, end tag [" + label + "] is expected."); - } - else if (result.getType() != TagType.END) - { - throw new XMLBadFormatException("End tag [" + label + "] is missing."); - } - else if (!StringUtils.equals(result.getLabel(), label)) - { - throw new XMLBadFormatException("Tag with label [" + label + "] is missing."); - } + // + if (result == null) + { + throw new XMLBadFormatException("XML file ends prematurely, end tag [" + label + "] is expected."); + } + else if (result.getType() != TagType.END) + { + throw new XMLBadFormatException("End tag [" + label + "] is missing."); + } + else if (!StringUtils.equals(result.getLabel(), label)) + { + throw new XMLBadFormatException("Tag with label [" + label + "] is missing."); + } - // - return result; - } + // + return result; + } - /** - * - * @param label - * @return - * @throws XMLStreamException - * @throws XMLBadFormatException - * @throws Exception - */ - public XMLTag readListTag(final String label) throws XMLStreamException, XMLBadFormatException - { - XMLTag result; + /** + * + * @param label + * @return + * @throws XMLStreamException + * @throws XMLBadFormatException + * @throws Exception + */ + public XMLTag readListTag(final String label) throws XMLStreamException, XMLBadFormatException + { + XMLTag result; - // - result = readTag(); + // + result = readTag(); - // - if (result == null) - { - throw new XMLBadFormatException("XML file ends prematurely, tag [" + label + "] is expected."); - } - 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())) - { - throw new XMLBadFormatException("Tag with label [" + label + "] is missing."); - } + // + if (result == null) + { + throw new XMLBadFormatException("XML file ends prematurely, tag [" + label + "] is expected."); + } + 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())) + { + throw new XMLBadFormatException("Tag with label [" + label + "] is missing."); + } - // - return result; - } + // + return result; + } - /** - * - * @param label - * @return - * @throws XMLStreamException - * @throws XMLBadFormatException - * @throws Exception - */ - public XMLTag readNullableContentTag(final String label) throws XMLStreamException, XMLBadFormatException - { - XMLTag result; + /** + * + * @param label + * @return + * @throws XMLStreamException + * @throws XMLBadFormatException + * @throws Exception + */ + public XMLTag readNullableContentTag(final String label) throws XMLStreamException, XMLBadFormatException + { + XMLTag result; - // - result = readTag(); + // + result = readTag(); - // - if (result == null) - { - throw new XMLBadFormatException("XML file ends prematurely, tag [" + label + "] is expected."); - } - 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)) - { - throw new XMLBadFormatException("Nullable content tag [" + label + "] is missing."); - } + // + if (result == null) + { + throw new XMLBadFormatException("XML file ends prematurely, tag [" + label + "] is expected."); + } + 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)) + { + throw new XMLBadFormatException("Nullable content tag [" + label + "] is missing."); + } - // - return result; - } + // + return result; + } - /** - * - * @param label - * @return - * @throws XMLStreamException - * @throws XMLBadFormatException - * @throws Exception - */ - public XMLTag readNullableStartTag(final String label) throws XMLStreamException, XMLBadFormatException - { - XMLTag result; + /** + * + * @param label + * @return + * @throws XMLStreamException + * @throws XMLBadFormatException + * @throws Exception + */ + public XMLTag readNullableStartTag(final String label) throws XMLStreamException, XMLBadFormatException + { + XMLTag result; - // - result = readTag(); + // + 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."); - } + // + 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; - } + // + return result; + } - /** - * - * @param label - * @return - * @throws XMLStreamException - * @throws XMLBadFormatException - * @throws Exception - */ - public XMLTag readStartTag(final String label) throws XMLStreamException, XMLBadFormatException - { - XMLTag result; + /** + * + * @param label + * @return + * @throws XMLStreamException + * @throws XMLBadFormatException + * @throws Exception + */ + public XMLTag readStartTag(final String label) throws XMLStreamException, XMLBadFormatException + { + XMLTag result; - // - result = readTag(); + // + result = readTag(); - // - if (result == null) - { - throw new XMLBadFormatException("XML file ends prematurely, start tag [" + label + "] is expected."); - } - else if (result.getType() != TagType.START) - { - throw new XMLBadFormatException("Start tag [" + label + "] is missing."); - } - else if (!StringUtils.equals(result.getLabel(), label)) - { - throw new XMLBadFormatException("Tag with label [" + label + "] is missing."); - } + // + if (result == null) + { + throw new XMLBadFormatException("XML file ends prematurely, start tag [" + label + "] is expected."); + } + else if (result.getType() != TagType.START) + { + 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; - } + // + return result; + } - /** - * Transducer graph : - *