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: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  a 2  s. c  o m*/
    log.info("Memory usage after getting aid eSummary document: " + memUsage());
    return handler.getMap();
}

From source file:de.uzk.hki.da.model.RightsSectionURNMetsXmlReader.java

/**
 * Read urn./*from   w w  w . j  a va 2s  . c om*/
 *
 * @param file the file
 * @return The URN specified in the METS file or null if the METS file doesn't specify an URN
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws ParseException the parse exception
 * @author Thomas Kleinke
 */
public String readURN(File file) throws IOException, ParseException {

    FileInputStream fileInputStream = new FileInputStream(file);
    BOMInputStream bomInputStream = new BOMInputStream(fileInputStream);

    XMLReader xmlReader = null;
    SAXParserFactory spf = SAXParserFactory.newInstance();
    try {
        xmlReader = spf.newSAXParser().getXMLReader();
    } catch (Exception e) {
        fileInputStream.close();
        bomInputStream.close();
        throw new IOException("Error creating SAX parser", e);
    }
    xmlReader.setErrorHandler(err);
    NodeFactory nodeFactory = new PremisXmlReaderNodeFactory();
    Builder parser = new Builder(xmlReader, false, nodeFactory);
    logger.trace("Successfully built builder and XML reader");

    try {
        String urn = null;

        Document doc = parser.build(bomInputStream);
        Element root = doc.getRootElement();

        Element dmdSecEl = root.getFirstChildElement("dmdSec", METS_NS);
        if (dmdSecEl == null)
            return null;

        Element mdWrapEl = dmdSecEl.getFirstChildElement("mdWrap", METS_NS);
        if (mdWrapEl == null)
            return null;

        Element xmlDataEl = mdWrapEl.getFirstChildElement("xmlData", METS_NS);
        if (xmlDataEl == null)
            return null;

        Element modsEl = xmlDataEl.getFirstChildElement("mods", MODS_NS);
        if (modsEl == null)
            return null;

        Elements identifierEls = modsEl.getChildElements("identifier", MODS_NS);
        for (int i = 0; i < identifierEls.size(); i++) {
            Element element = identifierEls.get(i);
            Attribute attribute = element.getAttribute("type");
            if (attribute.getValue().toLowerCase().equals("urn"))
                urn = element.getValue();
        }

        if (urn != null && urn.equals(""))
            urn = null;

        return urn;
    } catch (ValidityException ve) {
        throw new IOException(ve);
    } catch (ParsingException pe) {
        throw new IOException(pe);
    } catch (IOException ie) {
        throw new IOException(ie);
    } finally {
        fileInputStream.close();
        bomInputStream.close();
    }
}

From source file:com.wooki.services.ImportServiceImpl.java

public Book importDocbook(InputStream generatedXhtml) {
    HTMLParser handler = new HTMLParser();
    SAXParserFactory factory = SAXParserFactory.newInstance();

    // cration d'un parseur SAX
    SAXParser parser;//from  w  ww. j a  v  a2 s .  c  o  m

    try {
        parser = factory.newSAXParser();
        parser.parse(new InputSource(generatedXhtml), handler);
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
        logger.error(e.getLocalizedMessage());
        return null;
    } catch (SAXException e) {
        e.printStackTrace();
        logger.error(e.getLocalizedMessage());
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        logger.error(e.getLocalizedMessage());
        return null;
    }

    Book book = handler.getBook();
    Book toReturn = getBookManager().create(book.getTitle());
    return toReturn;
}

From source file:com.tomdoel.mpg2dcm.EndoscopicFileProcessor.java

private EndoscopicXmlParser parseEndoscopicXmlFile(final File xmlFile)
        throws IOException, SAXException, ParserConfigurationException {
    final SAXParserFactory factory = SAXParserFactory.newInstance();
    final SAXParser saxParser = factory.newSAXParser();
    final EndoscopicXmlParser parser = new EndoscopicXmlParser();
    saxParser.parse(xmlFile, parser);/*from  w ww  .  j  av  a2 s  . c  om*/
    return parser;
}

From source file:com.aurel.track.exchange.track.importer.ImporterDropdownParser.java

public SortedMap<String, List<ISerializableLabelBean>> parse(File xml, Map<Integer, Integer> fieldMatcher) {
    this.fieldMatcher = fieldMatcher;
    //get a factory
    SAXParserFactory spf = SAXParserFactory.newInstance();
    try {/*  w  w  w .  j av 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
        LOGGER.debug("Dropdown parser started...");
        sp.parse(xml, this);
        LOGGER.debug("Dropdown parser done");
        return externalDropdowns;
    } catch (SAXException se) {
        LOGGER.error(ExceptionUtils.getStackTrace(se));
    } catch (ParserConfigurationException pce) {
        LOGGER.error(ExceptionUtils.getStackTrace(pce));
    } catch (IOException ie) {
        LOGGER.error(ExceptionUtils.getStackTrace(ie));
    }
    return null;
}

From source file:com.zazuko.wikidata.municipalities.SparqlClient.java

List<Map<String, RDFTerm>> queryResultSet(final String query) throws IOException, URISyntaxException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    URIBuilder builder = new URIBuilder(endpoint);
    builder.addParameter("query", query);
    HttpGet httpGet = new HttpGet(builder.build());
    /*List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair("query", query));
    httpGet.setEntity(new UrlEncodedFormEntity(nvps));*/
    CloseableHttpResponse response2 = httpclient.execute(httpGet);

    try {//from w w  w  .ja  v a2 s.  co  m
        HttpEntity entity2 = response2.getEntity();
        InputStream in = entity2.getContent();
        if (debug) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            for (int ch = in.read(); ch != -1; ch = in.read()) {
                System.out.print((char) ch);
                baos.write(ch);
            }
            in = new ByteArrayInputStream(baos.toByteArray());
        }
        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:cn.com.loopj.android.http.SaxAsyncHttpResponseHandler.java

/**
 * Deconstructs response into given content handler
 *
 * @param entity returned HttpEntity/*from w ww.  ja v  a2 s  . co m*/
 * @return deconstructed response
 * @throws IOException if there is problem assembling SAX response from stream
 * @see HttpEntity
 */
@Override
protected byte[] getResponseData(HttpEntity entity) throws IOException {
    if (entity != null) {
        InputStream instream = entity.getContent();
        InputStreamReader inputStreamReader = null;
        if (instream != null) {
            try {
                SAXParserFactory sfactory = SAXParserFactory.newInstance();
                SAXParser sparser = sfactory.newSAXParser();
                XMLReader rssReader = sparser.getXMLReader();
                rssReader.setContentHandler(handler);
                inputStreamReader = new InputStreamReader(instream, getCharset());
                rssReader.parse(new InputSource(inputStreamReader));
            } catch (SAXException e) {
                AsyncHttpClient.log.e(LOG_TAG, "getResponseData exception", e);
            } catch (ParserConfigurationException e) {
                AsyncHttpClient.log.e(LOG_TAG, "getResponseData exception", e);
            } finally {
                AsyncHttpClient.silentCloseInputStream(instream);
                if (inputStreamReader != null) {
                    try {
                        inputStreamReader.close();
                    } catch (IOException e) {
                        /*ignore*/ }
                }
            }
        }
    }
    return null;
}

From source file:com.sap.prd.mobile.ios.mios.xcodeprojreader.jaxb.JAXBPlistParser.java

private SAXSource createSAXSource(InputSource project, final InputSource dtd)
        throws SAXException, ParserConfigurationException {
    XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
    xmlReader.setEntityResolver(new EntityResolver() {
        @Override// www.  j  a va2 s. c om
        public InputSource resolveEntity(String pid, String sid) throws SAXException {
            if (sid.equals("http://www.apple.com/DTDs/PropertyList-1.0.dtd"))
                return dtd;
            throw new SAXException("unable to resolve remote entity, sid = " + sid);
        }
    });
    SAXSource ss = new SAXSource(xmlReader, project);
    return ss;
}

From source file:com.fdwills.external.http.SaxAsyncHttpResponseHandler.java

/**
 * Deconstructs response into given content handler
 *
 * @param entity returned HttpEntity//from ww w . j a  v  a 2  s  . c o  m
 * @return deconstructed response
 * @throws java.io.IOException
 * @see org.apache.http.HttpEntity
 */
@Override
protected byte[] getResponseData(HttpEntity entity) throws IOException {
    if (entity != null) {
        InputStream instream = entity.getContent();
        InputStreamReader inputStreamReader = null;
        if (instream != null) {
            try {
                SAXParserFactory sfactory = SAXParserFactory.newInstance();
                SAXParser sparser = sfactory.newSAXParser();
                XMLReader rssReader = sparser.getXMLReader();
                rssReader.setContentHandler(handler);
                inputStreamReader = new InputStreamReader(instream, DEFAULT_CHARSET);
                rssReader.parse(new InputSource(inputStreamReader));
            } catch (SAXException e) {
                Log.e(LOG_TAG, "getResponseData exception", e);
            } catch (ParserConfigurationException e) {
                Log.e(LOG_TAG, "getResponseData exception", e);
            } finally {
                AsyncHttpClient.silentCloseInputStream(instream);
                if (inputStreamReader != null) {
                    try {
                        inputStreamReader.close();
                    } catch (IOException e) { /*ignore*/
                    }
                }
            }
        }
    }
    return null;
}

From source file:com.android.yijiang.kzx.http.SaxAsyncHttpResponseHandler.java

/**
 * Deconstructs response into given content handler
 *
 * @param entity returned HttpEntity//w  w  w.ja  v  a2  s . com
 * @return deconstructed response
 * @throws java.io.IOException
 * @see org.apache.http.HttpEntity
 */
@Override
protected byte[] getResponseData(HttpEntity entity) throws IOException {
    if (entity != null) {
        InputStream instream = entity.getContent();
        InputStreamReader inputStreamReader = null;
        if (instream != null) {
            try {
                SAXParserFactory sfactory = SAXParserFactory.newInstance();
                SAXParser sparser = sfactory.newSAXParser();
                XMLReader rssReader = sparser.getXMLReader();
                rssReader.setContentHandler(handler);
                inputStreamReader = new InputStreamReader(instream, DEFAULT_CHARSET);
                rssReader.parse(new InputSource(inputStreamReader));
            } catch (SAXException e) {
                Log.e(LOG_TAG, "getResponseData exception", e);
            } catch (ParserConfigurationException e) {
                Log.e(LOG_TAG, "getResponseData exception", e);
            } finally {
                AsyncHttpClient.silentCloseInputStream(instream);
                if (inputStreamReader != null) {
                    try {
                        inputStreamReader.close();
                    } catch (IOException e) {
                        /*ignore*/ }
                }
            }
        }
    }
    return null;
}