Example usage for javax.xml.stream XMLStreamConstants END_ELEMENT

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

Introduction

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

Prototype

int END_ELEMENT

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

Click Source Link

Document

Indicates an event is an end element

Usage

From source file:Main.java

private static void process(XMLStreamReader reader) {
    int eventType = reader.getEventType();
    switch (eventType) {
    case XMLStreamConstants.START_ELEMENT:
        System.out.println("Start element: " + reader.getLocalName());
        int count = reader.getAttributeCount();
        for (int i = 0; i < count; i++) {
            String name = reader.getAttributeLocalName(i);
            String value = reader.getAttributeValue(i);
            System.out.println("\tAttribute name/value: " + name + "/" + value);
        }//from   ww w  .j  a  v a  2 s  . c o  m
        break;
    case XMLStreamConstants.END_ELEMENT:
        System.out.println("End element: " + reader.getLocalName());
        break;

    case XMLStreamConstants.CHARACTERS:
        System.out.println("Text: " + reader.getText());
        break;
    default:
        break;
    }
}

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 w  w.  j  a v a  2s . c  o  m
    return "UNKNOWN_EVENT_TYPE";
}

From source file:XMLStreamReaderDemo.java

private static void process(XMLStreamReader reader) {
    int eventType = reader.getEventType();
    switch (eventType) {
    case XMLStreamConstants.START_ELEMENT:
        System.out.println("Start element: " + reader.getLocalName());

        int count = reader.getAttributeCount();
        for (int i = 0; i < count; i++) {
            String name = reader.getAttributeLocalName(i);
            String value = reader.getAttributeValue(i);
            System.out.println("\tAttribute name/value: " + name + "/" + value);
        }/*w  w w  .  j  a va 2s  .  c o m*/
        break;

    case XMLStreamConstants.END_ELEMENT:
        System.out.println("End element: " + reader.getLocalName());
        break;

    case XMLStreamConstants.CHARACTERS:
        System.out.println("Text: " + reader.getText());
        break;
    default:
        break;
    }
}

From source file:Main.java

public static void skipToEndElement(XMLStreamReader xmlRdr) throws XMLStreamException {
    do {// w  ww. j  a  v  a  2  s . c  o m
        if (xmlRdr.getEventType() == XMLStreamConstants.END_ELEMENT) {
            break;
        }
        xmlRdr.next();
    } while (xmlRdr.hasNext());
}

From source file:Main.java

/**
 * Test if reader is on an end tag named tagLocalName.
 *
 * @param reader       xml stream reader
 * @param tagLocalName tag local name/*  w  ww.  ja  v  a2 s.  c o m*/
 * @return true if reader is on an end tag named tagLocalName
 */
public static boolean isEndTag(XMLStreamReader reader, String tagLocalName) {
    return (reader.getEventType() == XMLStreamConstants.END_ELEMENT)
            && (reader.getLocalName().equals(tagLocalName));
}

From source file:Main.java

/**
 * Skips a tag or subtree./*from www.  j  ava2 s.  c  om*/
 */
public static void skip(XMLStreamReader parser) throws XMLStreamException {
    assert parser != null;

    int level = 1;
    while (next(parser)) {
        int event = parser.getEventType();
        if (event == XMLStreamConstants.START_ELEMENT) {
            level++;
        } else if (event == XMLStreamConstants.END_ELEMENT) {
            level--;
            if (level == 0) {
                break;
            }
        }
    }

    next(parser);
}

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 www .j  a v a  2s . 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

/**
 * //from   ww w .ja va  2 s.  c  o m
 * @param is
 * @param os
 * @param elementNames
 * @throws XMLStreamException
 * @throws FactoryConfigurationError
 * @throws UnsupportedEncodingException
 */
public static void stripElements(final InputStream is, final OutputStream os,
        final Collection<String> elementNames)
        throws XMLStreamException, UnsupportedEncodingException, FactoryConfigurationError {
    final XMLEventReader xmlEventReader = XMLInputFactory.newInstance()
            .createXMLEventReader(new InputStreamReader(is, Charset.defaultCharset().name()));
    final XMLEventWriter xmlEventWriter = XMLOutputFactory.newInstance().createXMLEventWriter(os);

    String elementName = null;

    while (xmlEventReader.peek() != null) {
        final XMLEvent event = (XMLEvent) xmlEventReader.next();

        switch (event.getEventType()) {
        case XMLStreamConstants.START_DOCUMENT:
        case XMLStreamConstants.END_DOCUMENT: {
            // Ignore.
            break;
        }
        case XMLStreamConstants.START_ELEMENT: {
            final StartElement startElement = event.asStartElement();
            final QName name = startElement.getName();

            if (elementNames.contains(name.getLocalPart())) {
                elementName = name.getLocalPart();
            }

            if (elementName == null) {
                xmlEventWriter.add(event);
            }

            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            final EndElement endElement = event.asEndElement();
            final QName name = endElement.getName();

            if (elementName == null) {
                xmlEventWriter.add(event);
            } else if (elementName.equals(name.getLocalPart())) {
                elementName = null;
            }

            break;
        }
        default: {
            if (elementName == null) {
                xmlEventWriter.add(event);
            }
        }
        }
    }

    xmlEventWriter.flush();
}

From source file:Main.java

/**
 * /*from   w ww.ja  v  a2 s. co m*/
 * Checks indentation (over a single line - multipline text nodes is not supported)
 * 
 * @param out
 * @param indentSize
 * @return
 * @throws Exception
 */

public static boolean isIndented(String out, int indentSize) throws Exception {
    BufferedReader reader = new BufferedReader(new StringReader(out));

    boolean indentated = false;

    int level = 0;
    int line = 0;

    String string = reader.readLine();
    while (string != null) {
        int newLevel = 0;
        while (newLevel < string.length()) {
            if (!Character.isWhitespace(string.charAt(newLevel))) {
                break;
            }
            newLevel++;
        }
        if ((newLevel % indentSize) != 0) {
            throw new IllegalArgumentException("Unexpected " + newLevel + " whitespace chars at line " + line);
        }
        if (Math.abs(level - newLevel) > indentSize) {
            throw new IllegalArgumentException("Unexpected jump from " + level + " to " + newLevel
                    + " whitespace chars at line " + line + " for indenting with " + indentSize + " chars");
        }
        level = newLevel;

        string = reader.readLine();
        line++;

        if (level > 0) {
            indentated = true;
        }
    }

    if (!indentated) {
        // see if a simple xml piece
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        XMLStreamReader parser = inputFactory.createXMLStreamReader(new StringReader(out));

        int elementMaxLevel = -1;
        int elementLevel = 0;
        do {
            int event = parser.next();
            if (event == XMLStreamConstants.START_ELEMENT) {
                elementLevel++;

                if (elementMaxLevel < elementLevel) {
                    elementMaxLevel = elementLevel;
                }
            } else if (event == XMLStreamConstants.END_ELEMENT) {
                elementLevel--;
            }
        } while (parser.hasNext());

        if (elementMaxLevel > 1) { // should be indentated
            return false;
        }
        return true;
    }

    return indentated;
}

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]";
                }//from   w ww  .ja va2s  .  co 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);
        }
    }
}