Example usage for javax.xml.stream XMLInputFactory createXMLStreamReader

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

Introduction

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

Prototype

public abstract XMLStreamReader createXMLStreamReader(java.io.InputStream stream) throws XMLStreamException;

Source Link

Document

Create a new XMLStreamReader from a java.io.InputStream

Usage

From source file:com.flexive.chemistry.webdav.TextDocumentResource.java

protected void processXmlProperties(InputStream in) {
    final XMLInputFactory factory = XMLInputFactory.newInstance();
    try {// w  w w .j av a2  s .c o m
        final XMLStreamReader parser = factory.createXMLStreamReader(in);
        for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
            switch (event) {
            case XMLStreamConstants.START_ELEMENT:
                if ("property".equals(parser.getLocalName())) {
                    processProperty(parser);
                } else if ("name".equals(parser.getLocalName())) {
                    processName(parser);
                }

            }
        }
    } catch (XMLStreamException e) {
        throw new RuntimeException("Failed to replace content: " + e.getMessage(), e);
    }
}

From source file:com.activiti.service.activiti.ProcessDefinitionService.java

protected BpmnModel executeRequestForXML(HttpUriRequest request, ServerConfig serverConfig,
        int expectedStatusCode) {

    ActivitiServiceException exception = null;
    CloseableHttpClient client = clientUtil.getHttpClient(serverConfig);
    try {//from ww  w  .j  a  va2  s . c o m
        CloseableHttpResponse response = client.execute(request);

        try {
            InputStream responseContent = response.getEntity().getContent();
            XMLInputFactory xif = XMLInputFactory.newInstance();
            InputStreamReader in = new InputStreamReader(responseContent, "UTF-8");
            XMLStreamReader xtr = xif.createXMLStreamReader(in);
            BpmnModel bpmnModel = new BpmnXMLConverter().convertToBpmnModel(xtr);

            boolean success = response.getStatusLine() != null
                    && response.getStatusLine().getStatusCode() == expectedStatusCode;

            if (success) {
                return bpmnModel;
            } else {
                exception = new ActivitiServiceException(
                        "An error occured while calling Activiti: " + response.getStatusLine());
            }
        } catch (Exception e) {
            log.warn("Error consuming response from uri " + request.getURI(), e);
            exception = clientUtil.wrapException(e, request);
        } finally {
            response.close();
        }

    } catch (Exception e) {
        log.error("Error executing request to uri " + request.getURI(), e);
        exception = clientUtil.wrapException(e, request);

    } finally {
        try {
            client.close();
        } catch (Exception e) {
            log.warn("Error closing http client instance", e);
        }
    }

    if (exception != null) {
        throw exception;
    }

    return null;
}

From source file:org.flowable.admin.service.engine.ProcessDefinitionService.java

protected BpmnModel executeRequestForXML(HttpUriRequest request, ServerConfig serverConfig,
        int expectedStatusCode) {

    FlowableServiceException exception = null;
    CloseableHttpClient client = clientUtil.getHttpClient(serverConfig);
    try {//from ww w. j  a  v  a 2  s  .  c o m
        CloseableHttpResponse response = client.execute(request);

        try {
            InputStream responseContent = response.getEntity().getContent();
            XMLInputFactory xif = XMLInputFactory.newInstance();
            InputStreamReader in = new InputStreamReader(responseContent, "UTF-8");
            XMLStreamReader xtr = xif.createXMLStreamReader(in);
            BpmnModel bpmnModel = new BpmnXMLConverter().convertToBpmnModel(xtr);

            boolean success = response.getStatusLine() != null
                    && response.getStatusLine().getStatusCode() == expectedStatusCode;

            if (success) {
                return bpmnModel;
            } else {
                exception = new FlowableServiceException(
                        "An error occurred while calling Flowable: " + response.getStatusLine());
            }
        } catch (Exception e) {
            log.warn("Error consuming response from uri {}", request.getURI(), e);
            exception = clientUtil.wrapException(e, request);
        } finally {
            response.close();
        }

    } catch (Exception e) {
        log.error("Error executing request to uri {}", request.getURI(), e);
        exception = clientUtil.wrapException(e, request);

    } finally {
        try {
            client.close();
        } catch (Exception e) {
            log.warn("Error closing http client instance", e);
        }
    }

    if (exception != null) {
        throw exception;
    }

    return null;
}

From source file:com.activiti.image.BpmnImageTest.java

protected BpmnModel getBpmnModel(String file) throws Exception {
    BpmnXMLConverter xmlConverter = new BpmnXMLConverter();
    InputStream xmlStream = this.getClass().getClassLoader().getResourceAsStream(file);
    XMLInputFactory xif = XMLInputFactory.newInstance();
    InputStreamReader in = new InputStreamReader(xmlStream);
    XMLStreamReader xtr = xif.createXMLStreamReader(in);
    BpmnModel bpmnModel = xmlConverter.convertToBpmnModel(xtr);
    return bpmnModel;
}

From source file:eionet.webq.converter.XmlSchemaExtractor.java

/**
 * Extracts {@code @xsi:noNamespaceSchemaLocation} or {@code @xsi:schemaLocation} attribute value from xml root element.
 *
 * @param source source to be searched.//from  w w  w  .j  a v a 2s  .co m
 * @return {@code @xsi:noNamespaceSchemaLocation} or {@code @xsi:schemaLocation} attribute value, default {@code null}
 */
public String extractXmlSchema(byte[] source) {
    XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    ByteArrayInputStream bais = new ByteArrayInputStream(source);
    XMLStreamReader xmlStreamReader = null;
    try {
        xmlStreamReader = xmlInputFactory.createXMLStreamReader(bais);
        while (xmlStreamReader.hasNext()) {
            if (xmlStreamReader.next() == START_ELEMENT) {
                return StringUtils.defaultString(
                        parseNoNamespaceSchemaLocation(xmlStreamReader.getAttributeValue(XSI_NAMESPACE_URI,
                                "noNamespaceSchemaLocation")),
                        parseSchemaLocation(
                                xmlStreamReader.getAttributeValue(XSI_NAMESPACE_URI, "schemaLocation")));
            }
        }
    } catch (Exception e) {
        LOGGER.warn("exception thrown during extracting xml schema", e);
    } finally {
        IOUtils.closeQuietly(bais);
        if (xmlStreamReader != null) {
            try {
                xmlStreamReader.close();
            } catch (XMLStreamException e) {
                LOGGER.warn("unable to close xml stream", e);
            }
        }
    }
    return null;
}

From source file:edu.harvard.i2b2.crc.axis2.QueryService.java

/**
 * Function constructs OMElement for the given String
 * // w w  w .  j  a v  a 2s.  c o m
 * @param xmlString
 * @return OMElement
 * @throws XMLStreamException
 */
private OMElement buildOMElementFromString(String xmlString) throws XMLStreamException {
    XMLInputFactory xif = XMLInputFactory.newInstance();
    StringReader strReader = new StringReader(xmlString);
    XMLStreamReader reader = xif.createXMLStreamReader(strReader);
    StAXOMBuilder builder = new StAXOMBuilder(reader);
    OMElement element = builder.getDocumentElement();
    return element;
}

From source file:edu.harvard.i2b2.crc.dao.setfinder.querybuilder.temporal.TemporalPanelCellQueryItem.java

private OMElement buildOMElement(RequestMessageType requestMessageType, String requestXml)
        throws XMLStreamException, JAXBUtilException {
    StringWriter strWriter = new StringWriter();
    edu.harvard.i2b2.crc.datavo.i2b2message.ObjectFactory hiveof = new edu.harvard.i2b2.crc.datavo.i2b2message.ObjectFactory();
    jaxbUtil.marshaller(hiveof.createRequest(requestMessageType), strWriter);

    //insert request
    String msgBody = "<message_body/>";
    String xmlrequest = strWriter.toString();
    int index = xmlrequest.indexOf(msgBody);
    if (index >= 0) {
        xmlrequest = xmlrequest.substring(0, index) + "<message_body>" + requestXml + "</message_body>"
                + xmlrequest.substring(index + msgBody.length() + 1);
    }/*from ww  w  .ja v a 2  s  .c  om*/

    StringReader strReader = new StringReader(xmlrequest);
    XMLInputFactory xif = XMLInputFactory.newInstance();
    XMLStreamReader reader = xif.createXMLStreamReader(strReader);
    StAXOMBuilder builder = new StAXOMBuilder(reader);
    OMElement request = builder.getDocumentElement();
    return request;
}

From source file:fr.inria.oak.paxquery.pact.io.XmlNavTreePatternInputFormat.java

@Override
public void open(FileInputSplit split) throws IOException {
    super.open(split);

    this.documentID = split.getPath().toString();

    XMLInputFactory factory = XMLInputFactory.newInstance();
    try {/*from   w  ww .j  a  v a  2  s.  c  o  m*/
        this.streamReader = factory.createXMLStreamReader(this.stream);
    } catch (XMLStreamException e) {
        logger.error("XMLStreamException", e);
    }

    this.extractor = new SingleDocumentExtractor(this.navigationTreePattern, this.streamReader);
}

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);
}