Example usage for javax.xml.stream XMLStreamReader isStartElement

List of usage examples for javax.xml.stream XMLStreamReader isStartElement

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamReader isStartElement.

Prototype

public boolean isStartElement();

Source Link

Document

Returns true if the cursor points to a start tag (otherwise false)

Usage

From source file:org.mule.module.xml.filters.AbstractJaxpFilter.java

public Node toDOMNode(Object src) throws Exception {
    if (src instanceof Node) {
        return (Document) src;
    } else if (src instanceof org.dom4j.Document) {
        org.dom4j.Document dom4j = (org.dom4j.Document) src;
        DOMDocument dom = new DOMDocument();
        dom.setDocument(dom4j);/*  w  ww.ja  va 2s  .  co  m*/
        return dom;
    } else if (src instanceof OutputHandler) {
        OutputHandler handler = ((OutputHandler) src);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        handler.write(RequestContext.getEvent(), output);
        InputStream stream = new ByteArrayInputStream(output.toByteArray());
        return getDocumentBuilderFactory().newDocumentBuilder().parse(stream);
    } else if (src instanceof byte[]) {
        ByteArrayInputStream stream = new ByteArrayInputStream((byte[]) src);
        return getDocumentBuilderFactory().newDocumentBuilder().parse(stream);
    } else if (src instanceof InputStream) {
        return getDocumentBuilderFactory().newDocumentBuilder().parse((InputStream) src);
    } else if (src instanceof String) {
        return getDocumentBuilderFactory().newDocumentBuilder()
                .parse(new InputSource(new StringReader((String) src)));
    } else if (src instanceof XMLStreamReader) {
        XMLStreamReader xsr = (XMLStreamReader) src;

        // StaxSource requires that we advance to a start element/document event
        if (!xsr.isStartElement() && xsr.getEventType() != XMLStreamConstants.START_DOCUMENT) {
            xsr.nextTag();
        }

        return getDocumentBuilderFactory().newDocumentBuilder().parse(new InputSource());
    } else if (src instanceof DelayedResult) {
        DelayedResult result = ((DelayedResult) src);
        DOMResult domResult = new DOMResult();
        result.write(domResult);
        return domResult.getNode();
    } else {
        return (Node) xmlToDom.transform(src);
    }
}

From source file:org.mule.module.xml.util.XMLUtils.java

/**
 * Convert our object to a Source type efficiently.
 *//*from  ww  w.j  a v a 2  s . com*/
public static javax.xml.transform.Source toXmlSource(javax.xml.stream.XMLInputFactory xmlInputFactory,
        boolean useStaxSource, Object src) throws Exception {
    if (src instanceof javax.xml.transform.Source) {
        return (Source) src;
    } else if (src instanceof byte[]) {
        ByteArrayInputStream stream = new ByteArrayInputStream((byte[]) src);
        return toStreamSource(xmlInputFactory, useStaxSource, stream);
    } else if (src instanceof InputStream) {
        return toStreamSource(xmlInputFactory, useStaxSource, (InputStream) src);
    } else if (src instanceof String) {
        if (useStaxSource) {
            return new StaxSource(xmlInputFactory.createXMLStreamReader(new StringReader((String) src)));
        } else {
            return new StreamSource(new StringReader((String) src));
        }
    } else if (src instanceof org.dom4j.Document) {
        return new DocumentSource((org.dom4j.Document) src);
    } else if (src instanceof org.xml.sax.InputSource) {
        return new SAXSource((InputSource) src);
    }
    // TODO MULE-3555
    else if (src instanceof XMLStreamReader) {
        XMLStreamReader xsr = (XMLStreamReader) src;

        // StaxSource requires that we advance to a start element/document event
        if (!xsr.isStartElement() && xsr.getEventType() != XMLStreamConstants.START_DOCUMENT) {
            xsr.nextTag();
        }

        return new StaxSource((XMLStreamReader) src);
    } else if (src instanceof org.w3c.dom.Document || src instanceof org.w3c.dom.Element) {
        return new DOMSource((org.w3c.dom.Node) src);
    } else if (src instanceof DelayedResult) {
        DelayedResult result = ((DelayedResult) src);
        DOMResult domResult = new DOMResult();
        result.write(domResult);
        return new DOMSource(domResult.getNode());
    } else if (src instanceof OutputHandler) {
        OutputHandler handler = ((OutputHandler) src);
        ByteArrayOutputStream output = new ByteArrayOutputStream();

        handler.write(RequestContext.getEvent(), output);

        return toStreamSource(xmlInputFactory, useStaxSource, new ByteArrayInputStream(output.toByteArray()));
    } else {
        return null;
    }
}

From source file:org.osaf.cosmo.model.text.XhtmlCollectionFormat.java

public CollectionItem parse(String source, EntityFactory entityFactory) throws ParseException {
    CollectionItem collection = entityFactory.createCollection();

    try {//from   ww  w  .  j  a v a2 s . c om
        if (source == null)
            throw new ParseException("Source has no XML data", -1);
        StringReader sr = new StringReader(source);
        XMLStreamReader reader = createXmlReader(sr);

        boolean inCollection = false;
        while (reader.hasNext()) {
            reader.next();
            if (!reader.isStartElement())
                continue;

            if (hasClass(reader, "collection")) {
                if (log.isDebugEnabled())
                    log.debug("found collection element");
                inCollection = true;
                continue;
            }

            if (inCollection && hasClass(reader, "name")) {
                if (log.isDebugEnabled())
                    log.debug("found name element");

                String name = reader.getElementText();
                if (StringUtils.isBlank(name))
                    throw new ParseException("Empty name not allowed",
                            reader.getLocation().getCharacterOffset());
                collection.setDisplayName(name);

                continue;
            }

            if (inCollection && hasClass(reader, "uuid")) {
                if (log.isDebugEnabled())
                    log.debug("found uuid element");

                String uuid = reader.getElementText();
                if (StringUtils.isBlank(uuid))
                    throw new ParseException("Empty uuid not allowed",
                            reader.getLocation().getCharacterOffset());
                collection.setUid(uuid);

                continue;
            }
        }

        reader.close();
    } catch (XMLStreamException e) {
        handleXmlException("Error reading XML", e);
    }

    return collection;
}

From source file:org.osaf.cosmo.model.text.XhtmlPreferenceFormat.java

public Preference parse(String source, EntityFactory entityFactory) throws ParseException {
    Preference pref = entityFactory.createPreference();

    try {/*from w ww .j  ava2 s .  c  o  m*/
        if (source == null)
            throw new ParseException("Source has no XML data", -1);
        StringReader sr = new StringReader(source);
        XMLStreamReader reader = createXmlReader(sr);

        boolean inPreference = false;
        while (reader.hasNext()) {
            reader.next();
            if (!reader.isStartElement())
                continue;

            if (hasClass(reader, "preference")) {
                if (log.isDebugEnabled())
                    log.debug("found preference element");
                inPreference = true;
                continue;
            }

            if (inPreference && hasClass(reader, "key")) {
                if (log.isDebugEnabled())
                    log.debug("found key element");

                String key = reader.getElementText();
                if (StringUtils.isBlank(key))
                    handleParseException("Key element must not be empty", reader);
                pref.setKey(key);

                continue;
            }

            if (inPreference && hasClass(reader, "value")) {
                if (log.isDebugEnabled())
                    log.debug("found value element");

                String value = reader.getElementText();
                if (StringUtils.isBlank(value))
                    value = "";
                pref.setValue(value);

                continue;
            }
        }

        reader.close();
    } catch (XMLStreamException e) {
        handleXmlException("Error reading XML", e);
    }

    return pref;
}

From source file:org.osaf.cosmo.model.text.XhtmlSubscriptionFormat.java

public CollectionSubscription parse(String source, EntityFactory entityFactory) throws ParseException {
    CollectionSubscription sub = entityFactory.createCollectionSubscription();

    try {/*from  ww w .ja v a2 s .  c o m*/
        if (source == null)
            throw new ParseException("Source has no XML data", -1);
        StringReader sr = new StringReader(source);
        XMLStreamReader reader = createXmlReader(sr);

        boolean inLocalSub = false;
        boolean inCollection = false;
        boolean inTicket = false;
        while (reader.hasNext()) {
            reader.next();
            if (!reader.isStartElement())
                continue;

            if (hasClass(reader, "local-subscription")) {
                if (log.isDebugEnabled())
                    log.debug("found local-subscription element");
                inLocalSub = true;
                continue;
            }

            if (inLocalSub && hasClass(reader, "name")) {
                if (log.isDebugEnabled())
                    log.debug("found name element");

                String name = reader.getElementText();
                if (StringUtils.isBlank(name))
                    handleParseException("Name element must not be empty", reader);
                sub.setDisplayName(name);

                continue;
            }

            if (inLocalSub && hasClass(reader, "collection")) {
                if (log.isDebugEnabled())
                    log.debug("found collection element");
                inCollection = true;
                inTicket = false;
                continue;
            }

            if (inCollection && hasClass(reader, "uuid")) {
                if (log.isDebugEnabled())
                    log.debug("found uuid element");

                String uuid = reader.getElementText();
                if (StringUtils.isBlank(uuid))
                    handleParseException("Uuid element must not be empty", reader);
                sub.setCollectionUid(uuid);

                continue;
            }

            if (inLocalSub && hasClass(reader, "ticket")) {
                if (log.isDebugEnabled())
                    log.debug("found ticket element");
                inCollection = false;
                inTicket = true;
                continue;
            }

            if (inTicket && hasClass(reader, "key")) {
                if (log.isDebugEnabled())
                    log.debug("found key element");

                String key = reader.getElementText();
                if (StringUtils.isBlank(key))
                    handleParseException("Key element must not be empty", reader);
                sub.setTicketKey(key);

                continue;
            }
        }

        reader.close();
    } catch (XMLStreamException e) {
        handleXmlException("Error reading XML", e);
    }

    return sub;
}

From source file:org.osaf.cosmo.model.text.XhtmlTicketFormat.java

public Ticket parse(String source, EntityFactory entityFactory) throws ParseException {

    String key = null;//  w w  w  . j a v  a 2 s . c om
    TicketType type = null;
    Integer timeout = null;
    try {
        if (source == null)
            throw new ParseException("Source has no XML data", -1);
        StringReader sr = new StringReader(source);
        XMLStreamReader reader = createXmlReader(sr);

        boolean inTicket = false;
        while (reader.hasNext()) {
            reader.next();
            if (!reader.isStartElement())
                continue;

            if (hasClass(reader, "ticket")) {
                if (log.isDebugEnabled())
                    log.debug("found ticket element");
                inTicket = true;
                continue;
            }

            if (inTicket && hasClass(reader, "key")) {
                if (log.isDebugEnabled())
                    log.debug("found key element");

                key = reader.getElementText();
                if (StringUtils.isBlank(key))
                    handleParseException("Key element must not be empty", reader);

                continue;
            }

            if (inTicket && hasClass(reader, "type")) {
                if (log.isDebugEnabled())
                    log.debug("found type element");

                String typeId = reader.getAttributeValue(null, "title");
                if (StringUtils.isBlank(typeId))
                    handleParseException("Ticket type title must not be empty", reader);
                type = TicketType.createInstance(typeId);

                continue;
            }
            if (inTicket && hasClass(reader, "timeout")) {
                if (log.isDebugEnabled())
                    log.debug("found timeout element");

                String timeoutString = reader.getAttributeValue(null, "title");
                if (StringUtils.isBlank(timeoutString))
                    timeout = null;
                else
                    timeout = Integer.getInteger(timeoutString);

                continue;
            }
        }
        if (type == null || key == null)
            handleParseException("Ticket must have type and key", reader);
        reader.close();
    } catch (XMLStreamException e) {
        handleXmlException("Error reading XML", e);
    }

    Ticket ticket = entityFactory.createTicket(type);
    ticket.setKey(key);
    if (timeout == null)
        ticket.setTimeout(Ticket.TIMEOUT_INFINITE);
    else
        ticket.setTimeout(timeout);

    return ticket;
}

From source file:org.osaf.cosmo.xml.DomReader.java

private static Node readNode(Document d, XMLStreamReader reader) throws XMLStreamException {
    Node root = null;// w  w  w  .j  av a 2 s  .com
    Node current = null;

    while (reader.hasNext()) {
        reader.next();

        if (reader.isEndElement()) {
            //log.debug("Finished reading " + current.getNodeName());

            if (current.getParentNode() == null)
                break;

            //log.debug("Setting current to " +
            //current.getParentNode().getNodeName());
            current = current.getParentNode();
        }

        if (reader.isStartElement()) {
            Element e = readElement(d, reader);
            if (root == null) {
                //log.debug("Setting root to " + e.getNodeName());
                root = e;
            }

            if (current != null) {
                //log.debug("Appending child " + e.getNodeName() + " to " +
                //current.getNodeName());
                current.appendChild(e);
            }

            //log.debug("Setting current to " + e.getNodeName());
            current = e;

            continue;
        }

        if (reader.isCharacters()) {
            CharacterData cd = d.createTextNode(reader.getText());
            if (root == null)
                return cd;
            if (current == null)
                return cd;

            //log.debug("Appending text '" + cd.getData() + "' to " +
            //current.getNodeName());
            current.appendChild(cd);

            continue;
        }
    }

    return root;
}

From source file:org.tobarsegais.webapp.data.Index.java

public static Index read(String bundle, XMLStreamReader reader) throws XMLStreamException {
    while (reader.hasNext() && !reader.isStartElement()) {
        reader.next();//from   w  w w  .j a  va 2s .c  om
    }
    if (reader.getEventType() != XMLStreamConstants.START_ELEMENT) {
        throw new IllegalStateException("Expecting a start element");
    }
    if (!"index".equals(reader.getLocalName())) {
        throw new IllegalStateException("Expecting a <index> element, found a <" + reader.getLocalName() + ">");
    }
    List<IndexEntry> entries = new ArrayList<IndexEntry>();
    int depth = 0;
    while (reader.hasNext() && depth >= 0) {
        switch (reader.next()) {
        case XMLStreamConstants.START_ELEMENT:
            if (depth == 0 && "entry".equals(reader.getLocalName())) {
                entries.add(IndexEntry.read(bundle, Collections.<String>emptyList(), reader));
            } else {
                depth++;
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            depth--;
            break;
        }
    }
    return new Index(entries);
}

From source file:org.tobarsegais.webapp.data.Plugin.java

public static Plugin read(XMLStreamReader reader) throws XMLStreamException {
    while (reader.hasNext() && !reader.isStartElement()) {
        reader.next();/*from w w  w . j a va2  s  .c o  m*/
    }
    if (reader.getEventType() != XMLStreamConstants.START_ELEMENT) {
        throw new IllegalStateException("Expecting a start element");
    }
    if (!"plugin".equals(reader.getLocalName())) {
        throw new IllegalStateException("Expecting a <plugin> element");
    }
    String name = reader.getAttributeValue(null, "name");
    String id = reader.getAttributeValue(null, "id");
    String version = reader.getAttributeValue(null, "version");
    String providerName = reader.getAttributeValue(null, "provider-name");
    List<Extension> extensions = new ArrayList<Extension>();
    int depth = 0;
    while (reader.hasNext() && depth >= 0) {
        switch (reader.next()) {
        case XMLStreamConstants.START_ELEMENT:
            if (depth == 0 && "extension".equals(reader.getLocalName())) {
                extensions.add(Extension.read(reader));
            } else {
                depth++;
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            depth--;
            break;
        }
    }
    return new Plugin(name, id, version, providerName, extensions);
}

From source file:org.tobarsegais.webapp.data.Toc.java

public static Toc read(XMLStreamReader reader) throws XMLStreamException {
    while (reader.hasNext() && !reader.isStartElement()) {
        reader.next();//from ww  w .j  av  a2 s .co m
    }
    if (reader.getEventType() != XMLStreamConstants.START_ELEMENT) {
        throw new IllegalStateException("Expecting a start element");
    }
    if (!"toc".equals(reader.getLocalName())) {
        throw new IllegalStateException("Expecting a <toc> element");
    }
    String label = reader.getAttributeValue(null, "label");
    String topic = reader.getAttributeValue(null, "topic");
    List<Topic> topics = new ArrayList<Topic>();
    int depth = 0;
    while (reader.hasNext() && depth >= 0) {
        switch (reader.next()) {
        case XMLStreamConstants.START_ELEMENT:
            if (depth == 0 && "topic".equals(reader.getLocalName())) {
                topics.add(Topic.read(reader));
            } else {
                depth++;
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            depth--;
            break;
        }
    }
    return new Toc(label, topic, topics);
}