Search in sources :

Example 6 with ValidationEventHandler

use of javax.xml.bind.ValidationEventHandler in project onebusaway-application-modules by camsys.

the class SiriXmlSerializerV2 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) {
            _log.error(event.getMessage(), event.getLinkedException());
            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 ns6 namespaces on siri root namespace. super hack, please fix me!
    String outputAsString = output.toString();
    /*outputAsString = outputAsString.replaceAll("<ns6:", "<");
    outputAsString = outputAsString.replaceAll("</ns6:", "</");
    outputAsString = outputAsString.replaceAll("xmlns:ns6", "xmlns");
*/
    String[] searchList = { "<siriExtensionWrapper>", "</siriExtensionWrapper>", "<siriUpcomingServiceExtension>", "</siriUpcomingServiceExtension>", "<siriPolyLinesExtension>", "</siriPolyLinesExtension>" };
    String[] replacementList = { "", "", "", "", "", "" };
    outputAsString = StringUtils.replaceEach(outputAsString, searchList, replacementList);
    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)

Example 7 with ValidationEventHandler

use of javax.xml.bind.ValidationEventHandler in project tomee by apache.

the class JaxbOpenejbJar3 method unmarshal.

public static <T> T unmarshal(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 = getContext(type);
    final Unmarshaller unmarshaller = ctx.createUnmarshaller();
    unmarshaller.setEventHandler(new ValidationEventHandler() {

        public boolean handleEvent(final ValidationEvent validationEvent) {
            // System.out.println(validationEvent);
            return false;
        }
    });
    final NamespaceFilter xmlFilter = new NamespaceFilter(parser.getXMLReader());
    xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler());
    final SAXSource source = new SAXSource(xmlFilter, inputSource);
    final Object o = unmarshaller.unmarshal(source);
    if (o instanceof JAXBElement) {
        final JAXBElement element = (JAXBElement) o;
        return (T) element.getValue();
    }
    return (T) o;
}
Also used : InputSource(org.xml.sax.InputSource) ValidationEventHandler(javax.xml.bind.ValidationEventHandler) SAXSource(javax.xml.transform.sax.SAXSource) ValidationEvent(javax.xml.bind.ValidationEvent) SAXParser(javax.xml.parsers.SAXParser) JAXBContext(javax.xml.bind.JAXBContext) JAXBElement(javax.xml.bind.JAXBElement) Unmarshaller(javax.xml.bind.Unmarshaller) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 8 with ValidationEventHandler

use of javax.xml.bind.ValidationEventHandler in project tomee by apache.

the class JaxbJavaee method unmarshalJavaee.

private static <T> Object unmarshalJavaee(final Class<T> type, final InputStream in, boolean filter) 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;
        }
    });
    SAXSource source = null;
    if (filter) {
        final JavaeeNamespaceFilter xmlFilter = new JavaeeNamespaceFilter(parser.getXMLReader());
        xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler());
        // unmarshall
        source = new SAXSource(xmlFilter, new InputSource(in));
    } else {
        source = new SAXSource(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 9 with ValidationEventHandler

use of javax.xml.bind.ValidationEventHandler in project tomee by apache.

the class JaxbWls method unmarshal.

public static <T> Object unmarshal(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 = JaxbWls.getContext(type);
    final Unmarshaller unmarshaller = ctx.createUnmarshaller();
    unmarshaller.setEventHandler(new ValidationEventHandler() {

        public boolean handleEvent(final ValidationEvent validationEvent) {
            System.out.println(validationEvent);
            return false;
        }
    });
    final JaxbWls.NamespaceFilter xmlFilter = new JaxbWls.NamespaceFilter(parser.getXMLReader());
    xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler());
    final SAXSource source = new SAXSource(xmlFilter, inputSource);
    JaxbWls.currentPublicId.set(new TreeSet<String>());
    try {
        return unmarshaller.unmarshal(source, type);
    } finally {
        JaxbWls.currentPublicId.set(null);
    }
}
Also used : InputSource(org.xml.sax.InputSource) ValidationEventHandler(javax.xml.bind.ValidationEventHandler) 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 10 with ValidationEventHandler

use of javax.xml.bind.ValidationEventHandler in project tomee by apache.

the class JaxbSun method unmarshal.

public static <T> Object unmarshal(final Class<T> type, final InputStream in, final boolean logErrors) throws ParserConfigurationException, SAXException, JAXBException {
    // create a parser with validation disabled
    final SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(false);
    final SAXParser parser = factory.newSAXParser();
    // Get the JAXB context -- this should be cached
    final JAXBContext ctx = JAXBContextFactory.newInstance(type);
    // get the unmarshaller
    final Unmarshaller unmarshaller = ctx.createUnmarshaller();
    // log errors?
    unmarshaller.setEventHandler(new ValidationEventHandler() {

        public boolean handleEvent(final ValidationEvent validationEvent) {
            if (logErrors) {
                System.out.println(validationEvent);
            }
            return false;
        }
    });
    // add our XMLFilter which disables dtd downloading
    final NamespaceFilter xmlFilter = new NamespaceFilter(parser.getXMLReader());
    xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler());
    // Wrap the input stream with our filter
    final SAXSource source = new SAXSource(xmlFilter, new InputSource(in));
    // unmarshal the document
    return unmarshaller.unmarshal(source);
}
Also used : ValidationEventHandler(javax.xml.bind.ValidationEventHandler) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) ValidationEvent(javax.xml.bind.ValidationEvent) SAXParser(javax.xml.parsers.SAXParser) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

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