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.slc.sli.modeling.xmi.reader.XmiReader.java

protected static final Model readXMI(final XMLStreamReader reader) {
    try {/*from w w  w . j  a va2s  .  c  o  m*/
        if ("XMI".equals(reader.getLocalName())) {
            Model model = null;
            while (reader.hasNext()) {
                reader.next();
                switch (reader.getEventType()) {
                case XMLStreamConstants.START_ELEMENT: {
                    if ("XMI.header".equals(reader.getLocalName())) {
                        skipElement(reader, false);
                        break;
                    } else if ("XMI.content".equals(reader.getLocalName())) {
                        model = readContent(reader);
                        if (model == null) {
                            throw new IllegalStateException();
                        }
                        break;
                    } else {
                        throw new XmiRuntimeException("Expecting Foo element, got: " + reader.getLocalName());
                    }
                }
                case XMLStreamConstants.END_ELEMENT: {
                    if ("XMI".equals(reader.getLocalName())) {
                        if (model == null) {
                            throw new IllegalStateException();
                        }
                        return model;
                    } else {
                        throw new AssertionError(reader.getLocalName());
                    }
                }
                case XMLStreamConstants.CHARACTERS: {
                    // Ignore.
                    break;
                }
                default: {
                    throw new AssertionError(reader.getEventType());
                }
                }
            }
        } else {
            throw new AssertionError(reader.getLocalName());
        }
    } catch (final XMLStreamException e) {
        LOG.warn(e.getMessage());
    }
    return null;
}

From source file:org.slc.sli.modeling.xmi.reader.XmiReader.java

/**
 * Skips (recursively) over the element in question. Also useful during development.
 *
 * @param reader/*w w w.j  a  va  2 s .co  m*/
 *            The StAX {@link XMLStreamReader}.
 */
protected static final void skipElement(final XMLStreamReader reader, final boolean check)
        throws XMLStreamException {
    if (check) {
        throw new AssertionError(reader.getName());
    }
    final String localName = reader.getLocalName();
    while (reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            skipElement(reader, check);
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            if (localName.equals(reader.getLocalName())) {
                return;
            } else {
                throw new AssertionError(reader.getLocalName());
            }
        }
        case XMLStreamConstants.CHARACTERS: {
            // Ignore.
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    throw new AssertionError();
}

From source file:org.socraticgrid.workbench.ontomodel.service.SyntacticalOntoModelServiceImpl.java

/**
 * Extracts all the Fact Types used in a process definition.
 * Each Fact Type is declared as://from www  . jav  a 2  s  . c  o  m
 * 
 *   <bpmn2:textAnnotation id="_92807518-95EC-447B-A292-D46C9D5958E9">
 *       <bpmn2:text>KMRCustom--Diagnosis--code--49320</bpmn2:text>
 *   </bpmn2:textAnnotation>
 * 
 * In the previous example, Diagnosis is the Fact Type
 * 
 * @param processXml
 * @return
 * @throws XMLStreamException 
 */
private Set<String> getProcessFactTypesFromXml(String processXml) throws XMLStreamException {

    Set<String> facts = new HashSet<String>();

    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLStreamReader reader = factory.createXMLStreamReader(new ByteArrayInputStream(processXml.getBytes()));

    boolean parsingTextAnnotation = false;

    //<bpmn2:text>KMRCustom--Diagnosis--code--49320</bpmn2:text>
    while (reader.hasNext()) {
        switch (reader.next()) {
        case XMLStreamReader.START_ELEMENT:
            if ("bpmn2".equals(reader.getName().getPrefix())
                    && "textAnnotation".equals(reader.getName().getLocalPart())) {
                parsingTextAnnotation = true;
            }
            if (parsingTextAnnotation && "bpmn2".equals(reader.getName().getPrefix())
                    && "text".equals(reader.getName().getLocalPart())) {
                String text = reader.getElementText();
                if (text.startsWith("KMRCustom--")) {
                    String[] parts = text.split("--");
                    facts.add(parts[1]);
                }
            }
            break;
        case XMLStreamReader.END_ELEMENT:
            if ("bpmn2".equals(reader.getName().getPrefix())
                    && "textAnnotation".equals(reader.getName().getLocalPart())) {
                parsingTextAnnotation = false;
            }
        }
    }

    return facts;
}

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

/**
 * Creates a new <code>java.util.List</code> from the current element being
 * read in the specified <code>StreamReader</code>.
 * /*from w w w.  ja  v a 2 s  . co  m*/
 * @param reader
 *          the <code>StreamReader</code>.
 * @return the new array of <code>java.util.List</code>s.
 * @throws XmlRpcInvalidPayloadException
 *           if the element contains an unknown child. Only one "data" element
 *           is allowed inside an "array" element.
 * @see #parseDataElement(XMLStreamReader)
 */
protected final XmlRpcArray parseArrayElement(XMLStreamReader reader) throws XMLStreamException {

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

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

            if (XmlRpcElementNames.DATA.equals(localName)) {
                return parseDataElement(reader);
            }
            XmlRpcParsingUtils.handleUnexpectedElementFound(localName);
        }
    }

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

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

/**
 * Creates a new <code>java.util.List</code> from the current element being
 * read in the specified <code>StreamReader</code>.
 * /*from  w w  w  .java2  s.  c om*/
 * @param reader
 *          the <code>StreamReader</code>.
 * @return the new array of <code>java.util.List</code>s.
 * @see #parseValueElement(XMLStreamReader)
 */
protected final XmlRpcArray parseDataElement(XMLStreamReader reader) throws XMLStreamException {
    XmlRpcArray array = new XmlRpcArray();

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

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

            if (XmlRpcElementNames.VALUE.equals(localName)) {
                XmlRpcElement element = parseValueElement(reader);
                array.add(element);
            }
            break;

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

            if (XmlRpcElementNames.DATA.equals(localName) || XmlRpcElementNames.ARRAY.equals(localName)) {
                return array;
            }
        }
    }

    // we should not 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>.
 * /*w  w  w.  ja  v a2 s . c o  m*/
 * @param reader
 *          the <code>StreamReader</code>.
 * @return the created Object.
 * @throws XmlRpcInvalidPayloadException
 *           if the element contains an unknown child.
 * @see #parseValueElement(XMLStreamReader)
 */
protected final XmlRpcElement parseParameterElement(XMLStreamReader reader) throws XMLStreamException {
    while (reader.hasNext()) {
        int event = reader.next();

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

            if (XmlRpcElementNames.VALUE.equals(localName)) {
                return parseValueElement(reader);
            }
            XmlRpcParsingUtils.handleUnexpectedElementFound(localName);
        }
    }

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

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  ww w .j a  va2  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>.
 * //  www. j  av a 2 s.  c  o 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  ww  . jav  a 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>.
 * /*from   www  . ja v a  2 s .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;
}