use of javax.xml.bind.helpers.DefaultValidationEventHandler in project ddf by codice.
the class TestXmlParser method testUnmarshal.
@Test
public void testUnmarshal() throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
parser.marshal(configurator, mother, os);
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
MotherElement unmarshal = parser.unmarshal(configurator, MotherElement.class, is);
assertEquals(mother.getAge(), unmarshal.getAge());
assertEquals(mother.getFirstname(), unmarshal.getFirstname());
assertEquals(mother.getLastname(), unmarshal.getLastname());
assertEquals(mother.getChild().size(), unmarshal.getChild().size());
assertEquals(luke.getFirstname(), unmarshal.getChild().get(0).getFirstname());
assertEquals(leia.getAge(), unmarshal.getChild().get(1).getAge());
configurator.setHandler(new DefaultValidationEventHandler());
is = new ByteArrayInputStream(os.toByteArray());
unmarshal = parser.unmarshal(configurator, MotherElement.class, is);
assertEquals(mother.getAge(), unmarshal.getAge());
configurator.addProperty("UnknownProperty", Boolean.TRUE);
is = new ByteArrayInputStream(os.toByteArray());
thrown.expect(ParserException.class);
parser.unmarshal(configurator, MotherElement.class, is);
}
use of javax.xml.bind.helpers.DefaultValidationEventHandler in project btrace by btraceio.
the class ProbeDescriptorLoader method load.
// unmarshall BTrace probe descriptor from XML
private ProbeDescriptor load(InputStream stream) {
try {
JAXBContext jc = JAXBContext.newInstance("com.sun.btrace.annotations:com.sun.btrace.runtime");
Unmarshaller u = jc.createUnmarshaller();
u.setEventHandler(new DefaultValidationEventHandler());
ProbeDescriptor pd = (ProbeDescriptor) u.unmarshal(stream);
pd.setProbes(pd.getProbes());
return pd;
} catch (JAXBException exp) {
if (debug.isDebug())
debug.debug(exp);
return null;
}
}
use of javax.xml.bind.helpers.DefaultValidationEventHandler in project OpenAM by OpenRock.
the class Utils method convertJAXBToElement.
/**
* Converts a JAXB object to a <code>org.w3c.dom.Element</code>.
*
* @param jaxbObj a JAXB object
* @return a <code>org.w3c.dom.Element</code>
* @throws JAXBException if an error occurs while converting JAXB object.
* @supported.api
*/
public static Element convertJAXBToElement(Object jaxbObj, boolean checkIdref) throws JAXBException {
Marshaller m = jc.createMarshaller();
try {
m.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapperImpl());
} catch (PropertyException ex) {
debug.error("Utils.convertJAXBToElement", ex);
}
if (!checkIdref) {
m.setEventHandler(new DefaultValidationEventHandler() {
public boolean handleEvent(ValidationEvent event) {
if (event instanceof NotIdentifiableEvent) {
return true;
}
return super.handleEvent(event);
}
});
}
Document doc = null;
try {
doc = XMLUtils.newDocument();
} catch (Exception ex) {
debug.error("Utils.convertJAXBToElement:", ex);
}
m.marshal(jaxbObj, doc);
return doc.getDocumentElement();
}
use of javax.xml.bind.helpers.DefaultValidationEventHandler in project ddf by codice.
the class GeometryTransformer method transform.
public BinaryContent transform(Attribute attribute) throws CatalogTransformerException {
ParserConfigurator parserConfigurator = getParserConfigurator().setHandler(new DefaultValidationEventHandler());
try {
ByteArrayOutputStream os = new ByteArrayOutputStream(BUFFER_SIZE);
getParser().marshal(parserConfigurator, GeometryAdapter.marshalFrom(attribute), os);
ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray());
return new BinaryContentImpl(bais, MIME_TYPE);
} catch (ParserException e) {
throw new CatalogTransformerException("Failed to marshall geometry data", e);
}
}
use of javax.xml.bind.helpers.DefaultValidationEventHandler in project ddf by codice.
the class TestXmlParser method testMarshal.
@Test
public void testMarshal() throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
parser.marshal(configurator, mother, os);
String outputXml = os.toString();
assertXpathEvaluatesTo("Padme", "/mother/@firstname", outputXml);
assertXpathEvaluatesTo("25", "/mother/@age", outputXml);
assertXpathExists("/mother/child/@firstname", outputXml);
assertXpathExists("/mother/child[@firstname='Luke']", outputXml);
assertXpathNotExists("/mother/child[@firstname='Anakin']", outputXml);
configurator.setHandler(new DefaultValidationEventHandler());
os = new ByteArrayOutputStream();
parser.marshal(configurator, mother, os);
outputXml = os.toString();
assertXpathEvaluatesTo("Padme", "/mother/@firstname", outputXml);
configurator.addProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
os = new ByteArrayOutputStream();
parser.marshal(configurator, mother, os);
outputXml = os.toString();
assertXpathEvaluatesTo("Padme", "/mother/@firstname", outputXml);
}
Aggregations