use of javax.xml.bind.ValidationEventHandler 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 javax.xml.bind.ValidationEventHandler in project camel by apache.
the class JaxbDataFormat method createMarshaller.
protected Marshaller createMarshaller() throws JAXBException, SAXException, FileNotFoundException, MalformedURLException {
Marshaller marshaller = getContext().createMarshaller();
if (schema != null) {
marshaller.setSchema(cachedSchema);
marshaller.setEventHandler(new ValidationEventHandler() {
public boolean handleEvent(ValidationEvent event) {
// stop marshalling if the event is an ERROR or FATAL ERROR
return event.getSeverity() == ValidationEvent.WARNING;
}
});
}
return marshaller;
}
use of javax.xml.bind.ValidationEventHandler in project ddf by codice.
the class TestXmlParserConfigurator method setup.
@Before
public void setup() {
pc = new XmlParserConfigurator();
testHandler = new ValidationEventHandler() {
@Override
public boolean handleEvent(ValidationEvent event) {
return false;
}
};
testAdapter = new XmlAdapter() {
@Override
public Object unmarshal(Object v) throws Exception {
return null;
}
@Override
public Object marshal(Object v) throws Exception {
return null;
}
};
}
use of javax.xml.bind.ValidationEventHandler in project tomee by apache.
the class JaxbJavaee method unmarshalJavaee.
/**
* Convert the namespaceURI in the input to the javaee URI, do not validate the xml, and read in a T.
*
* @param type Class of object to be read in
* @param in input stream to read
* @param <T> class of object to be returned
* @return a T read from the input stream
* @throws ParserConfigurationException is the SAX parser can not be configured
* @throws SAXException if there is an xml problem
* @throws JAXBException if the xml cannot be marshalled into a T.
*/
public static <T> Object unmarshalJavaee(final Class<T> type, final InputStream in) throws ParserConfigurationException, SAXException, JAXBException {
final SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(false);
final SAXParser parser = factory.newSAXParser();
final JAXBContext ctx = JaxbJavaee.getContext(type);
final Unmarshaller unmarshaller = ctx.createUnmarshaller();
unmarshaller.setEventHandler(new ValidationEventHandler() {
public boolean handleEvent(final ValidationEvent validationEvent) {
final String verbose = System.getProperty("openejb.validation.output.level");
if (verbose != null && "VERBOSE".equals(verbose.toUpperCase(Locale.ENGLISH))) {
System.err.println(validationEvent);
}
return false;
}
});
final JavaeeNamespaceFilter xmlFilter = new JavaeeNamespaceFilter(parser.getXMLReader());
xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler());
// unmarshall
final SAXSource source = new SAXSource(xmlFilter, new InputSource(in));
currentPublicId.set(new TreeSet<String>());
try {
final JAXBElement<T> element = unmarshaller.unmarshal(source, type);
return element.getValue();
} finally {
currentPublicId.set(null);
}
}
use of javax.xml.bind.ValidationEventHandler in project onebusaway-application-modules by camsys.
the class SiriXmlSerializer method getXml.
public String getXml(Siri siri) throws Exception {
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
marshaller.setEventHandler(new ValidationEventHandler() {
public boolean handleEvent(ValidationEvent event) {
throw new RuntimeException(event.getMessage(), event.getLinkedException());
}
});
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
Writer output = new StringWriter();
marshaller.marshal(siri, output);
// FIXME: strip off ns5 namespaces on siri root namespace. super hack, please fix me!
String outputAsString = output.toString();
outputAsString = outputAsString.replaceAll("<ns5:", "<");
outputAsString = outputAsString.replaceAll("</ns5:", "</");
outputAsString = outputAsString.replaceAll("xmlns:ns5", "xmlns");
outputAsString = outputAsString.replaceAll("<siriExtensionWrapper>", "");
outputAsString = outputAsString.replaceAll("</siriExtensionWrapper>", "");
return outputAsString;
}
Aggregations