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();
}
}
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();
}
}
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();
}
}
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;
}
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);
}
}
Aggregations