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:edu.psu.citeseerx.updates.external.metadata.ACMMetadataUpdater.java

public void updateACM() {
    try {//from www  .ja v  a  2 s .c o m
        // Get the SAX factory.
        SAXParserFactory factory = SAXParserFactory.newInstance();

        // Neither we want validation nor namespaces.
        factory.setNamespaceAware(false);
        factory.setValidating(true);

        SAXParser parser = factory.newSAXParser();

        /*xmlReader.setFeature(
            "http://apache.org/xml/features/nonvalidating/load-external-dtd", 
            false);*/

        parser.parse(ACMDataFile, acmHandler);
    } catch (ParserConfigurationException e) {
        logger.error("The underlaying parser doesn't support the " + "requested feature", e);
    } catch (SAXException e) {
        logger.error("Error", e);
    } catch (IOException e) {
        logger.error("A parsing error has occurred: " + ACMDataFile, e);
    }
}

From source file:de.tudarmstadt.ukp.dkpro.core.io.ancora.AncoraReader.java

@Override
public void getNext(JCas aJCas) throws IOException, CollectionException {
    Resource res = nextFile();/*from  w w  w . j a v  a  2 s . c o m*/
    initCas(aJCas, res);

    // Set up language
    if (getLanguage() != null) {
        aJCas.setDocumentLanguage(getLanguage());
    }

    // Configure mapping only now, because now the language is set in the CAS
    try {
        posMappingProvider.configure(aJCas.getCas());
    } catch (AnalysisEngineProcessException e1) {
        throw new IOException(e1);
    }

    InputStream is = null;
    try {
        is = CompressionUtils.getInputStream(res.getLocation(), res.getInputStream());

        // Create handler
        AncoraHandler handler = new AncoraHandler();
        handler.setJCas(aJCas);
        handler.setLogger(getLogger());

        // Parse XML
        SAXParserFactory pf = SAXParserFactory.newInstance();
        SAXParser parser = pf.newSAXParser();

        InputSource source = new InputSource(is);
        source.setPublicId(res.getLocation());
        source.setSystemId(res.getLocation());
        parser.parse(source, handler);
    } catch (ParserConfigurationException | SAXException e) {
        throw new IOException(e);
    } finally {
        closeQuietly(is);
    }

    if (dropSentencesMissingPosTags) {
        List<FeatureStructure> toRemove = new ArrayList<>();

        // Remove sentences without pos TAGs
        for (Sentence s : select(aJCas, Sentence.class)) {
            boolean remove = false;
            for (Token t : selectCovered(Token.class, s)) {
                if (t.getPos() == null) {
                    toRemove.add(s);
                    remove = true;
                    break;
                }
            }

            if (remove) {
                for (Token t : selectCovered(Token.class, s)) {
                    toRemove.add(t);
                    if (t.getLemma() != null) {
                        toRemove.add(t.getLemma());
                    }
                    if (t.getPos() != null) {
                        toRemove.add(t.getPos());
                    }
                }
            }
        }

        for (FeatureStructure fs : toRemove) {
            aJCas.getCas().removeFsFromIndexes(fs);
        }

        // Remove tokens without pos tags that are located *BETWEEN* sentences!
        toRemove.clear();
        for (Token t : select(aJCas, Token.class)) {
            if (t.getPos() == null) {
                toRemove.add(t);
                if (t.getLemma() != null) {
                    toRemove.add(t.getLemma());
                }
                if (t.getPos() != null) {
                    toRemove.add(t.getPos());
                }
            }
        }

        for (FeatureStructure fs : toRemove) {
            aJCas.getCas().removeFsFromIndexes(fs);
        }
    }
}

From source file:eu.scape_project.planning.xml.PlanMigrator.java

/**
 * Detect the version of the given XML representation of plans. If the
 * version of the XML representation is not up to date, necessary
 * transformations are applied./*from   w w  w  . j av a  2  s  .c  o  m*/
 * 
 * @param importData
 * @return null if the transformation fails, otherwise an up to date XML
 *         representation
 * @throws IOException
 *             if parsing the XML representation fails
 * @throws SAXException
 *             if parsing the XML representation fails
 */
public String getCurrentVersionData(final InputStream in, final String tempPath,
        final List<String> appliedTransformations) throws PlatoException {
    String originalFile = tempPath + "_original.xml";
    try {
        FileUtils.writeToFile(in, new FileOutputStream(originalFile));

        /** check for the version of the file **/

        // The version of the read xml file is unknown, so it is not possible to
        // validate it
        // moreover, in old plans the version attribute was on different
        // nodes(project, projects),
        // with a different name (fileVersion)
        // to be backwards compatible we create rules for all these attributes
        fileVersion = "xxx";
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(false);
        Digester d = new Digester(factory.newSAXParser());

        d.setValidating(false);
        // StrictErrorHandler errorHandler = new StrictErrorHandler();
        // d.setErrorHandler(errorHandler);
        d.push(this);
        // to read the version we have to support all versions:
        d.addSetProperties("*/projects", "version", "fileVersion");
        // manually migrated projects may have the file version in the node
        // projects/project
        d.addSetProperties("*/projects/project", "version", "fileVersion");
        // pre V1.3 version info was stored in the project node
        d.addSetProperties("*/project", "version", "fileVersion");
        // since V1.9 the root node is plans:
        d.addSetProperties("plans", "version", "fileVersion");

        InputStream inV = new FileInputStream(originalFile);
        d.parse(inV);
        inV.close();
        /** this could be more sophisticated, but for now this is enough **/
        String version = "1.0";
        if (fileVersion != null) {
            version = fileVersion;
        }

        String fileTo = originalFile;
        String fileFrom = originalFile;

        boolean success = true;
        if ("xxx".equals(version)) {
            fileFrom = fileTo;
            fileTo = fileFrom + "_V1.3.xml";
            /** this is an old export file, transform it to the 1.3 schema **/
            success = transformXmlData(fileFrom, fileTo, "data/xslt/Vxxx-to-V1.3.xsl");
            appliedTransformations.add("Vxxx-to-V1.3.xsl");
            version = "1.3";
        }
        if (success && "1.3".equals(version)) {
            fileFrom = fileTo;
            fileTo = fileFrom + "_V1.9.xml";
            success = transformXmlData(fileFrom, fileTo, "data/xslt/V1.3-to-V1.9.xsl");
            appliedTransformations.add("V1.3-to-V1.9.xsl");
            version = "1.9";
        }
        // with release of Plato 2.0 and its schema ProjectExporter creates
        // documents with version 2.0
        if (success && "1.9".equals(version)) {
            version = "2.0";
        }
        if (success && "2.0".equals(version)) {
            // transform the document to version 2.1
            fileFrom = fileTo;
            fileTo = fileFrom + "_V2.1.xml";
            success = transformXmlData(fileFrom, fileTo, "data/xslt/V2.0-to-V2.1.xsl");
            appliedTransformations.add("V2.0-to-V2.1.xsl");
            version = "2.1";
        }
        if (success && "2.1".equals(version)) {
            // transform the document to version 2.1.2
            fileFrom = fileTo;
            fileTo = fileFrom + "_V2.1.2.xml";
            success = transformXmlData(fileFrom, fileTo, "data/xslt/V2.1-to-V2.1.2.xsl");
            appliedTransformations.add("V2.1-to-V2.1.2.xsl");
            version = "2.1.2";
        }
        if (success && "2.1.1".equals(version)) {
            // transform the document to version 2.1.2
            fileFrom = fileTo;
            fileTo = fileFrom + "_V2.1.2.xml";
            success = transformXmlData(fileFrom, fileTo, "data/xslt/V2.1.1-to-V2.1.2.xsl");
            appliedTransformations.add("V2.1.1-to-V2.1.2.xsl");
            version = "2.1.2";
        }

        if (success && "2.1.2".equals(version)) {
            // transform the document to version 3.0.0
            fileFrom = fileTo;
            fileTo = fileFrom + "_V3.0.0.xml";
            success = transformXmlData(fileFrom, fileTo, "data/xslt/V2.1.2-to-V3.0.0.xsl");
            appliedTransformations.add("V2.1.2-to-V3.0.0.xsl");
            version = "3.0.0";
        }
        if (success && "3.0.0".equals(version)) {
            // transform the document to version 3.0.1
            fileFrom = fileTo;
            fileTo = fileFrom + "_V3.0.1.xml";
            success = transformXmlData(fileFrom, fileTo, "data/xslt/V3.0.0-to-V3.0.1.xsl");
            appliedTransformations.add("V3.0.0-to-V3.0.1.xsl");
            version = "3.0.1";
        }
        if (success && "3.0.1".equals(version)) {
            // transform the document to version 3.9.0
            fileFrom = fileTo;
            fileTo = fileFrom + "_V3.9.0.xml";
            success = transformXmlData(fileFrom, fileTo, "data/xslt/V3.0.1-to-V3.9.0.xsl");
            appliedTransformations.add("V3.0.1-to-V3.9.0.xsl");
            version = "3.9.0";
        }
        if (success && "3.9.0".equals(version)) {
            // transform the document to version 3.9.9
            fileFrom = fileTo;
            fileTo = fileFrom + "_V3.9.9.xml";
            success = transformXmlData(fileFrom, fileTo, "data/xslt/V3.9.0-to-V3.9.9.xsl");
            appliedTransformations.add("V3.9.0-to-V3.9.9.xsl");
            version = "3.9.9";
        }
        if (success && "3.9.9".equals(version)) {
            // transform the document to version 4.0.0
            fileFrom = fileTo;
            fileTo = fileFrom + "_V4.0.1.xml";
            success = transformXmlData(fileFrom, fileTo, "data/xslt/V3.9.9-to-V4.0.1.xsl");
            appliedTransformations.add("V3.9.9-to-V4.0.1.xsl");
            version = "4.0.1";
        }
        if (success && "4.0.1".equals(version)) {
            // transform the document to version 4.0.0
            fileFrom = fileTo;
            fileTo = fileFrom + "_V4.0.2.xml";
            success = transformXmlData(fileFrom, fileTo, "data/xslt/V4.0.1-to-V4.0.2.xsl");
            appliedTransformations.add("V4.0.1-to-V4.0.2.xsl");
            version = "4.0.2";
        }

        if (success) {
            return fileTo;
        } else {
            return null;
        }
    } catch (Exception e) {
        throw new PlatoException("Failed to update plan to current version.", e);
    }
}

From source file:com.trailmagic.image.util.ImagesParserImpl.java

@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void parseFile(File metadataFile) {
    try {//from   w ww .ja va  2s .c  o m

        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setValidating(true);
        SAXParser parser = factory.newSAXParser();

        s_logger.info("Parsing " + metadataFile.getPath() + "...");
        parser.parse(metadataFile, this);
        s_logger.info("done");
    } catch (Throwable t) {
        t.printStackTrace();
        System.exit(1);
    }

}

From source file:com.trailmagic.image.util.ImagesParserImpl.java

@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void parse(InputStream inputStream) {
    try {//ww w  .j av  a 2  s . c o m

        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setValidating(true);
        SAXParser parser = factory.newSAXParser();
        XMLReader reader = parser.getXMLReader();

        //            CatalogManager cm = new CatalogManager();
        //            Catalog catalog = cm.getCatalog();
        //            catalog.parseCatalog(getClass().getClassLoader().getResource("CatalogManager.properties"));
        //            reader.setEntityResolver(new CatalogResolver(cm));

        s_logger.info("Parsing input stream...");
        parser.parse(inputStream, this);
        s_logger.info("done");
    } catch (Throwable t) {
        t.printStackTrace();
        System.exit(1);
    }
}

From source file:com.determinato.feeddroid.parser.RssParser.java

/**
 * Persists RSS item to the database./*www  .ja va  2 s .  c  o  m*/
 * @param id item ID
 * @param folderId ID of containing folder
 * @param rssurl URL of RSS feed
 * @return long containing ID of inserted item
 * @throws Exception
 */
public long syncDb(long id, long folderId, String rssurl) throws Exception {
    mId = id;
    mFolderId = folderId;
    mRssUrl = rssurl;

    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser parser = factory.newSAXParser();
    XMLReader reader = parser.getXMLReader();

    reader.setContentHandler(this);
    reader.setErrorHandler(this);

    URL url = new URL(mRssUrl);

    URLConnection c = url.openConnection();
    // TODO: Is this a known user agent, or do I need to come up with my own?
    c.setRequestProperty("User-Agent", "Android/m3-rc37a");

    try {
        BufferedReader bufReader = new BufferedReader(new InputStreamReader(c.getInputStream()), 65535);
        reader.parse(new InputSource(bufReader));
    } catch (NullPointerException e) {
        Log.e(TAG, Log.getStackTraceString(e));
        Log.e(TAG, "Failed to load URL" + url.toString());
    }

    return mId;
}

From source file:com.esri.gpt.server.csw.client.CswCatalog.java

/**
 * Execute GetCapabilities using SAX objects. Send GetCapabilities request,
 * receive the response from a service, and parse the response to get URLs for
 * "GetRecords" and "GetRecordsById"./* www .j  a v a2  s.  co  m*/
 * 
 * @return the csw catalog capabilities
 * @throws SAXException the sAX exception
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws ParserConfigurationException the parser configuration exception
 * @return Csw Capabilities object
 */
private CswCatalogCapabilities executeGetCapabilitiesWithSAX()
        throws SAXException, IOException, ParserConfigurationException {
    CswCatalogCapabilities capabilities = new CswCatalogCapabilities();

    CswClient client = new CswClient();
    client.setConnectTimeout(this.getConnectionTimeoutMs());
    client.setReadTimeout(this.getResponseTimeoutMs());
    client.setBatchHttpClient(getBatchHttpClient());
    // Execute submission and parsing into response element
    InputStream responseStream = client.submitHttpRequest("GET", url, "");

    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);
    CapabilitiesParse cParse = new CapabilitiesParse(capabilities);
    factory.newSAXParser().parse(new InputSource(responseStream), cParse);

    this.capabilities = capabilities;
    Utils.close(responseStream);
    return capabilities;
}

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

private void loadXsd(File file) throws SchemaParseException {
    try {// w ww.j a  va2  s.  c  o 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:architecture.common.util.L10NUtils.java

private void loadProps(String resource, boolean breakOnError) throws IOException {

    HashSet<URL> hashset = new HashSet<URL>();
    if (log.isDebugEnabled()) {
        log.debug((new StringBuilder()).append("Searching ").append(resource).toString());
    }//from ww w  .j  a v a2s. c om
    Enumeration<URL> urls = Thread.currentThread().getContextClassLoader().getResources(resource);
    if (urls != null) {
        URL url;
        for (; urls.hasMoreElements(); hashset.add(url)) {
            url = urls.nextElement();
            if (log.isDebugEnabled())
                log.debug((new StringBuilder()).append("Adding ").append(url).toString());
        }
    }

    for (URL url : hashset) {
        if (log.isDebugEnabled())
            log.debug((new StringBuilder()).append("Loading from ").append(url).toString());
        InputStream is = null;
        try {
            is = url.openStream();
            InputSource input = new InputSource(is);
            SAXParserFactory factory = SAXParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XMLReader xmlreader = factory.newSAXParser().getXMLReader();
            I18nParsingHandler handler = new I18nParsingHandler();
            xmlreader.setContentHandler(handler);
            xmlreader.setDTDHandler(handler);
            xmlreader.setEntityResolver(handler);
            xmlreader.setErrorHandler(handler);
            xmlreader.parse(input);
            localizers.addAll(handler.localizers);
        } catch (IOException e) {
            if (log.isDebugEnabled())
                log.debug((new StringBuilder()).append("Skipping ").append(url).toString());
            if (breakOnError)
                throw e;
        } catch (Exception e) {
            log.error(e);
        } finally {
            if (is != null)
                IOUtils.closeQuietly(is);
        }
    }

}