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:com.aionemu.gameserver.dataholders.loadingutils.XmlMerger.java

/**
 * Check for modifications of included files.
 *
 * @return <code>true</code> if at least one of included files has
 * modifications./*w  ww .  ja v  a 2s  . c  o m*/
 * @throws IOException                  IO Error.
 * @throws SAXException                 Document parsing error.
 * @throws ParserConfigurationException if a SAX parser cannot be created
 *                                      which satisfies the requested configuration.
 */
private boolean checkFileModifications() throws Exception {
    long destFileTime = destFile.lastModified();

    if (sourceFile.lastModified() > destFileTime) {
        logger.debug("Source file was modified ");
        return true;
    }

    Properties metadata = restoreFileModifications(metaDataFile);

    if (metadata == null) // new file or smth else.
    {
        return true;
    }

    SAXParserFactory parserFactory = SAXParserFactory.newInstance();

    SAXParser parser = parserFactory.newSAXParser();

    TimeCheckerHandler handler = new TimeCheckerHandler(baseDir, metadata);

    parser.parse(sourceFile, handler);

    return handler.isModified();
}

From source file:ch.entwine.weblounge.common.impl.content.page.PageReader.java

/**
 * This method is called when a <code>Page</code> object is instantiated.
 * /*from   w  w  w.ja v a  2  s.  c  o m*/
 * @param is
 *          the xml input stream
 * @param site
 *          the page's site
 * 
 * @throws IOException
 *           if reading the input stream fails
 */
public PageImpl read(InputStream is, Site site) throws SAXException, IOException, ParserConfigurationException {
    reset();
    page = new PageImpl(new PageURIImpl(site, "/"));
    readHeader = true;
    readBody = true;
    SAXParser parser = parserRef.get();
    if (parser == null) {
        parser = parserFactory.newSAXParser();
        parserRef = new WeakReference<SAXParser>(parser);
    }
    parser.parse(is, this);
    return page;
}

From source file:geeshang.nasaimage.MainActivity.java

private void download() {
    new Thread() {
        @Override/*from w w w  .  j  a v a2s  .  co m*/
        public void run() {
            super.run();
            MyDefaultHandler defaultHandler = new MyDefaultHandler(MainActivity.this);
            try {
                SAXParserFactory factory = SAXParserFactory.newInstance();
                SAXParser parser = factory.newSAXParser();
                InputStream inputStream = downloader.download(NASA_URL);
                if (inputStream != null) {
                    parser.parse(inputStream, defaultHandler);
                }
            } catch (ParserConfigurationException | SAXException | IOException e) {
                e.printStackTrace();
            }
            MainActivity.this.myHandler.post(new Runnable() {
                @Override
                public void run() {
                    resetView();
                }
            });
            getImage();
            setProgressBarGone();
            MainActivity.this.myHandler.post(new Runnable() {
                @Override
                public void run() {
                    resetView();
                }
            });
        }
    }.start();
}

From source file:ch.entwine.weblounge.common.impl.content.page.PageReader.java

/**
 * This method is called when a <code>Page</code> object is instantiated.
 * /* w  w w .j  a  v  a2  s  .  c  o m*/
 * @param is
 *          the xml input stream
 * @param site
 *          the page's site
 * @throws ParserConfigurationException
 *           if the SAX parser setup failed
 * @throws IOException
 *           if reading the input stream fails
 * @throws SAXException
 *           if an error occurs while parsing
 */
public PageImpl readHeader(InputStream is, Site site)
        throws SAXException, IOException, ParserConfigurationException {
    if (page == null) {
        page = new PageImpl(new PageURIImpl(site, "/"));
    }
    readHeader = true;
    readBody = false;
    SAXParser parser = parserRef.get();
    if (parser == null) {
        parser = parserFactory.newSAXParser();
        parserRef = new WeakReference<SAXParser>(parser);
    }
    parser.parse(is, this);
    return page;
}

From source file:ch.entwine.weblounge.common.impl.content.page.PageReader.java

/**
 * This method is called when a <code>Page</code> object is instantiated.
 * /*from w w w.j  av a 2  s  .c  om*/
 * @param is
 *          the xml input stream
 * @param site
 *          the page's site
 * @throws ParserConfigurationException
 *           if the SAX parser setup failed
 * @throws IOException
 *           if reading the input stream fails
 * @throws SAXException
 *           if an error occurs while parsing
 */
public PageImpl readBody(InputStream is, Site site)
        throws SAXException, IOException, ParserConfigurationException {
    if (page == null) {
        page = new PageImpl(new PageURIImpl(site, "/"));
    }
    readHeader = false;
    readBody = true;
    SAXParser parser = parserRef.get();
    if (parser == null) {
        parser = parserFactory.newSAXParser();
        parserRef = new WeakReference<SAXParser>(parser);
    }
    parser.parse(is, this);
    return page;
}

From source file:fr.gouv.finances.dgfip.xemelios.importers.DefaultImporter.java

protected XmlSplitter splitFile(File fHeader, File fRef, File fFooter, File tmpDir, File fToImport,
        String fileEncoding)//w ww  .j  a v  a2s .  co  m
        throws FileNotFoundException, UnsupportedEncodingException, SAXException, ParserConfigurationException {
    FileOutputStream fisHeader = new FileOutputStream(fHeader);
    FileOutputStream fisRef = new FileOutputStream(fRef);
    FileOutputStream fisFooter = new FileOutputStream(fFooter);
    XmlSplitter xs = new XmlSplitter(fisHeader, fisRef, fisFooter, tmpDir, dm, fileEncoding);
    xs.setImportLogPrintWriter(importTimingOS);
    xs.setSplittedFileName(fToImport.getName());
    SAXParserFactory sf = FactoryProvider.getSaxParserFactory();
    SAXParser parser = sf.newSAXParser();
    try {
        parser.parse(fToImport, xs);
    } catch (Exception ex) {
        ex.printStackTrace();
        String position = "line " + xs.getLocator().getLineNumber() + ": ";
        throw new SAXException(position + ex.getMessage(), ex);
    } finally {
        if (fisHeader != null) {
            try {
                fisHeader.flush();
                fisHeader.close();
            } catch (Throwable t) {
            }
        }
        if (fisRef != null) {
            try {
                fisRef.flush();
                fisRef.close();
            } catch (Throwable t) {
            }
        }
        if (fisFooter != null) {
            try {
                fisFooter.flush();
                fisFooter.close();
            } catch (Throwable t) {
            }
        }
    }
    return xs;
}

From source file:edu.scripps.fl.pubchem.web.entrez.EUtilsWebSession.java

public Collection<Long> getIds(String query, String db, Collection<Long> ids, int chunkSize) throws Exception {
    int retStart = 0;
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser = factory.newSAXParser();
    ESearchHandler handler = new ESearchHandler(ids);
    InputStream is = getInputStream("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi", "db", db,
            "term", query, "retstart", retStart, "retmax", chunkSize, "usehistory", "y", "version", "2.0")
                    .call();/*w ww  . j  a va 2  s  .c om*/
    while (true) {
        saxParser.parse(is, handler);
        if ((handler.retStart + handler.retMax) < (handler.count - 1)) {
            retStart += handler.retMax;
            is = getInputStream("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi", "db", db, "term",
                    query, "retstart", retStart, "retmax", "" + chunkSize, "usehistory", "y", "QueryKey",
                    handler.queryKey, "WebEnv", handler.webEnv, "version", "2.0").call();
        } else
            break;
    }
    return ids;
}

From source file:com.panet.imeta.job.entries.xmlwellformed.JobEntryXMLWellFormed.java

private boolean CheckWellFormed(FileObject file, LogWriter log) {
    boolean retval = false;
    try {/*from  www  .j  a  v  a 2  s  .  com*/
        SAXParserFactory factory = SAXParserFactory.newInstance();
        XMLTreeHandler handler = new XMLTreeHandler();

        // Parse the input.
        SAXParser saxParser = factory.newSAXParser();
        saxParser.parse(new File(KettleVFS.getFilename(file)), handler);
        retval = true;
    } catch (Exception e) {

        log.logError(toString(),
                Messages.getString("JobXMLWellFormed.Log.ErrorCheckingFile", file.toString(), e.getMessage()));

    }

    return retval;
}

From source file:org.semantictools.frame.api.OntologyManager.java

private void loadXsd(File file) throws SchemaParseException {
    try {//from w w w  .  j  a  v a  2 s . co  m
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();
        XMLReader reader = parser.getXMLReader();
        reader.setFeature("http://xml.org/sax/features/namespaces", true);
        NamespaceReader handler = new NamespaceReader();
        reader.setContentHandler(handler);

        parser.parse(file, handler);

        String namespace = handler.getTargetNamespace();
        if (namespace == null) {
            logger.warn("Ignoring schema since targetNamespace is not declared: " + file.getPath());
        } else {
            OntologyEntity entity = new OntologyEntity(XML_FORMAT, file, namespace);
            uri2OntologyEntity.put(namespace, entity);
        }
    } catch (Throwable oops) {
        throw new SchemaParseException(oops);
    }

}

From source file:br.org.indt.ndg.server.client.TemporaryOpenRosaBussinessDelegate.java

public boolean parseAndPersistResult(InputStreamReader inputStreamReader, String contentType)
        throws IOException {
    String resultString = parseMultipartEncodedFile(inputStreamReader, contentType, "filename");
    SAXParserFactory factory = SAXParserFactory.newInstance();
    OpenRosaResultsHandler handler = new OpenRosaResultsHandler();
    try {//  ww  w .j a v  a  2 s . c  o m
        ByteArrayInputStream streamToParse = new ByteArrayInputStream(resultString.getBytes());
        SAXParser saxParser = factory.newSAXParser();
        saxParser.parse(streamToParse, handler);
        streamToParse.close();
    } catch (Throwable err) {
        err.printStackTrace();
    }
    String resultId = handler.getResultId();
    String deviceId = handler.getDeviceId();
    String surveyId = handler.getSurveyId();
    if (resultId == null || deviceId == null || surveyId == null) {
        return false;
    } else {
        return persistResult(resultString, surveyId, resultId, deviceId);
    }
}