Example usage for javax.xml.bind ValidationEvent getMessage

List of usage examples for javax.xml.bind ValidationEvent getMessage

Introduction

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

Prototype

public String getMessage();

Source Link

Document

Retrieve the text message for this warning/error.

Usage

From source file:org.cloudgraph.config.CloudGraphConfigValidationEventHandler.java

public boolean handleEvent(ValidationEvent ve) {
    boolean result = this.cumulative;
    this.errorCount++;
    ValidationEventLocator vel = ve.getLocator();

    String message = "Line:Col:Offset[" + vel.getLineNumber() + ":" + vel.getColumnNumber() + ":"
            + String.valueOf(vel.getOffset()) + "] - " + ve.getMessage();

    switch (ve.getSeverity()) {
    case ValidationEvent.WARNING:
        log.warn(message);/*from   ww w.j  a v  a  2s .c om*/
        break;
    case ValidationEvent.ERROR:
    case ValidationEvent.FATAL_ERROR:
        log.fatal(message);
        throw new CloudGraphConfigurationException(message);
    default:
        log.error(message);
    }
    return result;
}

From source file:org.cloudgraph.examples.test.model.XCESTest.java

public void testXCESAnaUnmarshal() throws IOException, JAXBException, SAXException {

    String text = getContent(new File("./src/test/resources/fiction/eggan/TheStory.txt"));
    char[] chars = text.toCharArray();

    XCESAnaDataBinding binding = new XCESAnaDataBinding(new BindingValidationEventHandler() {

        @Override/*from   w  w w .  ja va  2  s .  c o  m*/
        public boolean handleEvent(ValidationEvent event) {
            log.error(event.getMessage());
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public int getErrorCount() {
            // TODO Auto-generated method stub
            return 0;
        }

    });
    //File file = new File("./src/test/resources/fiction/eggan/TheStory-s.xml");
    File file = new File("./src/test/resources/fiction/eggan/TheStory-np.xml");

    CesAna root = (CesAna) binding.unmarshal(new FileInputStream(file));
    assertTrue(root != null);
    log.info(root.getClass().getName());
    //String xml = binding.marshal(root);
    //log.info(xml);

    //multibyte characters in the text throw of the from/to character index numbers
    // probably need to read text using proper encoding
    int max = 25, count = 0;
    for (StructType struct : root.getStructs()) {
        int len = struct.getTo() - struct.getFrom();
        char[] buf = new char[len];
        System.arraycopy(chars, struct.getFrom(), buf, 0, len);
        String chunk = new String(buf);
        //String chunk = text.substring(struct.getFrom(), struct.getTo());
        log.info(chunk);
        count++;
        if (count > max)
            break;
    }

}

From source file:org.cloudgraph.store.mapping.StoreMappingValidationEventHandler.java

public boolean handleEvent(ValidationEvent ve) {
    boolean result = this.cumulative;
    this.errorCount++;
    ValidationEventLocator vel = ve.getLocator();

    String message = "Line:Col:Offset[" + vel.getLineNumber() + ":" + vel.getColumnNumber() + ":"
            + String.valueOf(vel.getOffset()) + "] - " + ve.getMessage();

    switch (ve.getSeverity()) {
    case ValidationEvent.WARNING:
        log.warn(message);/*from   www  .ja v  a 2 s  . com*/
        break;
    case ValidationEvent.ERROR:
    case ValidationEvent.FATAL_ERROR:
        log.fatal(message);
        throw new StoreMappingException(message);
    default:
        log.error(message);
    }
    return result;
}

From source file:org.dkpro.core.io.xces.XcesBasicXmlReader.java

@Override
public void getNext(JCas aJCas) throws IOException, CollectionException {

    Resource res = nextFile();/*from  w ww  .  ja v  a2  s  .  c  o m*/
    initCas(aJCas, res);

    InputStream is = null;

    try {
        is = CompressionUtils.getInputStream(res.getLocation(), res.getInputStream());
        XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
        XMLEventReader xmlEventReaderBasic = xmlInputFactory.createXMLEventReader(is);

        //JAXB context for XCES body with basic type
        JAXBContext contextBasic = JAXBContext.newInstance(XcesBodyBasic.class);
        Unmarshaller unmarshallerBasic = contextBasic.createUnmarshaller();

        unmarshallerBasic.setEventHandler(new ValidationEventHandler() {
            public boolean handleEvent(ValidationEvent event) {
                throw new RuntimeException(event.getMessage(), event.getLinkedException());
            }
        });

        JCasBuilder jb = new JCasBuilder(aJCas);

        XMLEvent eBasic = null;
        while ((eBasic = xmlEventReaderBasic.peek()) != null) {
            if (isStartElement(eBasic, "body")) {
                try {
                    XcesBodyBasic parasBasic = (XcesBodyBasic) unmarshallerBasic
                            .unmarshal(xmlEventReaderBasic, XcesBodyBasic.class).getValue();
                    readPara(jb, parasBasic);
                } catch (RuntimeException ex) {
                    getLogger().warn("Input is not in basic xces format.");
                }
            } else {
                xmlEventReaderBasic.next();
            }

        }
        jb.close();

    } catch (XMLStreamException ex1) {
        throw new IOException(ex1);
    } catch (JAXBException e1) {
        throw new IOException(e1);
    } finally {
        closeQuietly(is);
    }

}

From source file:org.dkpro.core.io.xces.XcesXmlReader.java

@Override
public void getNext(JCas aJCas) throws IOException, CollectionException {

    Resource res = nextFile();// w  w  w.  j  av  a2 s. c  om
    initCas(aJCas, res);

    InputStream is = null;

    try {
        is = CompressionUtils.getInputStream(res.getLocation(), res.getInputStream());

        XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
        XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(is);

        JAXBContext context = JAXBContext.newInstance(XcesBody.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();

        unmarshaller.setEventHandler(new ValidationEventHandler() {
            public boolean handleEvent(ValidationEvent event) {
                throw new RuntimeException(event.getMessage(), event.getLinkedException());
            }
        });

        JCasBuilder jb = new JCasBuilder(aJCas);

        XMLEvent e = null;
        while ((e = xmlEventReader.peek()) != null) {

            if (isStartElement(e, "body")) {
                try {
                    XcesBody paras = (XcesBody) unmarshaller.unmarshal(xmlEventReader, XcesBody.class)
                            .getValue();
                    readPara(jb, paras);
                } catch (RuntimeException ex) {
                    System.out.println("Unable to parse XCES format: " + ex);
                }
            } else {
                xmlEventReader.next();
            }

        }
        jb.close();

    } catch (XMLStreamException ex1) {
        throw new IOException(ex1);
    } catch (JAXBException e1) {
        throw new IOException(e1);
    } finally {
        closeQuietly(is);
    }

}

From source file:org.eclipse.smila.connectivity.framework.schema.internal.JaxbPluginContext.java

/**
 * Creates the validating unmarshaller.//from   w  ww .  ja  v a2 s.c  om
 * 
 * @return the unmarshaller
 * 
 * @throws JAXBException
 *           the JAXB exception
 * @throws SchemaNotFoundException
 *           the index order schema not found exception
 */
public Unmarshaller createValidatingUnmarshaller() throws JAXBException, SchemaNotFoundException {
    initilize();
    assertNotNull(_context);
    final Unmarshaller unmarshaller = _context.createUnmarshaller();
    final SchemaFactory sf = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI);
    try {
        sf.setResourceResolver(new XSDContextURIResolver(sf.getResourceResolver()));
        final javax.xml.validation.Schema schema = sf
                .newSchema(Platform.getBundle(_id).getEntry(_plugIn.getSchemaLocation()));
        unmarshaller.setSchema(schema);
        unmarshaller.setEventHandler(new ValidationEventHandler() {
            public boolean handleEvent(final ValidationEvent ve) {
                if (ve.getSeverity() != ValidationEvent.WARNING) {
                    final ValidationEventLocator vel = ve.getLocator();
                    _log.error("Line:Col[" + vel.getLineNumber() + ":" + vel.getColumnNumber() + "]:"
                            + ve.getMessage());
                    return false;
                }
                return true;
            }
        });
    } catch (final org.xml.sax.SAXException se) {
        throw new SchemaRuntimeException("Unable to validate due to following error.", se);
    }
    return unmarshaller;
}

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

/**
 * Creates the validation event handler.
 * //from   w  ww .j av  a 2 s  .c om
 * @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 ww w . j  ava2s.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(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;//  w ww .  j a v a  2 s . c  om
    }

    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  .j a  v a  2s  .com
    }

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