Search in sources :

Example 31 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project Synthese_2BIN by TheYoungSensei.

the class MainSAXB method main.

public static void main(String[] args) {
    try {
        File inputFile = new File("library.xml");
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        SAXB userHandler = new SAXB();
        saxParser.parse(inputFile, userHandler);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : SAXParser(javax.xml.parsers.SAXParser) File(java.io.File) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 32 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project Synthese_2BIN by TheYoungSensei.

the class MainSAXC method main.

public static void main(String[] args) {
    try {
        File inputFile = new File("course.xml");
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        SAXC userHandler = new SAXC();
        saxParser.parse(inputFile, userHandler);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : SAXParser(javax.xml.parsers.SAXParser) File(java.io.File) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 33 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project zm-mailbox by Zimbra.

the class W3cDomUtil method getDom4jSAXParserWhichUsesSecureProcessing.

public static SAXParser getDom4jSAXParserWhichUsesSecureProcessing() throws XmlParseException {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setXIncludeAware(false);
    factory.setValidating(false);
    try {
        factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
    } catch (SAXNotRecognizedException | SAXNotSupportedException | ParserConfigurationException ex) {
        ZimbraLog.misc.error("Problem setting up SAXParser which supports secure XML processing", ex);
        throw XmlParseException.PARSE_ERROR();
    }
    try {
        return factory.newSAXParser();
    } catch (ParserConfigurationException | SAXException e) {
        ZimbraLog.misc.error("Problem setting up SAXParser", e);
        throw XmlParseException.PARSE_ERROR();
    }
}
Also used : SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 34 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project intellij-community by JetBrains.

the class ValidateXmlActionHandler method createParser.

protected SAXParser createParser() throws SAXException, ParserConfigurationException {
    if (!needsDtdChecking() && !needsSchemaChecking() && !myForceChecking) {
        return null;
    }
    SAXParserFactory factory = new SAXParserFactoryImpl();
    boolean schemaChecking = false;
    if (hasDtdDeclaration()) {
        factory.setValidating(true);
    }
    if (needsSchemaChecking()) {
        factory.setValidating(true);
        factory.setNamespaceAware(true);
        //jdk 1.5 API
        try {
            factory.setXIncludeAware(true);
        } catch (NoSuchMethodError ignore) {
        }
        schemaChecking = true;
    }
    try {
        factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
    } catch (Exception ignore) {
    }
    SAXParser parser = factory.newSAXParser();
    parser.setProperty(ENTITY_RESOLVER_PROPERTY_NAME, myXmlResourceResolver);
    try {
        parser.getXMLReader().setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
    } catch (Exception ignore) {
    }
    if (schemaChecking) {
        // when dtd checking schema refs could not be validated @see http://marc.theaimsgroup.com/?l=xerces-j-user&m=112504202423704&w=2
        XMLGrammarPool grammarPool = getGrammarPool(myFile, myForceChecking);
        configureEntityManager(myFile, parser);
        parser.getXMLReader().setProperty(GRAMMAR_FEATURE_ID, grammarPool);
    }
    try {
        if (schemaChecking) {
            parser.setProperty(JAXPConstants.JAXP_SCHEMA_LANGUAGE, JAXPConstants.W3C_XML_SCHEMA);
            parser.getXMLReader().setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, true);
            if (Boolean.TRUE.equals(Boolean.getBoolean(XmlResourceResolver.HONOUR_ALL_SCHEMA_LOCATIONS_PROPERTY_KEY))) {
                parser.getXMLReader().setFeature("http://apache.org/xml/features/honour-all-schemaLocations", true);
            }
            parser.getXMLReader().setFeature("http://apache.org/xml/features/validation/warn-on-undeclared-elemdef", Boolean.TRUE);
            parser.getXMLReader().setFeature("http://apache.org/xml/features/validation/warn-on-duplicate-attdef", Boolean.TRUE);
        }
        parser.getXMLReader().setFeature("http://apache.org/xml/features/warn-on-duplicate-entitydef", Boolean.TRUE);
        parser.getXMLReader().setFeature("http://apache.org/xml/features/validation/unparsed-entity-checking", Boolean.FALSE);
    } catch (SAXNotRecognizedException ex) {
        // it is possible to continue work with configured parser
        LOG.info("Xml parser installation seems screwed", ex);
    }
    return parser;
}
Also used : XMLGrammarPool(org.apache.xerces.xni.grammars.XMLGrammarPool) SAXParserFactoryImpl(org.apache.xerces.jaxp.SAXParserFactoryImpl) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 35 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project jangaroo-tools by CoreMedia.

the class ExmlValidator method setupSAXParser.

private SAXParser setupSAXParser() throws IOException {
    try {
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        ExmlSchemaResolver exmlSchemaResolver = new ExmlSchemaResolver();
        schemaFactory.setResourceResolver(exmlSchemaResolver);
        List<Source> schemas = new ArrayList<Source>();
        schemas.add(new StreamSource(getClass().getResourceAsStream(Exmlc.EXML_SCHEMA_LOCATION), "exml"));
        schemas.add(new StreamSource(getClass().getResourceAsStream(Exmlc.EXML_UNTYPED_SCHEMA_LOCATION), "untyped"));
        Collection<ExmlSchemaSource> exmlSchemaSources = exmlSchemaSourceByNamespace.values();
        for (ExmlSchemaSource exmlSchemaSource : exmlSchemaSources) {
            schemas.add(exmlSchemaSource.newStreamSource());
        }
        Schema exmlSchema = schemaFactory.newSchema(schemas.toArray(new Source[schemas.size()]));
        final SAXParserFactory saxFactory = SAXParserFactory.newInstance();
        saxFactory.setNamespaceAware(true);
        saxFactory.setSchema(exmlSchema);
        SAXParser saxParser = saxFactory.newSAXParser();
        saxParser.getXMLReader().setEntityResolver(new EntityResolver() {

            @Override
            public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                //To change body of implemented methods use File | Settings | File Templates.
                return null;
            }
        });
        return saxParser;
    } catch (ParserConfigurationException e) {
        throw new IllegalStateException("A default dom builder should be provided.", e);
    } catch (SAXParseException e) {
        // SAX parser error while parsing EXML schemas: log only, will cause error or warning, depending on configuration:
        logSAXParseException(null, e, true);
        return null;
    } catch (SAXException e) {
        throw new IllegalStateException("SAX parser does not support validation.", e);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputSource(org.xml.sax.InputSource) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) ArrayList(java.util.ArrayList) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXException(org.xml.sax.SAXException) SAXParseException(org.xml.sax.SAXParseException) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Aggregations

SAXParserFactory (javax.xml.parsers.SAXParserFactory)183 SAXParser (javax.xml.parsers.SAXParser)141 InputSource (org.xml.sax.InputSource)76 SAXException (org.xml.sax.SAXException)75 IOException (java.io.IOException)62 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)53 XMLReader (org.xml.sax.XMLReader)37 DefaultHandler (org.xml.sax.helpers.DefaultHandler)27 InputStream (java.io.InputStream)22 File (java.io.File)21 SAXSource (javax.xml.transform.sax.SAXSource)21 ByteArrayInputStream (java.io.ByteArrayInputStream)16 StringReader (java.io.StringReader)15 Unmarshaller (javax.xml.bind.Unmarshaller)13 Attributes (org.xml.sax.Attributes)13 JAXBContext (javax.xml.bind.JAXBContext)12 SAXParseException (org.xml.sax.SAXParseException)10 InputStreamReader (java.io.InputStreamReader)9 ArrayList (java.util.ArrayList)9 ValidationEvent (javax.xml.bind.ValidationEvent)9