use of org.xml.sax.XMLFilter in project opennms by OpenNMS.
the class JaxbUtils method getXMLFilterForClass.
public static <T> XMLFilter getXMLFilterForClass(final Class<T> clazz) throws SAXException {
final String namespace = getNamespaceForClass(clazz);
XMLFilter filter = namespace == null ? new SimpleNamespaceFilter("", false) : new SimpleNamespaceFilter(namespace, true);
LOG.trace("namespace filter for class {}: {}", clazz, filter);
final XMLReader xmlReader = XMLReaderFactory.createXMLReader();
xmlReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
filter.setParent(xmlReader);
return filter;
}
use of org.xml.sax.XMLFilter in project fabric8 by jboss-fuse.
the class JaxbUtil method unmarshalNoValidate.
private static Features unmarshalNoValidate(String uri, InputStream stream) {
try {
Unmarshaller unmarshaller = FEATURES_CONTEXT.createUnmarshaller();
XMLFilter xmlFilter = new NoSourceAndNamespaceFilter(XmlUtils.xmlReader());
xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler());
InputSource is = new InputSource(uri);
if (stream != null) {
is.setByteStream(stream);
}
SAXSource source = new SAXSource(xmlFilter, new InputSource(uri));
return (Features) unmarshaller.unmarshal(source);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException("Unable to load " + uri, e);
}
}
use of org.xml.sax.XMLFilter in project cxf by apache.
the class XSLTJaxbProvider method unmarshalFromInputStream.
@Override
protected Object unmarshalFromInputStream(Unmarshaller unmarshaller, InputStream is, Annotation[] anns, MediaType mt) throws JAXBException {
try {
Templates t = createTemplates(getInTemplates(anns, mt), inParamsMap, inProperties);
if (t == null && supportJaxbOnly) {
return super.unmarshalFromInputStream(unmarshaller, is, anns, mt);
}
if (unmarshaller.getClass().getName().contains("eclipse")) {
// eclipse MOXy doesn't work properly with the XMLFilter/Reader thing
// so we need to bounce through a DOM
Source reader = new StaxSource(StaxUtils.createXMLStreamReader(is));
DOMResult dom = new DOMResult();
t.newTransformer().transform(reader, dom);
return unmarshaller.unmarshal(dom.getNode());
}
XMLFilter filter;
try {
filter = factory.newXMLFilter(t);
} catch (TransformerConfigurationException ex) {
TemplatesImpl ti = (TemplatesImpl) t;
filter = factory.newXMLFilter(ti.getTemplates());
trySettingProperties(filter, ti);
}
XMLReader reader = new StaxSource(StaxUtils.createXMLStreamReader(is));
filter.setParent(reader);
SAXSource source = new SAXSource();
source.setXMLReader(filter);
if (systemId != null) {
source.setSystemId(systemId);
}
return unmarshaller.unmarshal(source);
} catch (TransformerException ex) {
LOG.warning("Transformation exception : " + ex.getMessage());
throw ExceptionUtils.toInternalServerErrorException(ex, null);
}
}
use of org.xml.sax.XMLFilter in project opennms by OpenNMS.
the class XmlTest method validateJaxbXmlAgainstSchema.
@Test
public void validateJaxbXmlAgainstSchema() throws Exception {
final String schemaFile = getSchemaFile();
if (schemaFile == null) {
LOG.warn("Skipping validation.");
return;
}
LOG.debug("Validating against XSD: {}", schemaFile);
javax.xml.bind.Unmarshaller unmarshaller = JaxbUtils.getUnmarshallerFor(getSampleClass(), null, true);
final SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
final Schema schema = factory.newSchema(new StreamSource(schemaFile));
unmarshaller.setSchema(schema);
unmarshaller.setEventHandler(new ValidationEventHandler() {
@Override
public boolean handleEvent(final ValidationEvent event) {
LOG.warn("Received validation event: {}", event, event.getLinkedException());
return false;
}
});
try {
final InputSource inputSource = new InputSource(getSampleXmlInputStream());
final XMLFilter filter = JaxbUtils.getXMLFilterForClass(getSampleClass());
final SAXSource source = new SAXSource(filter, inputSource);
@SuppressWarnings("unchecked") T obj = (T) unmarshaller.unmarshal(source);
assertNotNull(obj);
} finally {
unmarshaller.setSchema(null);
}
}
use of org.xml.sax.XMLFilter in project opennms by OpenNMS.
the class JaxbUtils method unmarshal.
public static <T> T unmarshal(final Class<T> clazz, final InputSource inputSource, final JAXBContext jaxbContext, final boolean validate) {
final Unmarshaller um = getUnmarshallerFor(clazz, jaxbContext, validate);
LOG.trace("unmarshalling class {} from input source {} with unmarshaller {}", clazz.getSimpleName(), inputSource, um);
try {
final XMLFilter filter = getXMLFilterForClass(clazz);
final SAXSource source = new SAXSource(filter, inputSource);
um.setEventHandler(new LoggingValidationEventHandler());
final JAXBElement<T> element = um.unmarshal(source, clazz);
return element.getValue();
} catch (final SAXException e) {
throw EXCEPTION_TRANSLATOR.translate("creating an XML reader object", e);
} catch (final JAXBException e) {
throw EXCEPTION_TRANSLATOR.translate("unmarshalling an object (" + clazz.getSimpleName() + ")", e);
}
}
Aggregations