Search in sources :

Example 31 with ValidationEvent

use of javax.xml.bind.ValidationEvent 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

ValidationEvent (javax.xml.bind.ValidationEvent)31 ValidationEventHandler (javax.xml.bind.ValidationEventHandler)18 Unmarshaller (javax.xml.bind.Unmarshaller)13 JAXBContext (javax.xml.bind.JAXBContext)12 InputSource (org.xml.sax.InputSource)11 SAXSource (javax.xml.transform.sax.SAXSource)10 SAXParser (javax.xml.parsers.SAXParser)9 SAXParserFactory (javax.xml.parsers.SAXParserFactory)9 SAXException (org.xml.sax.SAXException)6 ValidationEventLocatorExImpl (com.sun.xml.bind.util.ValidationEventLocatorExImpl)5 Marshaller (javax.xml.bind.Marshaller)5 PrintConversionEventImpl (javax.xml.bind.helpers.PrintConversionEventImpl)5 ValidationEventImpl (javax.xml.bind.helpers.ValidationEventImpl)5 ValidationEventLocatorImpl (javax.xml.bind.helpers.ValidationEventLocatorImpl)5 JAXBException (javax.xml.bind.JAXBException)3 SchemaFactory (javax.xml.validation.SchemaFactory)3 StringWriter (java.io.StringWriter)2 Writer (java.io.Writer)2 PropertyException (javax.xml.bind.PropertyException)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2