Search in sources :

Example 1 with ValidationEvent

use of javax.xml.bind.ValidationEvent in project jOOQ by jOOQ.

the class GenerationTool method load.

/**
     * Load a jOOQ codegen configuration file from an input stream
     */
public static Configuration load(InputStream in) throws IOException {
    // [#1149] If there is no namespace defined, add the default one
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    copyLarge(in, out);
    String xml = out.toString();
    // TODO [#1201] Add better error handling here
    xml = xml.replaceAll("<(\\w+:)?configuration xmlns(:\\w+)?=\"http://www.jooq.org/xsd/jooq-codegen-\\d+\\.\\d+\\.\\d+.xsd\">", "<$1configuration xmlns$2=\"" + Constants.NS_CODEGEN + "\">");
    xml = xml.replace("<configuration>", "<configuration xmlns=\"" + Constants.NS_CODEGEN + "\">");
    try {
        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        javax.xml.validation.Schema schema = sf.newSchema(GenerationTool.class.getResource("/xsd/" + Constants.XSD_CODEGEN));
        JAXBContext ctx = JAXBContext.newInstance(Configuration.class);
        Unmarshaller unmarshaller = ctx.createUnmarshaller();
        unmarshaller.setSchema(schema);
        unmarshaller.setEventHandler(new ValidationEventHandler() {

            @Override
            public boolean handleEvent(ValidationEvent event) {
                log.warn("Unmarshal warning", event.getMessage());
                return true;
            }
        });
        return (Configuration) unmarshaller.unmarshal(new StringReader(xml));
    } catch (Exception e) {
        throw new GeneratorException("Error while reading XML configuration", e);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) ValidationEventHandler(javax.xml.bind.ValidationEventHandler) Configuration(org.jooq.util.jaxb.Configuration) JAXBContext(javax.xml.bind.JAXBContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StringUtils.defaultString(org.jooq.tools.StringUtils.defaultString) SQLException(java.sql.SQLException) IOException(java.io.IOException) ValidationEvent(javax.xml.bind.ValidationEvent) StringReader(java.io.StringReader) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 2 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)

Example 3 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)

Example 4 with ValidationEvent

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

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 5 with ValidationEvent

use of javax.xml.bind.ValidationEvent in project camel by apache.

the class JaxbDataFormat method createUnmarshaller.

protected Unmarshaller createUnmarshaller() throws JAXBException, SAXException, FileNotFoundException, MalformedURLException {
    Unmarshaller unmarshaller = getContext().createUnmarshaller();
    if (schema != null) {
        unmarshaller.setSchema(cachedSchema);
        unmarshaller.setEventHandler(new ValidationEventHandler() {

            public boolean handleEvent(ValidationEvent event) {
                // ERROR
                return event.getSeverity() == ValidationEvent.WARNING;
            }
        });
    }
    return unmarshaller;
}
Also used : ValidationEventHandler(javax.xml.bind.ValidationEventHandler) ValidationEvent(javax.xml.bind.ValidationEvent) Unmarshaller(javax.xml.bind.Unmarshaller)

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