Search in sources :

Example 1 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project camel by apache.

the class StaxConverterTest method testEncodingXmlEventReader.

public void testEncodingXmlEventReader() throws Exception {
    TEST_XML_WITH_XML_HEADER_ISO_8859_1_AS_BYTE_ARRAY_STREAM.reset();
    XMLEventReader reader = null;
    XMLEventWriter writer = null;
    ByteArrayOutputStream output = null;
    try {
        // enter text encoded with Latin1
        reader = context.getTypeConverter().mandatoryConvertTo(XMLEventReader.class, TEST_XML_WITH_XML_HEADER_ISO_8859_1_AS_BYTE_ARRAY_STREAM);
        output = new ByteArrayOutputStream();
        // ensure UTF-8 encoding
        Exchange exchange = new DefaultExchange(context);
        exchange.setProperty(Exchange.CHARSET_NAME, UTF_8.toString());
        writer = context.getTypeConverter().mandatoryConvertTo(XMLEventWriter.class, exchange, output);
        while (reader.hasNext()) {
            writer.add(reader.nextEvent());
        }
    } finally {
        if (reader != null) {
            reader.close();
        }
        if (writer != null) {
            writer.close();
        }
    }
    assertNotNull(output);
    String result = new String(output.toByteArray(), UTF_8.name());
    // normalize the auotation mark
    if (result.indexOf('\'') > 0) {
        result = result.replace('\'', '"');
    }
    boolean equals = TEST_XML_WITH_XML_HEADER.equals(result) || TEST_XML_WITH_XML_HEADER_ISO_8859_1.equals(result);
    assertTrue("Should match header", equals);
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) XMLEventWriter(javax.xml.stream.XMLEventWriter) XMLEventReader(javax.xml.stream.XMLEventReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 2 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project hadoop by apache.

the class NodeInfo method parseConf.

private static List<NodeInfo> parseConf(InputStream in) throws XMLStreamException {
    QName configuration = new QName("configuration");
    QName property = new QName("property");
    List<NodeInfo> nodes = new ArrayList<NodeInfo>();
    Stack<NodeInfo> parsed = new Stack<NodeInfo>();
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLEventReader reader = factory.createXMLEventReader(in);
    while (reader.hasNext()) {
        XMLEvent event = reader.nextEvent();
        if (event.isStartElement()) {
            StartElement currentElement = event.asStartElement();
            NodeInfo currentNode = new NodeInfo(currentElement);
            if (parsed.isEmpty()) {
                if (!currentElement.getName().equals(configuration)) {
                    return null;
                }
            } else {
                NodeInfo parentNode = parsed.peek();
                QName parentName = parentNode.getStartElement().getName();
                if (parentName.equals(configuration) && currentNode.getStartElement().getName().equals(property)) {
                    @SuppressWarnings("unchecked") Iterator<Attribute> it = currentElement.getAttributes();
                    while (it.hasNext()) {
                        currentNode.addAttribute(it.next());
                    }
                } else if (parentName.equals(property)) {
                    parentNode.addElement(currentElement);
                }
            }
            parsed.push(currentNode);
        } else if (event.isEndElement()) {
            NodeInfo node = parsed.pop();
            if (parsed.size() == 1) {
                nodes.add(node);
            }
        } else if (event.isCharacters()) {
            if (2 < parsed.size()) {
                NodeInfo parentNode = parsed.pop();
                StartElement parentElement = parentNode.getStartElement();
                NodeInfo grandparentNode = parsed.peek();
                if (grandparentNode.getElement(parentElement) == null) {
                    grandparentNode.setElement(parentElement, event.asCharacters());
                }
                parsed.push(parentNode);
            }
        }
    }
    return nodes;
}
Also used : Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) Stack(java.util.Stack) StartElement(javax.xml.stream.events.StartElement) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 3 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project GCViewer by chewiebug.

the class DataReaderIBM_J9_R28 method read.

@Override
public GCModel read() throws IOException {
    GCModel model = new GCModel();
    try {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLEventReader eventReader = factory.createXMLEventReader(in);
        GCEvent currentGcEvent = null;
        String eventNameStart = null;
        while (eventReader.hasNext()) {
            try {
                XMLEvent event = eventReader.nextEvent();
                if (event.isStartElement()) {
                    StartElement startElement = event.asStartElement();
                    switch(startElement.getName().getLocalPart()) {
                        case VERBOSEGC:
                            handleVerboseGC(startElement);
                            break;
                        case INITIALIZED:
                            handleInitialized(eventReader);
                            break;
                        case EXCLUSIVE_START:
                            currentGcEvent = handleExclusiveStart(startElement);
                            break;
                        case SYS_START:
                            assert eventNameStart == null : "eventNameStart was expected to be null, but was " + eventNameStart;
                            eventNameStart = handleSysStart(eventReader, startElement);
                            break;
                        case AF_START:
                            assert eventNameStart == null : "eventNameStart was expected to be null, but was " + eventNameStart;
                            eventNameStart = handleAfStart(eventReader, startElement);
                            break;
                        case GC_START:
                            handleGcStart(eventReader, startElement, currentGcEvent, eventNameStart);
                            break;
                        case GC_END:
                            if (currentGcEvent.getTypeAsString() != null) {
                                handleGcEnd(eventReader, currentGcEvent);
                            }
                            break;
                        case EXCLUSIVE_END:
                            handleExclusiveEnd(startElement, currentGcEvent);
                            if (currentGcEvent.getExtendedType() == null) {
                                if (getLogger().isLoggable(Level.FINE))
                                    getLogger().fine("event at " + in.getLineNumber() + " doesn't contain any information, the parser can handle");
                            } else {
                                model.add(currentGcEvent);
                            }
                            currentGcEvent = null;
                            eventNameStart = null;
                            break;
                    }
                }
            } catch (Exception e) {
                if (e instanceof XMLStreamException) {
                    throw e;
                }
                if (getLogger().isLoggable(Level.WARNING))
                    getLogger().warning("line " + in.getLineNumber() + ": " + e.toString());
                if (getLogger().isLoggable(Level.FINE))
                    getLogger().log(Level.FINE, "line " + in.getLineNumber() + ": " + e.getMessage(), e);
            }
        }
    } catch (XMLStreamException e) {
        if (getLogger().isLoggable(Level.WARNING))
            getLogger().warning("line " + in.getLineNumber() + ": " + e.toString());
        if (getLogger().isLoggable(Level.FINE))
            getLogger().log(Level.FINE, "line " + in.getLineNumber() + ": " + e.getMessage(), e);
    } finally {
        if (getLogger().isLoggable(Level.INFO))
            getLogger().info("Done reading.");
    }
    return model;
}
Also used : StartElement(javax.xml.stream.events.StartElement) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) AbstractGCEvent(com.tagtraum.perf.gcviewer.model.AbstractGCEvent) GCEvent(com.tagtraum.perf.gcviewer.model.GCEvent) GCModel(com.tagtraum.perf.gcviewer.model.GCModel) XMLInputFactory(javax.xml.stream.XMLInputFactory) IOException(java.io.IOException) DateTimeParseException(java.time.format.DateTimeParseException) XMLStreamException(javax.xml.stream.XMLStreamException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 4 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project buck by facebook.

the class FatJar method load.

/**
   * @return the {@link FatJar} object deserialized from the resource name via {@code loader}.
   */
public static FatJar load(ClassLoader loader) throws XMLStreamException, JAXBException, IOException {
    InputStream inputStream = loader.getResourceAsStream(FAT_JAR_INFO_RESOURCE);
    try {
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        try {
            XMLEventReader xmlEventReader = XMLInputFactory.newFactory().createXMLEventReader(bufferedInputStream);
            JAXBContext context = JAXBContext.newInstance(FatJar.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            JAXBElement<FatJar> jaxbElementA = unmarshaller.unmarshal(xmlEventReader, FatJar.class);
            return jaxbElementA.getValue();
        } finally {
            bufferedInputStream.close();
        }
    } finally {
        inputStream.close();
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) XMLEventReader(javax.xml.stream.XMLEventReader) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 5 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project hibernate-orm by hibernate.

the class XmlParserHelper method getJaxbRoot.

public <T> T getJaxbRoot(InputStream stream, Class<T> clazz, Schema schema) throws XmlParsingException {
    XMLEventReader staxEventReader;
    try {
        staxEventReader = createXmlEventReader(stream);
    } catch (XMLStreamException e) {
        throw new XmlParsingException("Unable to create stax reader", e);
    }
    ContextProvidingValidationEventHandler handler = new ContextProvidingValidationEventHandler();
    try {
        staxEventReader = new JpaNamespaceTransformingEventReader(staxEventReader);
        JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        unmarshaller.setSchema(schema);
        unmarshaller.setEventHandler(handler);
        return clazz.cast(unmarshaller.unmarshal(staxEventReader));
    } catch (JAXBException e) {
        StringBuilder builder = new StringBuilder();
        builder.append("Unable to perform unmarshalling at line number ");
        builder.append(handler.getLineNumber());
        builder.append(" and column ");
        builder.append(handler.getColumnNumber());
        builder.append(". Message: ");
        builder.append(handler.getMessage());
        throw new XmlParsingException(builder.toString(), e);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) JAXBException(javax.xml.bind.JAXBException) XMLEventReader(javax.xml.stream.XMLEventReader) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller)

Aggregations

XMLEventReader (javax.xml.stream.XMLEventReader)62 XMLInputFactory (javax.xml.stream.XMLInputFactory)30 XMLEvent (javax.xml.stream.events.XMLEvent)30 XMLStreamException (javax.xml.stream.XMLStreamException)21 StringReader (java.io.StringReader)18 InputStream (java.io.InputStream)15 Test (org.junit.Test)14 StAXSource (javax.xml.transform.stax.StAXSource)13 StartElement (javax.xml.stream.events.StartElement)10 IOException (java.io.IOException)8 StringWriter (java.io.StringWriter)6 Unmarshaller (javax.xml.bind.Unmarshaller)6 Attribute (javax.xml.stream.events.Attribute)6 Document (org.w3c.dom.Document)6 ArrayList (java.util.ArrayList)5 JAXBContext (javax.xml.bind.JAXBContext)5 Diff (org.custommonkey.xmlunit.Diff)5 InputSource (org.xml.sax.InputSource)5 JAXBException (javax.xml.bind.JAXBException)4 DOMSource (javax.xml.transform.dom.DOMSource)4