Example usage for javax.xml.stream XMLStreamReader hasNext

List of usage examples for javax.xml.stream XMLStreamReader hasNext

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamReader hasNext.

Prototype

public boolean hasNext() throws XMLStreamException;

Source Link

Document

Returns true if there are more parsing events and false if there are no more events.

Usage

From source file:org.talend.dataprep.schema.xls.XlsUtils.java

/**
 * read workbook xml spec to get non hidden sheets
 *
 * @param inputStream//from w ww.  j  a  va 2  s  .com
 * @return
 */
public static List<String> getActiveSheetsFromWorkbookSpec(InputStream inputStream) throws XMLStreamException {
    // If doesn't support mark, wrap up
    if (!inputStream.markSupported()) {
        inputStream = new PushbackInputStream(inputStream, 8);
    }
    XMLStreamReader streamReader = XML_INPUT_FACTORY.createXMLStreamReader(inputStream);
    try {
        /*
         *
         * <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <workbook
         * xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
         * xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"> <fileVersion appName="xl"
         * lastEdited="5" lowestEdited="5" rupBuild="9303" codeName="{8C4F1C90-05EB-6A55-5F09-09C24B55AC0B}"/>
         * <workbookPr codeName="ThisWorkbook" defaultThemeVersion="124226"/> <bookViews> <workbookView xWindow="0"
         * yWindow="732" windowWidth="22980" windowHeight="8868" firstSheet="1" activeTab="8"/> </bookViews>
         * <sheets> <sheet name="formdata" sheetId="4" state="hidden" r:id="rId1"/> <sheet name="MONDAY" sheetId="1"
         * r:id="rId2"/> <sheet name="TUESDAY" sheetId="8" r:id="rId3"/> <sheet name="WEDNESDAY" sheetId="10"
         * r:id="rId4"/> <sheet name="THURSDAY" sheetId="11" r:id="rId5"/> <sheet name="FRIDAY" sheetId="12"
         * r:id="rId6"/> <sheet name="SATURDAY" sheetId="13" r:id="rId7"/> <sheet name="SUNDAY" sheetId="14"
         * r:id="rId8"/> <sheet name="WEEK SUMMARY" sheetId="15" r:id="rId9"/> </sheets>
         *
         */
        // we only want sheets not with state=hidden

        List<String> names = new ArrayList<>();

        while (streamReader.hasNext()) {
            switch (streamReader.next()) {
            case START_ELEMENT:
                if (StringUtils.equals(streamReader.getLocalName(), "sheet")) {
                    Map<String, String> attributesValues = getAttributesNameValue(streamReader);
                    if (!attributesValues.isEmpty()) {
                        String sheetState = attributesValues.get("state");
                        if (!StringUtils.equals(sheetState, "hidden")) {
                            String sheetName = attributesValues.get("name");
                            names.add(sheetName);
                        }
                    }
                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                if (StringUtils.equals(streamReader.getLocalName(), "sheets")) {
                    // shortcut to stop parsing
                    return names;
                }
                break;
            default:
                // no op
            }
        }
        return names;
    } finally {
        if (streamReader != null) {
            streamReader.close();
        }
    }
}

From source file:org.talend.dataprep.schema.xls.XlsUtils.java

/**
 *
 * @param inputStream xls sheet inputStream
 * @return the column number from reading sheet metadata or -1 if unknown
 * @throws XMLStreamException//ww  w.  j a  v  a2  s. co m
 * @throws IOException
 */
public static int getColumnsNumber(InputStream inputStream) throws XMLStreamException, IOException {
    // If doesn't support mark, wrap up
    if (!inputStream.markSupported()) {
        inputStream = new PushbackInputStream(inputStream, 8);
    }

    int colNumber = 0;

    // TDP-1781 xlsx files may not containing dimension so we fallback to col element number

    XMLStreamReader streamReader = XML_INPUT_FACTORY.createXMLStreamReader(inputStream);
    try {
        while (streamReader.hasNext()) {
            switch (streamReader.next()) {
            case START_ELEMENT:
                if (StringUtils.equals(streamReader.getLocalName(), "dimension")) {
                    Map<String, String> attributesValues = getAttributesNameValue(streamReader);
                    if (!attributesValues.isEmpty()) {
                        return getColumnsNumberFromDimension(attributesValues.get("ref"));
                    }
                }
                if (StringUtils.equals(streamReader.getLocalName(), "col")) {
                    colNumber++;
                }
                break;
            case END_ELEMENT:
                if (StringUtils.equals(streamReader.getLocalName(), "cols")) {
                    return colNumber;
                }
            default:
                // no op
            }
        }
    } finally {
        if (streamReader != null) {
            streamReader.close();
        }
    }
    return -1;
}

From source file:org.tobarsegais.webapp.data.Index.java

public static Index read(String bundle, XMLStreamReader reader) throws XMLStreamException {
    while (reader.hasNext() && !reader.isStartElement()) {
        reader.next();/*from w  w  w.j  ava  2 s . co  m*/
    }
    if (reader.getEventType() != XMLStreamConstants.START_ELEMENT) {
        throw new IllegalStateException("Expecting a start element");
    }
    if (!"index".equals(reader.getLocalName())) {
        throw new IllegalStateException("Expecting a <index> element, found a <" + reader.getLocalName() + ">");
    }
    List<IndexEntry> entries = new ArrayList<IndexEntry>();
    int depth = 0;
    while (reader.hasNext() && depth >= 0) {
        switch (reader.next()) {
        case XMLStreamConstants.START_ELEMENT:
            if (depth == 0 && "entry".equals(reader.getLocalName())) {
                entries.add(IndexEntry.read(bundle, Collections.<String>emptyList(), reader));
            } else {
                depth++;
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            depth--;
            break;
        }
    }
    return new Index(entries);
}

From source file:org.tobarsegais.webapp.data.Plugin.java

public static Plugin read(XMLStreamReader reader) throws XMLStreamException {
    while (reader.hasNext() && !reader.isStartElement()) {
        reader.next();/*from   www .  j  av a  2  s .co m*/
    }
    if (reader.getEventType() != XMLStreamConstants.START_ELEMENT) {
        throw new IllegalStateException("Expecting a start element");
    }
    if (!"plugin".equals(reader.getLocalName())) {
        throw new IllegalStateException("Expecting a <plugin> element");
    }
    String name = reader.getAttributeValue(null, "name");
    String id = reader.getAttributeValue(null, "id");
    String version = reader.getAttributeValue(null, "version");
    String providerName = reader.getAttributeValue(null, "provider-name");
    List<Extension> extensions = new ArrayList<Extension>();
    int depth = 0;
    while (reader.hasNext() && depth >= 0) {
        switch (reader.next()) {
        case XMLStreamConstants.START_ELEMENT:
            if (depth == 0 && "extension".equals(reader.getLocalName())) {
                extensions.add(Extension.read(reader));
            } else {
                depth++;
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            depth--;
            break;
        }
    }
    return new Plugin(name, id, version, providerName, extensions);
}

From source file:org.tobarsegais.webapp.data.Toc.java

public static Toc read(XMLStreamReader reader) throws XMLStreamException {
    while (reader.hasNext() && !reader.isStartElement()) {
        reader.next();/* w  w  w . j a  v a 2s  .co m*/
    }
    if (reader.getEventType() != XMLStreamConstants.START_ELEMENT) {
        throw new IllegalStateException("Expecting a start element");
    }
    if (!"toc".equals(reader.getLocalName())) {
        throw new IllegalStateException("Expecting a <toc> element");
    }
    String label = reader.getAttributeValue(null, "label");
    String topic = reader.getAttributeValue(null, "topic");
    List<Topic> topics = new ArrayList<Topic>();
    int depth = 0;
    while (reader.hasNext() && depth >= 0) {
        switch (reader.next()) {
        case XMLStreamConstants.START_ELEMENT:
            if (depth == 0 && "topic".equals(reader.getLocalName())) {
                topics.add(Topic.read(reader));
            } else {
                depth++;
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            depth--;
            break;
        }
    }
    return new Toc(label, topic, topics);
}

From source file:org.unitedinternet.cosmo.model.text.XhtmlCollectionFormat.java

public CollectionItem parse(String source, EntityFactory entityFactory) throws ParseException {
    CollectionItem collection = entityFactory.createCollection();

    try {//from ww  w.  j a v a 2s .c o m
        if (source == null) {
            throw new ParseException("Source has no XML data", -1);
        }
        StringReader sr = new StringReader(source);
        XMLStreamReader reader = createXmlReader(sr);

        boolean inCollection = false;
        while (reader.hasNext()) {
            reader.next();
            if (!reader.isStartElement()) {
                continue;
            }

            if (hasClass(reader, "collection")) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("found collection element");
                }
                inCollection = true;
                continue;
            }

            if (inCollection && hasClass(reader, "name")) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("found name element");
                }

                String name = reader.getElementText();
                if (StringUtils.isBlank(name)) {
                    throw new ParseException("Empty name not allowed",
                            reader.getLocation().getCharacterOffset());
                }
                collection.setDisplayName(name);

                continue;
            }

            if (inCollection && hasClass(reader, "uuid")) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("found uuid element");
                }

                String uuid = reader.getElementText();
                if (StringUtils.isBlank(uuid)) {
                    throw new ParseException("Empty uuid not allowed",
                            reader.getLocation().getCharacterOffset());
                }
                collection.setUid(uuid);

                continue;
            }
        }

        reader.close();
    } catch (XMLStreamException e) {
        handleXmlException("Error reading XML", e);
    }

    return collection;
}

From source file:org.unitedinternet.cosmo.model.text.XhtmlPreferenceFormat.java

public Preference parse(String source, EntityFactory entityFactory) throws ParseException {
    Preference pref = entityFactory.createPreference();

    try {//from w ww.j  a v  a 2  s.  c  om
        if (source == null) {
            throw new ParseException("Source has no XML data", -1);
        }
        StringReader sr = new StringReader(source);
        XMLStreamReader reader = createXmlReader(sr);

        boolean inPreference = false;
        while (reader.hasNext()) {
            reader.next();
            if (!reader.isStartElement()) {
                continue;
            }

            if (hasClass(reader, "preference")) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("found preference element");
                }
                inPreference = true;
                continue;
            }

            if (inPreference && hasClass(reader, "key")) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("found key element");
                }

                String key = reader.getElementText();
                if (StringUtils.isBlank(key)) {
                    handleParseException("Key element must not be empty", reader);
                }
                pref.setKey(key);

                continue;
            }

            if (inPreference && hasClass(reader, "value")) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("found value element");
                }

                String value = reader.getElementText();
                if (StringUtils.isBlank(value)) {
                    value = "";
                }
                pref.setValue(value);

                continue;
            }
        }

        reader.close();
    } catch (XMLStreamException e) {
        handleXmlException("Error reading XML", e);
    }

    return pref;
}

From source file:org.unitedinternet.cosmo.model.text.XhtmlSubscriptionFormat.java

public CollectionSubscription parse(String source, EntityFactory entityFactory) throws ParseException {
    CollectionSubscription sub = entityFactory.createCollectionSubscription();

    try {//from ww w. j  av  a2  s  .c  om
        if (source == null) {
            throw new ParseException("Source has no XML data", -1);
        }
        StringReader sr = new StringReader(source);
        XMLStreamReader reader = createXmlReader(sr);

        boolean inLocalSub = false;
        boolean inCollection = false;
        boolean inTicket = false;
        while (reader.hasNext()) {
            reader.next();
            if (!reader.isStartElement()) {
                continue;
            }

            if (hasClass(reader, "local-subscription")) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("found local-subscription element");
                }
                inLocalSub = true;
                continue;
            }

            if (inLocalSub && hasClass(reader, "name")) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("found name element");
                }

                String name = reader.getElementText();
                if (StringUtils.isBlank(name)) {
                    handleParseException("Name element must not be empty", reader);
                }
                sub.setDisplayName(name);

                continue;
            }

            if (inLocalSub && hasClass(reader, "collection")) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("found collection element");
                }
                inCollection = true;
                inTicket = false;
                continue;
            }

            if (inCollection && hasClass(reader, "uuid")) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("found uuid element");
                }

                String uuid = reader.getElementText();
                if (StringUtils.isBlank(uuid)) {
                    handleParseException("Uuid element must not be empty", reader);
                }
                sub.setCollectionUid(uuid);

                continue;
            }

            if (inLocalSub && hasClass(reader, "ticket")) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("found ticket element");
                }
                inCollection = false;
                inTicket = true;
                continue;
            }

            if (inTicket && hasClass(reader, "key")) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("found key element");
                }

                String key = reader.getElementText();
                if (StringUtils.isBlank(key)) {
                    handleParseException("Key element must not be empty", reader);
                }
                sub.setTicketKey(key);

                continue;
            }
        }

        reader.close();
    } catch (XMLStreamException e) {
        handleXmlException("Error reading XML", e);
    }

    return sub;
}

From source file:org.unitedinternet.cosmo.model.text.XhtmlTicketFormat.java

public Ticket parse(String source, EntityFactory entityFactory) throws ParseException {

    String key = null;//from   ww w .  ja  va  2  s .  c om
    TicketType type = null;
    Integer timeout = null;
    try {
        if (source == null) {
            throw new ParseException("Source has no XML data", -1);
        }
        StringReader sr = new StringReader(source);
        XMLStreamReader reader = createXmlReader(sr);

        boolean inTicket = false;
        while (reader.hasNext()) {
            reader.next();
            if (!reader.isStartElement()) {
                continue;
            }

            if (hasClass(reader, "ticket")) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("found ticket element");
                }
                inTicket = true;
                continue;
            }

            if (inTicket && hasClass(reader, "key")) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("found key element");
                }

                key = reader.getElementText();
                if (StringUtils.isBlank(key)) {
                    handleParseException("Key element must not be empty", reader);
                }

                continue;
            }

            if (inTicket && hasClass(reader, "type")) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("found type element");
                }

                String typeId = reader.getAttributeValue(null, "title");
                if (StringUtils.isBlank(typeId)) {
                    handleParseException("Ticket type title must not be empty", reader);
                }
                type = TicketType.createInstance(typeId);

                continue;
            }
            if (inTicket && hasClass(reader, "timeout")) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("found timeout element");
                }

                String timeoutString = reader.getAttributeValue(null, "title");
                if (StringUtils.isBlank(timeoutString)) {
                    timeout = null;
                } else {
                    timeout = Integer.getInteger(timeoutString);
                }

                continue;
            }
        }
        if (type == null || key == null) {
            handleParseException("Ticket must have type and key", reader);
        }
        reader.close();
    } catch (XMLStreamException e) {
        handleXmlException("Error reading XML", e);
    }

    Ticket ticket = entityFactory.createTicket(type);
    ticket.setKey(key);
    if (timeout == null) {
        ticket.setTimeout(Ticket.TIMEOUT_INFINITE);
    } else {
        ticket.setTimeout(timeout);
    }

    return ticket;
}

From source file:org.unitedinternet.cosmo.util.DomReader.java

private static Node readNode(Document d, XMLStreamReader reader) throws XMLStreamException {
    Node root = null;//from  ww  w. j a  va2s  . c  o m
    Node current = null;

    while (reader.hasNext()) {
        reader.next();

        if (reader.isEndElement()) {
            //log.debug("Finished reading " + current.getNodeName());

            if (current.getParentNode() == null) {
                break;
            }

            //log.debug("Setting current to " +
            //current.getParentNode().getNodeName());
            current = current.getParentNode();
        }

        if (reader.isStartElement()) {
            Element e = readElement(d, reader);
            if (root == null) {
                //log.debug("Setting root to " + e.getNodeName());
                root = e;
            }

            if (current != null) {
                //log.debug("Appending child " + e.getNodeName() + " to " +
                //current.getNodeName());
                current.appendChild(e);
            }

            //log.debug("Setting current to " + e.getNodeName());
            current = e;

            continue;
        }

        if (reader.isCharacters()) {
            CharacterData cd = d.createTextNode(reader.getText());
            if (root == null) {
                return cd;
            }
            if (current == null) {
                return cd;
            }

            //log.debug("Appending text '" + cd.getData() + "' to " +
            //current.getNodeName());
            current.appendChild(cd);

            continue;
        }
    }

    return root;
}