Search in sources :

Example 1 with XMLInputFactory

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

the class StaxConverter method createXMLInputFactory.

public static XMLInputFactory createXMLInputFactory(boolean nsAware) {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    setProperty(factory, XMLInputFactory.IS_NAMESPACE_AWARE, nsAware);
    setProperty(factory, XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
    setProperty(factory, XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);
    setProperty(factory, XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
    factory.setXMLResolver(new XMLResolver() {

        public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace) throws XMLStreamException {
            throw new XMLStreamException("Reading external entities is disabled");
        }
    });
    if (isWoodstox(factory)) {
        // just log a debug as we are good then
        LOG.debug("Created Woodstox XMLInputFactory: {}", factory);
    } else {
        // log a hint that woodstock may be a better factory to use
        LOG.info("Created XMLInputFactory: {}. DOMSource/DOMResult may have issues with {}. We suggest using Woodstox.", factory, factory);
    }
    return factory;
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) XMLResolver(javax.xml.stream.XMLResolver) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 2 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory 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 XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project hbase by apache.

the class RemoteAdmin method getInputStream.

/**
   * Convert the REST server's response to an XML reader.
   *
   * @param response The REST server's response.
   * @return A reader over the parsed XML document.
   * @throws IOException If the document fails to parse
   */
private XMLStreamReader getInputStream(Response response) throws IOException {
    try {
        // Prevent the parser from reading XMl with external entities defined
        XMLInputFactory xif = XMLInputFactory.newFactory();
        xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
        xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
        return xif.createXMLStreamReader(new ByteArrayInputStream(response.getBody()));
    } catch (XMLStreamException e) {
        throw new IOException("Failed to parse XML", e);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 4 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory 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 5 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project OpenRefine by OpenRefine.

the class XmlImporter method createXMLStreamReader.

private static final XMLStreamReader createXMLStreamReader(InputStream inputStream) throws XMLStreamException, IOException {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    factory.setProperty(XMLInputFactory.IS_COALESCING, true);
    factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, true);
    return factory.createXMLStreamReader(wrapPrefixRemovingInputStream(inputStream));
}
Also used : XMLInputFactory(javax.xml.stream.XMLInputFactory)

Aggregations

XMLInputFactory (javax.xml.stream.XMLInputFactory)154 XMLStreamReader (javax.xml.stream.XMLStreamReader)98 XMLStreamException (javax.xml.stream.XMLStreamException)63 StringReader (java.io.StringReader)43 InputStream (java.io.InputStream)41 IOException (java.io.IOException)33 XMLEventReader (javax.xml.stream.XMLEventReader)30 Test (org.junit.Test)22 ByteArrayInputStream (java.io.ByteArrayInputStream)19 InputStreamReader (java.io.InputStreamReader)14 JAXBException (javax.xml.bind.JAXBException)14 StAXSource (javax.xml.transform.stax.StAXSource)14 StreamSource (javax.xml.transform.stream.StreamSource)14 Unmarshaller (javax.xml.bind.Unmarshaller)13 ArrayList (java.util.ArrayList)12 XMLEvent (javax.xml.stream.events.XMLEvent)12 DOMSource (javax.xml.transform.dom.DOMSource)11 DeploymentUnitProcessingException (org.jboss.as.server.deployment.DeploymentUnitProcessingException)10 JAXBContext (javax.xml.bind.JAXBContext)9 HashMap (java.util.HashMap)8