Example usage for javax.xml.parsers SAXParserFactory newSAXParser

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

Introduction

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

Prototype


public abstract SAXParser newSAXParser() throws ParserConfigurationException, SAXException;

Source Link

Document

Creates a new instance of a SAXParser using the currently configured factory parameters.

Usage

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  w ww. j  a  v a 2s .c  om
    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:io.lightlink.excel.StreamingExcelTransformer.java

private List<String> processSharedStrings(byte[] bytes)
        throws ParserConfigurationException, SAXException, IOException {

    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser = factory.newSAXParser();

    SharedStringsHandler handler = new SharedStringsHandler();
    saxParser.parse(new ByteArrayInputStream(bytes), handler);

    return handler.getSharedStings();

}

From source file:io.lightlink.excel.StreamingExcelTransformer.java

private void processSheet(byte[] bytes, OutputStream out, ExcelStreamVisitor visitor)
        throws ParserConfigurationException, SAXException, IOException {

    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser = factory.newSAXParser();
    Writer printWriter = new OutputStreamWriter(out, "UTF-8");

    saxParser.parse(new ByteArrayInputStream(bytes),
            new SheetTemplateHandler(printWriter, sharedStrings, visitor));

    printWriter.flush();/*from   w  w  w  .  j av a 2s  . c o  m*/

}

From source file:org.energyos.espi.datacustodian.integration.utils.ATOMContentHandlerTests.java

@Test
@Ignore//from  ww  w  .  j a v a  2  s .  c o  m
public void processEnty() throws Exception {
    JAXBContext context = marshaller.getJaxbContext();

    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);
    XMLReader reader = factory.newSAXParser().getXMLReader();
    // EntryProcessorServiceImpl procssor =
    // mock(EntryProcessorServiceImpl.class);
    ATOMContentHandler atomContentHandler = new ATOMContentHandler(context, entryProcessorService);

    reader.setContentHandler(atomContentHandler);

    reader.parse(new InputSource(FixtureFactory.newUsagePointInputStream(UUID.randomUUID())));

    // verify(procssor).process(any(EntryType.class));
}

From source file:net.cpollet.jixture.fixtures.transformers.XmlFileFixtureTransformer.java

private SAXParser createSaxParser() {
    SAXParserFactory parserFactor = SAXParserFactory.newInstance();
    try {//from w ww. j a v a2 s  . c om
        return parserFactor.newSAXParser();
    } catch (Exception e) {
        throw ExceptionUtils.wrapInRuntimeException(e);
    }
}

From source file:eionet.gdem.conversion.excel.ExcelProcessor.java

/**
 * Converts XML string to OutputStream//  www  .  j av  a 2  s.  c  o m
 * @param sIn Input string
 * @param sOut OutputStream
 * @throws GDEMException In case an error occurs.
 */
public void makeExcel(String sIn, OutputStream sOut) throws GDEMException {

    if (sIn == null) {
        return;
    }
    if (sOut == null) {
        return;
    }

    try {
        ExcelConversionHandlerIF excel = ExcelUtils.getExcelConversionHandler();
        //excel.setFileName(sOut);

        ExcelXMLHandler handler = new ExcelXMLHandler(excel);
        SAXParserFactory spfact = SAXParserFactory.newInstance();
        SAXParser parser = spfact.newSAXParser();
        XMLReader reader = parser.getXMLReader();
        spfact.setValidating(true);

        reader.setContentHandler(handler);
        reader.parse(sIn);
        excel.writeToFile(sOut);
    } catch (Exception e) {
        throw new GDEMException("Error generating Excel file: " + e.toString(), e);
    }

    return;
}

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 {//  ww  w .  j a  v  a  2 s . c  om
        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.inferiorhumanorgans.WayToGo.Agency.BART.Route.RouteTask.java

@Override
protected Void doInBackground(BARTAgency... someAgencies) {
    super.doInBackground(someAgencies);
    Log.i(LOG_NAME, "Trying to get BART route list.");

    InputStream content = null;//from  ww w  . j a va  2s  .  c  om
    ClientConnectionManager connman = new ThreadSafeClientConnManager(params, registry);
    DefaultHttpClient hc = new DefaultHttpClient(connman, params);

    Log.i(LOG_NAME, "Fetching from: " + BART_URL + BARTAgency.API_KEY);
    HttpGet getRequest = new HttpGet(BART_URL + BARTAgency.API_KEY);
    try {
        content = hc.execute(getRequest).getEntity().getContent();
    } catch (ClientProtocolException ex) {
        Logger.getLogger(LOG_NAME).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(LOG_NAME).log(Level.SEVERE, null, ex);
    }
    Log.i(LOG_NAME, "Put the station list in the background.");

    try {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();

        XMLReader xr = sp.getXMLReader();

        xr.setContentHandler(dataHandler);

        xr.parse(new InputSource(content));

    } catch (ParserConfigurationException pce) {
        Log.e(LOG_NAME + " SAX XML", "sax parse error", pce);
    } catch (AbortXMLParsingException abrt) {
        Log.i(LOG_NAME + " AsyncXML", "Cancelled!!!!!");
    } catch (SAXException se) {
        Log.e(LOG_NAME + " SAX XML", "sax error", se);
    } catch (IOException ioe) {
        Log.e(LOG_NAME + " SAX XML", "sax parse io error", ioe);
    }
    Log.i(LOG_NAME + " SAX XML", "Done parsing BART station XML");
    return null;
}

From source file:edu.scripps.fl.pubchem.promiscuity.OverallListsAndMapsFactory.java

public Map<Long, List<Protein>> getAIDProteinMap(List<Long> aids) throws Exception {
    log.info("Number of aids in eSummary request: " + aids.size());
    log.info("Memory usage before getting aid eSummary document: " + memUsage());
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser = factory.newSAXParser();
    AssayESummaryHandler handler = new AssayESummaryHandler();
    InputStream is = EUtilsFactory.getInstance().getSummaries(aids, "pcassay");
    saxParser.parse(is, handler);//from w w  w  .j  a v  a2  s  . com
    log.info("Memory usage after getting aid eSummary document: " + memUsage());
    return handler.getMap();
}

From source file:com.knowledgebooks.info_spiders.DBpediaLookupClient.java

public DBpediaLookupClient(String query) throws Exception {
    this.query = query;
    //System.out.println("\n query: " + query);
    HttpClient client = new HttpClient();

    String query2 = query.replaceAll(" ", "+"); // URLEncoder.encode(query, "utf-8");
    //System.out.println("\n query2: " + query2);
    HttpMethod method = new GetMethod(
            "http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryString=" + query2);
    try {//from  ww w .  j a  v  a2  s .  c o m
        System.out.println("\n method: " + method.getURI());
        client.executeMethod(method);
        System.out.println(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();
}