Example usage for javax.xml.bind ValidationEvent getLinkedException

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

Introduction

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

Prototype

public Throwable getLinkedException();

Source Link

Document

Retrieve the linked exception for this warning/error.

Usage

From source file:org.opennms.core.test.xml.XmlTest.java

@Test
public void validateJaxbXmlAgainstSchema() throws Exception {
    final String schemaFile = getSchemaFile();
    if (schemaFile == null) {
        LOG.warn("Skipping validation.");
        return;//  ww  w  .  java 2 s  . co m
    }
    LOG.debug("Validating against XSD: {}", schemaFile);
    javax.xml.bind.Unmarshaller unmarshaller = JaxbUtils.getUnmarshallerFor(getSampleClass(), null, true);
    final SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    final Schema schema = factory.newSchema(new StreamSource(schemaFile));
    unmarshaller.setSchema(schema);
    unmarshaller.setEventHandler(new ValidationEventHandler() {
        @Override
        public boolean handleEvent(final ValidationEvent event) {
            LOG.warn("Received validation event: {}", event, event.getLinkedException());
            return false;
        }
    });
    try {
        final InputSource inputSource = new InputSource(getSampleXmlInputStream());
        final XMLFilter filter = JaxbUtils.getXMLFilterForClass(getSampleClass());
        final SAXSource source = new SAXSource(filter, inputSource);
        @SuppressWarnings("unchecked")
        T obj = (T) unmarshaller.unmarshal(source);
        assertNotNull(obj);
    } finally {
        unmarshaller.setSchema(null);
    }
}

From source file:org.rhq.enterprise.server.plugins.url.XmlIndexParser.java

@SuppressWarnings("unchecked")
protected Map<String, RemotePackageInfo> jaxbParse(InputStream indexStream, URL indexUrl, String rootUrlString)
        throws Exception {

    JAXBContext jaxbContext = JAXBContext.newInstance(XmlSchemas.PKG_CONTENTSOURCE_PACKAGEDETAILS);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

    // Enable schema validation
    URL pluginSchemaURL = XmlIndexParser.class.getClassLoader().getResource(PLUGIN_SCHEMA_PATH);
    Schema pluginSchema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
            .newSchema(pluginSchemaURL);
    unmarshaller.setSchema(pluginSchema);

    ValidationEventCollector vec = new ValidationEventCollector();
    unmarshaller.setEventHandler(vec);/*w  w  w. ja  va  2s.c  o  m*/

    BufferedReader reader = new BufferedReader(new InputStreamReader(indexStream));
    JAXBElement<PackageType> packagesXml = (JAXBElement<PackageType>) unmarshaller.unmarshal(reader);

    for (ValidationEvent event : vec.getEvents()) {
        log.debug("URL content source index [" + indexUrl + "] message {Severity: " + event.getSeverity()
                + ", Message: " + event.getMessage() + ", Exception: " + event.getLinkedException() + "}");
    }

    Map<String, RemotePackageInfo> fileList = new HashMap<String, RemotePackageInfo>();

    List<PackageDetailsType> allPackages = packagesXml.getValue().getPackage();
    for (PackageDetailsType pkg : allPackages) {
        URL locationUrl = new URL(rootUrlString + pkg.getLocation());
        ContentProviderPackageDetails details = translateXmlToDomain(pkg);
        FullRemotePackageInfo rpi = new FullRemotePackageInfo(locationUrl, details);
        fileList.put(stripLeadingSlash(rpi.getLocation()), rpi);
    }

    return fileList;
}

From source file:org.squidy.manager.parser.ModelHandler.java

/**
 * Parses a given input stream (should contain model and diagram code as xml
 * structure) and returns it as an object structure.
 * // w ww.j a v a2 s.c om
 * @param inputStream
 *            The input stream should contain model and diagram structure.
 * @return The parsed XMI document containing model and diagram in object
 *         representation.
 */
@SuppressWarnings("restriction")
public Data load(InputStream inputStream) {
    try {
        Unmarshaller unmarshaller = getContext().createUnmarshaller();
        unmarshaller.setEventHandler(new ValidationEventHandler() {

            /*
             * (non-Javadoc)
             * 
             * @see
             * javax.xml.bind.ValidationEventHandler#handleEvent(javax.xml
             * .bind.ValidationEvent)
             */
            public boolean handleEvent(ValidationEvent e) {
                // Ignore this as workspace-shape is located in not
                // referenced squidy-designer module
                if (e.getMessage().startsWith("unexpected element (uri:\"\", local:\"workspace-shape\")")) {
                    return true;
                }

                ValidationEventLocator locator = e.getLocator();
                if (LOG.isErrorEnabled()) {
                    LOG.error("Error while reading input stream: \"" + e.getMessage() + "\" [line="
                            + locator.getLineNumber() + ", column=" + locator.getColumnNumber() + "]"
                            + e.getLinkedException());
                }
                return true;
            }
        });

        return (Data) unmarshaller.unmarshal(inputStream);
    } catch (JAXBException e) {
        if (LOG.isErrorEnabled()) {
            LOG.error(e.getMessage(), e);
        }
        return null;
    }
}

From source file:securitytools.veracode.http.VeracodeValidationEventHandler.java

@Override
public boolean handleEvent(ValidationEvent event) {
    StringBuilder sb = new StringBuilder();
    sb.append("Severity=").append(event.getSeverity()).append(" ");
    sb.append("Message=").append(event.getMessage()).append(" ");
    sb.append("LinkedException=").append(event.getLinkedException()).append(" ");
    sb.append("Line=").append(event.getLocator().getLineNumber()).append(" ");
    sb.append("Column=").append(event.getLocator().getColumnNumber()).append(" ");
    sb.append("Offset=").append(event.getLocator().getOffset()).append(" ");
    sb.append("Object=").append(event.getLocator().getObject()).append(" ");
    sb.append("Node=").append(event.getLocator().getNode()).append(" ");
    sb.append("URL=").append(event.getLocator().getURL());

    LOG.error(sb.toString(), event.getLinkedException());
    return true;//  w w  w  . ja v a  2s  .  c o m
}