Search in sources :

Example 1 with DefaultValidationEventHandler

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);
}
Also used : MotherElement(org.codice.ddf.parser.xml.domain.MotherElement) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DefaultValidationEventHandler(javax.xml.bind.helpers.DefaultValidationEventHandler) Test(org.junit.Test)

Example 2 with DefaultValidationEventHandler

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;
    }
}
Also used : DefaultValidationEventHandler(javax.xml.bind.helpers.DefaultValidationEventHandler)

Example 3 with DefaultValidationEventHandler

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();
}
Also used : Marshaller(javax.xml.bind.Marshaller) PropertyException(javax.xml.bind.PropertyException) ValidationEvent(javax.xml.bind.ValidationEvent) DefaultValidationEventHandler(javax.xml.bind.helpers.DefaultValidationEventHandler) Document(org.w3c.dom.Document) NotIdentifiableEvent(javax.xml.bind.NotIdentifiableEvent) PropertyException(javax.xml.bind.PropertyException) JAXBException(javax.xml.bind.JAXBException)

Example 4 with DefaultValidationEventHandler

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);
    }
}
Also used : ParserConfigurator(org.codice.ddf.parser.ParserConfigurator) ParserException(org.codice.ddf.parser.ParserException) ByteArrayInputStream(java.io.ByteArrayInputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) DefaultValidationEventHandler(javax.xml.bind.helpers.DefaultValidationEventHandler)

Example 5 with DefaultValidationEventHandler

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);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) DefaultValidationEventHandler(javax.xml.bind.helpers.DefaultValidationEventHandler) Test(org.junit.Test)

Aggregations

DefaultValidationEventHandler (javax.xml.bind.helpers.DefaultValidationEventHandler)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Test (org.junit.Test)2 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)1 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)1 JAXBException (javax.xml.bind.JAXBException)1 Marshaller (javax.xml.bind.Marshaller)1 NotIdentifiableEvent (javax.xml.bind.NotIdentifiableEvent)1 PropertyException (javax.xml.bind.PropertyException)1 ValidationEvent (javax.xml.bind.ValidationEvent)1 ParserConfigurator (org.codice.ddf.parser.ParserConfigurator)1 ParserException (org.codice.ddf.parser.ParserException)1 MotherElement (org.codice.ddf.parser.xml.domain.MotherElement)1 Document (org.w3c.dom.Document)1