use of javax.xml.transform.sax.SAXSource in project robovm by robovm.
the class SourceTreeManager method getXMLReader.
/**
* This method returns the SAX2 parser to use with the InputSource
* obtained from this URI.
* It may return null if any SAX2-conformant XML parser can be used,
* or if getInputSource() will also return null. The parser must
* be free for use (i.e.
* not currently in use for another parse().
*
* @param inputSource The value returned from the URIResolver.
* @return a SAX2 XMLReader to use to resolve the inputSource argument.
* @param locator The location of the original caller, for diagnostic purposes.
*
* @throws TransformerException if the reader can not be created.
*/
public static XMLReader getXMLReader(Source inputSource, SourceLocator locator) throws TransformerException {
try {
XMLReader reader = (inputSource instanceof SAXSource) ? ((SAXSource) inputSource).getXMLReader() : null;
if (null == reader) {
try {
javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
javax.xml.parsers.SAXParser jaxpParser = factory.newSAXParser();
reader = jaxpParser.getXMLReader();
} catch (javax.xml.parsers.ParserConfigurationException ex) {
throw new org.xml.sax.SAXException(ex);
} catch (javax.xml.parsers.FactoryConfigurationError ex1) {
throw new org.xml.sax.SAXException(ex1.toString());
} catch (NoSuchMethodError ex2) {
} catch (AbstractMethodError ame) {
}
if (null == reader)
reader = XMLReaderFactory.createXMLReader();
}
try {
reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
} catch (org.xml.sax.SAXException se) {
// What can we do?
// TODO: User diagnostics.
}
return reader;
} catch (org.xml.sax.SAXException se) {
throw new TransformerException(se.getMessage(), locator, se);
}
}
use of javax.xml.transform.sax.SAXSource in project spring-framework by spring-projects.
the class Jaxb2Marshaller method loadSchema.
// on JDK 9
@SuppressWarnings("deprecation")
private Schema loadSchema(Resource[] resources, String schemaLanguage) throws IOException, SAXException {
if (logger.isDebugEnabled()) {
logger.debug("Setting validation schema to " + StringUtils.arrayToCommaDelimitedString(this.schemaResources));
}
Assert.notEmpty(resources, "No resources given");
Assert.hasLength(schemaLanguage, "No schema language provided");
Source[] schemaSources = new Source[resources.length];
XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
for (int i = 0; i < resources.length; i++) {
Assert.notNull(resources[i], "Resource is null");
Assert.isTrue(resources[i].exists(), "Resource " + resources[i] + " does not exist");
InputSource inputSource = SaxResourceUtils.createInputSource(resources[i]);
schemaSources[i] = new SAXSource(xmlReader, inputSource);
}
SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage);
if (this.schemaResourceResolver != null) {
schemaFactory.setResourceResolver(this.schemaResourceResolver);
}
return schemaFactory.newSchema(schemaSources);
}
use of javax.xml.transform.sax.SAXSource in project spring-framework by spring-projects.
the class Jaxb2MarshallerTests method unmarshalSaxSourceWithXmlOptions.
// SPR-10806
@Test
public void unmarshalSaxSourceWithXmlOptions() throws Exception {
final javax.xml.bind.Unmarshaller unmarshaller = mock(javax.xml.bind.Unmarshaller.class);
Jaxb2Marshaller marshaller = new Jaxb2Marshaller() {
@Override
protected javax.xml.bind.Unmarshaller createUnmarshaller() {
return unmarshaller;
}
};
// 1. external-general-entities and dtd support disabled (default)
marshaller.unmarshal(new SAXSource(new InputSource("1")));
ArgumentCaptor<SAXSource> sourceCaptor = ArgumentCaptor.forClass(SAXSource.class);
verify(unmarshaller).unmarshal(sourceCaptor.capture());
SAXSource result = sourceCaptor.getValue();
assertEquals(true, result.getXMLReader().getFeature("http://apache.org/xml/features/disallow-doctype-decl"));
assertEquals(false, result.getXMLReader().getFeature("http://xml.org/sax/features/external-general-entities"));
// 2. external-general-entities and dtd support enabled
reset(unmarshaller);
marshaller.setProcessExternalEntities(true);
marshaller.setSupportDtd(true);
marshaller.unmarshal(new SAXSource(new InputSource("1")));
verify(unmarshaller).unmarshal(sourceCaptor.capture());
result = sourceCaptor.getValue();
assertEquals(false, result.getXMLReader().getFeature("http://apache.org/xml/features/disallow-doctype-decl"));
assertEquals(true, result.getXMLReader().getFeature("http://xml.org/sax/features/external-general-entities"));
}
use of javax.xml.transform.sax.SAXSource in project spring-framework by spring-projects.
the class Jaxb2MarshallerTests method unmarshalStreamSourceWithXmlOptions.
// SPR-10806
@Test
public void unmarshalStreamSourceWithXmlOptions() throws Exception {
final javax.xml.bind.Unmarshaller unmarshaller = mock(javax.xml.bind.Unmarshaller.class);
Jaxb2Marshaller marshaller = new Jaxb2Marshaller() {
@Override
protected javax.xml.bind.Unmarshaller createUnmarshaller() {
return unmarshaller;
}
};
// 1. external-general-entities and dtd support disabled (default)
marshaller.unmarshal(new StreamSource("1"));
ArgumentCaptor<SAXSource> sourceCaptor = ArgumentCaptor.forClass(SAXSource.class);
verify(unmarshaller).unmarshal(sourceCaptor.capture());
SAXSource result = sourceCaptor.getValue();
assertEquals(true, result.getXMLReader().getFeature("http://apache.org/xml/features/disallow-doctype-decl"));
assertEquals(false, result.getXMLReader().getFeature("http://xml.org/sax/features/external-general-entities"));
// 2. external-general-entities and dtd support enabled
reset(unmarshaller);
marshaller.setProcessExternalEntities(true);
marshaller.setSupportDtd(true);
marshaller.unmarshal(new StreamSource("1"));
verify(unmarshaller).unmarshal(sourceCaptor.capture());
result = sourceCaptor.getValue();
assertEquals(false, result.getXMLReader().getFeature("http://apache.org/xml/features/disallow-doctype-decl"));
assertEquals(true, result.getXMLReader().getFeature("http://xml.org/sax/features/external-general-entities"));
}
use of javax.xml.transform.sax.SAXSource in project spring-framework by spring-projects.
the class CastorUnmarshallerTests method unmarshalSaxSourceWithXmlOptions.
@Test
public void unmarshalSaxSourceWithXmlOptions() throws Exception {
final AtomicReference<XMLReader> result = new AtomicReference<>();
CastorMarshaller marshaller = new CastorMarshaller() {
@Override
protected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource) {
result.set(xmlReader);
return null;
}
};
// 1. external-general-entities and dtd support disabled (default)
marshaller.unmarshal(new SAXSource(new InputSource("1")));
assertNotNull(result.get());
assertEquals(true, result.get().getFeature("http://apache.org/xml/features/disallow-doctype-decl"));
assertEquals(false, result.get().getFeature("http://xml.org/sax/features/external-general-entities"));
// 2. external-general-entities and dtd support enabled
result.set(null);
marshaller.setSupportDtd(true);
marshaller.setProcessExternalEntities(true);
marshaller.unmarshal(new SAXSource(new InputSource("1")));
assertNotNull(result.get());
assertEquals(false, result.get().getFeature("http://apache.org/xml/features/disallow-doctype-decl"));
assertEquals(true, result.get().getFeature("http://xml.org/sax/features/external-general-entities"));
}
Aggregations