Search in sources :

Example 96 with XMLInputFactory

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

the class AbstractBinder method buildStaxFactory.

@SuppressWarnings({ "UnnecessaryLocalVariable" })
private XMLInputFactory buildStaxFactory() {
    XMLInputFactory staxFactory = XMLInputFactory.newInstance();
    staxFactory.setXMLResolver(xmlResourceResolver);
    return staxFactory;
}
Also used : XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 97 with XMLInputFactory

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

the class StAXJAXBIteratorExpression method evaluate.

@Override
@SuppressWarnings("unchecked")
public Object evaluate(Exchange exchange) {
    try {
        XMLEventReader reader;
        if (isNamespaceAware) {
            reader = exchange.getIn().getMandatoryBody(XMLEventReader.class);
        } else {
            InputStream inputStream = exchange.getIn().getMandatoryBody(InputStream.class);
            XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
            xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false);
            reader = xmlInputFactory.createXMLEventReader(inputStream);
        }
        Class<T> clazz = handled;
        if (clazz == null && handledName != null) {
            clazz = (Class<T>) exchange.getContext().getClassResolver().resolveMandatoryClass(handledName);
        }
        return createIterator(reader, clazz);
    } catch (InvalidPayloadException e) {
        exchange.setException(e);
        return null;
    } catch (JAXBException e) {
        exchange.setException(e);
        return null;
    } catch (ClassNotFoundException e) {
        exchange.setException(e);
        return null;
    } catch (XMLStreamException e) {
        exchange.setException(e);
        return null;
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) InputStream(java.io.InputStream) JAXBException(javax.xml.bind.JAXBException) XMLEventReader(javax.xml.stream.XMLEventReader) InvalidPayloadException(org.apache.camel.InvalidPayloadException) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 98 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project hudson-2.x by hudson.

the class ExternalRun method acceptRemoteSubmission.

/**
     * Instead of performing a build, accept the log and the return code
     * from a remote machine.
     *
     * <p>
     * The format of the XML is:
     *
     * <pre><xmp>
     * <run>
     *  <log>...console output...</log>
     *  <result>exit code</result>
     * </run>
     * </xmp></pre>
     */
@SuppressWarnings({ "Since15" })
@IgnoreJRERequirement
public void acceptRemoteSubmission(final Reader in) throws IOException {
    final long[] duration = new long[1];
    run(new Runner() {

        private String elementText(XMLStreamReader r) throws XMLStreamException {
            StringBuilder buf = new StringBuilder();
            while (true) {
                int type = r.next();
                if (type == CHARACTERS || type == CDATA)
                    buf.append(r.getTextCharacters(), r.getTextStart(), r.getTextLength());
                else
                    return buf.toString();
            }
        }

        public Result run(BuildListener listener) throws Exception {
            PrintStream logger = new PrintStream(new DecodingStream(listener.getLogger()));
            XMLInputFactory xif = XMLInputFactory.newInstance();
            XMLStreamReader p = xif.createXMLStreamReader(in);
            // get to the <run>
            p.nextTag();
            // get to the <log>
            p.nextTag();
            charset = p.getAttributeValue(null, "content-encoding");
            while (p.next() != END_ELEMENT) {
                int type = p.getEventType();
                if (type == CHARACTERS || type == CDATA)
                    logger.print(p.getText());
            }
            // get to <result>
            p.nextTag();
            Result r = Integer.parseInt(elementText(p)) == 0 ? Result.SUCCESS : Result.FAILURE;
            // get to <duration> (optional)
            p.nextTag();
            if (p.getEventType() == START_ELEMENT && p.getLocalName().equals("duration")) {
                duration[0] = Long.parseLong(elementText(p));
            }
            return r;
        }

        public void post(BuildListener listener) {
        // do nothing
        }

        public void cleanUp(BuildListener listener) {
        // do nothing
        }
    });
    if (duration[0] != 0) {
        super.duration = duration[0];
        // save the updated duration
        save();
    }
}
Also used : PrintStream(java.io.PrintStream) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) DecodingStream(hudson.util.DecodingStream) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) XMLInputFactory(javax.xml.stream.XMLInputFactory) IgnoreJRERequirement(org.jvnet.animal_sniffer.IgnoreJRERequirement)

Example 99 with XMLInputFactory

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

the class OSMInputFile method openXMLStream.

private void openXMLStream(InputStream in) throws XMLStreamException {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    parser = factory.createXMLStreamReader(in, "UTF-8");
    int event = parser.next();
    if (event != XMLStreamConstants.START_ELEMENT || !parser.getLocalName().equalsIgnoreCase("osm")) {
        throw new IllegalArgumentException("File is not a valid OSM stream");
    }
    // See https://wiki.openstreetmap.org/wiki/PBF_Format#Definition_of_the_OSMHeader_fileblock
    String timestamp = parser.getAttributeValue(null, "osmosis_replication_timestamp");
    if (timestamp == null)
        timestamp = parser.getAttributeValue(null, "timestamp");
    if (timestamp != null) {
        try {
            fileheader = new OSMFileHeader();
            fileheader.setTag("timestamp", timestamp);
        } catch (Exception ex) {
        }
    }
    eof = false;
}
Also used : XMLInputFactory(javax.xml.stream.XMLInputFactory) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 100 with XMLInputFactory

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

the class InterfaceBuilderClassesPlugin method findCustomClassesInIBFile.

private List<String> findCustomClassesInIBFile(File file) throws XMLStreamException, IOException {
    List<String> customClasses = new ArrayList<>();
    try (FileInputStream fis = FileUtils.openInputStream(file)) {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLStreamReader reader = factory.createXMLStreamReader(fis);
        while (reader.hasNext()) {
            int event = reader.next();
            switch(event) {
                case XMLStreamConstants.START_ELEMENT:
                    String customClass = reader.getAttributeValue(null, "customClass");
                    if (customClass != null && !customClass.trim().isEmpty()) {
                        customClasses.add(customClass);
                    }
            }
        }
        reader.close();
    }
    return customClasses;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) ArrayList(java.util.ArrayList) FileInputStream(java.io.FileInputStream) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Aggregations

XMLInputFactory (javax.xml.stream.XMLInputFactory)182 XMLStreamReader (javax.xml.stream.XMLStreamReader)114 XMLStreamException (javax.xml.stream.XMLStreamException)74 InputStream (java.io.InputStream)54 StringReader (java.io.StringReader)45 IOException (java.io.IOException)40 XMLEventReader (javax.xml.stream.XMLEventReader)36 ByteArrayInputStream (java.io.ByteArrayInputStream)30 Test (org.junit.Test)29 InputStreamReader (java.io.InputStreamReader)19 Unmarshaller (javax.xml.bind.Unmarshaller)18 XMLEvent (javax.xml.stream.events.XMLEvent)15 StAXSource (javax.xml.transform.stax.StAXSource)15 StreamSource (javax.xml.transform.stream.StreamSource)15 ArrayList (java.util.ArrayList)14 JAXBException (javax.xml.bind.JAXBException)14 HashMap (java.util.HashMap)13 DOMSource (javax.xml.transform.dom.DOMSource)12 StartElement (javax.xml.stream.events.StartElement)10 DeploymentUnitProcessingException (org.jboss.as.server.deployment.DeploymentUnitProcessingException)10