use of javax.xml.transform.sax.SAXSource in project webservices-axiom by apache.
the class TestDetachWithSAXSource method runTest.
@Override
protected void runTest() throws Throwable {
DummyXMLReader xmlReader = new DummyXMLReader();
OMXMLParserWrapper builder = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), new SAXSource(xmlReader, new InputSource()), false);
assertThat(xmlReader.isParsed()).isFalse();
builder.detach();
assertThat(xmlReader.isParsed()).isTrue();
}
use of javax.xml.transform.sax.SAXSource 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.transform.sax.SAXSource 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);
}
use of javax.xml.transform.sax.SAXSource in project tomee by apache.
the class JaxbOpenejbJar3 method unmarshal.
public static <T> T unmarshal(final Class<T> type, final InputStream in) throws ParserConfigurationException, SAXException, JAXBException {
final InputSource inputSource = new InputSource(in);
final SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(false);
final SAXParser parser = factory.newSAXParser();
final JAXBContext ctx = getContext(type);
final Unmarshaller unmarshaller = ctx.createUnmarshaller();
unmarshaller.setEventHandler(new ValidationEventHandler() {
public boolean handleEvent(final ValidationEvent validationEvent) {
// System.out.println(validationEvent);
return false;
}
});
final NamespaceFilter xmlFilter = new NamespaceFilter(parser.getXMLReader());
xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler());
final SAXSource source = new SAXSource(xmlFilter, inputSource);
final Object o = unmarshaller.unmarshal(source);
if (o instanceof JAXBElement) {
final JAXBElement element = (JAXBElement) o;
return (T) element.getValue();
}
return (T) o;
}
use of javax.xml.transform.sax.SAXSource in project tomee by apache.
the class JaxbSun method unmarshal.
public static <T> Object unmarshal(final Class<T> type, final InputStream in, final boolean logErrors) throws ParserConfigurationException, SAXException, JAXBException {
// create a parser with validation disabled
final SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(false);
final SAXParser parser = factory.newSAXParser();
// Get the JAXB context -- this should be cached
final JAXBContext ctx = JAXBContextFactory.newInstance(type);
// get the unmarshaller
final Unmarshaller unmarshaller = ctx.createUnmarshaller();
// log errors?
unmarshaller.setEventHandler(new ValidationEventHandler() {
public boolean handleEvent(final ValidationEvent validationEvent) {
if (logErrors) {
System.out.println(validationEvent);
}
return false;
}
});
// add our XMLFilter which disables dtd downloading
final NamespaceFilter xmlFilter = new NamespaceFilter(parser.getXMLReader());
xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler());
// Wrap the input stream with our filter
final SAXSource source = new SAXSource(xmlFilter, new InputSource(in));
// unmarshal the document
return unmarshaller.unmarshal(source);
}
Aggregations