Search in sources :

Example 11 with ValidationEvent

use of javax.xml.bind.ValidationEvent in project Payara by payara.

the class Util method handleTypeMismatchError.

/**
 * Reports that the type of an object in a property is unexpected.
 */
public static void handleTypeMismatchError(XMLSerializer serializer, Object parentObject, String fieldName, Object childObject) throws AbortSerializationException {
    ValidationEvent ve = new ValidationEventImpl(// maybe it should be a fatal error.
    ValidationEvent.ERROR, Messages.format(Messages.ERR_TYPE_MISMATCH, getUserFriendlyTypeName(parentObject), fieldName, getUserFriendlyTypeName(childObject)), new ValidationEventLocatorExImpl(parentObject, fieldName));
    serializer.reportError(ve);
}
Also used : ValidationEventImpl(javax.xml.bind.helpers.ValidationEventImpl) ValidationEvent(javax.xml.bind.ValidationEvent) ValidationEventLocatorExImpl(com.sun.xml.bind.util.ValidationEventLocatorExImpl)

Example 12 with ValidationEvent

use of javax.xml.bind.ValidationEvent in project sldeditor by robward-scisys.

the class ParseXML method parseFile.

/**
 * Parses the xml files, validates against schema and reports any errors.
 *
 * @param resourceFolder the resource folder
 * @param resourceName the resource name
 * @param schemaResource the schema resource
 * @param classToParse the class to parse
 * @return the object
 */
public static Object parseFile(String resourceFolder, String resourceName, String schemaResource, Class<?> classToParse) {
    String fullResourceName = resourceFolder + resourceName;
    logger.debug("Reading : " + fullResourceName);
    InputStream inputStream = ParseXML.class.getResourceAsStream(fullResourceName);
    if (inputStream == null) {
        File file = new File(fullResourceName);
        if (!file.exists()) {
            ConsoleManager.getInstance().error(ParseXML.class, Localisation.getField(ParseXML.class, "ParseXML.failedToFindResource") + fullResourceName);
            return null;
        }
        try {
            inputStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            ConsoleManager.getInstance().error(ParseXML.class, Localisation.getField(ParseXML.class, "ParseXML.failedToFindResource") + fullResourceName);
            return null;
        }
    }
    ValidationEventCollector vec = new ValidationEventCollector();
    URL xsdURL = ParseXML.class.getResource(schemaResource);
    try {
        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = sf.newSchema(xsdURL);
        JAXBContext jaxbContext = JAXBContext.newInstance(classToParse);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        jaxbUnmarshaller.setSchema(schema);
        jaxbUnmarshaller.setEventHandler(vec);
        return jaxbUnmarshaller.unmarshal(inputStream);
    } catch (SAXException e) {
        ConsoleManager.getInstance().exception(ParseXML.class, e);
    } catch (javax.xml.bind.UnmarshalException ex) {
        if (vec != null && vec.hasEvents()) {
            for (ValidationEvent ve : vec.getEvents()) {
                String msg = ve.getMessage();
                ValidationEventLocator vel = ve.getLocator();
                String message = String.format("%s %s %s %s %s %d %s %d %s", Localisation.getField(ParseXML.class, "ParseXML.failedToValidate"), fullResourceName, Localisation.getField(ParseXML.class, "ParseXML.usingXSD"), xsdURL.toString(), Localisation.getField(ParseXML.class, "ParseXML.line"), vel.getLineNumber(), Localisation.getField(ParseXML.class, "ParseXML.column"), vel.getColumnNumber(), msg);
                ConsoleManager.getInstance().error(ParseXML.class, message);
            }
        }
    } catch (JAXBException e) {
        ConsoleManager.getInstance().exception(ParseXML.class, e);
    }
    return null;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Schema(javax.xml.validation.Schema) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) JAXBContext(javax.xml.bind.JAXBContext) FileInputStream(java.io.FileInputStream) URL(java.net.URL) SAXException(org.xml.sax.SAXException) ValidationEvent(javax.xml.bind.ValidationEvent) ValidationEventLocator(javax.xml.bind.ValidationEventLocator) Unmarshaller(javax.xml.bind.Unmarshaller) File(java.io.File) ValidationEventCollector(javax.xml.bind.util.ValidationEventCollector)

Example 13 with ValidationEvent

use of javax.xml.bind.ValidationEvent in project onebusaway-application-modules by camsys.

the class SiriXmlSerializerV2 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) {
            _log.error(event.getMessage(), event.getLinkedException());
            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 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;
}
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)

Example 14 with ValidationEvent

use of javax.xml.bind.ValidationEvent in project TranskribusCore by Transkribus.

the class JaxbUtils method checkEvents.

private static void checkEvents(ValidationEventCollector vec) {
    if (vec.hasEvents()) {
        logger.info("Events occured while marshalling xml file: " + vec.getEvents().length);
        ValidationEvent[] events = vec.getEvents();
        for (ValidationEvent e : events) {
            logger.info(e.getMessage());
        }
    } else {
        logger.debug("No events occured during marshalling xml file!");
    }
}
Also used : ValidationEvent(javax.xml.bind.ValidationEvent)

Example 15 with ValidationEvent

use of javax.xml.bind.ValidationEvent in project OpenAM by OpenRock.

the class Util method handlePrintConversionException.

/**
     * Reports a print conversion error while marshalling.
     */
public static void handlePrintConversionException(Object caller, Exception e, XMLSerializer serializer) throws SAXException {
    if (e instanceof SAXException)
        //        will be thrown) 
        throw (SAXException) e;
    String message = e.getMessage();
    if (message == null) {
        message = e.toString();
    }
    ValidationEvent ve = new PrintConversionEventImpl(ValidationEvent.ERROR, message, new ValidationEventLocatorImpl(caller), e);
    serializer.reportError(ve);
}
Also used : ValidationEvent(javax.xml.bind.ValidationEvent) ValidationEventLocatorImpl(javax.xml.bind.helpers.ValidationEventLocatorImpl) PrintConversionEventImpl(javax.xml.bind.helpers.PrintConversionEventImpl) SAXException(org.xml.sax.SAXException)

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