Example usage for javax.xml.bind ValidationEventHandler ValidationEventHandler

List of usage examples for javax.xml.bind ValidationEventHandler ValidationEventHandler

Introduction

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

Prototype

ValidationEventHandler

Source Link

Usage

From source file:org.eclipse.smila.management.jmx.client.helpers.ConfigLoader.java

/**
 * Creates the validation event handler.
 * //from w  ww .j ava  2 s . c o m
 * @return the validation event handler
 */
public static ValidationEventHandler createValidationEventHandler() {
    return new ValidationEventHandler() {
        public boolean handleEvent(final ValidationEvent ve) {
            final Log log = LogFactory.getLog(Main.class);
            if (ve.getSeverity() != ValidationEvent.WARNING) {
                final ValidationEventLocator vel = ve.getLocator();
                if (log.isErrorEnabled()) {
                    log.error("Line:Col[" + vel.getLineNumber() + ":" + vel.getColumnNumber() + "]:"
                            + ve.getMessage());
                }
                return false;
            }
            return true;
        }
    };
}

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

/**
 * Creates the validation event handler.
 * //from   w w  w  .  j a  v  a  2s .  co m
 * @return the validation event handler
 */
public static ValidationEventHandler createValidationEventHandler() {
    return new ValidationEventHandler() {
        public boolean handleEvent(final ValidationEvent ve) {
            final Log log = LogFactory.getLog(JaxbUtils.class);
            if (ve.getSeverity() != ValidationEvent.WARNING) {
                final ValidationEventLocator vel = ve.getLocator();
                if (log.isErrorEnabled()) {
                    log.error("Line:Col[" + vel.getLineNumber() + ":" + vel.getColumnNumber() + "]:"
                            + ve.getMessage());
                }
                return false;
            }
            return true;
        }
    };
}

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

public void load(File file) throws JAXBException {
    if (!file.exists()) {
        return;/*from  w  ww  .  java 2  s  .  c o m*/
    }

    JAXBContext jaxbContext = JAXBContext.newInstance(OrderMonitor[].class);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    unmarshaller.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;
        }
    });
    JAXBElement<OrderMonitor[]> element = unmarshaller.unmarshal(new StreamSource(file), OrderMonitor[].class);
    if (element != null) {
        Calendar today = Calendar.getInstance();
        Calendar order = Calendar.getInstance();
        for (OrderMonitor monitor : element.getValue()) {
            order.setTime(monitor.getOrder().getDate());
            if (order.get(Calendar.DAY_OF_YEAR) == today.get(Calendar.DAY_OF_YEAR)) {
                pendingOrders.add(monitor);
            }
        }
    }
}

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  ww . java  2  s  .co 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.news.internal.connectors.RSSNewsProvider.java

protected FeedSource[] getActiveSubscriptions() {
    List<FeedSource> list = new ArrayList<FeedSource>();

    try {/*from w w  w .  j av a  2 s  . c  o m*/
        File file = Activator.getDefault().getStateLocation().append(HEADLINES_FILE).toFile();
        if (file.exists()) {
            JAXBContext jaxbContext = JAXBContext.newInstance(FeedSource[].class);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            unmarshaller.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.getDefault().getLog().log(status);
                    return true;
                }
            });
            JAXBElement<FeedSource[]> element = unmarshaller.unmarshal(new StreamSource(file),
                    FeedSource[].class);
            for (FeedSource source : element.getValue()) {
                if (source.isEnabled()) {
                    list.add(source);
                }
            }
        }
    } catch (Exception e) {
        Status status = new Status(IStatus.WARNING, Activator.PLUGIN_ID, 0, "Error reading RSS subscriptions", //$NON-NLS-1$
                null);
        Activator.getDefault().getLog().log(status);
    }

    return list.toArray(new FeedSource[list.size()]);
}

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

public void startUp() throws JAXBException {
    File file = YahooActivator.getDefault().getStateLocation().append(HEADLINES_FILE).toFile();
    if (file.exists()) {
        JAXBContext jaxbContext = JAXBContext.newInstance(HeadLine[].class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        unmarshaller.setEventHandler(new ValidationEventHandler() {

            @Override/*from ww w  . ja  va 2 s  . co m*/
            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;
            }
        });
        JAXBElement<HeadLine[]> element = unmarshaller.unmarshal(new StreamSource(file), HeadLine[].class);
        oldItems.addAll(Arrays.asList(element.getValue()));

        Date limitDate = getLimitDate();
        for (Iterator<HeadLine> iter = oldItems.iterator(); iter.hasNext();) {
            if (iter.next().getDate().before(limitDate)) {
                iter.remove();
            }
        }

        int hoursAsRecent = YahooActivator.getDefault().getPreferenceStore()
                .getInt(YahooActivator.PREFS_HOURS_AS_RECENT);

        Calendar today = Calendar.getInstance();
        today.add(Calendar.HOUR_OF_DAY, -hoursAsRecent);
        Date recentLimitDate = today.getTime();

        for (HeadLine headLine : oldItems) {
            if (!headLine.getDate().before(recentLimitDate)) {
                headLine.setRecent(true);
            }
        }
    }
}

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();/*from   w ww .j  a  va  2  s . com*/
    }

    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

public void startUp() throws JAXBException {
    File file = YahooJapanActivator.getDefault().getStateLocation().append(HEADLINES_FILE).toFile();
    if (file.exists()) {
        JAXBContext jaxbContext = JAXBContext.newInstance(HeadLine[].class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        unmarshaller.setEventHandler(new ValidationEventHandler() {

            @Override/*from w w w  .  j  av  a 2 s . c  o  m*/
            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;
            }
        });
        JAXBElement<HeadLine[]> element = unmarshaller.unmarshal(new StreamSource(file), HeadLine[].class);
        oldItems.addAll(Arrays.asList(element.getValue()));

        Date limitDate = getLimitDate();
        for (Iterator<HeadLine> iter = oldItems.iterator(); iter.hasNext();) {
            if (iter.next().getDate().before(limitDate)) {
                iter.remove();
            }
        }

        int hoursAsRecent = YahooJapanActivator.getDefault().getPreferenceStore()
                .getInt(YahooJapanActivator.PREFS_HOURS_AS_RECENT);

        Calendar today = Calendar.getInstance();
        today.add(Calendar.HOUR_OF_DAY, -hoursAsRecent);
        Date recentLimitDate = today.getTime();

        for (HeadLine headLine : oldItems) {
            if (!headLine.getDate().before(recentLimitDate)) {
                headLine.setRecent(true);
            }
        }
    }
}

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();//from  ww w.  j ava2 s.c  om
    }

    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.fuin.units4j.Units4JUtils.java

/**
 * Unmarshals the given data using a given context. A <code>null</code> XML
 * data argument returns <code>null</code>.
 * /*w  w w . j a v  a  2 s .c  om*/
 * @param ctx
 *            Context to use.
 * @param xmlData
 *            XML data or <code>null</code>.
 * @param adapters
 *            Adapters to associate with the unmarshaller or
 *            <code>null</code>.
 * 
 * @return Data or <code>null</code>.
 * 
 * @param <T>
 *            Type of the expected data.
 */
@SuppressWarnings("unchecked")
public static <T> T unmarshal(@NotNull final JAXBContext ctx, final String xmlData,
        final XmlAdapter<?, ?>[] adapters) {
    if (xmlData == null) {
        return null;
    }
    try {
        final Unmarshaller unmarshaller = ctx.createUnmarshaller();
        if (adapters != null) {
            for (XmlAdapter<?, ?> adapter : adapters) {
                unmarshaller.setAdapter(adapter);
            }
        }
        unmarshaller.setEventHandler(new ValidationEventHandler() {
            @Override
            public boolean handleEvent(final ValidationEvent event) {
                if (event.getSeverity() > 0) {
                    if (event.getLinkedException() == null) {
                        throw new RuntimeException("Error unmarshalling the data: " + event.getMessage());
                    }
                    throw new RuntimeException("Error unmarshalling the data", event.getLinkedException());
                }
                return true;
            }
        });
        return (T) unmarshaller.unmarshal(new StringReader(xmlData));
    } catch (final JAXBException ex) {
        throw new RuntimeException("Error unmarshalling test data", ex);
    }
}