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:edu.harvard.i2b2.eclipse.plugins.fr.ws.CrcServiceDriver.java

/**
 * Function to convert Ont requestVdo to OMElement
 * /*from w  w w  .j  a v a2  s.  c o m*/
 * @param requestVdo   String requestVdo to send to Ont web service
 * @return An OMElement containing the Ont web service requestVdo
 */
public static OMElement getCrcPayLoad(String requestVdo) throws Exception {
    OMElement lineItem = null;
    try {
        StringReader strReader = new StringReader(requestVdo);
        XMLInputFactory xif = XMLInputFactory.newInstance();
        XMLStreamReader reader = xif.createXMLStreamReader(strReader);

        StAXOMBuilder builder = new StAXOMBuilder(reader);
        lineItem = builder.getDocumentElement();
    } catch (FactoryConfigurationError e) {
        log.error(e.getMessage());
        throw new Exception(e);
    }
    return lineItem;
}

From source file:edu.harvard.i2b2.eclipse.plugins.fr.ws.FrServiceDriver.java

/**
 * Function to convert Ont requestVdo to OMElement
 * /*w  ww.j a v a  2s  .co  m*/
 * @param requestVdo   String requestVdo to send to Ont web service
 * @return An OMElement containing the Ont web service requestVdo
 */
public static OMElement getFrPayLoad(String requestVdo) throws Exception {
    OMElement lineItem = null;
    try {
        StringReader strReader = new StringReader(requestVdo);
        XMLInputFactory xif = XMLInputFactory.newInstance();
        XMLStreamReader reader = xif.createXMLStreamReader(strReader);

        StAXOMBuilder builder = new StAXOMBuilder(reader);
        lineItem = builder.getDocumentElement();
    } catch (FactoryConfigurationError e) {
        log.error(e.getMessage());
        throw new Exception(e);
    }
    return lineItem;
}

From source file:Main.java

/**
 * Converts the XML file specified into the specified POJO type
 * @param <T> the object type of the POJO
 * @param xmlfile the XML file to convert
 * @param classOfT the class of the POJO
 * @return the POJO object if conversion was successful
 * @throws JAXBException/*from  w w w . ja v  a  2  s  . com*/
 * @throws XMLStreamException
 * @throws FileNotFoundException 
 */
public static <T> T convertToPojo(File xmlfile, Class<T> classOfT)
        throws JAXBException, XMLStreamException, FileNotFoundException {
    JAXBContext jaxbContext = JAXBContext.newInstance(classOfT);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

    XMLInputFactory xif = XMLInputFactory.newFactory();
    // settings to prevent xxe // would be funny if this tool is itsef is vulnerable to xxe :D
    xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
    xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);

    XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader(xmlfile));
    T t = (T) jaxbUnmarshaller.unmarshal(xsr);//(xmlfile);

    return t;
}

From source file:edu.harvard.i2b2.analysis.queryClient.QueryClient.java

public static OMElement getQueryPayLoad(String XMLstr) throws Exception {
    StringReader strReader = new StringReader(XMLstr);
    XMLInputFactory xif = XMLInputFactory.newInstance();
    XMLStreamReader reader = xif.createXMLStreamReader(strReader);

    StAXOMBuilder builder = new StAXOMBuilder(reader);
    OMElement lineItem = builder.getDocumentElement();
    // System.out.println("Line item string " + lineItem.toString());
    return lineItem;
}

From source file:com.googlesource.gerrit.plugins.supermanifest.JiriManifestParser.java

private static JiriManifest parseManifest(Repository repo, String ref, String file)
        throws JAXBException, IOException, XMLStreamException {
    byte[] b = Utils.readBlob(repo, ref + ":" + file);
    JAXBContext jc = JAXBContext.newInstance(JiriManifest.class);

    XMLInputFactory inf = XMLInputFactory.newFactory();
    inf.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
    inf.setProperty(XMLInputFactory.SUPPORT_DTD, false);
    XMLStreamReader sr = inf.createXMLStreamReader(new StreamSource(new ByteArrayInputStream(b)));

    return (JiriManifest) jc.createUnmarshaller().unmarshal(sr);
}

From source file:edu.harvard.i2b2.workplace.ws.MessageFactory.java

/**
 * Function creates Workplace response OMElement from xml string
 * @param xmlString//from ww  w  .  j a  va  2s  .  c o m
 * @return OMElement
 * @throws XMLStreamException
 */
public static OMElement createResponseOMElementFromString(String xmlString) throws I2B2Exception {
    OMElement returnElement = null;

    try {
        StringReader strReader = new StringReader(xmlString);
        XMLInputFactory xif = XMLInputFactory.newInstance();
        XMLStreamReader reader = xif.createXMLStreamReader(strReader);

        StAXOMBuilder builder = new StAXOMBuilder(reader);
        returnElement = builder.getDocumentElement();

    } catch (XMLStreamException e) {
        log.error("xml stream response WDO to OMElement");
        throw new I2B2Exception("XML Stream error ", e);
    } catch (Exception e) {
        log.error("Error while converting Workplace response WDO to OMElement");
        throw new I2B2Exception("Response OMElement creation error ", e);
    }
    return returnElement;
}

From source file:MDRevealer.ResistanceTests.java

private static ArrayList<HashMap<String, String>> parse_test(File rt_input)
        throws XMLStreamException, FileNotFoundException {
    XMLInputFactory xif = XMLInputFactory.newInstance();
    XMLStreamReader xsr = xif.createXMLStreamReader(new FileInputStream(rt_input));
    ArrayList<HashMap<String, String>> test = new ArrayList<HashMap<String, String>>();
    HashMap<String, String> hm = new HashMap<String, String>();
    String key = "null";
    while (xsr.hasNext() == true) {

        int constant = xsr.next();

        switch (constant) {
        case XMLStreamConstants.START_ELEMENT:
            key = xsr.getLocalName();/*from   w ww . ja v a2 s  . c om*/
            if (xsr.getAttributeCount() > 0) {
                for (int i = 0; i < xsr.getAttributeCount(); i++) {
                    hm.put(xsr.getAttributeLocalName(i), xsr.getAttributeValue(i));
                }
                test.add(hm);
                hm = new HashMap<String, String>();
            }
            break;
        case XMLStreamConstants.CHARACTERS:
            if (!xsr.isWhiteSpace()) {
                hm.put(key, xsr.getText());
                test.add(hm);
            }
            hm = new HashMap<String, String>();
            break;
        case XMLStreamConstants.END_ELEMENT:
            if (xsr.getLocalName().equals("condition")) {
                hm.put("condition_over", "true");
                test.add(hm);
                hm = new HashMap<String, String>();
            }
            break;
        }
    }
    return test;
}

From source file:com.auditbucket.client.Importer.java

static long processXMLFile(String file, AbRestClient abExporter, XmlMappable mappable, boolean simulateOnly)
        throws ParserConfigurationException, IOException, SAXException, JDOMException, DatagioException {
    try {//w ww.ja va2 s  .c  om
        long rows = 0;
        StopWatch watch = new StopWatch();
        StreamSource source = new StreamSource(file);
        XMLInputFactory xif = XMLInputFactory.newFactory();
        XMLStreamReader xsr = xif.createXMLStreamReader(source);
        mappable.positionReader(xsr);
        List<CrossReferenceInputBean> referenceInputBeans = new ArrayList<>();

        String docType = mappable.getDataType();
        watch.start();
        try {
            long then = new DateTime().getMillis();
            while (xsr.getLocalName().equals(docType)) {
                XmlMappable row = mappable.newInstance(simulateOnly);
                String json = row.setXMLData(xsr);
                MetaInputBean header = (MetaInputBean) row;
                if (!header.getCrossReferences().isEmpty()) {
                    referenceInputBeans.add(new CrossReferenceInputBean(header.getFortress(),
                            header.getCallerRef(), header.getCrossReferences()));
                    rows = rows + header.getCrossReferences().size();
                }
                LogInputBean logInputBean = new LogInputBean("system", new DateTime(header.getWhen()), json);
                header.setLog(logInputBean);
                //logger.info(json);
                xsr.nextTag();
                writeAudit(abExporter, header, mappable.getClass().getCanonicalName());
                rows++;
                if (rows % 500 == 0 && !simulateOnly)
                    logger.info("Processed {} elapsed seconds {}", rows,
                            new DateTime().getMillis() - then / 1000d);

            }
        } finally {
            abExporter.flush(mappable.getClass().getCanonicalName(), mappable.getABType());
        }
        if (!referenceInputBeans.isEmpty()) {
            logger.debug("Wrote [{}] cross references",
                    writeCrossReferences(abExporter, referenceInputBeans, "Cross References"));
        }
        return endProcess(watch, rows);

    } catch (XMLStreamException | JAXBException e1) {
        throw new IOException(e1);
    }
}

From source file:TestXjc.java

private static void processXquery(String query, String input)
        throws XQException, ParserConfigurationException, SAXException, IOException, XMLStreamException {
    XQDataSource ds = new SaxonXQDataSource();
    XQConnection xqjc = ds.getConnection();
    XQPreparedExpression xqje = //xqjc.prepareExpression(new FileInputStream("/Users/kbw19/git/res/xjctestmvn/src/main/resources/transform/I2b2ToFhir/i2b2MedsToFHIRMeds.xquery"));
            xqjc.prepareExpression(new ByteArrayInputStream(query.getBytes(StandardCharsets.UTF_8)));

    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLStreamReader streamReader = //factory.createXMLStreamReader(new FileReader("/Users/kbw19/git/res/xjctestmvn/src/main/resources/example/i2b2/i2b2medspod.txt"));
            factory.createXMLStreamReader(new StringReader(input));
    xqje.bindDocument(XQConstants.CONTEXT_ITEM, streamReader, xqjc.createDocumentType());

    XQResultSequence xqjs = xqje.executeQuery();

    xqjs.writeSequence(System.out, null);
}

From source file:TestXjc.java

private static void processXqueryBase(String query, String input)
        throws XQException, ParserConfigurationException, SAXException, IOException, XMLStreamException {
    XQDataSource xqs = new BaseXXQDataSource();
    xqs.setProperty("serverName", "localhost");
    xqs.setProperty("port", "1984");

    // Change USERNAME and PASSWORD values
    XQConnection xqjc = xqs.getConnection("admin", "admin");

    XQPreparedExpression xqje = //xqjc.prepareExpression(new FileInputStream("/Users/kbw19/git/res/xjctestmvn/src/main/resources/transform/I2b2ToFhir/i2b2MedsToFHIRMeds.xquery"));
            xqjc.prepareExpression(new ByteArrayInputStream(query.getBytes(StandardCharsets.UTF_8)));

    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLStreamReader streamReader = //factory.createXMLStreamReader(new FileReader("/Users/kbw19/git/res/xjctestmvn/src/main/resources/example/i2b2/i2b2medspod.txt"));
            factory.createXMLStreamReader(new StringReader(input));
    xqje.bindDocument(XQConstants.CONTEXT_ITEM, streamReader, xqjc.createDocumentType());

    XQResultSequence xqjs = xqje.executeQuery();

    xqjs.writeSequence(System.out, null);
}