Search in sources :

Example 66 with SAXParser

use of javax.xml.parsers.SAXParser in project aima-java by aimacode.

the class OsmReader method parseMap.

protected void parseMap(InputStream inputStream, MapBuilder consumer) throws SAXException, IOException {
    SAXParser parser = createParser();
    parser.parse(inputStream, new OsmHandler(consumer));
}
Also used : SAXParser(javax.xml.parsers.SAXParser)

Example 67 with SAXParser

use of javax.xml.parsers.SAXParser in project aries by apache.

the class AbstractModelBuilder method parse.

private BeansModel parse(List<URL> osgiBeansDescriptorURLs, List<URL> beanDescriptorURLs) {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(false);
    factory.setNamespaceAware(true);
    if (osgiBeansDescriptorURLs.isEmpty()) {
        throw new IllegalArgumentException("Missing osgi-beans descriptors");
    }
    SAXParser parser;
    try {
        parser = factory.newSAXParser();
    } catch (ParserConfigurationException | SAXException e) {
        return Throw.exception(e);
    }
    OSGiBeansHandler handler = getHandler(beanDescriptorURLs);
    for (URL osgiBeansDescriptorURL : osgiBeansDescriptorURLs) {
        try (InputStream inputStream = osgiBeansDescriptorURL.openStream()) {
            InputSource source = new InputSource(inputStream);
            if (source.getByteStream().available() == 0) {
                throw new IllegalArgumentException("Specified osgi-beans descriptor is empty: " + osgiBeansDescriptorURL);
            }
            try {
                parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
                parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", loadXsds());
            } catch (IllegalArgumentException | SAXNotRecognizedException | SAXNotSupportedException e) {
            // No op, we just don't validate the XML
            }
            parser.parse(source, handler);
        } catch (IOException | SAXException e) {
            return Throw.exception(e);
        }
    }
    return handler.createBeansModel();
}
Also used : InputSource(org.xml.sax.InputSource) InputStream(java.io.InputStream) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) IOException(java.io.IOException) URL(java.net.URL) SAXException(org.xml.sax.SAXException) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 68 with SAXParser

use of javax.xml.parsers.SAXParser in project asterixdb by apache.

the class TestSuiteParser method parse.

public org.apache.asterix.testframework.xml.TestSuite parse(File testSuiteCatalog) throws Exception {
    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    saxParserFactory.setNamespaceAware(true);
    saxParserFactory.setXIncludeAware(true);
    SAXParser saxParser = saxParserFactory.newSAXParser();
    saxParser.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "file");
    JAXBContext ctx = JAXBContext.newInstance(org.apache.asterix.testframework.xml.TestSuite.class);
    Unmarshaller um = ctx.createUnmarshaller();
    return (org.apache.asterix.testframework.xml.TestSuite) um.unmarshal(new SAXSource(saxParser.getXMLReader(), new InputSource(testSuiteCatalog.toURI().toString())));
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) SAXParser(javax.xml.parsers.SAXParser) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 69 with SAXParser

use of javax.xml.parsers.SAXParser in project cloudstack by apache.

the class DatabaseConfig method doConfig.

@DB
protected void doConfig() {
    try {
        final File configFile = new File(_configFileName);
        SAXParserFactory spfactory = SAXParserFactory.newInstance();
        final SAXParser saxParser = spfactory.newSAXParser();
        final DbConfigXMLHandler handler = new DbConfigXMLHandler();
        handler.setParent(this);
        Transaction.execute(new TransactionCallbackWithExceptionNoReturn<Exception>() {

            @Override
            public void doInTransactionWithoutResult(TransactionStatus status) throws Exception {
                // Save user configured values for all fields
                saxParser.parse(configFile, handler);
                // Save default values for configuration fields
                saveVMTemplate();
                saveRootDomain();
                saveDefaultConfiguations();
            }
        });
        // Check pod CIDRs against each other, and against the guest ip network/netmask
        pzc.checkAllPodCidrSubnets();
    } catch (Exception ex) {
        System.out.print("ERROR IS" + ex);
        s_logger.error("error", ex);
    }
}
Also used : SAXParser(javax.xml.parsers.SAXParser) TransactionStatus(com.cloud.utils.db.TransactionStatus) File(java.io.File) URISyntaxException(java.net.URISyntaxException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SAXException(org.xml.sax.SAXException) SAXParserFactory(javax.xml.parsers.SAXParserFactory) DB(com.cloud.utils.db.DB)

Example 70 with SAXParser

use of javax.xml.parsers.SAXParser in project geode by apache.

the class CacheXmlParser method parse.

////////////////////// Static Methods //////////////////////
/**
   * Parses XML data and from it creates an instance of <code>CacheXmlParser</code> that can be used
   * to {@link #create}the {@link Cache}, etc.
   *
   * @param is the <code>InputStream</code> of XML to be parsed
   *
   * @return a <code>CacheXmlParser</code>, typically used to create a cache from the parsed XML
   *
   * @throws CacheXmlException Something went wrong while parsing the XML
   *
   * @since GemFire 4.0
   *
   */
public static CacheXmlParser parse(InputStream is) {
    /**
     * The API doc http://java.sun.com/javase/6/docs/api/org/xml/sax/InputSource.html for the SAX
     * InputSource says: "... standard processing of both byte and character streams is to close
     * them on as part of end-of-parse cleanup, so applications should not attempt to re-use such
     * streams after they have been handed to a parser."
     *
     * In order to block the parser from closing the stream, we wrap the InputStream in a filter,
     * i.e., UnclosableInputStream, whose close() function does nothing.
     * 
     */
    class UnclosableInputStream extends BufferedInputStream {

        public UnclosableInputStream(InputStream stream) {
            super(stream);
        }

        @Override
        public void close() {
        }
    }
    CacheXmlParser handler = new CacheXmlParser();
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setFeature(DISALLOW_DOCTYPE_DECL_FEATURE, true);
        factory.setValidating(true);
        factory.setNamespaceAware(true);
        UnclosableInputStream bis = new UnclosableInputStream(is);
        try {
            SAXParser parser = factory.newSAXParser();
            // Parser always reads one buffer plus a little extra worth before
            // determining that the DTD is there. Setting mark twice the parser
            // buffer size.
            bis.mark((Integer) parser.getProperty(BUFFER_SIZE) * 2);
            parser.setProperty(JAXP_SCHEMA_LANGUAGE, XMLConstants.W3C_XML_SCHEMA_NS_URI);
            parser.parse(bis, new DefaultHandlerDelegate(handler));
        } catch (CacheXmlException e) {
            if (null != e.getCause() && e.getCause().getMessage().contains(DISALLOW_DOCTYPE_DECL_FEATURE)) {
                // Not schema based document, try dtd.
                bis.reset();
                factory.setFeature(DISALLOW_DOCTYPE_DECL_FEATURE, false);
                SAXParser parser = factory.newSAXParser();
                parser.parse(bis, new DefaultHandlerDelegate(handler));
            } else {
                throw e;
            }
        }
        return handler;
    } catch (Exception ex) {
        if (ex instanceof CacheXmlException) {
            while (true) /* ex instanceof CacheXmlException */
            {
                Throwable cause = ex.getCause();
                if (!(cause instanceof CacheXmlException)) {
                    break;
                } else {
                    ex = (CacheXmlException) cause;
                }
            }
            throw (CacheXmlException) ex;
        } else if (ex instanceof SAXException) {
            // Silly JDK 1.4.2 XML parser wraps RunTime exceptions in a
            // SAXException. Pshaw!
            SAXException sax = (SAXException) ex;
            Exception cause = sax.getException();
            if (cause instanceof CacheXmlException) {
                while (true) /* cause instanceof CacheXmlException */
                {
                    Throwable cause2 = cause.getCause();
                    if (!(cause2 instanceof CacheXmlException)) {
                        break;
                    } else {
                        cause = (CacheXmlException) cause2;
                    }
                }
                throw (CacheXmlException) cause;
            }
        }
        throw new CacheXmlException(LocalizedStrings.CacheXmlParser_WHILE_PARSING_XML.toLocalizedString(), ex);
    }
}
Also used : CacheXmlException(org.apache.geode.cache.CacheXmlException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) SAXParser(javax.xml.parsers.SAXParser) TimeoutException(org.apache.geode.cache.TimeoutException) InternalGemFireException(org.apache.geode.InternalGemFireException) EmptyStackException(java.util.EmptyStackException) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) RegionExistsException(org.apache.geode.cache.RegionExistsException) CacheXmlException(org.apache.geode.cache.CacheXmlException) SAXException(org.xml.sax.SAXException) GatewayException(org.apache.geode.cache.GatewayException) CacheWriterException(org.apache.geode.cache.CacheWriterException) SAXParseException(org.xml.sax.SAXParseException) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Aggregations

SAXParser (javax.xml.parsers.SAXParser)235 SAXParserFactory (javax.xml.parsers.SAXParserFactory)142 SAXException (org.xml.sax.SAXException)112 InputSource (org.xml.sax.InputSource)95 IOException (java.io.IOException)80 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)71 DefaultHandler (org.xml.sax.helpers.DefaultHandler)37 XMLReader (org.xml.sax.XMLReader)36 File (java.io.File)35 ByteArrayInputStream (java.io.ByteArrayInputStream)28 StringReader (java.io.StringReader)27 InputStream (java.io.InputStream)24 Attributes (org.xml.sax.Attributes)22 SAXSource (javax.xml.transform.sax.SAXSource)17 SAXParseException (org.xml.sax.SAXParseException)17 ArrayList (java.util.ArrayList)13 JAXBContext (javax.xml.bind.JAXBContext)12 Unmarshaller (javax.xml.bind.Unmarshaller)12 FileNotFoundException (java.io.FileNotFoundException)10 ValidationEvent (javax.xml.bind.ValidationEvent)9