Java Code Examples for javax.xml.stream.XMLStreamReader#getEventType()

The following examples show how to use javax.xml.stream.XMLStreamReader#getEventType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JAXBMessage.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public XMLStreamReader readPayload() throws XMLStreamException {
   try {
        if(infoset==null) {
                            if (rawContext != null) {
                    XMLStreamBufferResult sbr = new XMLStreamBufferResult();
                                    Marshaller m = rawContext.createMarshaller();
                                    m.setProperty("jaxb.fragment", Boolean.TRUE);
                                    m.marshal(jaxbObject, sbr);
                    infoset = sbr.getXMLStreamBuffer();
                            } else {
                                MutableXMLStreamBuffer buffer = new MutableXMLStreamBuffer();
                                writePayloadTo(buffer.createFromXMLStreamWriter());
                                infoset = buffer;
                            }
        }
        XMLStreamReader reader = infoset.readAsXMLStreamReader();
        if(reader.getEventType()== START_DOCUMENT)
            XMLStreamReaderUtil.nextElementContent(reader);
        return reader;
    } catch (JAXBException e) {
       // bug 6449684, spec 4.3.4
       throw new WebServiceException(e);
    }
}
 
Example 2
Source File: WSS4JStaxInInterceptor.java    From cxf with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc}*/
public void handleMessage(SoapMessage message) throws Fault {
    if (isGET(message)) {
        LOG.fine("StartBodyInterceptor skipped in HTTP GET method");
        return;
    }
    XMLStreamReader xmlReader = message.getContent(XMLStreamReader.class);
    try {
        int i = xmlReader.getEventType();
        while (i == XMLStreamConstants.NAMESPACE
            || i == XMLStreamConstants.ATTRIBUTE
            || i == XMLStreamConstants.CHARACTERS) {
            i = xmlReader.next();
        }
    } catch (XMLStreamException e) {
        throw new SoapFault(new Message("XML_STREAM_EXC", LOG, e.getMessage()), e,
                            message.getVersion().getSender());
    }

}
 
Example 3
Source File: Bug6481678.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testReadingNamespace() {
    is = new java.io.ByteArrayInputStream(getXML().getBytes());
    try {
        XMLStreamReader sr = factory.createFilteredReader(factory.createXMLStreamReader(is), (StreamFilter) filter);

        while (sr.hasNext()) {
            int eventType = sr.getEventType();
            if (eventType == XMLStreamConstants.START_ELEMENT) {
                if (sr.getLocalName().equals(rootElement)) {
                    Assert.assertTrue(sr.getNamespacePrefix(0).equals(prefixApple) && sr.getNamespaceURI(0).equals(namespaceURIApple));
                }
            }
            eventType = sr.next();
        }
    } catch (Exception ex) {
        Assert.fail("Exception: " + ex.getMessage());
    }
}
 
Example 4
Source File: DeploymentDescriptorParser.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Parses the handler and role information and sets it
 * on the {@link WSBinding}.
 *
 * @return true if <handler-chains> element present in DD
 *         false otherwise.
 */
protected boolean setHandlersAndRoles(WSBinding binding, XMLStreamReader reader, QName serviceName, QName portName) {

    if (reader.getEventType() == XMLStreamConstants.END_ELEMENT ||
            !reader.getName().equals(HandlerChainsModel.QNAME_HANDLER_CHAINS)) {
        return false;
    }

    HandlerAnnotationInfo handlerInfo = HandlerChainsModel.parseHandlerFile(
            reader, classLoader, serviceName, portName, binding);

    binding.setHandlerChain(handlerInfo.getHandlers());
    if (binding instanceof SOAPBinding) {
        ((SOAPBinding) binding).setRoles(handlerInfo.getRoles());
    }

    // move past </handler-chains>
    XMLStreamReaderUtil.nextContent(reader);
    return true;
}
 
Example 5
Source File: StreamReaderBufferCreator.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private void store(XMLStreamReader reader) throws XMLStreamException {
    if (!reader.hasNext()) {
        return;
    }

    _eventType = reader.getEventType();
    switch (_eventType) {
        case XMLStreamReader.START_DOCUMENT:
            storeDocumentAndChildren(reader);
            break;
        case XMLStreamReader.START_ELEMENT:
            storeElementAndChildren(reader);
            break;
        default:
            throw new XMLStreamException("XMLStreamReader not positioned at a document or element");
    }

    increaseTreeCount();
}
 
Example 6
Source File: DeploymentDescriptorParser.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Parses the handler and role information and sets it
 * on the {@link WSBinding}.
 *
 * @return true if <handler-chains> element present in DD
 *         false otherwise.
 */
protected boolean setHandlersAndRoles(WSBinding binding, XMLStreamReader reader, QName serviceName, QName portName) {

    if (reader.getEventType() == XMLStreamConstants.END_ELEMENT ||
            !reader.getName().equals(HandlerChainsModel.QNAME_HANDLER_CHAINS)) {
        return false;
    }

    HandlerAnnotationInfo handlerInfo = HandlerChainsModel.parseHandlerFile(
            reader, classLoader, serviceName, portName, binding);

    binding.setHandlerChain(handlerInfo.getHandlers());
    if (binding instanceof SOAPBinding) {
        ((SOAPBinding) binding).setRoles(handlerInfo.getRoles());
    }

    // move past </handler-chains>
    XMLStreamReaderUtil.nextContent(reader);
    return true;
}
 
Example 7
Source File: EndpointArgumentsBuilder.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
void readRequest( Object[] args, XMLStreamReader r, AttachmentSet att) throws JAXBException {
    Object obj = null;
    AttachmentUnmarshallerImpl au = (att != null)?new AttachmentUnmarshallerImpl(att):null;
    if (bridge instanceof RepeatedElementBridge) {
        RepeatedElementBridge rbridge = (RepeatedElementBridge)bridge;
        ArrayList list = new ArrayList();
        QName name = r.getName();
        while (r.getEventType()==XMLStreamReader.START_ELEMENT && name.equals(r.getName())) {
            list.add(rbridge.unmarshal(r, au));
            XMLStreamReaderUtil.toNextTag(r, name);
        }
        obj = rbridge.collectionHandler().convert(list);
    } else {
        obj = bridge.unmarshal(r, au);
    }
    setter.put(obj,args);
}
 
Example 8
Source File: XMLStreamReaderUtil.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void toNextTag(XMLStreamReader reader, QName name) {
    // skip any whitespace
    if (reader.getEventType() != XMLStreamConstants.START_ELEMENT &&
            reader.getEventType() != XMLStreamConstants.END_ELEMENT) {
        XMLStreamReaderUtil.nextElementContent(reader);
    }
    if(reader.getEventType() == XMLStreamConstants.END_ELEMENT && name.equals(reader.getName())) {
        XMLStreamReaderUtil.nextElementContent(reader);
    }
}
 
Example 9
Source File: EPRHeader.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Nullable
public String getAttribute(@NotNull String nsUri, @NotNull String localName) {
    try {
        XMLStreamReader sr = epr.read("EndpointReference"/*doesn't matter*/);
        while(sr.getEventType()!= XMLStreamConstants.START_ELEMENT)
            sr.next();

        return sr.getAttributeValue(nsUri,localName);
    } catch (XMLStreamException e) {
        // since we are reading from buffer, this can't happen.
        throw new AssertionError(e);
    }
}
 
Example 10
Source File: WadlReader.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
private static final Option readOption(final XMLStreamReader reader) throws XMLStreamException {
    assertName(WadlElementName.OPTION, reader);
    final String value = getStringAttribute(WadlAttributeName.VALUE, null, reader);
    final List<Documentation> doc = new LinkedList<Documentation>();
    while (reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
            case XMLStreamConstants.START_ELEMENT: {
                if (match(WadlElementName.DOCUMENTATION, reader)) {
                    doc.add(readDocumentation(reader));
                } else {
                    skipForeignNamespaceElement(reader);
                }
                break;
            }
            case XMLStreamConstants.END_ELEMENT: {
                assertName(WadlElementName.OPTION, reader);
                return new Option(value, doc);
            }
            case XMLStreamConstants.CHARACTERS: {
                break;
            }
            default: {
                throw new AssertionError(reader.getEventType());
            }
        }
    }
    throw new AssertionError();
}
 
Example 11
Source File: StreamReaderBufferCreator.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates the buffer from a stream reader that is an element fragment.
 * <p>
 * The stream reader will be moved to the position of the next start of
 * an element if the stream reader is not already positioned at the start
 * of an element.
 * <p>
 * The element and all its children will be stored and after storing the stream
 * will be positioned at the next event after the end of the element.
 * <p>
 * @param storeInScopeNamespaces true if in-scope namespaces of the element
 *        fragment should be stored.
 * @return the mutable stream buffer.
 * @throws XMLStreamException if the stream reader cannot be positioned at
 *         the start of an element.
 */
public MutableXMLStreamBuffer createElementFragment(XMLStreamReader reader,
        boolean storeInScopeNamespaces) throws XMLStreamException {
    if (_buffer == null) {
        createBuffer();
    }

    if (!reader.hasNext()) {
        return _buffer;
    }

    _storeInScopeNamespacesOnElementFragment = storeInScopeNamespaces;

    _eventType = reader.getEventType();
    if (_eventType != XMLStreamReader.START_ELEMENT) {
        do {
            _eventType = reader.next();
        } while(_eventType != XMLStreamReader.START_ELEMENT && _eventType != XMLStreamReader.END_DOCUMENT);
    }

    if (storeInScopeNamespaces) {
        _inScopePrefixes = new HashMap<String,Integer>();
    }

    storeElementAndChildren(reader);

    return getXMLStreamBuffer();
}
 
Example 12
Source File: DatabaseIO.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Consumes the rest of the current element. This assumes that the current XML stream
 * event type is not START_ELEMENT.
 * 
 * @param reader The xml reader
 */
private void consumeRestOfElement(XMLStreamReader reader) throws XMLStreamException
{
    int eventType = reader.getEventType();

    while ((eventType != XMLStreamReader.END_ELEMENT) && (eventType != XMLStreamReader.END_DOCUMENT))
    {
        eventType = reader.next();
    }
}
 
Example 13
Source File: XmiReader.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
protected static final List<TaggedValue> readTaggedValueGroup(final XMLStreamReader reader) {
    final List<TaggedValue> taggedValues = new LinkedList<TaggedValue>();
    try {
        if (match(XmiElementName.MODEL_ELEMENT_DOT_TAGGED_VALUE, reader)) {
            while (reader.hasNext()) {
                reader.next();
                switch (reader.getEventType()) {
                    case XMLStreamConstants.START_ELEMENT: {
                        if ("TaggedValue".equals(reader.getLocalName())) {
                            taggedValues.add(assertNotNull(readTaggedValue(reader)));
                            break;
                        } else {
                            throw new AssertionError(reader.getLocalName());
                        }
                    }
                    case XMLStreamConstants.END_ELEMENT: {
                        if (match(XmiElementName.MODEL_ELEMENT_DOT_TAGGED_VALUE, reader)) {
                            return taggedValues;
                        } 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());
    }
    throw new AssertionError();
}
 
Example 14
Source File: XmiMappingReader.java    From secure-data-service with Apache License 2.0 4 votes vote down vote up
private static final XmiMappingStatus readStatus(final XMLStreamReader reader) throws XMLStreamException {
    assertStartElement(reader);
    assertName(XmiMappingConstants.STATUS, reader);
    final StringBuilder sb = new StringBuilder();
    boolean done = false;
    while (!done && reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
            case XMLStreamConstants.START_ELEMENT: {
                throw new AssertionError(reader.getLocalName());
            }
            case XMLStreamConstants.END_ELEMENT: {
                assertName(XmiMappingConstants.STATUS, reader);
                done = true;
                break;
            }
            case XMLStreamConstants.CHARACTERS: {
                sb.append(reader.getText());
                break;
            }
            default: {
                throw new AssertionError(reader.getEventType());
            }
        }
    }
    // Intentionally make the coupling loose between the serialization and
    // the enumeration.
    final String value = sb.toString().trim();
    if (XmiMappingValues.STATUS_MATCH.equals(value)) {
        return XmiMappingStatus.MATCH;
    } else if (XmiMappingValues.STATUS_IGNORABLE.equals(value)) {
        return XmiMappingStatus.IGNORABLE;
    } else if (XmiMappingValues.STATUS_ALIGN.equals(value)) {
        return XmiMappingStatus.ALIGN;
    } else if (XmiMappingValues.STATUS_UNKNOWN.equals(value)) {
        return XmiMappingStatus.UNKNOWN;
    } else if (XmiMappingValues.STATUS_TRANSIENT.equals(value)) {
        return XmiMappingStatus.TRANSIENT;
    } else if (XmiMappingValues.STATUS_BUG.equals(value)) {
        return XmiMappingStatus.BUG;
    } else if (XmiMappingValues.STATUS_FINANCIAL.equals(value)) {
        return XmiMappingStatus.FINANCIAL;
    } else {
        throw new AssertionError(XmiMappingConstants.STATUS + " : " + value);
    }
}
 
Example 15
Source File: Base64Type.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Override
public Object readObject(MessageReader mreader, Context context) throws DatabindingException {
    XMLStreamReader reader = mreader.getXMLStreamReader();

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    try {
        reader.next();
        while (!reader.isCharacters() && !reader.isEndElement() && !reader.isStartElement()) {
            reader.next();
        }

        if (reader.isStartElement() && reader.getName().equals(AbstractXOPType.XOP_INCLUDE)) {
            return optimizedType.readMtoM(mreader, context);
        }

        if (reader.isEndElement()) {
            reader.next();
            return new byte[0];
        }

        CharArrayWriter writer = new CharArrayWriter(2048);
        while (reader.isCharacters()) {
            writer.write(reader.getTextCharacters(),
                         reader.getTextStart(),
                         reader.getTextLength());
            reader.next();
        }
        Base64Utility.decode(writer.toCharArray(), 0, writer.size(), bos);

        while (reader.getEventType() != XMLStreamConstants.END_ELEMENT) {
            reader.next();
        }

        // Advance just past the end element
        reader.next();

        return bos.toByteArray();
    } catch (Base64Exception | XMLStreamException e) {
        throw new DatabindingException("Could not parse base64Binary data.", e);
    }
}
 
Example 16
Source File: HasNextTypeFilter.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public boolean accept(XMLStreamReader r) {
    return types[r.getEventType()];
}
 
Example 17
Source File: XmiReader.java    From secure-data-service with Apache License 2.0 4 votes vote down vote up
/**
 * The UML:Namespace.ownedElement is simply a list of model elements that can belong in a
 * name-space.
 *
 * It has no identity of its own and is only present in the serialization format.
 */
protected static final List<NamespaceOwnedElement> readNamespaceOwnedElement(final XMLStreamReader reader)
        throws XMLStreamException {
    assertName(XmiElementName.NAMESPACE_DOT_OWNED_ELEMENT, reader);
    final List<NamespaceOwnedElement> ownedElements = new LinkedList<NamespaceOwnedElement>();
    while (reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
            case XMLStreamConstants.START_ELEMENT: {
                if (match(XmiElementName.CLASS, reader)) {
                    ownedElements.add(assertNotNull(readClassType(reader.getName(), reader)));
                } else if (match(XmiElementName.ASSOCIATION_CLASS, reader)) {
                    ownedElements.add(assertNotNull(readClassType(reader.getName(), reader)));
                } else if (match(XmiElementName.ASSOCIATION, reader)) {
                    ownedElements.add(assertNotNull(readClassType(reader.getName(), reader)));
                } else if (match(XmiElementName.DATA_TYPE, reader)) {
                    ownedElements.add(assertNotNull(readDataType(reader)));
                } else if (match(XmiElementName.ENUMERATION, reader)) {
                    ownedElements.add(assertNotNull(readEnumeration(reader)));
                } else if (match(XmiElementName.TAG_DEFINITION, reader)) {
                    ownedElements.add(assertNotNull(readTagDefinition(reader)));
                } else if (match(XmiElementName.GENERALIZATION, reader)) {
                    ownedElements.add(assertNotNull(readGeneralization(reader)));
                } else if (match(XmiElementName.COMMENT, reader)) {
                    skipElement(reader, false);
                } else if (match(XmiElementName.PACKAGE, reader)) {
                    ownedElements.add(assertNotNull(readPackage(reader)));
                } else {
                    throw new AssertionError(reader.getName());
                    // skipElement(reader);
                }
                break;
            }
            case XMLStreamConstants.END_ELEMENT: {
                assertName(XmiElementName.NAMESPACE_DOT_OWNED_ELEMENT, reader);
                return Collections.unmodifiableList(ownedElements);
            }
            case XMLStreamConstants.CHARACTERS: {
                // Ignore.
                break;
            }
            default: {
                throw new AssertionError(reader.getEventType());
            }
        }
    }
    throw new AssertionError();
}
 
Example 18
Source File: WadlReader.java    From secure-data-service with Apache License 2.0 4 votes vote down vote up
private static final Resource readResource(final XMLStreamReader reader) throws XMLStreamException {
    assertName(WadlElementName.RESOURCE, reader);
    final String id = getId(reader);
    final List<String> type = getStringListType(WadlAttributeName.TYPE, reader);
    final String queryType = getQueryType(reader, WadlSyntax.QUERY_TYPE_APPLICATION_X_WWW_FORM_URLENCODED);
    final String path = getStringAttribute(WadlAttributeName.PATH, null, reader);
    final String resourceClass = getStringAttribute(WadlAttributeName.RESOURCE_CLASS, null, reader);
    final List<Documentation> documentation = new LinkedList<Documentation>();
    final List<Param> params = new LinkedList<Param>();
    final List<Method> methods = new LinkedList<Method>();
    final List<Resource> resources = new LinkedList<Resource>();
    while (reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
            case XMLStreamConstants.START_ELEMENT: {
                if (match(WadlElementName.RESOURCE, reader)) {
                    resources.add(readResource(reader));
                } else if (match(WadlElementName.DOCUMENTATION, reader)) {
                    documentation.add(readDocumentation(reader));
                } else if (match(WadlElementName.PARAM, reader)) {
                    params.add(readParam(reader));
                } else if (match(WadlElementName.METHOD, reader)) {
                    methods.add(readMethod(reader));
                } else {
                    skipForeignNamespaceElement(reader);
                }
                break;
            }
            case XMLStreamConstants.END_ELEMENT: {
                assertName(WadlElementName.RESOURCE, reader);
                return new Resource(id, type, queryType, path, documentation, params, methods, resources, resourceClass);
            }
            case XMLStreamConstants.CHARACTERS:
            case XMLStreamConstants.COMMENT: {
                // Ignore.
                break;
            }
            default: {
                throw new AssertionError(reader.getEventType());
            }
        }
    }
    throw new AssertionError();
}
 
Example 19
Source File: DocumentationReader.java    From secure-data-service with Apache License 2.0 4 votes vote down vote up
private static final void assertStartDocument(final XMLStreamReader reader) {
    if (XMLStreamConstants.START_DOCUMENT != reader.getEventType()) {
        throw new AssertionError(reader.getLocalName());
    }
}
 
Example 20
Source File: StAXEventAllocatorBase.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
XMLEvent getXMLEvent(XMLStreamReader reader){
    XMLEvent event = null;
    //returns the current event
    int eventType = reader.getEventType();
    //this needs to be set before creating events
    factory.setLocation(reader.getLocation());
    switch(eventType){

        case XMLEvent.START_ELEMENT:
        {
            StartElementEvent startElement = (StartElementEvent)factory.createStartElement(reader.getPrefix(),
                                reader.getNamespaceURI(), reader.getLocalName());

            addAttributes(startElement,reader);
            addNamespaces(startElement, reader);
            //need to fix it along with the Reader
            //setNamespaceContext(startElement,reader);
            event = startElement;
            break;
        }
        case XMLEvent.END_ELEMENT:
        {
            EndElementEvent endElement = (EndElementEvent)factory.createEndElement(
                    reader.getPrefix(), reader.getNamespaceURI(), reader.getLocalName());
            addNamespaces(endElement,reader);
            event = endElement ;
            break;
        }
        case XMLEvent.PROCESSING_INSTRUCTION:
        {
            event = factory.createProcessingInstruction(reader.getPITarget(),reader.getPIData());
            break;
        }
        case XMLEvent.CHARACTERS:
        {
            if (reader.isWhiteSpace())
              event = factory.createSpace(reader.getText());
            else
              event = factory.createCharacters(reader.getText());

            break;
        }
        case XMLEvent.COMMENT:
        {
            event = factory.createComment(reader.getText());
            break;
        }
        case XMLEvent.START_DOCUMENT:
        {
            StartDocumentEvent docEvent = (StartDocumentEvent)factory.createStartDocument(
                    reader.getVersion(), reader.getEncoding(), reader.isStandalone());
            if(reader.getCharacterEncodingScheme() != null){
                docEvent.setDeclaredEncoding(true);
            }else{
                docEvent.setDeclaredEncoding(false);
            }
            event = docEvent ;
            break;
        }
        case XMLEvent.END_DOCUMENT:{
            EndDocumentEvent endDocumentEvent = new EndDocumentEvent() ;
            event = endDocumentEvent ;
            break;
        }
        case XMLEvent.ENTITY_REFERENCE:{
            event = factory.createEntityReference(reader.getLocalName(),
                    new EntityDeclarationImpl(reader.getLocalName(),reader.getText()));
            break;

        }
        case XMLEvent.ATTRIBUTE:{
            event = null ;
            break;
        }
        case XMLEvent.DTD:{
            event = factory.createDTD(reader.getText());
            break;
        }
        case XMLEvent.CDATA:{
            event = factory.createCData(reader.getText());
            break;
        }
        case XMLEvent.SPACE:{
            event = factory.createSpace(reader.getText());
            break;
        }
    }
    return event ;
}