Example usage for javax.xml.stream XMLStreamConstants START_ELEMENT

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

Introduction

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

Prototype

int START_ELEMENT

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

Click Source Link

Document

Indicates an event is a start element

Usage

From source file:Main.java

/**
 * //w ww. java2  s .co m
 * @param elementName
 * @param is
 * @param onlyValues
 * @return Collection
 * @throws XMLStreamException
 * @throws UnsupportedEncodingException
 */
public static Collection<String> getElements(final String elementName, final InputStream is,
        final boolean onlyValues) throws XMLStreamException, UnsupportedEncodingException {
    final Collection<String> elements = new ArrayList<>();
    final ByteArrayOutputStream os = new ByteArrayOutputStream();
    final XMLEventReader reader = XMLInputFactory.newInstance()
            .createXMLEventReader(new InputStreamReader(is, Charset.defaultCharset().name()));
    final XMLEventWriter writer = XMLOutputFactory.newInstance()
            .createXMLEventWriter(new OutputStreamWriter(os, Charset.defaultCharset().name()));
    boolean read = false;
    String characters = null;

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

        switch (event.getEventType()) {
        case XMLStreamConstants.START_DOCUMENT:
        case XMLStreamConstants.END_DOCUMENT: {
            // Ignore.
            break;
        }
        case XMLStreamConstants.START_ELEMENT: {
            read = read || elementName.equals(event.asStartElement().getName().getLocalPart());

            if (read && !onlyValues) {
                writer.add(event);
            }

            break;
        }
        case XMLStreamConstants.ATTRIBUTE: {
            if (read && !onlyValues) {
                writer.add(event);
            }
            break;
        }
        case XMLStreamConstants.CHARACTERS: {
            if (read && !onlyValues) {
                writer.add(event);
            }
            characters = event.asCharacters().getData();
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            if (read && !onlyValues) {
                writer.add(event);
            }
            if (elementName.equals(event.asEndElement().getName().getLocalPart())) {
                writer.flush();

                if (characters != null) {
                    elements.add(characters);
                }

                os.reset();
                read = false;
            }
            break;
        }
        default: {
            // Ignore
            break;
        }
        }
    }

    return elements;
}

From source file:Main.java

private boolean accept(int eventType) {
    return eventType == XMLStreamConstants.START_ELEMENT || eventType == XMLStreamConstants.END_ELEMENT;
}

From source file:org.maodian.flyingcat.xmpp.codec.SessionCodec.java

@Override
public Object decode(XMLStreamReader xmlsr) {
    try {//w w w. ja v a  2  s  .  c  o m
        xmlsr.require(XMLStreamConstants.START_ELEMENT, XmppNamespace.SESSION, "session");
        return new Session();
    } catch (XMLStreamException e) {
        throw new XmppException(e, StreamError.INVALID_XML);
    }
}

From source file:org.maodian.flyingcat.xmpp.codec.BindCodec.java

@Override
public Bind decode(XMLStreamReader xmlsr) {
    try {// ww w . j av a 2 s.com
        xmlsr.nextTag();
        xmlsr.require(XMLStreamConstants.START_ELEMENT, XmppNamespace.BIND, "resource");
        Bind bind = new Bind();
        bind.setResource(xmlsr.getElementText());
        return bind;
    } catch (XMLStreamException e) {
        throw new XmppException(e, StreamError.INVALID_XML);
    }
}

From source file:org.maodian.flyingcat.xmpp.extensions.xep0030.QueryItemCodec.java

@Override
public Object decode(XMLStreamReader xmlsr) {
    try {//from w  ww . j a  v  a 2s .c om
        xmlsr.require(XMLStreamConstants.START_ELEMENT, ServiceDiscovery.ITEM, "query");
        return new QueryItem();
    } catch (XMLStreamException e) {
        throw new XmppException(e, StreamError.INVALID_XML);
    }
}

From source file:org.maodian.flyingcat.xmpp.extensions.xep0030.QueryInfoCodec.java

@Override
public Object decode(XMLStreamReader xmlsr) {
    try {//from  w  ww  . j av  a 2  s.c om
        xmlsr.require(XMLStreamConstants.START_ELEMENT, ServiceDiscovery.INFORMATION, "query");
        return new QueryInfo();
    } catch (XMLStreamException e) {
        throw new XmppException(e, StreamError.INVALID_XML);
    }
}

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

From source file:com.rockhoppertech.music.midi.js.xml.ModeFactoryXMLHelper.java

/**
 * Read modes.xml and create {@code Scale instances} from those definitions.
 *///from  w w  w .  j a v  a2  s.  co  m
public static void init() {
    List<Integer> intervals = null;
    Scale currentMode = null;
    String tagContent = null;
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLStreamReader reader = null;
    try {
        reader = factory.createXMLStreamReader(ClassLoader.getSystemResourceAsStream("modes.xml"));
    } catch (XMLStreamException e) {
        e.printStackTrace();
        return;
    }

    try {
        while (reader.hasNext()) {
            int event = reader.next();

            switch (event) {
            case XMLStreamConstants.START_ELEMENT:
                String el = reader.getLocalName();
                logger.debug("start element '{}'", el);
                if ("mode".equals(el)) {
                    currentMode = new Scale();
                    intervals = new ArrayList<>();
                }
                if ("modes".equals(el)) {
                    modeList = new ArrayList<>();
                }
                break;

            case XMLStreamConstants.CHARACTERS:
                tagContent = reader.getText().trim();
                logger.debug("tagcontent '{}'", tagContent);
                break;

            case XMLStreamConstants.END_ELEMENT:
                switch (reader.getLocalName()) {
                case "mode":
                    // wow. both guava and commmons to get an int[] array
                    Integer[] array = FluentIterable.from(intervals).toArray(Integer.class);
                    // same as Integer[] array = intervals.toArray(new
                    // Integer[intervals.size()]);
                    int[] a = ArrayUtils.toPrimitive(array);
                    currentMode.setIntervals(a);
                    modeList.add(currentMode);
                    ScaleFactory.registerScale(currentMode);
                    break;
                case "interval":
                    logger.debug("interval '{}'", tagContent);
                    logger.debug("intervals '{}'", intervals);
                    intervals.add(Integer.parseInt(tagContent));
                    break;
                case "name":
                    currentMode.setName(tagContent);
                    break;
                }
                break;

            case XMLStreamConstants.START_DOCUMENT:
                modeList = new ArrayList<>();
                intervals = new ArrayList<>();
                break;
            }
        }
    } catch (XMLStreamException e) {
        logger.error(e.getLocalizedMessage(), e);
        e.printStackTrace();
    }

    logger.debug("mode list \n{}", modeList);
}

From source file:jodtemplate.pptx.io.xml.SlideXmlRelsReader.java

@Override
public Slide read(final String path, final Resources resources, final XMLInputFactory xmlInputFactory,
        final Slide slide) throws XMLStreamException, IOException {
    final Resource slideXmlRelsRes = resources.getResource(Utils.removePrefixSeparator(path));
    try (final InputStream is = slideXmlRelsRes.getInputStream()) {
        final XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(is);
        int event = xmlStreamReader.next();
        while (event != XMLStreamConstants.END_DOCUMENT) {
            if (xmlStreamReader.getEventType() == XMLStreamConstants.START_ELEMENT) {
                final String elementNS = xmlStreamReader.getName().getNamespaceURI();
                final String elementName = xmlStreamReader.getName().getLocalPart();
                if (OOXMLDocument.RELATIONSHIP_ELEMENT.equals(elementName)
                        && OOXMLDocument.RELATIONSHIPS_RELS_NAMESPACE.equals(elementNS)) {
                    final Relationship relationship = createRelationshipElement(xmlStreamReader);
                    slide.addOtherRelationship(relationship);
                }// w w w  . jav a2s.  c  o m
            }
            event = xmlStreamReader.next();
        }
    }

    return slide;
}

From source file:jodtemplate.io.xml.ContentTypesReader.java

@Override
public ContentTypes read(final String path, final Resources resources, final XMLInputFactory xmlInputFactory,
        final ContentTypes contentTypes) throws XMLStreamException, IOException {
    final Resource contentTypesRes = resources.getResource(Utils.removePrefixSeparator(path));
    try (final InputStream is = contentTypesRes.getInputStream()) {
        final XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(is);
        int event = xmlStreamReader.next();
        while (event != XMLStreamConstants.END_DOCUMENT) {
            if (xmlStreamReader.getEventType() == XMLStreamConstants.START_ELEMENT) {
                final String elementNS = xmlStreamReader.getName().getNamespaceURI();
                final String elementName = xmlStreamReader.getName().getLocalPart();
                if (OOXMLDocument.DEFAULT_ELEMENT.equals(elementName)
                        && OOXMLDocument.CONTENT_TYPES_NAMESPACE.equals(elementNS)) {
                    contentTypes.addDefaultElement(createDefaultElement(xmlStreamReader));
                }//from   w w  w  . j  a  v  a2s .  c  o m
                if (OOXMLDocument.OVERRIDE_ELEMENT.equals(elementName)
                        && OOXMLDocument.CONTENT_TYPES_NAMESPACE.equals(elementNS)) {
                    contentTypes.addOverrideElement(createOverrideElement(xmlStreamReader));
                }
            }
            event = xmlStreamReader.next();
        }
    }

    return contentTypes;
}