Example usage for javax.xml.stream XMLStreamConstants START_DOCUMENT

List of usage examples for javax.xml.stream XMLStreamConstants START_DOCUMENT

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamConstants START_DOCUMENT.

Prototype

int START_DOCUMENT

To view the source code for javax.xml.stream XMLStreamConstants START_DOCUMENT.

Click Source Link

Document

Indicates an event is a start document

Usage

From source file:edu.utah.further.core.api.xml.XmlUtil.java

/**
 * Basic StAX element printout. For more sophisticated functionality, use
 * {@link XmlStreamPrinter}./*from   w  ww  .  j  a  va  2  s .co m*/
 * 
 * @param reader
 *            reader
 * @return reader's next element textual representation
 */
public static String getEventSimpleString(final XMLStreamReader reader) {
    switch (reader.getEventType()) {
    case XMLStreamConstants.START_ELEMENT:
        return "START_ELEMENT:\t\"" + reader.getLocalName() + "\"";
    case XMLStreamConstants.END_ELEMENT:
        return "END_ELEMENT:\t\"" + reader.getLocalName() + "\"";
    case XMLStreamConstants.START_DOCUMENT:
        return "START_DOCUMENT";
    case XMLStreamConstants.END_DOCUMENT:
        return "END_DOCUMENT";
    case XMLStreamConstants.CHARACTERS:
        return "CHARACTERS:\t\"" + reader.getText() + "\"" + " blank? "
                + StringUtils.isWhitespace(reader.getText());
    case XMLStreamConstants.SPACE:
        return "SPACE:\t\"" + reader.getText() + "\"";
    default:
        return "EVENT:\t" + reader.getEventType();
    }
}

From source file:ca.efendi.datafeeds.messaging.FtpSubscriptionMessageListener.java

private void parse(FtpSubscription ftpSubscription, final InputStream is) throws XMLStreamException {
    final XMLInputFactory factory = XMLInputFactory.newInstance();
    factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, true);
    factory.setProperty(XMLInputFactory.IS_COALESCING, true);
    final XMLStreamReader reader = factory.createXMLStreamReader(is, "UTF-8");
    CJProduct product = null;//from   www . ja v a 2  s .c  om
    String tagContent = null;
    //final ServiceContext serviceContext = new ServiceContext();

    //ServiceContext serviceContext = ServiceContextFactory.getInstance(
    //        BlogsEntry.class.getName(), actionRequest);

    //serviceContext.setScopeGroupId(20159);
    while (reader.hasNext()) {
        final int event = reader.next();
        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            //tagContent = "";
            if ("product".equals(reader.getLocalName())) {
                product = _cjProductLocalService.createCJProduct(0);
            }
            break;
        case XMLStreamConstants.CHARACTERS:
            //tagContent += reader.getText().trim();
            tagContent = reader.getText().trim();
            break;
        case XMLStreamConstants.END_ELEMENT:
            switch (reader.getLocalName()) {
            case "product":
                try {

                    _log.warn("refreshing document...");
                    _cjProductLocalService.refresh(ftpSubscription, product);
                } catch (final SystemException e) {
                    _log.error(e);
                } catch (final PortalException e) {
                    _log.error(e);
                }
                break;
            case "programname":
                product.setProgramName(tagContent);
                break;
            case "programurl":
                product.setProgramUrl(tagContent);
                break;
            case "catalogname":
                product.setCatalogName(tagContent);
                break;
            case "lastupdated":
                product.setLastUpdated(tagContent);
                break;
            case "name":
                product.setName(tagContent);
                break;
            case "keywords":
                product.setKeywords(tagContent);
                break;
            case "description":
                product.setDescription(tagContent);
                break;
            case "sku":
                product.setSku(tagContent);
                break;
            case "manufacturer":
                product.setManufacturer(tagContent);
                break;
            case "manufacturerid":
                product.setManufacturerId(tagContent);
                break;
            case "currency":
                product.setCurrency(tagContent);
                break;
            case "price":
                product.setPrice(tagContent);
                break;
            case "buyurl":
                product.setBuyUrl(tagContent);
                break;
            case "impressionurl":
                product.setImpressionUrl(tagContent);
                break;
            case "imageurl":
                product.setImageUrl(tagContent);
                break;
            case "instock":
                product.setInStock(tagContent);
                break;
            }
            break;
        case XMLStreamConstants.START_DOCUMENT:
            break;
        }
    }
}

From source file:com.microsoft.windowsazure.services.table.client.AtomPubParser.java

/**
 * Reserved for internal use. Parses the operation response as an entity. Reads entity data from the specified
 * <code>XMLStreamReader</code> using the specified class type and optionally projects the entity result with the
 * specified resolver into a {@link TableResult} object.
 * /*from   ww  w. ja  v  a2 s.c o  m*/
 * @param xmlr
 *            The <code>XMLStreamReader</code> to read the data to parse from.
 * @param httpStatusCode
 *            The HTTP status code returned with the operation response.
 * @param clazzType
 *            The class type <code>T</code> implementing {@link TableEntity} for the entity returned. Set to
 *            <code>null</code> to ignore the returned entity and copy only response properties into the
 *            {@link TableResult} object.
 * @param resolver
 *            An {@link EntityResolver} instance to project the entity into an instance of type <code>R</code>. Set
 *            to <code>null</code> to return the entitys as instance of the class type <code>T</code>.
 * @param opContext
 *            An {@link OperationContext} object used to track the execution of the operation.
 * @return
 *         A {@link TableResult} object with the parsed operation response.
 * 
 * @throws XMLStreamException
 *             if an error occurs while accessing the stream.
 * @throws ParseException
 *             if an error occurs while parsing the stream.
 * @throws InstantiationException
 *             if an error occurs while constructing the result.
 * @throws IllegalAccessException
 *             if an error occurs in reflection while parsing the result.
 * @throws StorageException
 *             if a storage service error occurs.
 */
protected static <T extends TableEntity, R> TableResult parseSingleOpResponse(final XMLStreamReader xmlr,
        final int httpStatusCode, final Class<T> clazzType, final EntityResolver<R> resolver,
        final OperationContext opContext) throws XMLStreamException, ParseException, InstantiationException,
        IllegalAccessException, StorageException {
    xmlr.require(XMLStreamConstants.START_DOCUMENT, null, null);
    xmlr.next();

    final TableResult res = parseEntity(xmlr, clazzType, resolver, opContext);
    res.setHttpStatusCode(httpStatusCode);
    return res;
}

From source file:com.msopentech.odatajclient.testservice.utils.XMLUtilities.java

private void addAtomElement(final InputStream content, final XMLEventWriter writer) throws Exception {
    final XMLEventReader reader = getEventReader(content);

    final XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    XMLEvent newLine = eventFactory.createSpace("\n");

    try {//ww  w . j  av  a2  s.c o  m
        writer.add(newLine);

        while (reader.hasNext()) {
            final XMLEvent event = reader.nextEvent();

            if (event.getEventType() != XMLStreamConstants.START_DOCUMENT
                    && event.getEventType() != XMLStreamConstants.END_DOCUMENT
                    && event.getEventType() != XMLStreamConstants.COMMENT) {
                writer.add(event);
            }
        }
        writer.add(newLine);
    } finally {
        reader.close();
        IOUtils.closeQuietly(content);
    }
}

From source file:com.marklogic.contentpump.AggregateXMLReader.java

@Override
public boolean nextKeyValue() throws IOException, InterruptedException {
    if (xmlSR == null) {
        hasNext = false;/*from   w  w  w .  j a v  a2  s  .  c  o m*/
        return false;
    }

    try {
        while (xmlSR.hasNext()) {
            int eventType;
            //getCharacterOffset() returns int; 
            //int will overflows if file is larger than 2GB
            if (!overflow && xmlSR.getLocation().getCharacterOffset() < -1) {
                overflow = true;
                LOG.info("In progress...");
            }
            //do not update pos if offset overflows
            if (!overflow) {
                pos = xmlSR.getLocation().getCharacterOffset();
            }
            eventType = xmlSR.next();
            switch (eventType) {
            case XMLStreamConstants.START_ELEMENT:
                if (startOfRecord) {
                    // this is the start of the root, only copy
                    // namespaces
                    copyNameSpaceDecl();
                    startOfRecord = false;
                    continue;
                }
                processStartElement();
                break;
            case XMLStreamConstants.CHARACTERS:
                write(StringEscapeUtils.escapeXml(xmlSR.getText()));
                break;
            case XMLStreamConstants.CDATA:
                write("<![CDATA[");
                write(xmlSR.getText());
                write("]]>");
                break;
            case XMLStreamConstants.SPACE:
                write(xmlSR.getText());
                break;
            case XMLStreamConstants.ENTITY_REFERENCE:
                write("&");
                write(xmlSR.getLocalName());
                write(";");
                break;
            case XMLStreamConstants.DTD:
                write("<!DOCTYPE");
                write(xmlSR.getText());
                write(">");
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                write("<?");
                write(xmlSR.getPIData());
                write("?>");
                break;
            case XMLStreamConstants.COMMENT:
                write("<!--");
                write(xmlSR.getText());
                write("-->");
                break;
            case XMLStreamConstants.END_ELEMENT:
                keepGoing = processEndElement();
                if (!keepGoing) {
                    keepGoing = true;
                    return true;
                }
                break;
            case XMLStreamConstants.START_DOCUMENT:
                throw new XMLStreamException("unexpected start of document within record!\n" + "recordName = "
                        + recordName + ", recordNamespace = " + recordNamespace + " at " + xmlSR.getLocation());
            case XMLStreamConstants.END_DOCUMENT:
                if (currentId != null) {
                    throw new XMLStreamException(
                            "end of document before end of current record!\n" + "recordName = " + recordName
                                    + ", recordNamespace = " + recordNamespace + " at " + xmlSR.getLocation());
                } else {
                    if (compressed) {
                        //this doc is done, refer to the zip for next doc
                        hasNext = false;
                        return false;
                    } else {
                        //get next file from FileIterator
                        if (iterator != null && iterator.hasNext()) {
                            close();
                            initStreamReader(iterator.next());
                            continue;
                        } else {
                            hasNext = false;
                            return false;
                        }
                    }
                }
            default:
                throw new XMLStreamException("UNIMPLEMENTED: " + eventType);
            }
        }
    } catch (XMLStreamException e) {
        LOG.error("Parsing error", e);
        throw new IOException("Parsing error", e);
    }

    return false;
}

From source file:com.microsoft.windowsazure.storage.table.TableParser.java

/**
 * Reserved for internal use. Parses the operation response as a collection of entities. Reads entity data from the
 * specified input stream using the specified class type and optionally projects each entity result with the
 * specified resolver into an {@link ODataPayload} containing a collection of {@link TableResult} objects.
 * /*from   ww  w.  j  av a 2 s .  c om*/
 * @param inStream
 *            The <code>InputStream</code> to read the data to parse from.
 * @param clazzType
 *            The class type <code>T</code> implementing {@link TableEntity} for the entities returned. Set to
 *            <code>null</code> to ignore the returned entities and copy only response properties into the
 *            {@link TableResult} objects.
 * @param resolver
 *            An {@link EntityResolver} instance to project the entities into instances of type <code>R</code>. Set
 *            to <code>null</code> to return the entities as instances of the class type <code>T</code>.
 * @param opContext
 *            An {@link OperationContext} object used to track the execution of the operation.
 * @return
 *         An {@link ODataPayload} containing a collection of {@link TableResult} objects with the parsed operation
 *         response.
 * 
 * @throws XMLStreamException
 *             if an error occurs while accessing the stream.
 * @throws ParseException
 *             if an error occurs while parsing the stream.
 * @throws InstantiationException
 *             if an error occurs while constructing the result.
 * @throws IllegalAccessException
 *             if an error occurs in reflection while parsing the result.
 * @throws StorageException
 *             if a storage service error occurs.
 */
@SuppressWarnings("unchecked")
private static <T extends TableEntity, R> ODataPayload<?> parseAtomQueryResponse(final InputStream inStream,
        final Class<T> clazzType, final EntityResolver<R> resolver, final OperationContext opContext)
        throws XMLStreamException, ParseException, InstantiationException, IllegalAccessException,
        StorageException {
    ODataPayload<T> corePayload = null;
    ODataPayload<R> resolvedPayload = null;
    ODataPayload<?> commonPayload = null;

    if (resolver != null) {
        resolvedPayload = new ODataPayload<R>();
        commonPayload = resolvedPayload;
    } else {
        corePayload = new ODataPayload<T>();
        commonPayload = corePayload;
    }

    final XMLStreamReader xmlr = Utility.createXMLStreamReaderFromStream(inStream);
    int eventType = xmlr.getEventType();
    xmlr.require(XMLStreamConstants.START_DOCUMENT, null, null);
    eventType = xmlr.next();

    xmlr.require(XMLStreamConstants.START_ELEMENT, null, ODataConstants.FEED);
    // skip feed chars
    eventType = xmlr.next();

    while (xmlr.hasNext()) {
        eventType = xmlr.next();

        if (eventType == XMLStreamConstants.CHARACTERS) {
            xmlr.getText();
            continue;
        }

        final String name = xmlr.getName().toString();

        if (eventType == XMLStreamConstants.START_ELEMENT) {
            if (name.equals(ODataConstants.BRACKETED_ATOM_NS + ODataConstants.ENTRY)) {
                final TableResult res = parseAtomEntity(xmlr, clazzType, resolver, opContext);
                if (corePayload != null) {
                    corePayload.tableResults.add(res);
                }

                if (resolver != null) {
                    resolvedPayload.results.add((R) res.getResult());
                } else {
                    corePayload.results.add((T) res.getResult());
                }
            }
        } else if (eventType == XMLStreamConstants.END_ELEMENT
                && name.equals(ODataConstants.BRACKETED_ATOM_NS + ODataConstants.FEED)) {
            break;
        }
    }

    xmlr.require(XMLStreamConstants.END_ELEMENT, null, ODataConstants.FEED);
    return commonPayload;
}

From source file:com.microsoft.windowsazure.storage.table.TableParser.java

/**
 * Reserved for internal use. Parses the operation response as an entity. Reads entity data from the specified
 * <code>XMLStreamReader</code> using the specified class type and optionally projects the entity result with the
 * specified resolver into a {@link TableResult} object.
 * /*from  w w w.  j  a v a 2s. c  o  m*/
 * @param xmlr
 *            The <code>XMLStreamReader</code> to read the data to parse from.
 * @param httpStatusCode
 *            The HTTP status code returned with the operation response.
 * @param clazzType
 *            The class type <code>T</code> implementing {@link TableEntity} for the entity returned. Set to
 *            <code>null</code> to ignore the returned entity and copy only response properties into the
 *            {@link TableResult} object.
 * @param resolver
 *            An {@link EntityResolver} instance to project the entity into an instance of type <code>R</code>. Set
 *            to <code>null</code> to return the entitys as instance of the class type <code>T</code>.
 * @param opContext
 *            An {@link OperationContext} object used to track the execution of the operation.
 * @return
 *         A {@link TableResult} object with the parsed operation response.
 * 
 * @throws XMLStreamException
 *             if an error occurs while accessing the stream.
 * @throws ParseException
 *             if an error occurs while parsing the stream.
 * @throws InstantiationException
 *             if an error occurs while constructing the result.
 * @throws IllegalAccessException
 *             if an error occurs in reflection while parsing the result.
 * @throws StorageException
 *             if a storage service error occurs.
 */
private static <T extends TableEntity, R> TableResult parseSingleOpAtomResponse(final InputStream inStream,
        final int httpStatusCode, final Class<T> clazzType, final EntityResolver<R> resolver,
        final OperationContext opContext) throws XMLStreamException, ParseException, InstantiationException,
        IllegalAccessException, StorageException {
    XMLStreamReader xmlr = Utility.createXMLStreamReaderFromStream(inStream);

    try {
        xmlr.require(XMLStreamConstants.START_DOCUMENT, null, null);
        xmlr.next();

        final TableResult res = parseAtomEntity(xmlr, clazzType, resolver, opContext);
        res.setHttpStatusCode(httpStatusCode);
        return res;
    } finally {
        xmlr.close();
    }
}

From source file:ca.uhn.fhir.parser.XmlParser.java

private void encodeXhtml(XhtmlDt theDt, XMLStreamWriter theEventWriter) throws XMLStreamException {
    if (theDt == null || theDt.getValue() == null) {
        return;//from   w  ww .j av  a2  s  .c o m
    }

    boolean firstElement = true;
    for (XMLEvent event : theDt.getValue()) {
        switch (event.getEventType()) {
        case XMLStreamConstants.ATTRIBUTE:
            Attribute attr = (Attribute) event;
            if (isBlank(attr.getName().getPrefix())) {
                if (isBlank(attr.getName().getNamespaceURI())) {
                    theEventWriter.writeAttribute(attr.getName().getLocalPart(), attr.getValue());
                } else {
                    theEventWriter.writeAttribute(attr.getName().getNamespaceURI(),
                            attr.getName().getLocalPart(), attr.getValue());
                }
            } else {
                theEventWriter.writeAttribute(attr.getName().getPrefix(), attr.getName().getNamespaceURI(),
                        attr.getName().getLocalPart(), attr.getValue());
            }

            break;
        case XMLStreamConstants.CDATA:
            theEventWriter.writeCData(((Characters) event).getData());
            break;
        case XMLStreamConstants.CHARACTERS:
        case XMLStreamConstants.SPACE:
            String data = ((Characters) event).getData();
            theEventWriter.writeCharacters(data);
            break;
        case XMLStreamConstants.COMMENT:
            theEventWriter.writeComment(((Comment) event).getText());
            break;
        case XMLStreamConstants.END_ELEMENT:
            theEventWriter.writeEndElement();
            break;
        case XMLStreamConstants.ENTITY_REFERENCE:
            EntityReference er = (EntityReference) event;
            theEventWriter.writeEntityRef(er.getName());
            break;
        case XMLStreamConstants.NAMESPACE:
            Namespace ns = (Namespace) event;
            theEventWriter.writeNamespace(ns.getPrefix(), ns.getNamespaceURI());
            break;
        case XMLStreamConstants.START_ELEMENT:
            StartElement se = event.asStartElement();
            if (firstElement) {
                if (StringUtils.isBlank(se.getName().getPrefix())) {
                    String namespaceURI = se.getName().getNamespaceURI();
                    if (StringUtils.isBlank(namespaceURI)) {
                        namespaceURI = "http://www.w3.org/1999/xhtml";
                    }
                    theEventWriter.writeStartElement(se.getName().getLocalPart());
                    theEventWriter.writeDefaultNamespace(namespaceURI);
                } else {
                    String prefix = se.getName().getPrefix();
                    String namespaceURI = se.getName().getNamespaceURI();
                    theEventWriter.writeStartElement(prefix, se.getName().getLocalPart(), namespaceURI);
                    theEventWriter.writeNamespace(prefix, namespaceURI);
                }
                firstElement = false;
            } else {
                if (isBlank(se.getName().getPrefix())) {
                    if (isBlank(se.getName().getNamespaceURI())) {
                        theEventWriter.writeStartElement(se.getName().getLocalPart());
                    } else {
                        if (StringUtils.isBlank(se.getName().getPrefix())) {
                            theEventWriter.writeStartElement(se.getName().getLocalPart());
                            // theEventWriter.writeDefaultNamespace(se.getName().getNamespaceURI());
                        } else {
                            theEventWriter.writeStartElement(se.getName().getNamespaceURI(),
                                    se.getName().getLocalPart());
                        }
                    }
                } else {
                    theEventWriter.writeStartElement(se.getName().getPrefix(), se.getName().getLocalPart(),
                            se.getName().getNamespaceURI());
                }
                for (Iterator<?> attrIter = se.getAttributes(); attrIter.hasNext();) {
                    Attribute next = (Attribute) attrIter.next();
                    theEventWriter.writeAttribute(next.getName().getLocalPart(), next.getValue());
                }
            }
            break;
        case XMLStreamConstants.DTD:
        case XMLStreamConstants.END_DOCUMENT:
        case XMLStreamConstants.ENTITY_DECLARATION:
        case XMLStreamConstants.NOTATION_DECLARATION:
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
        case XMLStreamConstants.START_DOCUMENT:
            break;
        }

    }
}

From source file:org.apache.axiom.om.impl.builder.StAXOMBuilder.java

private OMDocument createDocument() {
    OMDocument document = omfactory.createOMDocument(this);
    if (charEncoding != null) {
        document.setCharsetEncoding(charEncoding);
    }//from ww w.j  a va  2 s .  c  o  m
    if (parser.getEventType() == XMLStreamConstants.START_DOCUMENT) {
        document.setXMLVersion(parser.getVersion());
        document.setXMLEncoding(parser.getCharacterEncodingScheme());
        document.setStandalone(parser.isStandalone() ? "yes" : "no");
    } else {
        // We allow creating a StAXOMWrapper from a parser in state START_ELEMENT. In that
        // case, we must not call getVersion or isStandalone since this is forbidden by the
        // StAX specs. Set some reasonable defaults.
        document.setXMLVersion("1.0");
        document.setStandalone("yes");
    }
    return document;
}