Example usage for javax.xml.stream XMLStreamConstants COMMENT

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

Introduction

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

Prototype

int COMMENT

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

Click Source Link

Document

Indicates an event is a comment

Usage

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 {/*w ww .  ja  v a2  s  . com*/
        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  www  .j a va2s.  co 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:edu.harvard.iq.dvn.ingest.statdataio.impl.plugins.ddi.DDIFileReader.java

private String getElementText(XMLStreamReader xmlr) throws XMLStreamException {
    if (xmlr.getEventType() != XMLStreamConstants.START_ELEMENT) {
        throw new XMLStreamException("parser must be on START_ELEMENT to read next text", xmlr.getLocation());
    }/*from  www .j a  v  a 2 s . c  o m*/
    int eventType = xmlr.next();
    StringBuffer content = new StringBuffer();
    while (eventType != XMLStreamConstants.END_ELEMENT) {
        if (eventType == XMLStreamConstants.CHARACTERS || eventType == XMLStreamConstants.CDATA
                || eventType == XMLStreamConstants.SPACE
        /* || eventType == XMLStreamConstants.ENTITY_REFERENCE*/) {
            content.append(xmlr.getText());
        } else if (eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
                || eventType == XMLStreamConstants.COMMENT
                || eventType == XMLStreamConstants.ENTITY_REFERENCE) {
            // skipping
        } else if (eventType == XMLStreamConstants.END_DOCUMENT) {
            throw new XMLStreamException("unexpected end of document when reading element text content");
        } else if (eventType == XMLStreamConstants.START_ELEMENT) {
            throw new XMLStreamException("element text content may not contain START_ELEMENT",
                    xmlr.getLocation());
        } else {
            throw new XMLStreamException("Unexpected event type " + eventType, xmlr.getLocation());
        }
        eventType = xmlr.next();
    }
    return content.toString();
}

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  a v a2  s .com
    }

    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

/**
 * Method next.//from ww w. j a v  a 2 s  . c  om
 *
 * @return Returns int.
 * @throws OMException
 */
public int next() throws OMException {
    try {
        // We need a loop here because we may decide to skip an event
        while (true) {
            if (done) {
                throw new OMException();
            }
            int token = parserNext();
            if (!cache) {
                return token;
            }

            // The current token should be the same as the 
            // one just obtained.  This bit of code is used to 
            // detect invalid parser state.
            if (doTrace) {
                int currentParserToken = parser.getEventType();
                if (currentParserToken != token) {

                    log.debug("WARNING: The current state of the parser is not equal to the "
                            + "state just received from the parser. The current state in the paser is "
                            + XMLEventUtils.getEventTypeString(currentParserToken)
                            + " the state just received is " + XMLEventUtils.getEventTypeString(token));

                    /*
                      throw new OMException("The current token " + token + 
                                 " does not match the current event " +
                                 "reported by the parser token.  The parser did not update its state correctly.  " +
                                 "The parser is " + parser);
                     */
                }
            }

            // Now log the current state of the parser
            if (doTrace) {
                logParserState();
            }

            switch (token) {
            case XMLStreamConstants.START_ELEMENT:
                elementLevel++;
                lastNode = createNextOMElement();
                break;
            case XMLStreamConstants.CHARACTERS:
                lastNode = createOMText(XMLStreamConstants.CHARACTERS);
                break;
            case XMLStreamConstants.CDATA:
                lastNode = createOMText(XMLStreamConstants.CDATA);
                break;
            case XMLStreamConstants.END_ELEMENT:
                endElement();
                elementLevel--;
                break;
            case XMLStreamConstants.END_DOCUMENT:
                done = true;
                ((OMContainerEx) this.document).setComplete(true);
                break;
            case XMLStreamConstants.SPACE:
                try {
                    lastNode = createOMText(XMLStreamConstants.SPACE);
                    if (lastNode == null) {
                        continue;
                    }
                } catch (OMHierarchyException ex) {
                    // The OM implementation doesn't allow text nodes at the current
                    // position in the tree. Since it is only whitespace, we can safely
                    // skip this event.
                    continue;
                }
                break;
            case XMLStreamConstants.COMMENT:
                lastNode = createComment();
                break;
            case XMLStreamConstants.DTD:
                createDTD();
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                lastNode = createPI();
                break;
            case XMLStreamConstants.ENTITY_REFERENCE:
                lastNode = createOMText(XMLStreamConstants.ENTITY_REFERENCE);
                break;
            default:
                throw new OMException();
            }
            return token;
        }
    } catch (XMLStreamException e) {
        throw new OMException(e);
    }
}

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

/**
 * Dump the current event of the parser.
 */// w ww . ja  va  2  s .  c  o  m
protected void logParserState() {
    if (doTrace) {
        int currentEvent = parser.getEventType();

        switch (currentEvent) {
        case XMLStreamConstants.START_ELEMENT:
            log.trace("START_ELEMENT: ");
            log.trace("  QName: " + parser.getName());
            break;
        case XMLStreamConstants.START_DOCUMENT:
            log.trace("START_DOCUMENT: ");
            break;
        case XMLStreamConstants.CHARACTERS:
            log.trace("CHARACTERS: ");
            // This can bust up a datahandler
            //log.trace(   "[" + parser.getText() + "]");
            break;
        case XMLStreamConstants.CDATA:
            log.trace("CDATA: ");
            // This can but
            //log.trace(   "[" + parser.getText() + "]");
            break;
        case XMLStreamConstants.END_ELEMENT:
            log.trace("END_ELEMENT: ");
            log.trace("  QName: " + parser.getName());
            break;
        case XMLStreamConstants.END_DOCUMENT:
            log.trace("END_DOCUMENT: ");
            break;
        case XMLStreamConstants.SPACE:
            log.trace("SPACE: ");
            //log.trace(   "[" + parser.getText() + "]");
            break;
        case XMLStreamConstants.COMMENT:
            log.trace("COMMENT: ");
            //log.trace(   "[" + parser.getText() + "]");
            break;
        case XMLStreamConstants.DTD:
            log.trace("DTD: ");
            log.trace("[" + parser.getText() + "]");
            break;
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
            log.trace("PROCESSING_INSTRUCTION: ");
            log.trace("   [" + parser.getPITarget() + "][" + parser.getPIData() + "]");
            break;
        case XMLStreamConstants.ENTITY_REFERENCE:
            log.trace("ENTITY_REFERENCE: ");
            log.trace("    " + parser.getLocalName() + "[" + parser.getText() + "]");
            break;
        default:
            log.trace("UNKNOWN_STATE: " + currentEvent);

        }
    }
}

From source file:org.apache.axiom.om.impl.llom.OMStAXWrapper.java

/**
 * Returns the next tag.//from  w  w  w .j  a  v  a2s  .c o  m
 *
 * @return Returns int.
 * @throws org.apache.axiom.om.impl.exception.OMStreamingException
 *
 * @throws XMLStreamException
 */
public int nextTag() throws XMLStreamException {
    int eventType = next();
    while ((eventType == XMLStreamConstants.CHARACTERS && isWhiteSpace()) // skip whitespace
            || (eventType == XMLStreamConstants.CDATA && isWhiteSpace()) // skip whitespace
            || eventType == XMLStreamConstants.SPACE || eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
            || eventType == XMLStreamConstants.COMMENT) {
        eventType = next();
    }
    if (eventType != XMLStreamConstants.START_ELEMENT && eventType != XMLStreamConstants.END_ELEMENT) {
        throw new XMLStreamException("expected start or end tag", getLocation());
    }
    return eventType;
}

From source file:org.apache.axiom.om.impl.llom.OMStAXWrapper.java

/**
 * @return Returns String./*w  w  w.j av  a  2  s  .c o m*/
 * @throws XMLStreamException
 * @see javax.xml.stream.XMLStreamReader#getElementText()
 */
public String getElementText() throws XMLStreamException {
    if (parser != null) {
        try {
            return parser.getElementText();
        } catch (XMLStreamException e) {
            throw new OMStreamingException(e);
        }
    } else {
        ///////////////////////////////////////////////////////
        //// Code block directly from the API documentation ///
        if (getEventType() != XMLStreamConstants.START_ELEMENT) {
            throw new XMLStreamException("parser must be on START_ELEMENT to read next text", getLocation());
        }
        int eventType = next();
        StringBuffer content = new StringBuffer();
        while (eventType != XMLStreamConstants.END_ELEMENT) {
            if (eventType == XMLStreamConstants.CHARACTERS || eventType == XMLStreamConstants.CDATA
                    || eventType == XMLStreamConstants.SPACE
                    || eventType == XMLStreamConstants.ENTITY_REFERENCE) {
                content.append(getText());
            } else if (eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
                    || eventType == XMLStreamConstants.COMMENT) {
                // skipping
            } else if (eventType == XMLStreamConstants.END_DOCUMENT) {
                throw new XMLStreamException("unexpected end of document when reading element text content");
            } else if (eventType == XMLStreamConstants.START_ELEMENT) {
                throw new XMLStreamException("element text content may not contain START_ELEMENT");
            } else {
                throw new XMLStreamException("Unexpected event type " + eventType, getLocation());
            }
            eventType = next();
        }
        return content.toString();
        ///////////////////////////////////////////////////////////////
    }

}

From source file:org.apache.axiom.om.impl.SwitchingWrapper.java

/**
 * @return Returns String.//from w ww .  jav a  2  s. c  om
 * @throws XMLStreamException
 * @see javax.xml.stream.XMLStreamReader#getElementText()
 */
public String getElementText() throws XMLStreamException {
    if (parser != null) {
        try {
            String elementText = parser.getElementText();
            currentEvent = END_ELEMENT;
            return elementText;
        } catch (XMLStreamException e) {
            throw new OMStreamingException(e);
        }
    } else {
        ///////////////////////////////////////////////////////
        //// Code block directly from the API documentation ///
        if (getEventType() != XMLStreamConstants.START_ELEMENT) {
            throw new XMLStreamException("parser must be on START_ELEMENT to read next text", getLocation());
        }
        int eventType = next();
        StringBuffer content = new StringBuffer();
        while (eventType != XMLStreamConstants.END_ELEMENT) {
            if (eventType == XMLStreamConstants.CHARACTERS || eventType == XMLStreamConstants.CDATA
                    || eventType == XMLStreamConstants.SPACE
                    || eventType == XMLStreamConstants.ENTITY_REFERENCE) {
                content.append(getText());
            } else if (eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
                    || eventType == XMLStreamConstants.COMMENT) {
                // skipping
            } else if (eventType == XMLStreamConstants.END_DOCUMENT) {
                throw new XMLStreamException("unexpected end of document when reading element text content");
            } else if (eventType == XMLStreamConstants.START_ELEMENT) {
                throw new XMLStreamException("element text content may not contain START_ELEMENT");
            } else {
                throw new XMLStreamException("Unexpected event type " + eventType, getLocation());
            }
            eventType = next();
        }
        return content.toString();
        ///////////////////////////////////////////////////////////////
    }

}