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.google.feedserver.util.XmlUtil.java

public XmlUtil() {
    parserFactory = SAXParserFactory.newInstance();
    parserFactory.setValidating(false);
}

From source file:com.markwatson.linkeddata.DBpediaLookupClient.java

public DBpediaLookupClient(String query) throws Exception {
    this.query = query;
    HttpClient client = new HttpClient();

    String query2 = query.replaceAll(" ", "+");
    HttpMethod method = new GetMethod(
            "http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryString=" + query2);
    try {/*from   w  w  w.  ja v  a 2s .  co  m*/
        client.executeMethod(method);
        InputStream ins = method.getResponseBodyAsStream();
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser sax = factory.newSAXParser();
        sax.parse(ins, this);
    } catch (HttpException he) {
        System.err.println("Http error connecting to lookup.dbpedia.org");
    } catch (IOException ioe) {
        System.err.println("Unable to connect to lookup.dbpedia.org");
    }
    method.releaseConnection();
}

From source file:com.knowledgebooks.rdf.SparqlClient.java

public SparqlClient(String endpoint_URL, String sparql) throws Exception {
    //System.out.println("SparqlClient("+endpoint_URL+", "+sparql+")");
    HttpClient client = new HttpClient();
    client.getHttpConnectionManager().getParams().setConnectionTimeout(10000);

    String req = URLEncoder.encode(sparql, "utf-8");
    HttpMethod method = new GetMethod(endpoint_URL + "?query=" + req);
    method.setFollowRedirects(false);//from   www.j a va  2 s. c o  m
    try {
        client.executeMethod(method);
        //System.out.println(method.getResponseBodyAsString());
        //if (true) return;
        InputStream ins = method.getResponseBodyAsStream();
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser sax = factory.newSAXParser();
        sax.parse(ins, this);
    } catch (HttpException he) {
        System.err.println("Http error connecting to '" + endpoint_URL + "'");
    } catch (IOException ioe) {
        System.err.println("Unable to connect to '" + endpoint_URL + "'");
    }
    method.releaseConnection();
}

From source file:Main.java

/**
 * Constructs a validating secure SAX Parser.
 *
 * @param schemaStream the schema to validate the XML against
 * @return a SAX Parser/* ww w  .ja v a2s.  co m*/
 * @throws ParserConfigurationException is 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(InputStream schemaStream)
        throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException, SAXException {
    final SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(true);
    factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
    factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
    //setting the following unfortunately breaks reading the old suppression files (version 1).
    //factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);

    final SAXParser saxParser = factory.newSAXParser();
    saxParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
    saxParser.setProperty(JAXP_SCHEMA_SOURCE, schemaStream);
    return saxParser;
}

From source file:net.eledge.android.europeana.tools.RssReader.java

public static List<BlogArticle> readFeed(String url, DateTime lastViewed) {
    InputStream is = null;//from   www . j  a  v a  2s .c  om
    try {
        HttpGet request = new HttpGet(url);
        AndroidHttpClient.modifyRequestToAcceptGzipResponse(request);
        HttpResponse response = new DefaultHttpClient().execute(request);
        is = AndroidHttpClient.getUngzippedContent(response.getEntity());

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

        RssFeedHandler rh = new RssFeedHandler(lastViewed);

        xr.setContentHandler(rh);
        xr.parse(new InputSource(is));

        return rh.articles;
    } catch (IOException | SAXException | ParserConfigurationException e) {
        Log.e("RssReader", e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(is);
    }

    return null;
}

From source file:com.haulmont.bali.util.Dom4j.java

public static SAXParser getParser() {
    SAXParser parser = saxParserHolder.get();
    if (parser == null) {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setValidating(false);/*  w  ww.  j  a  v a  2 s  .com*/
        factory.setNamespaceAware(false);
        try {
            parser = factory.newSAXParser();
        } catch (ParserConfigurationException | SAXException e) {
            throw new RuntimeException("Unable to create SAX parser", e);
        }
        saxParserHolder.set(parser);
    }
    return parser;
}

From source file:bbcdataservice.BBCProgrammesParser.java

protected static boolean parse(HashMap<Date, MutableChannelDayProgram> dayPrograms, File file, Channel channel,
        Date date) throws Exception {
    mHasNextDay = false;//from  w w  w.  j a  v  a  2  s . c o  m
    SAXParserFactory fac = SAXParserFactory.newInstance();
    fac.setValidating(false);
    SAXParser sax = fac.newSAXParser();
    sax.parse(file, new BBCProgrammesParser(dayPrograms, channel, date));
    return mHasNextDay;
}

From source file:mondrian.util.XmlParserFactoryProducer.java

/**
 * Creates an instance of {@link SAXParserFactory} class with enabled {@link XMLConstants#FEATURE_SECURE_PROCESSING} property.
 * Enabling this feature prevents from some XXE attacks (e.g. XML bomb)
 *
 * @throws ParserConfigurationException if a parser cannot
 *     be created which satisfies the requested configuration.
 *
 * @throws SAXNotRecognizedException When the underlying XMLReader does
 *            not recognize the property name.
 *
 * @throws SAXNotSupportedException When the underlying XMLReader
 *            recognizes the property name but doesn't support the
 *            property./*from   w w  w. j  a  v a  2s. c o  m*/
 */
public static SAXParserFactory createSecureSAXParserFactory()
        throws SAXNotSupportedException, SAXNotRecognizedException, ParserConfigurationException {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
    factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
    factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
    factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    return factory;
}

From source file:org.epics.archiverappliance.retrieval.channelarchiver.XMLRPCClient.java

/**
 * Internal method to make a XML_RPC post call and call the SAX handler on the returned document.
 * @param serverURL The Server URL// ww  w  .  ja v  a 2  s  .c o m
 * @param handler  DefaultHandler 
 * @param postEntity StringEntity 
 * @throws IOException  &emsp; 
 * @throws SAXException  &emsp; 
 */
private static void doHTTPPostAndCallSAXHandler(String serverURL, DefaultHandler handler,
        StringEntity postEntity) throws IOException, SAXException {
    logger.debug("Executing doHTTPPostAndCallSAXHandler with the server URL " + serverURL);
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost postMethod = new HttpPost(serverURL);
    postMethod.addHeader("Content-Type", "text/xml");
    postMethod.setEntity(postEntity);
    logger.debug("Executing the HTTP POST" + serverURL);
    HttpResponse response = httpclient.execute(postMethod);
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        logger.debug("Obtained a HTTP entity of length " + entity.getContentLength());
        try (InputStream is = entity.getContent()) {
            SAXParserFactory sfac = SAXParserFactory.newInstance();
            sfac.setNamespaceAware(false);
            sfac.setValidating(false);
            SAXParser parser = sfac.newSAXParser();
            parser.parse(is, handler);
        } catch (ParserConfigurationException pex) {
            throw new IOException(pex);
        }
    } else {
        throw new IOException("HTTP response did not have an entity associated with it");
    }
}

From source file:org.npr.api.Client.java

public void sax(ContentHandler handler) {
    try {//from  w  ww  .  ja  va2 s.c om
        XMLReader xr = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
        xr.setContentHandler(handler);
        xr.parse(new InputSource(download()));
    } catch (SAXException e) {
        Log.e(LOG_TAG, "error creating parser", e);
    } catch (ParserConfigurationException e) {
        Log.e(LOG_TAG, "error creating parser", e);
    } catch (FactoryConfigurationError e) {
        Log.e(LOG_TAG, "error creating parser", e);
    } catch (IOException e) {
        Log.e(LOG_TAG, "error parsing", e);
    }
}