Example usage for javax.xml.stream XMLStreamReader getClass

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

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLStreamReader sr = factory.createXMLStreamReader(new FileReader("test.xml"));
    System.out.println(sr.getClass());

    while (sr.hasNext()) {
        int eventType = sr.next();

        if (eventType == XMLStreamReader.START_DOCUMENT) {
            continue;
        } else if (eventType == XMLStreamReader.END_ELEMENT) {
            System.out.println("End Element:    " + sr.getLocalName());
        } else if (eventType == XMLStreamReader.START_ELEMENT) {
            System.out.println("Start Element:  " + sr.getLocalName());
        }/*from www  .  j  a va 2 s. co  m*/
    }
}

From source file:org.apache.axiom.om.util.StAXUtils.java

public static XMLStreamReader createXMLStreamReader(StAXParserConfiguration configuration, final InputStream in,
        final String encoding) throws XMLStreamException {

    final XMLInputFactory inputFactory = getXMLInputFactory(configuration);
    try {//w  w  w  .  ja v a2s .  co  m
        XMLStreamReader reader = (XMLStreamReader) AccessController
                .doPrivileged(new PrivilegedExceptionAction() {
                    public Object run() throws XMLStreamException {
                        return inputFactory.createXMLStreamReader(in, encoding);
                    }
                });
        if (isDebugEnabled) {
            log.debug("XMLStreamReader is " + reader.getClass().getName());
        }
        return reader;
    } catch (PrivilegedActionException pae) {
        throw (XMLStreamException) pae.getException();
    }
}

From source file:org.apache.axiom.om.util.StAXUtils.java

public static XMLStreamReader createXMLStreamReader(StAXParserConfiguration configuration, final InputStream in)
        throws XMLStreamException {

    final XMLInputFactory inputFactory = getXMLInputFactory(configuration);
    try {/*from  ww  w .j  a va2  s . c  o  m*/
        XMLStreamReader reader = (XMLStreamReader) AccessController
                .doPrivileged(new PrivilegedExceptionAction() {
                    public Object run() throws XMLStreamException {
                        return inputFactory.createXMLStreamReader(in);
                    }
                });

        if (isDebugEnabled) {
            log.debug("XMLStreamReader is " + reader.getClass().getName());
        }
        return reader;
    } catch (PrivilegedActionException pae) {
        throw (XMLStreamException) pae.getException();
    }
}

From source file:org.apache.axiom.om.util.StAXUtils.java

public static XMLStreamReader createXMLStreamReader(StAXParserConfiguration configuration, final Reader in)
        throws XMLStreamException {

    final XMLInputFactory inputFactory = getXMLInputFactory(configuration);
    try {/*from www  .  j a v  a2  s  .c o m*/
        XMLStreamReader reader = (XMLStreamReader) AccessController
                .doPrivileged(new PrivilegedExceptionAction() {
                    public Object run() throws XMLStreamException {
                        return inputFactory.createXMLStreamReader(in);
                    }
                });
        if (isDebugEnabled) {
            log.debug("XMLStreamReader is " + reader.getClass().getName());
        }
        return reader;
    } catch (PrivilegedActionException pae) {
        throw (XMLStreamException) pae.getException();
    }
}

From source file:org.apache.axiom.util.stax.XMLStreamReaderUtils.java

/**
 * Searches the wrapper and delegate classes to find the original {@link XMLStreamReader}.
 * This method should only be used when a consumer of Axiom really needs to 
 * access the original stream reader./* w  ww. j  av  a2 s .co  m*/
 * @param parser XMLStreamReader used by Axiom
 * @return original parser 
 */
public static XMLStreamReader getOriginalXMLStreamReader(XMLStreamReader parser) {
    if (log.isDebugEnabled()) {
        String clsName = (parser != null) ? parser.getClass().toString() : "null";
        log.debug("Entry getOriginalXMLStreamReader: " + clsName);
    }
    while (parser instanceof DelegatingXMLStreamReader) {
        parser = ((DelegatingXMLStreamReader) parser).getParent();
        if (log.isDebugEnabled()) {
            String clsName = (parser != null) ? parser.getClass().toString() : "null";
            log.debug("  parent: " + clsName);
        }
    }
    if (log.isDebugEnabled()) {
        String clsName = (parser != null) ? parser.getClass().toString() : "null";
        log.debug("Exit getOriginalXMLStreamReader: " + clsName);
    }
    return parser;
}

From source file:org.apache.axis2.datasource.jaxb.JAXBCustomBuilder.java

public OMElement create(String namespace, String localPart, OMContainer parent, XMLStreamReader reader,
        OMFactory factory) throws OMException {

    if (log.isDebugEnabled()) {
        log.debug("create namespace = " + namespace);
        log.debug("  localPart = " + localPart);
        log.debug("  reader = " + reader.getClass());
    }/* ww w  .j  ava 2s  .c o m*/

    // There are some situations where we want to use normal
    // unmarshalling, so return null
    if (!shouldUnmarshal(namespace, localPart)) {
        JAXBCustomBuilderMonitor.updateTotalFailedCreates();
        return null;
    }
    try {
        // Create an OMSourcedElement backed by an unmarshalled JAXB object
        OMNamespace ns = factory.createOMNamespace(namespace, reader.getPrefix());

        Object jaxb = jdsContext.unmarshal(reader);
        if (log.isDebugEnabled()) {
            log.debug("Successfully unmarshalled jaxb object " + jaxb);
        }

        OMDataSource ds = new JAXBDataSource(jaxb, jdsContext);
        if (log.isDebugEnabled()) {
            log.debug("The JAXBDataSource is " + ds);
        }
        OMSourcedElement omse = factory.createOMElement(ds, localPart, ns);

        parent.addChild(omse);
        JAXBCustomBuilderMonitor.updateTotalCreates();
        return omse;
    } catch (JAXBException e) {
        JAXBCustomBuilderMonitor.updateTotalFailedCreates();
        throw new OMException(e);
    }
}

From source file:org.apache.axis2.datasource.jaxb.JAXBDSContext.java

/**
 * Unmarshal the xml into a JAXB object//from   w ww .  j  ava  2 s . c  o m
 * @param inputReader
 * @return
 * @throws JAXBException
 */
public Object unmarshal(XMLStreamReader inputReader) throws JAXBException {

    if (DEBUG_ENABLED) {
        String clsText = (inputReader != null) ? inputReader.getClass().toString() : "null";
        log.debug("unmarshal with inputReader=" + clsText);
    }
    // See the Javadoc of the CustomBuilder interface for a complete explanation of
    // the following two instructions:
    XOPEncodedStream xopEncodedStream = XOPUtils.getXOPEncodedStream(inputReader);
    XMLStreamReader reader = XMLStreamReaderUtils.getOriginalXMLStreamReader(xopEncodedStream.getReader());
    if (DEBUG_ENABLED) {
        String clsText = (reader != null) ? reader.getClass().toString() : "null";
        log.debug("  originalReader=" + clsText);
    }

    // There may be a preferred classloader that should be used
    ClassLoader cl = getClassLoader();

    Unmarshaller u = JAXBUtils.getJAXBUnmarshaller(getJAXBContext(cl));

    // Create an attachment unmarshaller
    AttachmentUnmarshaller aum = createAttachmentUnmarshaller(xopEncodedStream.getMimePartProvider());

    if (aum != null) {
        if (DEBUG_ENABLED) {
            log.debug("Adding JAXBAttachmentUnmarshaller to Unmarshaller");
        }
        u.setAttachmentUnmarshaller(aum);
    }

    Object jaxb = null;

    // Unmarshal into the business object.
    if (getProcessType() == null) {
        jaxb = unmarshalByElement(u, reader); // preferred and always used for
                                              // style=document
    } else {
        jaxb = unmarshalByType(u, reader, getProcessType(), isxmlList(), getConstructionType());
    }

    // Successfully unmarshalled the object
    JAXBUtils.releaseJAXBUnmarshaller(getJAXBContext(cl), u);

    // Don't close the reader.  The reader is owned by the caller, and it
    // may contain other xml instance data (other than this JAXB object)
    // reader.close();
    return jaxb;
}

From source file:org.apache.axis2.jaxws.context.listener.ParserInputStreamCustomBuilder.java

public OMElement create(String namespace, String localPart, OMContainer parent, XMLStreamReader reader,
        OMFactory factory) throws OMException {

    if (log.isDebugEnabled()) {
        log.debug("create namespace = " + namespace);
        log.debug("  localPart = " + localPart);
        log.debug("  reader = " + reader.getClass());
    }/*from  www .jav a2 s . c o m*/

    if (!shouldUnmarshal(namespace, localPart)) {
        if (log.isDebugEnabled()) {
            log.debug("This element won't be unmarshalled with the custom builder");
        }
        return null;
    }

    /*
     * 1) Use the the parser to fetch the inputStream
     * 2) Use the inputStream to create a DataSource, delay reading of content as much as you can.
     * 3) Use the OMFactory to create OMSourcedElement, OMSourcedElement is backed by ParsedEntityDataSource.
     */
    try {
        ParsedEntityReaderFactory perf = (ParsedEntityReaderFactory) FactoryRegistry
                .getFactory(ParsedEntityReaderFactory.class);
        ParsedEntityReader entityReader = perf.getParsedEntityReader();
        if (log.isDebugEnabled()) {
            log.debug("ParsedEntityReader = " + entityReader);
        }
        //Do not user custom builder if Parser does not have ability to read sub content.
        if (!entityReader.isParsedEntityStreamAvailable()) {
            if (log.isDebugEnabled()) {
                log.debug("ParsedEntityStream is not available, defaulting to normal build");
            }
            return null;
        }
        // Create an OMSourcedElement backed by the ParsedData
        InputStream parsedStream = getPayloadContent(reader, entityReader);
        if (parsedStream == null) {
            //cant read content from EntityReader, returning null.
            if (log.isDebugEnabled()) {
                log.debug("Unable to read content from the entity reader, defaulting to normal build");
            }
            return null;
        }
        HashMap<String, String> nsElementDecls = getElementNamespaceDeclarations(reader);
        HashMap<String, String> attrElementDecls = getElementAttributeDeclarations(reader);

        //read the payload. Lets move the parser forward.
        if (reader.hasNext()) {
            reader.next();
        }
        if (namespace == null) {
            //lets look for ns in reader
            namespace = reader.getNamespaceURI();
            if (namespace == null) {
                //still cant find the namespace, just set it to "";
                namespace = "";
            }
        }
        OMNamespace ns = factory.createOMNamespace(namespace, reader.getPrefix());
        InputStream payload = ContextListenerUtils.createPayloadElement(parsedStream, ns, localPart, parent,
                nsElementDecls, attrElementDecls);

        ParserInputStreamDataSource ds = new ParserInputStreamDataSource(payload, encoding);
        OMSourcedElement om = null;
        if (parent instanceof SOAPHeader && factory instanceof SOAPFactory) {
            om = ((SOAPFactory) factory).createSOAPHeaderBlock(localPart, ns, ds);
        } else {
            om = factory.createOMElement(ds, localPart, ns);
        }
        //Add the new OMSourcedElement ot the parent
        parent.addChild(om);
        /*
        //Lets Mark the body as complete so Serialize calls dont fetch data from parser for body content.
        if(parent instanceof SOAPBodyImpl){
        ((SOAPBodyImpl)parent).setComplete(true);
        }
        */
        return om;
    } catch (OMException e) {
        throw e;
    } catch (Throwable t) {
        throw new OMException(t);
    }
}

From source file:org.apache.axis2.jaxws.context.listener.ParserInputStreamCustomBuilder.java

public OMElement create(String namespace, String localPart, OMContainer parent, XMLStreamReader reader,
        OMFactory factory, InputStream payload) throws OMException {

    if (log.isDebugEnabled()) {
        log.debug("create namespace = " + namespace);
        log.debug("  localPart = " + localPart);
        log.debug("  reader = " + reader.getClass());
    }/*from  w ww  .ja va  2s  .  c o  m*/
    /*
     * 1) Use the the parser to fetch the inputStream
     * 2) Use the inputStream to create a DataSource, delay reading of content as much as you can.
     * 3) Use the OMFactory to create OMSourcedElement, OMSourcedElement is backed by ParsedEntityDataSource.
     */
    try {
        if (namespace == null) {
            //lets look for ns in reader
            namespace = reader.getNamespaceURI();
            if (namespace == null) {
                //still cant find the namespace, just set it to "";
                namespace = "";
            }
        }
        if (!shouldUnmarshal(namespace, localPart)) {
            if (log.isDebugEnabled()) {
                log.debug("This element won't be unmarshalled with the custom builder");
            }
            return null;
        }
        OMNamespace ns = factory.createOMNamespace(namespace, reader.getPrefix());
        ParserInputStreamDataSource ds = new ParserInputStreamDataSource(payload, encoding);
        OMSourcedElement om = null;
        if (parent instanceof SOAPHeader && factory instanceof SOAPFactory) {
            om = ((SOAPFactory) factory).createSOAPHeaderBlock(localPart, ns, ds);
        } else {
            om = factory.createOMElement(ds, localPart, ns);
        }
        //Add the new OMSourcedElement ot the parent
        parent.addChild(om);
        return om;
    } catch (OMException e) {
        throw e;
    } catch (Throwable t) {
        throw new OMException(t);
    }
}

From source file:org.apache.synapse.commons.json.JsonReaderDelegate.java

public JsonReaderDelegate(XMLStreamReader reader, boolean processNCNames) {
    super(reader);
    if (logger.isDebugEnabled()) {
        logger.debug("#JsonReaderDelegate. Setting XMLStreamReader: " + reader.getClass().getName());
    }//from   w ww.j a va2  s  . co  m
    this.buildValidNCNames = processNCNames;
}