Example usage for javax.xml.stream XMLEventReader nextEvent

List of usage examples for javax.xml.stream XMLEventReader nextEvent

Introduction

In this page you can find the example usage for javax.xml.stream XMLEventReader nextEvent.

Prototype

public XMLEvent nextEvent() throws XMLStreamException;

Source Link

Document

Gets the next XMLEvent.

Usage

From source file:corpus.sinhala.crawler.blog.rss.RSSFeedParser.java

private String getCharacterData(XMLEvent event, XMLEventReader eventReader) throws XMLStreamException {
    String result = "";
    event = eventReader.nextEvent();
    if (event instanceof Characters) {
        result = event.asCharacters().getData();
    }//ww w. j  a va 2  s  .c  o m
    return result;
}

From source file:com.predic8.membrane.core.ws.relocator.Relocator.java

private XMLEvent getEvent(XMLEventReader parser) throws XMLStreamException {
    XMLEvent event = parser.nextEvent();
    if (!event.isStartElement())
        return event;

    /*//from w w w.  java  2s .  c o m
     * if (isAddressElement(event)) { return replace(event, "location"); }
     * else if (getElementName(event).equals(INCLUDE)) { return
     * replace(event, "schemaLocation"); } else if
     * (getElementName(event).equals(IMPORT)) { return replace(event,
     * "schemaLocation"); } else if
     * (getElementName(event).equals(WADL_RESOURCES)) { return
     * replace(event, "base"); } else if
     * (getElementName(event).equals(WADL_INCLUDE)) { return replace(event,
     * "href"); } else if (getElementName(event).getNamespaceURI().equals(
     * Constants.WSDL_SOAP11_NS) ||
     * getElementName(event).getNamespaceURI().equals(
     * Constants.WSDL_SOAP12_NS)) { wsdlFound = true; }
     */

    if (getElementName(event).getNamespaceURI().equals(Constants.WSDL_SOAP11_NS)
            || getElementName(event).getNamespaceURI().equals(Constants.WSDL_SOAP12_NS)) {
        wsdlFound = true;
    }

    for (QName qn : relocatingAttributes.keySet()) {
        if (getElementName(event).equals(qn)) {
            return replace(event, relocatingAttributes.get(qn));
        }
    }

    return event;
}

From source file:com.vistatec.ocelot.xliff.okapi.OkapiXLIFFFactory.java

@Override
public XLIFFVersion detectXLIFFVersion(File detectVersion) throws IOException, XMLStreamException {
    try (BOMInputStream bomInputStream = new BOMInputStream(new FileInputStream(detectVersion),
            ByteOrderMark.UTF_8, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_32BE,
            ByteOrderMark.UTF_32LE)) {//from   w ww  . ja  v  a2 s  .c  o  m
        String bom = "UTF-8";
        if (bomInputStream.hasBOM()) {
            bom = bomInputStream.getBOMCharsetName();
        }

        XMLInputFactory xml = XMLInputFactory.newInstance();
        XMLEventReader reader = xml.createXMLEventReader(bomInputStream, bom);
        while (reader.hasNext()) {
            XMLEvent event = reader.nextEvent();
            switch (event.getEventType()) {
            case XMLEvent.START_ELEMENT:
                StartElement startElement = (StartElement) event;
                String localPart = startElement.getName().getLocalPart();
                if (localPart.equals("xliff")) {
                    @SuppressWarnings("unchecked")
                    Iterator<Attribute> attrs = startElement.getAttributes();
                    while (attrs.hasNext()) {
                        Attribute attr = attrs.next();
                        if (isXliffVersionAttributeName(attr.getName())) {
                            String value = attr.getValue();
                            reader.close();
                            if ("2.0".equals(value)) {
                                return XLIFFVersion.XLIFF20;
                            } else {
                                return XLIFFVersion.XLIFF12;
                            }
                        }
                    }
                }
                break;

            default:
                break;
            }
        }
        throw new IllegalStateException("Could not detect XLIFF version");
    }
}

From source file:com.predic8.membrane.core.interceptor.xmlprotection.XMLProtector.java

public boolean protect(InputStreamReader isr) {
    try {//from   w w  w. j a v a  2s  . c o  m
        XMLEventReader parser;
        synchronized (xmlInputFactory) {
            parser = xmlInputFactory.createXMLEventReader(isr);
        }

        while (parser.hasNext()) {
            XMLEvent event = parser.nextEvent();
            if (event.isStartElement()) {
                StartElement startElement = (StartElement) event;
                if (maxElementNameLength != -1)
                    if (startElement.getName().getLocalPart().length() > maxElementNameLength) {
                        log.warn("Element name length: Limit exceeded.");
                        return false;
                    }
                if (maxAttibuteCount != -1) {
                    @SuppressWarnings("rawtypes")
                    Iterator i = startElement.getAttributes();
                    for (int attributeCount = 0; i.hasNext(); i.next())
                        if (++attributeCount == maxAttibuteCount) {
                            log.warn("Number of attributes per element: Limit exceeded.");
                            return false;
                        }
                }
            }
            if (event instanceof javax.xml.stream.events.DTD) {
                if (removeDTD) {
                    log.debug("removed DTD.");
                    continue;
                }
            }
            writer.add(event);
        }
        writer.flush();
    } catch (XMLStreamException e) {
        log.warn("Received not-wellformed XML.");
        return false;
    }
    return true;
}

From source file:com.xiongyingqi.util.xml.XMLEventStreamReader.java

XMLEventStreamReader(XMLEventReader eventReader) throws XMLStreamException {
    this.eventReader = eventReader;
    event = eventReader.nextEvent();
}

From source file:org.fcrepo.serialization.JcrXmlSerializer.java

private void validateJCRXML(final File file) throws InvalidSerializationFormatException, IOException {
    int depth = 0;
    try (final FileInputStream fis = new FileInputStream(file)) {
        final XMLEventReader reader = XMLInputFactory.newFactory().createXMLEventReader(fis);
        while (reader.hasNext()) {
            final XMLEvent event = reader.nextEvent();
            if (event.isStartElement()) {
                depth++;
                final StartElement startElement = event.asStartElement();
                final Attribute nameAttribute = startElement
                        .getAttributeByName(new QName("http://www.jcp.org/jcr/sv/1.0", "name"));
                if (depth == 1 && nameAttribute != null && "jcr:content".equals(nameAttribute.getValue())) {
                    throw new InvalidSerializationFormatException(
                            "Cannot import JCR/XML starting with content node.");
                }/*from  w  ww  .j a va 2  s  .c  o m*/
                if (depth == 1 && nameAttribute != null && "jcr:frozenNode".equals(nameAttribute.getValue())) {
                    throw new InvalidSerializationFormatException("Cannot import historic versions.");
                }
                final QName name = startElement.getName();
                if (!(name.getNamespaceURI().equals("http://www.jcp.org/jcr/sv/1.0")
                        && (name.getLocalPart().equals("node") || name.getLocalPart().equals("property")
                                || name.getLocalPart().equals("value")))) {
                    throw new InvalidSerializationFormatException(
                            "Unrecognized element \"" + name.toString() + "\", in import XML.");
                }
            } else {
                if (event.isEndElement()) {
                    depth--;
                }
            }
        }
        reader.close();
    } catch (XMLStreamException e) {
        throw new InvalidSerializationFormatException(
                "Unable to parse XML" + (e.getMessage() != null ? " (" + e.getMessage() + ")." : "."));
    }
}

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

@Override
public T consume(XMLEventReader eventReader, UnmarshallingContext context) throws XMLStreamException {
    // just skip element's START_ELEMENT event
    StartElement startElement = eventReader.nextEvent().asStartElement();

    // StartElement may contain attributes - these are NOT available as separate events in eventReader.nextEvent()!
    Iterator<?> attributes = startElement.getAttributes();
    List<Attribute> attrList = new LinkedList<Attribute>();
    while (attributes.hasNext()) {
        Attribute a = (Attribute) attributes.next();
        attrList.add(a);//from w  ww .  ja v  a 2  s .  c  o  m
    }

    T value = this.nestedPattern.consumeValue(new AttributesAwareXMLEventReader(eventReader, attrList),
            context);

    // skip element's END_ELEMENT event
    while (eventReader.peek() != null) {
        XMLEvent ev = eventReader.nextEvent();
        if (ev.getEventType() == XMLStreamConstants.END_ELEMENT)
            break;
    }

    return value;
}

From source file:com.msopentech.odatajclient.testservice.utils.XMLUtilities.java

public static StartElement getPropertyStartTag(final XMLEventReader propReader, final String[] path)
        throws Exception {
    int pos = 0;//w w  w. j  a v a2s.com

    StartElement property = null;

    while (propReader.hasNext() && pos < path.length) {
        final XMLEvent event = propReader.nextEvent();

        if (event.getEventType() == XMLStreamConstants.START_ELEMENT
                && (ATOM_PROPERTY_PREFIX + path[pos].trim())
                        .equals(event.asStartElement().getName().getLocalPart())) {
            pos++;
            if (path.length == pos) {
                property = event.asStartElement();
            }
        }
    }

    if (property == null) {
        throw new NotFoundException();
    }

    return property;
}

From source file:com.msopentech.odatajclient.testservice.utils.XMLUtilities.java

public static InputStream getAtomPropertyValue(final InputStream is, final String[] path) throws Exception {
    final XmlElement props = getAtomElement(is, PROPERTIES, 2, 3);
    final XMLEventReader propsReader = props.getContentReader();

    // search for property start element
    getPropertyStartTag(propsReader, ArrayUtils.subarray(path, 0, path.length - 1));

    final InputStream res;

    XMLEvent event = propsReader.nextEvent();

    // expected text node
    if (event.isCharacters()) {
        res = new ByteArrayInputStream(event.asCharacters().getData().getBytes());
    } else if (event.isEndElement()) {
        throw new NotFoundException();
    } else {//from w w  w  . j  ava 2s.c  om
        throw new Exception("The method or operation is not implemented.");
    }

    return res;
}

From source file:org.javelin.sws.ext.bind.jaxb.JaxbTest.java

@Test
public void namespacesOfAttributes() throws Exception {
    XMLEventReader reader = XMLInputFactory.newFactory().createXMLEventReader(
            new ClassPathResource("org/javelin/sws/ext/bind/jaxb/context6/1.xml").getInputStream());
    reader.nextEvent();
    StartElement start = reader.nextEvent().asStartElement();
    for (Iterator<?> it = start.getAttributes(); it.hasNext();) {
        System.out.println(((Attribute) it.next()).getName());
    }/*from  w  w  w  .jav  a 2  s.c  o m*/
    System.out.println("---");
    reader = XMLInputFactory.newFactory().createXMLEventReader(
            new ClassPathResource("org/javelin/sws/ext/bind/jaxb/context6/2.xml").getInputStream());
    reader.nextEvent();
    start = reader.nextEvent().asStartElement();
    for (Iterator<?> it = start.getAttributes(); it.hasNext();) {
        System.out.println(((Attribute) it.next()).getName());
    }

    System.out.println("---");
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    Document doc1 = dbf.newDocumentBuilder().parse(new InputSource(
            new ClassPathResource("org/javelin/sws/ext/bind/jaxb/context6/1.xml").getInputStream()));
    NamedNodeMap attributes1 = doc1.getDocumentElement().getAttributes();
    for (int i = 0; i < attributes1.getLength(); i++)
        System.out.println(((org.w3c.dom.Attr) attributes1.item(i)).getName() + " ("
                + ((org.w3c.dom.Attr) attributes1.item(i)).getNamespaceURI() + ")");

    System.out.println("---");
    doc1 = dbf.newDocumentBuilder().parse(new InputSource(
            new ClassPathResource("org/javelin/sws/ext/bind/jaxb/context6/2.xml").getInputStream()));
    attributes1 = doc1.getDocumentElement().getAttributes();
    for (int i = 0; i < attributes1.getLength(); i++)
        System.out.println(((org.w3c.dom.Attr) attributes1.item(i)).getName() + " ("
                + ((org.w3c.dom.Attr) attributes1.item(i)).getNamespaceURI() + ")");

    System.out.println("---");
    doc1 = dbf.newDocumentBuilder().parse(new InputSource(
            new ClassPathResource("org/javelin/sws/ext/bind/jaxb/context6/3.xml").getInputStream()));
    attributes1 = doc1.getDocumentElement().getAttributes();
    for (int i = 0; i < attributes1.getLength(); i++)
        System.out.println(((org.w3c.dom.Attr) attributes1.item(i)).getName() + " ("
                + ((org.w3c.dom.Attr) attributes1.item(i)).getNamespaceURI() + ")");
}