Example usage for javax.xml.stream XMLStreamReader next

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

Introduction

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

Prototype

public int next() throws XMLStreamException;

Source Link

Document

Get next parsing event - a processor may return all contiguous character data in a single chunk, or it may split it into several chunks.

Usage

From source file:org.springmodules.remoting.xmlrpc.stax.AbstractStaxXmlRpcParser.java

/**
 * Parses the given <code>XMLStreamReader</code> containing parameters for a
 * XML-RPC request/response./*from  w w w  . ja va  2 s .  co  m*/
 * 
 * @param reader
 *          the <code>StreamReader</code>.
 * @return the parameters of the XML-RPC request/response.
 */
protected final XmlRpcElement[] parseParametersElement(XMLStreamReader reader) throws XMLStreamException {
    List parameters = new ArrayList();

    while (reader.hasNext()) {
        int event = reader.next();

        String localName = null;
        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            localName = reader.getLocalName();

            if (XmlRpcElementNames.PARAM.equals(localName)) {
                XmlRpcElement parameter = parseParameterElement(reader);
                parameters.add(parameter);

            } else {
                XmlRpcParsingUtils.handleUnexpectedElementFound(localName);
            }
            break;

        case XMLStreamConstants.END_ELEMENT:
            localName = reader.getLocalName();

            if (XmlRpcElementNames.PARAMS.equals(localName)) {
                return (XmlRpcElement[]) parameters.toArray(new XmlRpcElement[parameters.size()]);
            }
        }
    }

    // we should not reach this point.
    return null;
}

From source file:org.springmodules.remoting.xmlrpc.stax.AbstractStaxXmlRpcParser.java

/**
 * Creates a new <code>java.util.Map</code> from the current element being
 * read in the specified <code>StreamReader</code>.
 * /*from w w  w .ja va 2s. co m*/
 * @param reader
 *          the <code>StreamReader</code>.
 * @return the new array of <code>java.util.Map</code>s.
 * @see #parseMemberElement(XMLStreamReader)
 */
protected final XmlRpcStruct parseStructElement(XMLStreamReader reader) throws XMLStreamException {
    XmlRpcStruct struct = new XmlRpcStruct();

    while (reader.hasNext()) {
        int event = reader.next();
        String localName = null;

        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            localName = reader.getLocalName();

            if (XmlRpcElementNames.MEMBER.equals(localName)) {
                XmlRpcMember member = parseMemberElement(reader);
                struct.add(member);
            }
            break;

        case XMLStreamConstants.END_ELEMENT:
            localName = reader.getLocalName();

            if (XmlRpcElementNames.STRUCT.equals(localName)) {
                return struct;
            }
        }
    }

    // we should not reach this point.
    return null;
}

From source file:org.springmodules.remoting.xmlrpc.stax.AbstractStaxXmlRpcParser.java

/**
 * Creates a new <code>StructMember</code> from the current element being
 * read in the specified <code>StreamReader</code>.
 * //from   w w w.j ava 2  s  . c  o m
 * @param reader
 *          the <code>StreamReader</code>.
 * @return the new <code>StructMember</code>.
 * @throws XmlRpcInvalidPayloadException
 *           if the element contains an unknown child. Only one "name" element
 *           and one "value" element are allowed inside an "member" element.
 * @see #parseValueElement(XMLStreamReader)
 */
protected final XmlRpcMember parseMemberElement(XMLStreamReader reader) throws XMLStreamException {
    String name = null;
    XmlRpcElement value = null;

    while (reader.hasNext()) {
        int event = reader.next();
        String localName = null;

        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            localName = reader.getLocalName();

            if (XmlRpcElementNames.NAME.equals(localName)) {
                name = reader.getElementText();

            } else if (XmlRpcElementNames.VALUE.equals(localName)) {
                value = parseValueElement(reader);

            } else {
                XmlRpcParsingUtils.handleUnexpectedElementFound(localName);
            }
            break;

        case XMLStreamConstants.END_ELEMENT:
            localName = reader.getLocalName();

            if (XmlRpcElementNames.MEMBER.equals(localName)) {
                if (!StringUtils.hasText(name)) {
                    throw new XmlRpcInvalidPayloadException("The struct member should have a name");
                }

                return new XmlRpcMember(name, value);
            }
        }
    }

    // we should never reach this point.
    return null;
}

From source file:org.springmodules.remoting.xmlrpc.stax.AbstractStaxXmlRpcParser.java

/**
 * Creates a new Object from the current element being read in the specified
 * <code>StreamReader</code>.
 * //  ww w  .  jav  a 2s. co m
 * @param reader
 *          the <code>StreamReader</code>.
 * @return the created Object.
 * @throws XmlRpcInvalidPayloadException
 *           if the element contains an unknown child.
 * @see #parseArrayElement(XMLStreamReader)
 * @see #parseStructElement(XMLStreamReader)
 */
protected final XmlRpcElement parseValueElement(XMLStreamReader reader) throws XMLStreamException {

    while (reader.hasNext()) {
        int event = reader.next();
        String localName = null;

        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            localName = reader.getLocalName();

            if (XmlRpcElementNames.ARRAY.equals(localName)) {
                return parseArrayElement(reader);

            } else if (XmlRpcElementNames.BASE_64.equals(localName)) {
                String source = reader.getElementText();
                return new XmlRpcBase64(source);

            } else if (XmlRpcElementNames.BOOLEAN.equals(localName)) {
                String source = reader.getElementText();
                return new XmlRpcBoolean(source);

            } else if (XmlRpcElementNames.DATE_TIME.equals(localName)) {
                String source = reader.getElementText();
                return new XmlRpcDateTime(source);

            } else if (XmlRpcElementNames.DOUBLE.equals(localName)) {
                String source = reader.getElementText();
                return new XmlRpcDouble(source);

            } else if (XmlRpcElementNames.I4.equals(localName) || XmlRpcElementNames.INT.equals(localName)) {
                String source = reader.getElementText();
                return new XmlRpcInteger(source);

            } else if (XmlRpcElementNames.STRING.equals(localName)
                    || XmlRpcElementNames.INT.equals(localName)) {
                String source = reader.getElementText();
                return new XmlRpcString(source);

            } else if (XmlRpcElementNames.STRUCT.equals(localName)) {
                return parseStructElement(reader);

            } else {
                XmlRpcParsingUtils.handleUnexpectedElementFound(localName);
            }

        case XMLStreamConstants.CHARACTERS:
            String source = reader.getText();
            return new XmlRpcString(source);
        }
    }

    // we should not reach this point.
    return null;
}

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

/**
 * read workbook xml spec to get non hidden sheets
 *
 * @param inputStream//w  ww.  j  a v a  2s . c  o m
 * @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/*from   w ww.  j  a  va2 s .  c om*/
 * @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 ww  w  . j av  a2  s .  c om*/
    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   w ww.ja  v a  2s  .com*/
    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 .  ja v  a2s .  c o 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   w w  w  .  j a  v  a 2s .c om*/
        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;
}