Example usage for javax.xml.parsers SAXParser parse

List of usage examples for javax.xml.parsers SAXParser parse

Introduction

In this page you can find the example usage for javax.xml.parsers SAXParser parse.

Prototype

public void parse(InputSource is, DefaultHandler dh) throws SAXException, IOException 

Source Link

Document

Parse the content given org.xml.sax.InputSource as XML using the specified org.xml.sax.helpers.DefaultHandler .

Usage

From source file:net.sf.ehcache.config.Configurator.java

/**
 * Configures a bean from an XML input stream
 *///from  ww w . j a va2s . c  o  m
public void configure(final Object bean, final InputStream inputStream) throws Exception {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Configuring ehcache from InputStream");
    }
    final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
    final BeanHandler handler = new BeanHandler(bean);
    parser.parse(inputStream, handler);
}

From source file:org.sansdemeure.zenindex.indexer.odt.TestHTMLConverter2.java

@Test
public void test() throws ParserConfigurationException, SAXException, IOException {
    TestAppender testAppender = new TestAppender();
    File testDir = FileUtil.prepareEmptyDirectory(TestHTMLConverter2.class);
    FileUtil.copyFromResources("docs/2016/2016_05_17_DZP.odt", testDir, "2016_05_17_DZP.odt");
    File odt = new File(testDir, "2016_05_17_DZP.odt");
    try (ODTResource odtRessource = new ODTResource(odt)) {
        InputStream in = odtRessource.openContentXML();
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        WriterForTest wt = new WriterForTest();
        OdtHTMLConverterHandler htmlConverter = new OdtHTMLConverterHandler();
        saxParser.parse(in, htmlConverter);
        Map<String, Object> model = htmlConverter.getModel();
        String content = (String) model.get("content");
        Assert.assertTrue(content.contains("Il y a beaucoup de"));
        Assert.assertTrue(content.contains("Les dents se touchent"));
    }/*from  w w  w  .  j  a v a2 s .  com*/

}

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);
    return parser;
}

From source file:de.topicmapslab.tmcledit.model.psiprovider.internal.Subj3ctPSIProvider.java

public Set<PSIProviderResult> getSubjectIdentifier() {
    if (getName().length() == 0)
        return Collections.emptySet();

    HttpMethod method = null;//from  w ww . jav  a  2  s. co  m
    try {
        String url = "http://api.subj3ct.com/subjects/search";

        HttpClient client = new HttpClient();
        method = new GetMethod(url);

        ArrayList<NameValuePair> params = new ArrayList<NameValuePair>(2);
        params.add(new NameValuePair("format", "xml"));
        params.add(new NameValuePair("query", getName()));
        method.setQueryString(params.toArray(new NameValuePair[params.size()]));

        client.getParams().setSoTimeout(5000);
        client.executeMethod(method);

        String result = method.getResponseBodyAsString();

        SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
        Subj3ctXmlHandler handler = new Subj3ctXmlHandler();
        parser.parse(new InputSource(new StringReader(result)), handler);

        List<Subje3ctResult> resultList = handler.getResultList();
        if (resultList.size() == 0) {
            return Collections.emptySet();
        }

        Set<PSIProviderResult> resultSet = new HashSet<PSIProviderResult>(resultList.size());
        for (Subje3ctResult r : resultList) {
            String description = "";
            if (r.name != null)
                description = "Name: " + r.name + "\n";
            if (r.description != null)
                description += "Description: " + r.description + "\n";

            description += "\n\nThis service is provided by http://www.subj3ct.com";

            resultSet.add(new PSIProviderResult(r.identifier, description));
        }

        return Collections.unmodifiableSet(resultSet);
    } catch (UnknownHostException e) {
        // no http connection -> no results
        TmcleditEditPlugin.logInfo(e);
        return Collections.emptySet();
    } catch (SocketTimeoutException e) {
        // timeout -> no results
        TmcleditEditPlugin.logInfo(e);
        return Collections.emptySet();
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (method != null)
            method.releaseConnection();
    }
}

From source file:com.musala.core.RssProcessorImpl.java

private void readData() {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser parser = null;
    try {//from   w  w  w  .ja  v a  2  s .  com
        parser = factory.newSAXParser();
        parser.parse(new InputSource(new URL(site.getRssLink()).openStream()), this);
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.google.enterprise.connector.sharepoint.wsclient.mock.XmlClientFactory.java

public XmlClientFactory(String xmlFilePath) {
    XmlHandler handler = new XmlHandler();
    try {/*from   w w  w .  j  a v a  2 s  . co m*/
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        saxParser.parse(xmlFilePath, handler);
    } catch (Exception e) {
        handler = null;
        LOGGER.log(Level.WARNING, "Unable to load mock xml.", e);
    }

    if (null != handler) {
        root = handler.getRoot();
    } else {
        root = null;
    }
}

From source file:com.joliciel.frenchTreebank.upload.TreebankSAXParser.java

public void parseDocument(String filePath) {
    //get or create the treebank file
    treebankFile = this.getTreebankService().loadOrCreateTreebankFile(filePath);

    //get a factory
    SAXParserFactory spf = SAXParserFactory.newInstance();
    try {/*from  w  w  w  .  ja  v  a 2  s.co  m*/

        //get a new instance of parser
        SAXParser sp = spf.newSAXParser();

        //parse the file and also register this class for call backs
        sp.parse(filePath, this);

    } catch (SAXException se) {
        se.printStackTrace();
    } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
    } catch (IOException ie) {
        ie.printStackTrace();
    }
}

From source file:com.bluecloud.ioc.parse.KernelXMLParser.java

public CompositeMetadata parse() throws KernelXMLParserException {
    KernelXMLParserHandler parserHandler = new KernelXMLParserHandler();
    if (validatedXML != null) {
        SAXParserFactory parserFactory = SAXParserFactory.newInstance();
        try {/*w ww . j av  a2 s  .co m*/
            SAXParser parser = parserFactory.newSAXParser();
            for (URL url : validatedXML) {
                InputStream is = url.openStream();
                parser.parse(is, parserHandler);
                is.close();
            }
        } catch (Exception e) {
            throw new KernelXMLParserException(e);
        }
    }
    return parserHandler.getCompositeMetadata();
}

From source file:com.sparsity.dex.etl.config.impl.XMLConfigurationProvider.java

public void load() throws DexUtilsException {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(true);/*from   w w  w .ja  v  a 2  s .c  o  m*/
    factory.setNamespaceAware(true);

    try {
        SAXParser parser = factory.newSAXParser();
        DexUtilsHandler handler = new DexUtilsHandler(config);
        parser.parse(xml.getAbsolutePath(), handler);
    } catch (Exception e) {
        String msg = new String("Parsing error.");
        log.error(msg, e);
        throw new DexUtilsException(msg, e);
    }
    log.info("Loaded configuration from " + xml.getAbsolutePath());
}

From source file:egat.cli.neresponse.NEResponseCommandHandler.java

protected void processSymmetricGame(MutableSymmetricGame game) throws CommandProcessingException {

    InputStream inputStream = null;
    try {//from w  w w  .j  a va  2s .  co m

        inputStream = new FileInputStream(profilePath);

    } catch (FileNotFoundException e) {

        throw new CommandProcessingException(e);

    }

    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();

        SAXParser parser = factory.newSAXParser();

        StrategyHandler handler = new StrategyHandler();

        parser.parse(inputStream, handler);

        Strategy strategy = handler.getStrategy();

        findNEResponse(strategy, game);
    } catch (NonexistentPayoffException e) {
        System.err.println(String.format("Could not calculate regret. %s", e.getMessage()));
    } catch (ParserConfigurationException e) {
        throw new CommandProcessingException(e);
    } catch (SAXException e) {
        throw new CommandProcessingException(e);
    } catch (IOException e) {
        throw new CommandProcessingException(e);
    }
}