Search in sources :

Example 16 with ValidationEventHandler

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);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) ValidationEventHandler(javax.xml.bind.ValidationEventHandler) InputSource(org.xml.sax.InputSource) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) XMLFilter(org.xml.sax.XMLFilter) SAXSource(javax.xml.transform.sax.SAXSource) ValidationEvent(javax.xml.bind.ValidationEvent) Test(org.junit.Test)

Example 17 with ValidationEventHandler

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;
}
Also used : Marshaller(javax.xml.bind.Marshaller) ValidationEventHandler(javax.xml.bind.ValidationEventHandler) ValidationEvent(javax.xml.bind.ValidationEvent)

Example 18 with ValidationEventHandler

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;
        }
    };
}
Also used : ValidationEventHandler(javax.xml.bind.ValidationEventHandler) ValidationEvent(javax.xml.bind.ValidationEvent) XmlAdapter(javax.xml.bind.annotation.adapters.XmlAdapter) Before(org.junit.Before)

Example 19 with ValidationEventHandler

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);
    }
}
Also used : ValidationEventHandler(javax.xml.bind.ValidationEventHandler) InputSource(org.xml.sax.InputSource) JAXBContext(javax.xml.bind.JAXBContext) SAXSource(javax.xml.transform.sax.SAXSource) ValidationEvent(javax.xml.bind.ValidationEvent) SAXParser(javax.xml.parsers.SAXParser) Unmarshaller(javax.xml.bind.Unmarshaller) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 20 with ValidationEventHandler

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;
}
Also used : Marshaller(javax.xml.bind.Marshaller) ValidationEventHandler(javax.xml.bind.ValidationEventHandler) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) StringWriter(java.io.StringWriter) ValidationEvent(javax.xml.bind.ValidationEvent) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Aggregations

ValidationEventHandler (javax.xml.bind.ValidationEventHandler)29 ValidationEvent (javax.xml.bind.ValidationEvent)21 Unmarshaller (javax.xml.bind.Unmarshaller)13 JAXBContext (javax.xml.bind.JAXBContext)12 InputSource (org.xml.sax.InputSource)12 SAXSource (javax.xml.transform.sax.SAXSource)11 SAXParser (javax.xml.parsers.SAXParser)10 SAXParserFactory (javax.xml.parsers.SAXParserFactory)10 JAXBException (javax.xml.bind.JAXBException)7 JAXBAssertionError (com.sun.xml.bind.JAXBAssertionError)5 Marshaller (javax.xml.bind.Marshaller)5 UnmarshalException (javax.xml.bind.UnmarshalException)5 SAXException (org.xml.sax.SAXException)5 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 Writer (java.io.Writer)2 Map (java.util.Map)2 MarshalException (javax.xml.bind.MarshalException)2 PropertyException (javax.xml.bind.PropertyException)2 XmlAdapter (javax.xml.bind.annotation.adapters.XmlAdapter)2