Example usage for javax.xml.parsers SAXParserFactory newInstance

List of usage examples for javax.xml.parsers SAXParserFactory newInstance

Introduction

In this page you can find the example usage for javax.xml.parsers SAXParserFactory newInstance.

Prototype


public static SAXParserFactory newInstance() 

Source Link

Document

Obtain a new instance of a SAXParserFactory .

Usage

From source file:com.aurel.track.plugin.PluginParser.java

private void parse(InputStream is) {
    //get a factory
    SAXParserFactory spf = SAXParserFactory.newInstance();
    try {//from   ww  w. j  a  v  a 2 s  .com
        //get a new instance of parser
        SAXParser sp = spf.newSAXParser();

        //parse the file and also register this class for call backs
        sp.parse(is, this);
    } catch (SAXException se) {
        LOGGER.error(ExceptionUtils.getStackTrace(se));
    } catch (ParserConfigurationException pce) {
        LOGGER.error(ExceptionUtils.getStackTrace(pce));
    } catch (IOException ie) {
        LOGGER.error(ExceptionUtils.getStackTrace(ie));
    }
}

From source file:com.zazuko.blv.outbreak.tools.SparqlClient.java

List<Map<String, RDFTerm>> queryResultSet(final String query) throws IOException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(endpoint);
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair("query", query));
    httpPost.setEntity(new UrlEncodedFormEntity(nvps));
    CloseableHttpResponse response2 = httpclient.execute(httpPost);

    try {//from  w w  w.jav a 2s.c  om
        HttpEntity entity2 = response2.getEntity();
        InputStream in = entity2.getContent();
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        SAXParser saxParser = spf.newSAXParser();
        XMLReader xmlReader = saxParser.getXMLReader();
        final SparqlsResultsHandler sparqlsResultsHandler = new SparqlsResultsHandler();
        xmlReader.setContentHandler(sparqlsResultsHandler);
        xmlReader.parse(new InputSource(in));
        /*
         for (int ch = in.read(); ch != -1; ch = in.read()) {
         System.out.print((char)ch);
         }
         */
        // do something useful with the response body
        // and ensure it is fully consumed
        EntityUtils.consume(entity2);
        return sparqlsResultsHandler.getResults();
    } catch (ParserConfigurationException ex) {
        throw new RuntimeException(ex);
    } catch (SAXException ex) {
        throw new RuntimeException(ex);
    } finally {
        response2.close();
    }

}

From source file:org.olat.core.commons.services.webdav.WebDAVExternalTest.java

private void parse(String response) {
    try {/*from w w  w  . j a  v a  2s  .c  o m*/
        SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
        InputSource in = new InputSource(new StringReader(response));
        saxParser.parse(in, new WebDAVHandler());
    } catch (ParserConfigurationException | SAXException | IOException e) {
        e.printStackTrace();
    }
}

From source file:com.esri.geoevent.solutions.adapter.geomessage.DefenseInboundAdapter.java

public DefenseInboundAdapter(AdapterDefinition definition)
        throws ComponentException, ParserConfigurationException, SAXException, IOException {
    super(definition);
    messageParser = new MessageParser(this);
    saxFactory = SAXParserFactory.newInstance();
    saxParser = saxFactory.newSAXParser();
}

From source file:org.solmix.runtime.support.spring.TunedDocumentLoader.java

TunedDocumentLoader() {
    try {//from  w  w  w  .j  a  v  a  2 s .  co m
        Class<?> cls = ClassLoaderUtils.loadClass("com.ctc.wstx.sax.WstxSAXParserFactory",
                TunedDocumentLoader.class);
        saxParserFactory = (SAXParserFactory) cls.newInstance();
        nsasaxParserFactory = (SAXParserFactory) cls.newInstance();
    } catch (Throwable e) {
        //woodstox not found, use any other Stax parser
        saxParserFactory = SAXParserFactory.newInstance();
        nsasaxParserFactory = SAXParserFactory.newInstance();
    }

    try {
        nsasaxParserFactory.setFeature("http://xml.org/sax/features/namespaces", true);
        nsasaxParserFactory.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
    } catch (Throwable e) {
        //ignore
    }

}

From source file:ddf.compression.exi.EXIEncoderTest.java

/**
 * Tests that the encode method converts xml into exi-compressed xml.
 *
 * @throws Exception//w  w  w .  j  a  va 2 s .c om
 */
@Test
public void testEncode() throws Exception {

    ByteArrayOutputStream exiStream = new ByteArrayOutputStream();

    InputStream xmlStream = getClass().getResourceAsStream(TEST_FILE);

    EXIEncoder.encode(xmlStream, exiStream);

    StringWriter stringWriter = new StringWriter();

    GrammarCache grammarCache;

    SAXTransformerFactory saxTransformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    saxParserFactory.setNamespaceAware(true);
    TransformerHandler transformerHandler = saxTransformerFactory.newTransformerHandler();

    EXIReader reader = new EXIReader();

    grammarCache = new GrammarCache(null, GrammarOptions.DEFAULT_OPTIONS);

    reader.setGrammarCache(grammarCache);

    transformerHandler.setResult(new StreamResult(stringWriter));

    reader.setContentHandler(transformerHandler);

    reader.parse(new InputSource(new ByteArrayInputStream(exiStream.toByteArray())));
    XMLUnit.setNormalize(true);
    XMLUnit.setNormalizeWhitespace(true);
    InputStream stream = getClass().getResourceAsStream(TEST_FILE);
    Diff diff = XMLUnit.compareXML(IOUtils.toString(stream), stringWriter.getBuffer().toString());
    IOUtils.closeQuietly(stream);
    assertTrue("The XML input file (" + TEST_FILE + ") did not match the EXI-decoded output", diff.similar());
}

From source file:com.ibm.jaql.lang.expr.xml.TypedXmlToJsonFn.java

@Override
public JsonValue eval(Context context) throws Exception {
    if (parser == null) {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);
        parser = factory.newSAXParser().getXMLReader();
        handler = new TypedXmlToJsonHandler2();
        parser.setContentHandler(handler);
    }//from  w  w w  .  j  av  a  2  s. c o m

    JsonString s = (JsonString) exprs[0].eval(context);
    parser.parse(new InputSource(new StringReader(s.toString())));
    return handler.result;
}

From source file:gov.nasa.ensemble.core.jscience.xml.XMLProfileParser.java

@Override
protected void doParse(URI uri, InputStream inputStream) {
    try {/*from   w  w  w.  j  a v a 2s  . co  m*/
        SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
        XMLProfileHandler xmlProfileHandler = new XMLProfileHandler();
        xmlProfileHandler.setParsingDataPoints(parsingDataPoints);
        parser.parse(inputStream, xmlProfileHandler);
        for (Profile profile : xmlProfileHandler.getProfiles())
            addProfile(uri, profile);
    } catch (Exception e) {
        errorHandler.unhandledException(e);
    }
}

From source file:Main.java

/**
 * Constructs a secure SAX Parser.//from   w ww  .ja  v  a 2  s.  c o  m
 *
 * @return a SAX Parser
 * @throws ParserConfigurationException thrown if there is a parser
 * configuration exception
 * @throws SAXNotRecognizedException thrown if there is an unrecognized
 * feature
 * @throws SAXNotSupportedException thrown if there is a non-supported
 * feature
 * @throws SAXException is thrown if there is a SAXException
 */
public static SAXParser buildSecureSaxParser()
        throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException, SAXException {
    final SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
    factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
    factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
    return factory.newSAXParser();
}

From source file:com.rightscale.service.FeedScraper.java

public void run() {
    int pollPeriod = DEFAULT_POLL_PERIOD;

    while (false == _shouldStop) {
        boolean error = false;
        int interesting = 0;

        try {//  ww w.j  a v  a  2 s . co m
            HttpEntity response = getEntity(null, null);

            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();

            AtomParser parser = new AtomParser(this);
            xr.setContentHandler(parser);
            xr.parse(new InputSource(response.getContent()));
            response.consumeContent();
            interesting = parser.getNumInteresting();
        } catch (Exception e) {
            Log.e("FeedScraper", e.getClass().getName());
            e.printStackTrace();
            error = true;
        }

        if (error) {
            pollPeriod = ERROR_POLL_PERIOD;
        } else if (interesting == 0) {
            pollPeriod = (int) (pollPeriod * 1.5);
            pollPeriod = Math.max(pollPeriod, MAX_POLL_PERIOD);
        } else {
            pollPeriod = DEFAULT_POLL_PERIOD;
        }

        try {
            Thread.sleep(pollPeriod);
        } catch (InterruptedException e) {
            //someone may have set _shouldStop on us...
        }
    }
}