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:Main.java

/** Get the human readable event name for the numeric event id */
public static String getEventName(int eventId) {
    switch (eventId) {
    case XMLStreamConstants.START_ELEMENT:
        return "StartElementEvent";
    case XMLStreamConstants.END_ELEMENT:
        return "EndElementEvent";
    case XMLStreamConstants.PROCESSING_INSTRUCTION:
        return "ProcessingInstructionEvent";
    case XMLStreamConstants.CHARACTERS:
        return "CharacterEvent";
    case XMLStreamConstants.COMMENT:
        return "CommentEvent";
    case XMLStreamConstants.START_DOCUMENT:
        return "StartDocumentEvent";
    case XMLStreamConstants.END_DOCUMENT:
        return "EndDocumentEvent";
    case XMLStreamConstants.ENTITY_REFERENCE:
        return "EntityReferenceEvent";
    case XMLStreamConstants.ATTRIBUTE:
        return "AttributeBase";
    case XMLStreamConstants.DTD:
        return "DTDEvent";
    case XMLStreamConstants.CDATA:
        return "CDATA";
    }//from  w ww.j ava  2 s.c  om
    return "UNKNOWN_EVENT_TYPE";
}

From source file:Main.java

public static XMLStreamReader getXMLStreamReader(InputStream is)
        throws XMLStreamException, FactoryConfigurationError {
    //return XMLInputFactory.newInstance().createXMLStreamReader(is);
    return new StreamReaderDelegate(XMLInputFactory.newInstance().createXMLStreamReader(is)) {
        public int next() throws XMLStreamException {
            while (true) {
                int event = super.next();
                switch (event) {
                case XMLStreamConstants.COMMENT:
                case XMLStreamConstants.PROCESSING_INSTRUCTION:
                    continue;
                default:
                    return event;
                }/*from www  .j a v  a2s  .c o m*/
            }
        }
    };
}

From source file:Main.java

/**
 * Get the string representation of a given StAX event type. The returned
 * value is the name of the constant in {@link XMLStreamConstants}
 * corresponding to the event type.//from  w w w.  ja  va2 s.co  m
 * 
 * @param event
 *            the event type as returned by
 *            {@link javax.xml.stream.events.XMLEvent#getEventType()},
 *            {@link javax.xml.stream.XMLStreamReader#getEventType()} or
 *            {@link javax.xml.stream.XMLStreamReader#next()}
 * @return a string representation of the event type
 */
public static String getEventTypeString(int event) {
    String state = null;
    switch (event) {
    case XMLStreamConstants.START_ELEMENT:
        state = "START_ELEMENT";
        break;
    case XMLStreamConstants.START_DOCUMENT:
        state = "START_DOCUMENT";
        break;
    case XMLStreamConstants.CHARACTERS:
        state = "CHARACTERS";
        break;
    case XMLStreamConstants.CDATA:
        state = "CDATA";
        break;
    case XMLStreamConstants.END_ELEMENT:
        state = "END_ELEMENT";
        break;
    case XMLStreamConstants.END_DOCUMENT:
        state = "END_DOCUMENT";
        break;
    case XMLStreamConstants.SPACE:
        state = "SPACE";
        break;
    case XMLStreamConstants.COMMENT:
        state = "COMMENT";
        break;
    case XMLStreamConstants.DTD:
        state = "DTD";
        break;
    case XMLStreamConstants.PROCESSING_INSTRUCTION:
        state = "PROCESSING_INSTRUCTION";
        break;
    case XMLStreamConstants.ENTITY_REFERENCE:
        state = "ENTITY_REFERENCE";
        break;
    default:
        state = "UNKNOWN_STATE: " + event;
    }
    return state;
}

From source file:Main.java

public static String parse(XMLStreamReader reader) {
    StringBuffer result = new StringBuffer();
    if (reader != null) {
        try {// ww  w . j av a2s  . c  o  m
            while (reader.hasNext()) {
                switch (reader.getEventType()) {

                case XMLStreamConstants.START_ELEMENT:
                    result.append("<");
                    printName(reader, result);
                    printNamespaces(reader, result);
                    printAttributes(reader, result);
                    result.append(">");
                    break;

                case XMLStreamConstants.END_ELEMENT:
                    result.append("</");
                    printName(reader, result);
                    result.append(">");
                    break;

                case XMLStreamConstants.SPACE:

                case XMLStreamConstants.CHARACTERS:
                    int start = reader.getTextStart();
                    int length = reader.getTextLength();
                    result.append(new String(reader.getTextCharacters(), start, length));
                    break;

                case XMLStreamConstants.PROCESSING_INSTRUCTION:
                    result.append("<?");
                    if (reader.hasText())
                        result.append(reader.getText());
                    result.append("?>");
                    break;

                case XMLStreamConstants.CDATA:
                    result.append("<![CDATA[");
                    start = reader.getTextStart();
                    length = reader.getTextLength();
                    result.append(new String(reader.getTextCharacters(), start, length));
                    result.append("]]>");
                    break;

                case XMLStreamConstants.COMMENT:
                    result.append("<!--");
                    if (reader.hasText())
                        result.append(reader.getText());
                    result.append("-->");
                    break;

                case XMLStreamConstants.ENTITY_REFERENCE:
                    result.append(reader.getLocalName()).append("=");
                    if (reader.hasText())
                        result.append("[").append(reader.getText()).append("]");
                    break;

                case XMLStreamConstants.START_DOCUMENT:
                    result.append("<?xml");
                    result.append(" version='").append(reader.getVersion()).append("'");
                    result.append(" encoding='").append(reader.getCharacterEncodingScheme()).append("'");
                    if (reader.isStandalone())
                        result.append(" standalone='yes'");
                    else
                        result.append(" standalone='no'");
                    result.append("?>");
                    break;
                }
                reader.next();
            } // end while
        } catch (XMLStreamException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                reader.close();
            } catch (XMLStreamException e) {
            }
        }
    }
    return result.toString();
}

From source file:Main.java

private static void parseRestOfDocument(XMLStreamReader reader) throws XMLStreamException {

    while (reader.hasNext()) {
        int type = reader.next();
        switch (type) {
        case XMLStreamConstants.START_ELEMENT:
            System.out.println(reader.getLocalName());
            if (reader.getNamespaceURI() != null) {
                String prefix = reader.getPrefix();
                if (prefix == null) {
                    prefix = "[None]";
                }//ww  w.j  a va  2s. c  o  m
                System.out.println("prefix = '" + prefix + "', URI = '" + reader.getNamespaceURI() + "'");
            }

            if (reader.getAttributeCount() > 0) {
                for (int i = 0; i < reader.getAttributeCount(); i++) {
                    System.out.println("Attribute (name = '" + reader.getAttributeLocalName(i) + "', value = '"
                            + reader.getAttributeValue(i) + "')");
                    String attURI = reader.getAttributeNamespace(i);
                    if (attURI != null) {
                        String attPrefix = reader.getAttributePrefix(i);
                        if (attPrefix == null || attPrefix.equals("")) {
                            attPrefix = "[None]";
                        }
                        System.out.println("prefix=" + attPrefix + ",URI=" + attURI);
                    }
                }
            }

            break;
        case XMLStreamConstants.END_ELEMENT:
            System.out.println("XMLStreamConstants.END_ELEMENT");
            break;
        case XMLStreamConstants.CHARACTERS:
            if (!reader.isWhiteSpace()) {
                System.out.println("CD:" + reader.getText());
            }
            break;
        case XMLStreamConstants.DTD:
            System.out.println("DTD:" + reader.getText());
            break;
        case XMLStreamConstants.SPACE:
            System.out.println(" ");
            break;
        case XMLStreamConstants.COMMENT:
            System.out.println(reader.getText());
            break;
        default:
            System.out.println(type);
        }
    }
}

From source file:com.widowcrawler.exo.parse.Parser.java

private static String getEventTypeString(int eventType) {
    switch (eventType) {
    case XMLStreamConstants.START_ELEMENT:
        return "START_ELEMENT";
    case XMLStreamConstants.END_ELEMENT:
        return "END_ELEMENT";
    case XMLStreamConstants.PROCESSING_INSTRUCTION:
        return "PROCESSING_INSTRUCTION";
    case XMLStreamConstants.CHARACTERS:
        return "CHARACTERS";
    case XMLStreamConstants.COMMENT:
        return "COMMENT";
    case XMLStreamConstants.START_DOCUMENT:
        return "START_DOCUMENT";
    case XMLStreamConstants.END_DOCUMENT:
        return "END_DOCUMENT";
    case XMLStreamConstants.ENTITY_REFERENCE:
        return "ENTITY_REFERENCE";
    case XMLStreamConstants.ATTRIBUTE:
        return "ATTRIBUTE";
    case XMLStreamConstants.DTD:
        return "DTD";
    case XMLStreamConstants.CDATA:
        return "CDATA";
    case XMLStreamConstants.SPACE:
        return "SPACE";
    case XMLStreamConstants.ENTITY_DECLARATION:
        return "ENTITY_DECLARATION";
    case XMLStreamConstants.NAMESPACE:
        return "NAMESPACE";
    case XMLStreamConstants.NOTATION_DECLARATION:
        return "NOTATION_DECLARATION";
    }//from  w  w  w . j a  v  a2s.co m
    return "UNKNOWN_EVENT_TYPE: " + eventType;
}

From source file:StAXStreamTreeViewer.java

private void parseRestOfDocument(XMLStreamReader reader, DefaultMutableTreeNode current)
        throws XMLStreamException {

    while (reader.hasNext()) {
        int type = reader.next();
        switch (type) {
        case XMLStreamConstants.START_ELEMENT:

            DefaultMutableTreeNode element = new DefaultMutableTreeNode(reader.getLocalName());
            current.add(element);/*  w  w  w .  j  ava  2  s  .  c o m*/
            current = element;

            if (reader.getNamespaceURI() != null) {
                String prefix = reader.getPrefix();
                if (prefix == null) {
                    prefix = "[None]";
                }
                DefaultMutableTreeNode namespace = new DefaultMutableTreeNode(
                        "prefix = '" + prefix + "', URI = '" + reader.getNamespaceURI() + "'");
                current.add(namespace);
            }

            if (reader.getAttributeCount() > 0) {
                for (int i = 0; i < reader.getAttributeCount(); i++) {
                    DefaultMutableTreeNode attribute = new DefaultMutableTreeNode(
                            "Attribute (name = '" + reader.getAttributeLocalName(i) + "', value = '"
                                    + reader.getAttributeValue(i) + "')");
                    String attURI = reader.getAttributeNamespace(i);
                    if (attURI != null) {
                        String attPrefix = reader.getAttributePrefix(i);
                        if (attPrefix == null || attPrefix.equals("")) {
                            attPrefix = "[None]";
                        }
                        DefaultMutableTreeNode attNamespace = new DefaultMutableTreeNode(
                                "prefix=" + attPrefix + ",URI=" + attURI);
                        attribute.add(attNamespace);
                    }
                    current.add(attribute);
                }
            }

            break;
        case XMLStreamConstants.END_ELEMENT:
            current = (DefaultMutableTreeNode) current.getParent();
            break;
        case XMLStreamConstants.CHARACTERS:
            if (!reader.isWhiteSpace()) {
                DefaultMutableTreeNode data = new DefaultMutableTreeNode("CD:" + reader.getText());
                current.add(data);
            }
            break;
        case XMLStreamConstants.DTD:
            DefaultMutableTreeNode dtd = new DefaultMutableTreeNode("DTD:" + reader.getText());
            current.add(dtd);
            break;
        case XMLStreamConstants.SPACE:
            break;
        case XMLStreamConstants.COMMENT:
            DefaultMutableTreeNode comment = new DefaultMutableTreeNode(reader.getText());
            current.add(comment);
            break;
        default:
            System.out.println(type);
        }
    }
}

From source file:lux.search.highlight.XmlHighlighter.java

@Override
public void handleEvent(XMLStreamReader reader, int eventType) throws XMLStreamException {

    switch (eventType) {
    case XMLStreamConstants.START_DOCUMENT:
        xmlStreamReader = reader; // cache the reader so we can pull events
        super.handleEvent(reader, eventType);
        break;//w  ww .j a  v a  2  s .c  om

    case XMLStreamConstants.START_ELEMENT:
        super.handleEvent(reader, eventType);
        xmlStreamTokens
                .pushElement(new QName(reader.getNamespaceURI(), reader.getLocalName(), reader.getPrefix()));
        break;

    case XMLStreamConstants.END_ELEMENT:
        super.handleEvent(reader, eventType);
        xmlStreamTokens.popElement();
        break;

    case XMLStreamConstants.COMMENT:
    case XMLStreamConstants.PROCESSING_INSTRUCTION:
        super.handleEvent(reader, eventType);
        break;

    case XMLStreamConstants.CDATA:
        throw new XMLStreamException("unexpected CDATA event");

    case XMLStreamConstants.SPACE:
        super.handleEvent(reader, eventType);
        break;

    case XMLStreamConstants.CHARACTERS:
        textReader.text();
        try {
            highlightTextNode();
        } catch (IOException e) {
            throw new XMLStreamException(e);
        }
        break;

    case XMLStreamConstants.ENTITY_REFERENCE:
        throw new XMLStreamException("unexpected entity reference event");

    default:
        super.handleEvent(reader, eventType);
    }

}

From source file:org.javelin.sws.ext.bind.internal.model.ComplexTypePattern.java

@Override
public T consumeValue(XMLEventReader eventReader, UnmarshallingContext context) throws XMLStreamException {
    // first create an object to be filled (using PropertyAccessors - direct or bean) according to the content model
    T object = BeanUtils.instantiate(this.getJavaType());

    // the order is dictated by incoming events, not by the mode
    // TODO: create a property to enable strict unmarshalling - dictated by content model
    // only this (ContentModel) pattern iterates over XML Events
    XMLEvent event = null;//from  ww w .ja va2  s . co  m
    PropertyMetadataValue<T, ?> pmv = null;

    // this loop will only handle first level of start elements and only single end element
    // deeper levels will be handled by nested patterns
    while (true) {
        boolean end = false;
        event = eventReader.peek();
        pmv = null;

        switch (event.getEventType()) {
        case XMLStreamConstants.ATTRIBUTE:
            pmv = this.consumeNestedAttribute(eventReader, context);
            break;
        case XMLStreamConstants.CDATA:
        case XMLStreamConstants.CHARACTERS:
            // TODO: XMLEvent.ENTITY_REFERENCE?
            if (this.simpleContent != null) {
                pmv = this.consumeSimpleContent(eventReader, context);
                break;
            }
        case XMLStreamConstants.COMMENT:
        case XMLStreamConstants.DTD:
        case XMLStreamConstants.SPACE:
        case XMLStreamConstants.ENTITY_DECLARATION:
        case XMLStreamConstants.NOTATION_DECLARATION:
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
            eventReader.nextEvent();
            break;
        case XMLStreamConstants.ENTITY_REFERENCE:
            // TODO: XMLEvent.ENTITY_REFERENCE?
            eventReader.nextEvent();
            break;
        case XMLStreamConstants.START_DOCUMENT:
            // strange
            break;
        case XMLStreamConstants.START_ELEMENT:
            pmv = this.consumeNestedElement(eventReader, context);
            break;
        case XMLStreamConstants.END_ELEMENT:
            // TODO: in mixed content there will be more than one end element it this content model's level
        case XMLStreamConstants.END_DOCUMENT:
            end = true;
            break;
        }

        if (end)
            break;

        if (pmv != null)
            pmv.getMetadata().setValue(object, pmv.getValue());
    }

    return (T) object;
}

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

private <T> T doXmlLoop(XMLEventReader streamReader, ParserState<T> parserState) {
    ourLog.trace("Entering XML parsing loop with state: {}", parserState);

    try {/*w w  w.ja  v  a  2 s  .  co m*/
        List<String> heldComments = new ArrayList<String>(1);

        while (streamReader.hasNext()) {
            XMLEvent nextEvent = streamReader.nextEvent();
            try {

                switch (nextEvent.getEventType()) {
                case XMLStreamConstants.START_ELEMENT: {
                    StartElement elem = nextEvent.asStartElement();

                    String namespaceURI = elem.getName().getNamespaceURI();

                    if ("extension".equals(elem.getName().getLocalPart())) {
                        Attribute urlAttr = elem.getAttributeByName(new QName("url"));
                        String url;
                        if (urlAttr == null || isBlank(urlAttr.getValue())) {
                            getErrorHandler().missingRequiredElement(new ParseLocation("extension"), "url");
                            url = null;
                        } else {
                            url = urlAttr.getValue();
                        }
                        parserState.enteringNewElementExtension(elem, url, false);
                    } else if ("modifierExtension".equals(elem.getName().getLocalPart())) {
                        Attribute urlAttr = elem.getAttributeByName(new QName("url"));
                        String url;
                        if (urlAttr == null || isBlank(urlAttr.getValue())) {
                            getErrorHandler().missingRequiredElement(new ParseLocation("modifierExtension"),
                                    "url");
                            url = null;
                        } else {
                            url = urlAttr.getValue();
                        }
                        parserState.enteringNewElementExtension(elem, url, true);
                    } else {
                        String elementName = elem.getName().getLocalPart();
                        parserState.enteringNewElement(namespaceURI, elementName);
                    }

                    if (!heldComments.isEmpty()) {
                        for (String next : heldComments) {
                            parserState.commentPre(next);
                        }
                        heldComments.clear();
                    }

                    @SuppressWarnings("unchecked")
                    Iterator<Attribute> attributes = elem.getAttributes();
                    for (Iterator<Attribute> iter = attributes; iter.hasNext();) {
                        Attribute next = iter.next();
                        parserState.attributeValue(next.getName().getLocalPart(), next.getValue());
                    }

                    break;
                }
                case XMLStreamConstants.END_DOCUMENT:
                case XMLStreamConstants.END_ELEMENT: {
                    if (!heldComments.isEmpty()) {
                        for (String next : heldComments) {
                            parserState.commentPost(next);
                        }
                        heldComments.clear();
                    }
                    parserState.endingElement();
                    //                  if (parserState.isComplete()) {
                    //                     return parserState.getObject();
                    //                  }
                    break;
                }
                case XMLStreamConstants.CHARACTERS: {
                    parserState.string(nextEvent.asCharacters().getData());
                    break;
                }
                case XMLStreamConstants.COMMENT: {
                    Comment comment = (Comment) nextEvent;
                    String commentText = comment.getText();
                    heldComments.add(commentText);
                    break;
                }
                }

                parserState.xmlEvent(nextEvent);

            } catch (DataFormatException e) {
                throw new DataFormatException("DataFormatException at [" + nextEvent.getLocation().toString()
                        + "]: " + e.getMessage(), e);
            }
        }
        return parserState.getObject();
    } catch (XMLStreamException e) {
        throw new DataFormatException(e);
    }
}