use of javax.xml.parsers.SAXParserFactory in project tika by apache.
the class ParseContext method getSAXParserFactory.
/**
* Returns the SAX parser factory specified in this parsing context.
* If a factory is not explicitly specified, then a default factory
* instance is created and returned. The default factory instance is
* configured to be namespace-aware, not validating, and to use
* {@link XMLConstants#FEATURE_SECURE_PROCESSING secure XML processing}.
*
* @since Apache Tika 0.8
* @return SAX parser factory
*/
public SAXParserFactory getSAXParserFactory() {
SAXParserFactory factory = get(SAXParserFactory.class);
if (factory == null) {
factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(false);
try {
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
} catch (ParserConfigurationException e) {
} catch (SAXNotSupportedException e) {
} catch (SAXNotRecognizedException e) {
// TIKA-271: Some XML parsers do not support the
// secure-processing feature, even though it's required by
// JAXP in Java 5. Ignoring the exception is fine here, as
// deployments without this feature are inherently vulnerable
// to XML denial-of-service attacks.
}
}
return factory;
}
use of javax.xml.parsers.SAXParserFactory in project sling by apache.
the class FelixJettySourceReferenceFinder method findSourceReferences.
@Override
public List<SourceReference> findSourceReferences(Bundle bundle) throws SourceReferenceException {
// so infer them from the X-Jetty-Version header
if (!bundle.getSymbolicName().equals("org.apache.felix.http.jetty")) {
return Collections.emptyList();
}
final Object jettyVersion = bundle.getHeaders().get("X-Jetty-Version");
if (!(jettyVersion instanceof String)) {
log.warn("Could not retrieve Jetty version from bundle '{}' because header 'X-Jetty-Version' is not set!", bundle);
return Collections.emptyList();
}
Enumeration<URL> entries = bundle.findEntries("META-INF/maven", "pom.xml", true);
if (entries != null && entries.hasMoreElements()) {
URL entry = entries.nextElement();
InputStream pom = null;
try {
pom = entry.openStream();
try {
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
SAXParser parser = parserFactory.newSAXParser();
PomHandler handler = new PomHandler((String) jettyVersion);
parser.parse(new InputSource(pom), handler);
return handler.getReferences();
} catch (SAXException e) {
throw new SourceReferenceException(e);
} catch (ParserConfigurationException e) {
throw new SourceReferenceException(e);
} finally {
IOUtils.closeQuietly(pom);
}
} catch (IOException e) {
throw new SourceReferenceException(e);
} finally {
IOUtils.closeQuietly(pom);
}
} else {
log.warn("Could not find a pom.xml in bundle '{}'!", bundle);
return Collections.emptyList();
}
}
use of javax.xml.parsers.SAXParserFactory in project webservices-axiom by apache.
the class SAXBuilderFactory method getBuilder.
@Override
public OMXMLParserWrapper getBuilder(OMMetaFactory metaFactory, InputSource inputSource) throws Exception {
SAXParserFactory factory = implementation.newSAXParserFactory();
factory.setNamespaceAware(true);
factory.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
SAXParser parser = factory.newSAXParser();
SAXSource source = new SAXSource(new CoalescingXMLFilter(parser.getXMLReader()), inputSource);
return OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), source, false);
}
use of javax.xml.parsers.SAXParserFactory in project tomee by apache.
the class JaxbJavaee method validateJavaee.
/**
* validate the inputStream, which should be a Java EE standard deployment descriptor against its schema type
* Note, this method will use the new Java EE 6 schema to validate the old descriptors after changing their namespace and version.
*
* @param type
* @param in
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException
*/
public static void validateJavaee(final JavaeeSchema type, final InputStream in) throws ParserConfigurationException, SAXException, IOException {
final URL javaeeSchemaURL = resolveJavaeeSchemaURL(type);
if (javaeeSchemaURL == null) {
throw new IllegalArgumentException("Can not find the xsd file against type:" + type);
}
final URL xmlSchemaURL = JaxbJavaee.getSchemaURL("xml.xsd");
if (xmlSchemaURL == null) {
throw new IllegalArgumentException("Can not find the xml.xsd file");
}
// get the parser
final SAXParserFactory parserfactory = SAXParserFactory.newInstance();
parserfactory.setNamespaceAware(true);
parserfactory.setValidating(false);
final SAXParser parser = parserfactory.newSAXParser();
// get the xml filter
final Javaee6SchemaFilter xmlFilter = new Javaee6SchemaFilter(parser.getXMLReader());
// get the source
final SAXSource sourceForValidate = new SAXSource(xmlFilter, new InputSource(in));
// get the schema
final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
final JaxbJavaeeSchemaResourceResolver resourceResolver = new JaxbJavaeeSchemaResourceResolver();
schemaFactory.setResourceResolver(resourceResolver);
final Schema schema = schemaFactory.newSchema(new Source[] { new StreamSource(xmlSchemaURL.openStream()), new StreamSource(javaeeSchemaURL.openStream()) });
// validate
schema.newValidator().validate(sourceForValidate);
}
use of javax.xml.parsers.SAXParserFactory in project tomee by apache.
the class JaxbPersistenceFactory method getPersistence.
public static <T> T getPersistence(final Class<T> clazz, final InputStream persistenceDescriptor) throws Exception {
final JAXBContext jc = clazz.getClassLoader() == JaxbPersistenceFactory.class.getClassLoader() ? JaxbJavaee.getContext(clazz) : JAXBContextFactory.newInstance(clazz);
final Unmarshaller u = jc.createUnmarshaller();
final UnmarshallerHandler uh = u.getUnmarshallerHandler();
// create a new XML parser
final SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
final SAXParser parser = factory.newSAXParser();
final XMLReader xmlReader = parser.getXMLReader();
// Create a filter to intercept events
final PersistenceFilter xmlFilter = new PersistenceFilter(xmlReader);
// Be sure the filter has the JAXB content handler set (or it wont work)
xmlFilter.setContentHandler(uh);
final SAXSource source = new SAXSource(xmlFilter, new InputSource(persistenceDescriptor));
return (T) u.unmarshal(source);
}
Aggregations