Example usage for javax.xml.stream XMLInputFactory setProperty

List of usage examples for javax.xml.stream XMLInputFactory setProperty

Introduction

In this page you can find the example usage for javax.xml.stream XMLInputFactory setProperty.

Prototype

public abstract void setProperty(java.lang.String name, Object value) throws java.lang.IllegalArgumentException;

Source Link

Document

Allows the user to set specific feature/property on the underlying implementation.

Usage

From source file:edu.cmu.lti.oaqa.knn4qa.collection_reader.YahooAnswersStreamParser.java

public YahooAnswersStreamParser(String fileName, boolean bDoCleanUp) throws IOException, XMLStreamException {
    mFile = new File(fileName);
    mDoCleanUp = bDoCleanUp;// w  w  w.  j a  va  2s .com

    final XMLInputFactory factory = XMLInputFactory.newInstance();
    factory.setProperty(XMLInputFactory.IS_COALESCING, true);
    mReader = factory.createXMLStreamReader(CompressUtils.createInputStream(fileName));
    mNextDoc = fetchNext();
}

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

public XMLEventReaderWrapper(final InputStream stream) throws Exception {
    final XMLInputFactory factory = XMLInputFactory.newInstance();
    factory.setProperty(XMLInputFactory.IS_VALIDATING, false);
    factory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false);

    this.wrapped = factory.createXMLEventReader(new ByteArrayInputStream(
            (XMLEventReaderWrapper.CONTENT_STAG + IOUtils.toString(stream) + XMLEventReaderWrapper.CONTENT_ETAG)
                    .getBytes()));//w w  w. ja  v  a  2s .c  om

    init(wrapped);
}

From source file:com.dulion.astatium.mesh.shredder.XmlShredder.java

public XmlShredder(MetaGraph metaGraph) {
    this.manager = (ContextManager) metaGraph;
    this.factory = ThreadLocal.withInitial(() -> {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        factory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
        factory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
        factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
        return factory;
    });/*from   www.  j a v a  2  s.c o m*/
}

From source file:org.owasp.webgoat.plugin.XXE.java

private SearchForm parseXml(String xml) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(SearchForm.class);

    XMLInputFactory xif = XMLInputFactory.newFactory();
    xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, true);
    xif.setProperty(XMLInputFactory.SUPPORT_DTD, true);
    XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(xml));

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    return (SearchForm) unmarshaller.unmarshal(xsr);
}

From source file:com.github.lindenb.jvarkit.tools.blast.BlastFilterJS.java

@Override
protected Collection<Throwable> call(String inputName) throws Exception {

    final CompiledScript compiledScript;
    Unmarshaller unmarshaller;/*from  w w w.jav  a 2s. c  o  m*/
    Marshaller marshaller;
    try {
        compiledScript = super.compileJavascript();

        JAXBContext jc = JAXBContext.newInstance("gov.nih.nlm.ncbi.blast");

        unmarshaller = jc.createUnmarshaller();
        marshaller = jc.createMarshaller();

        XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
        xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
        xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
        xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
        xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        PrintWriter pw = openFileOrStdoutAsPrintWriter();
        XMLOutputFactory xof = XMLOutputFactory.newFactory();
        xof.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.FALSE);
        XMLEventWriter w = xof.createXMLEventWriter(pw);

        StreamSource src = null;
        if (inputName == null) {
            LOG.info("Reading stdin");
            src = new StreamSource(stdin());
        } else {
            LOG.info("Reading file " + inputName);
            src = new StreamSource(new File(inputName));
        }

        XMLEventReader r = xmlInputFactory.createXMLEventReader(src);

        XMLEventFactory eventFactory = XMLEventFactory.newFactory();

        SimpleBindings bindings = new SimpleBindings();
        while (r.hasNext()) {
            XMLEvent evt = r.peek();
            switch (evt.getEventType()) {
            case XMLEvent.START_ELEMENT: {
                StartElement sE = evt.asStartElement();
                Hit hit = null;
                JAXBElement<Hit> jaxbElement = null;
                if (sE.getName().getLocalPart().equals("Hit")) {
                    jaxbElement = unmarshaller.unmarshal(r, Hit.class);
                    hit = jaxbElement.getValue();
                } else {
                    w.add(r.nextEvent());
                    break;
                }

                if (hit != null) {

                    bindings.put("hit", hit);

                    boolean accept = super.evalJavaScriptBoolean(compiledScript, bindings);

                    if (accept) {
                        marshaller.marshal(jaxbElement, w);
                        w.add(eventFactory.createCharacters("\n"));
                    }
                }

                break;
            }
            case XMLEvent.SPACE:
                break;
            default: {
                w.add(r.nextEvent());
                break;
            }
            }
            r.close();
        }
        w.flush();
        w.close();
        pw.flush();
        pw.close();
        return RETURN_OK;
    } catch (Exception err) {
        return wrapException(err);
    } finally {

    }
}

From source file:demo.SourceHttpMessageConverter.java

private Source readStAXSource(InputStream body) {
    try {/*from  w  ww.j av  a 2s  .c om*/
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, isSupportDtd());
        inputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, isProcessExternalEntities());
        if (!isProcessExternalEntities()) {
            inputFactory.setXMLResolver(NO_OP_XML_RESOLVER);
        }
        XMLStreamReader streamReader = inputFactory.createXMLStreamReader(body);
        return new StAXSource(streamReader);
    } catch (XMLStreamException ex) {
        throw new HttpMessageNotReadableException("Could not parse document: " + ex.getMessage(), ex);
    }
}

From source file:com.hp.mqm.clt.XmlProcessorTest.java

private void assertXml(List<TestResult> expectedTestResults, Set<XmlElement> expectedElements, File xmlFile)
        throws FileNotFoundException, XMLStreamException {
    FileInputStream fis = new FileInputStream(xmlFile);
    XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    xmlInputFactory.setProperty("javax.xml.stream.isCoalescing", true);
    XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(fis);

    boolean isFirstEvent = true;
    while (xmlStreamReader.hasNext()) {
        if (!isFirstEvent) {
            xmlStreamReader.next();//from  w ww. jav a 2  s.  c  o m
        } else {
            isFirstEvent = false;
        }

        if (xmlStreamReader.getEventType() == XMLStreamReader.START_ELEMENT) {
            String localName = xmlStreamReader.getLocalName();
            if ("taxonomy".equals(localName)) {
                assertElement(localName, false, xmlStreamReader, expectedElements);
            } else if ("test_field".equals(localName)) {
                assertElement(localName, false, xmlStreamReader, expectedElements);
            } else if ("product_area_ref".equals(localName)) {
                assertElement(localName, true, xmlStreamReader, expectedElements);
            } else if ("backlog_item_ref".equals(localName)) {
                assertElement(localName, true, xmlStreamReader, expectedElements);
            } else if ("release_ref".equals(localName)) {
                assertElement(localName, true, xmlStreamReader, expectedElements);
            } else if ("test_run".equals(localName)) {
                assertXmlTest(xmlStreamReader, expectedTestResults);
            }
        }
    }
    xmlStreamReader.close();
    IOUtils.closeQuietly(fis);
    Assert.assertTrue(expectedElements.isEmpty());
    Assert.assertTrue(expectedTestResults.isEmpty());
}

From source file:com.norconex.collector.http.sitemap.impl.StandardSitemapResolver.java

private void parseLocation(InputStream is, HttpClient httpClient, SitemapURLAdder sitemapURLAdder,
        Set<String> resolvedLocations, String location) throws XMLStreamException {

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    inputFactory.setProperty(XMLInputFactory.IS_COALESCING, true);
    XMLStreamReader xmlReader = inputFactory.createXMLStreamReader(is);
    ParseState parseState = new ParseState();

    String locationDir = StringUtils.substringBeforeLast(location, "/");
    int event = xmlReader.getEventType();
    while (true) {
        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            String tag = xmlReader.getLocalName();
            parseStartElement(parseState, tag);
            break;
        case XMLStreamConstants.CHARACTERS:
            String value = xmlReader.getText();
            if (parseState.sitemapIndex && parseState.loc) {
                resolveLocation(value, httpClient, sitemapURLAdder, resolvedLocations);
                parseState.loc = false;//from  w ww.j  a v  a  2 s  .  c o m
            } else if (parseState.baseURL != null) {
                parseCharacters(parseState, value);
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            tag = xmlReader.getLocalName();
            parseEndElement(sitemapURLAdder, parseState, locationDir, tag);
            break;
        }
        if (!xmlReader.hasNext()) {
            break;
        }
        event = xmlReader.next();
    }
}

From source file:com.pocketsoap.salesforce.soap.ChatterClient.java

private <T> T makeSoapRequest(String serverUrl, RequestEntity req, ResponseParser<T> respParser)
        throws XMLStreamException, IOException {
    PostMethod post = new PostMethod(serverUrl);
    post.addRequestHeader("SOAPAction", "\"\"");
    post.setRequestEntity(req);/* w  w w .j a  v a  2  s  .c  o  m*/

    HttpClient http = new HttpClient();
    int sc = http.executeMethod(post);
    if (sc != 200 && sc != 500)
        throw new IOException("request to " + serverUrl + " returned unexpected HTTP status code of " + sc
                + ", check configuration.");

    XMLInputFactory f = XMLInputFactory.newInstance();
    f.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);

    XMLStreamReader rdr = f.createXMLStreamReader(post.getResponseBodyAsStream());
    rdr.require(XMLStreamReader.START_DOCUMENT, null, null);
    rdr.nextTag();
    rdr.require(XMLStreamReader.START_ELEMENT, SOAP_NS, "Envelope");
    rdr.nextTag();
    // TODO, should handle a Header appearing in the response.
    rdr.require(XMLStreamReader.START_ELEMENT, SOAP_NS, "Body");
    rdr.nextTag();
    if (rdr.getLocalName().equals("Fault")) {
        throw handleSoapFault(rdr);
    }
    try {
        T response = respParser.parse(rdr);
        while (rdr.hasNext())
            rdr.next();
        return response;
    } finally {
        try {
            rdr.close();
        } finally {
            post.releaseConnection();
        }
    }
}