Example usage for javax.xml.bind Marshaller setEventHandler

List of usage examples for javax.xml.bind Marshaller setEventHandler

Introduction

In this page you can find the example usage for javax.xml.bind Marshaller setEventHandler.

Prototype

public void setEventHandler(ValidationEventHandler handler) throws JAXBException;

Source Link

Document

Allow an application to register a validation event handler.

Usage

From source file:org.datacleaner.configuration.JaxbConfigurationReader.java

/**
 * Convenience method to marshal a JAXB configuration object into an output
 * stream./*from  ww  w . ja va2 s.c  om*/
 * 
 * @param configuration
 * @param outputStream
 */
public void marshall(Configuration configuration, OutputStream outputStream) {
    try {
        final Marshaller marshaller = _jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.setEventHandler(new JaxbValidationEventHandler());
        marshaller.marshal(configuration, outputStream);
    } catch (JAXBException e) {
        throw new IllegalStateException(e);
    }
}

From source file:org.eclipse.smila.utils.jaxb.JaxbUtils.java

/**
 * Creates the validating marshaller./*www .j  av  a 2 s . c  o  m*/
 * 
 * @param context
 *          the context
 * @param schema
 *          the schema
 * 
 * @return the marshaller
 * 
 * @throws JAXBException
 *           the JAXB exception
 */
public static Marshaller createValidatingMarshaller(final JAXBContext context, final Schema schema)
        throws JAXBException {
    if (schema == null) {
        throw new IllegalArgumentException("Schema is not found!");
    }
    final Marshaller marshaller = context.createMarshaller();
    marshaller.setSchema(schema);
    marshaller.setEventHandler(createValidationEventHandler());
    return marshaller;
}

From source file:org.eclipsetrader.internal.brokers.paper.PaperBroker.java

public void save(File file) throws JAXBException, IOException {
    if (file.exists()) {
        file.delete();//from  w  w w .  j  a  v a2s . c  o m
    }

    JAXBContext jaxbContext = JAXBContext.newInstance(OrderMonitor[].class);
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setEventHandler(new ValidationEventHandler() {

        @Override
        public boolean handleEvent(ValidationEvent event) {
            Status status = new Status(IStatus.WARNING, Activator.PLUGIN_ID, 0,
                    "Error validating XML: " + event.getMessage(), null); //$NON-NLS-1$
            Activator.log(status);
            return true;
        }
    });
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.setProperty(Marshaller.JAXB_ENCODING, System.getProperty("file.encoding")); //$NON-NLS-1$

    OrderMonitor[] elements = pendingOrders.toArray(new OrderMonitor[pendingOrders.size()]);
    JAXBElement<OrderMonitor[]> element = new JAXBElement<OrderMonitor[]>(new QName("list"),
            OrderMonitor[].class, elements);
    marshaller.marshal(element, new FileWriter(file));
}

From source file:org.eclipsetrader.yahoo.internal.news.NewsProvider.java

protected void save() throws JAXBException, IOException {
    File file = YahooActivator.getDefault().getStateLocation().append(HEADLINES_FILE).toFile();
    if (file.exists()) {
        file.delete();/* ww w  . j a v a2 s  .co  m*/
    }

    JAXBContext jaxbContext = JAXBContext.newInstance(HeadLine[].class);
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setEventHandler(new ValidationEventHandler() {

        @Override
        public boolean handleEvent(ValidationEvent event) {
            Status status = new Status(IStatus.WARNING, YahooActivator.PLUGIN_ID, 0,
                    "Error validating XML: " + event.getMessage(), null); //$NON-NLS-1$
            YahooActivator.getDefault().getLog().log(status);
            return true;
        }
    });
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.setProperty(Marshaller.JAXB_ENCODING, System.getProperty("file.encoding")); //$NON-NLS-1$

    JAXBElement<HeadLine[]> element = new JAXBElement<HeadLine[]>(new QName("list"), HeadLine[].class,
            oldItems.toArray(new HeadLine[oldItems.size()]));
    marshaller.marshal(element, new FileWriter(file));
}

From source file:org.eclipsetrader.yahoojapan.internal.news.NewsProvider.java

protected void save() throws JAXBException, IOException {
    File file = YahooJapanActivator.getDefault().getStateLocation().append(HEADLINES_FILE).toFile();
    if (file.exists()) {
        file.delete();/*w  w w . ja  v  a2 s  .  co m*/
    }

    JAXBContext jaxbContext = JAXBContext.newInstance(HeadLine[].class);
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setEventHandler(new ValidationEventHandler() {

        @Override
        public boolean handleEvent(ValidationEvent event) {
            Status status = new Status(IStatus.WARNING, YahooJapanActivator.PLUGIN_ID, 0,
                    "Error validating XML: " + event.getMessage(), null); //$NON-NLS-1$
            YahooJapanActivator.getDefault().getLog().log(status);
            return true;
        }
    });
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.setProperty(Marshaller.JAXB_ENCODING, System.getProperty("file.encoding")); //$NON-NLS-1$

    JAXBElement<HeadLine[]> element = new JAXBElement<HeadLine[]>(new QName("list"), HeadLine[].class,
            oldItems.toArray(new HeadLine[oldItems.size()]));
    marshaller.marshal(element, new FileWriter(file));
}

From source file:org.eobjects.analyzer.configuration.JaxbConfigurationReader.java

/**
 * Convenience method to marshal a JAXB configuration object into an output
 * stream./*from  ww w.  j av a 2 s. co m*/
 * 
 * @param configuration
 * @param outputStream
 */
public void marshall(Configuration configuration, OutputStream outputStream) {
    try {
        Marshaller marshaller = _jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.setEventHandler(new JaxbValidationEventHandler());
        marshaller.marshal(configuration, outputStream);
    } catch (JAXBException e) {
        throw new IllegalStateException(e);
    }
}

From source file:org.onebusaway.transit_data_federation.siri.SiriXmlSerializerV2.java

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());
        }/*  w  w w .j  ava  2  s .  co m*/
    });

    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;
}

From source file:org.openestate.io.casa_it.CasaItUtils.java

/**
 * Creates a {@link Marshaller} to write JAXB objects into XML.
 *
 * @param encoding// ww w . j  a v  a2s . co m
 * encoding of written XML
 *
 * @param formatted
 * if written XML is pretty printed
 *
 * @return
 * created marshaller
 *
 * @throws JAXBException
 * if a problem with JAXB occured
 */
public static Marshaller createMarshaller(String encoding, boolean formatted) throws JAXBException {
    Marshaller m = getContext().createMarshaller();
    m.setProperty(Marshaller.JAXB_ENCODING, encoding);
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formatted);
    m.setEventHandler(new XmlValidationHandler());
    return m;
}

From source file:org.openestate.is24.restapi.utils.XmlUtils.java

/**
 * Write an {@link Attachment} as XML into an {@link OutputStream}.
 * <p>/*w w  w .j ava  2  s.c o m*/
 * The provided object is wrapped into a {@link JAXBElement} before XML
 * creation in order to match the requirements of the schema.
 * <p>
 * This method makes sure, that validation errors do not break XML creation.
 * Invalid entries are not written into XML.
 *
 * @param attachment
 * object to write
 *
 * @param marshaller
 * marshaller, that is used for XML generation
 *
 * @param output
 * stream, where the XML is written to
 *
 * @throws JAXBException
 * if an error occured during XML creation
 */
public static void writeXml(Attachment attachment, Marshaller marshaller, OutputStream output)
        throws JAXBException {
    final org.openestate.is24.restapi.xml.common.ObjectFactory factory = new org.openestate.is24.restapi.xml.common.ObjectFactory();

    marshaller.setEventHandler(new ValidationEventHandler() {
        @Override
        public boolean handleEvent(ValidationEvent ve) {
            return true;
        }
    });

    marshaller.marshal(factory.createAttachment(attachment), output);
}

From source file:org.openestate.is24.restapi.utils.XmlUtils.java

/**
 * Write a {@link City} as XML into an {@link OutputStream}.
 * <p>//from   w ww .  j a  v a  2  s.  c o  m
 * The provided object is wrapped into a {@link JAXBElement} before XML
 * creation in order to match the requirements of the schema.
 * <p>
 * This method makes sure, that validation errors do not break XML creation.
 * Invalid entries are not written into XML.
 *
 * @param city
 * object to write
 *
 * @param marshaller
 * marshaller, that is used for XML generation
 *
 * @param output
 * stream, where the XML is written to
 *
 * @throws JAXBException
 * if an error occured during XML creation
 */
public static void writeXml(City city, Marshaller marshaller, OutputStream output) throws JAXBException {
    final org.openestate.is24.restapi.xml.gis.ObjectFactory factory = new org.openestate.is24.restapi.xml.gis.ObjectFactory();

    marshaller.setEventHandler(new ValidationEventHandler() {
        @Override
        public boolean handleEvent(ValidationEvent ve) {
            return true;
        }
    });

    marshaller.marshal(factory.createCity(city), output);
}