use of javax.xml.bind.ValidationEventHandler in project beam by apache.
the class XmlSourceTest method testReadXMLInvalidRecordClassWithCustomEventHandler.
@Test
public void testReadXMLInvalidRecordClassWithCustomEventHandler() throws IOException {
File file = tempFolder.newFile("trainXMLSmall");
Files.write(file.toPath(), trainXML.getBytes(StandardCharsets.UTF_8));
ValidationEventHandler validationEventHandler = event -> {
throw new RuntimeException("MyCustomValidationEventHandler failure mesage");
};
BoundedSource<WrongTrainType> source = XmlIO.<WrongTrainType>read().from(file.toPath().toString()).withRootElement("trains").withRecordElement("train").withRecordClass(WrongTrainType.class).withValidationEventHandler(validationEventHandler).createSource();
exception.expect(RuntimeException.class);
// JAXB internationalizes the error message. So this is all we can match for.
exception.expectMessage("MyCustomValidationEventHandler failure mesage");
try (Reader<WrongTrainType> reader = source.createReader(null)) {
for (boolean available = reader.start(); available; available = reader.advance()) {
reader.getCurrent();
}
}
}
use of javax.xml.bind.ValidationEventHandler in project tomee by apache.
the class JaxbJavaee method unmarshal.
/**
* Read in a T from the input stream.
*
* @param type Class of object to be read in
* @param in input stream to read
* @param validate whether to validate the input.
* @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 unmarshal(final Class<T> type, final InputStream in, final boolean validate) throws ParserConfigurationException, SAXException, JAXBException {
final InputSource inputSource = new InputSource(in);
final SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(validate);
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) {
System.out.println(validationEvent);
return false;
}
});
final JaxbJavaee.NoSourceFilter xmlFilter = new JaxbJavaee.NoSourceFilter(parser.getXMLReader());
xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler());
final SAXSource source = new SAXSource(xmlFilter, inputSource);
currentPublicId.set(new TreeSet<String>());
try {
return unmarshaller.unmarshal(source);
} finally {
currentPublicId.set(null);
}
}
use of javax.xml.bind.ValidationEventHandler in project tomee by apache.
the class JaxbJavaee method unmarshalTaglib.
/**
* Convert the namespaceURI in the input to the taglib 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 unmarshalTaglib(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 = JaxbJavaee.getContext(type);
final Unmarshaller unmarshaller = ctx.createUnmarshaller();
unmarshaller.setEventHandler(new ValidationEventHandler() {
public boolean handleEvent(final ValidationEvent validationEvent) {
System.out.println(validationEvent);
return false;
}
});
final JaxbJavaee.TaglibNamespaceFilter xmlFilter = new JaxbJavaee.TaglibNamespaceFilter(parser.getXMLReader());
xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler());
final SAXSource source = new SAXSource(xmlFilter, inputSource);
currentPublicId.set(new TreeSet<String>());
try {
return unmarshaller.unmarshal(source);
} finally {
currentPublicId.set(null);
}
}
use of javax.xml.bind.ValidationEventHandler in project tomee by apache.
the class JaxbJavaee method unmarshalHandlerChains.
/**
* @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 unmarshalHandlerChains(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 = JaxbJavaee.getContext(type);
final Unmarshaller unmarshaller = ctx.createUnmarshaller();
unmarshaller.setEventHandler(new ValidationEventHandler() {
public boolean handleEvent(final ValidationEvent validationEvent) {
System.out.println(validationEvent);
return false;
}
});
final JaxbJavaee.HandlerChainsNamespaceFilter xmlFilter = new JaxbJavaee.HandlerChainsNamespaceFilter(parser.getXMLReader());
xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler());
final HandlerChainsStringQNameAdapter adapter = new HandlerChainsStringQNameAdapter();
adapter.setHandlerChainsNamespaceFilter(xmlFilter);
unmarshaller.setAdapter(HandlerChainsStringQNameAdapter.class, adapter);
final SAXSource source = new SAXSource(xmlFilter, inputSource);
currentPublicId.set(new TreeSet<String>());
try {
return unmarshaller.unmarshal(source);
} finally {
currentPublicId.set(null);
}
}
use of javax.xml.bind.ValidationEventHandler in project tomee by apache.
the class JaxbOpenejbJar2 method unmarshal.
public static <T> Object unmarshal(final Class<T> type, final InputStream in, final boolean logErrors) 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) {
if (logErrors) {
System.out.println(validationEvent);
}
return false;
}
});
unmarshaller.setListener(new Unmarshaller.Listener() {
public void afterUnmarshal(final Object object, final Object object1) {
super.afterUnmarshal(object, object1);
}
public void beforeUnmarshal(final Object target, final Object parent) {
super.beforeUnmarshal(target, parent);
}
});
final NamespaceFilter xmlFilter = new NamespaceFilter(parser.getXMLReader());
xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler());
final SAXSource source = new SAXSource(xmlFilter, inputSource);
return unmarshaller.unmarshal(source, type);
}
Aggregations