Search in sources :

Example 26 with ValidationEvent

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

the class JaxbJavaee method unmarshalJavaee.

/**
 * Convert the namespaceURI in the input to the javaee URI, do not validate the xml, and read in a T.
 *
 * @param type Class of object to be read in
 * @param in   input stream to read
 * @param <T>  class of object to be returned
 * @return a T read from the input stream
 * @throws ParserConfigurationException is the SAX parser can not be configured
 * @throws SAXException                 if there is an xml problem
 * @throws JAXBException                if the xml cannot be marshalled into a T.
 */
public static <T> Object unmarshalJavaee(final Class<T> type, final InputStream in) throws ParserConfigurationException, SAXException, JAXBException {
    final SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(false);
    final SAXParser parser = factory.newSAXParser();
    final JAXBContext ctx = JaxbJavaee.getContext(type);
    final Unmarshaller unmarshaller = ctx.createUnmarshaller();
    unmarshaller.setEventHandler(new ValidationEventHandler() {

        public boolean handleEvent(final ValidationEvent validationEvent) {
            final String verbose = System.getProperty("openejb.validation.output.level");
            if (verbose != null && "VERBOSE".equals(verbose.toUpperCase(Locale.ENGLISH))) {
                System.err.println(validationEvent);
            }
            return false;
        }
    });
    final JavaeeNamespaceFilter xmlFilter = new JavaeeNamespaceFilter(parser.getXMLReader());
    xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler());
    // unmarshall
    final SAXSource source = new SAXSource(xmlFilter, new InputSource(in));
    currentPublicId.set(new TreeSet<String>());
    try {
        final JAXBElement<T> element = unmarshaller.unmarshal(source, type);
        return element.getValue();
    } finally {
        currentPublicId.set(null);
    }
}
Also used : ValidationEventHandler(javax.xml.bind.ValidationEventHandler) InputSource(org.xml.sax.InputSource) JAXBContext(javax.xml.bind.JAXBContext) SAXSource(javax.xml.transform.sax.SAXSource) ValidationEvent(javax.xml.bind.ValidationEvent) SAXParser(javax.xml.parsers.SAXParser) Unmarshaller(javax.xml.bind.Unmarshaller) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 27 with ValidationEvent

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

the class JaxbJavaee method unmarshalTaglib.

/**
 * Convert the namespaceURI in the input to the taglib URI, do not validate the xml, and read in a T.
 *
 * @param type Class of object to be read in
 * @param in   input stream to read
 * @param <T>  class of object to be returned
 * @return a T read from the input stream
 * @throws ParserConfigurationException is the SAX parser can not be configured
 * @throws SAXException                 if there is an xml problem
 * @throws JAXBException                if the xml cannot be marshalled into a T.
 */
public static <T> Object unmarshalTaglib(final Class<T> type, final InputStream in) throws ParserConfigurationException, SAXException, JAXBException {
    final InputSource inputSource = new InputSource(in);
    final SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(false);
    final SAXParser parser = factory.newSAXParser();
    final JAXBContext ctx = JaxbJavaee.getContext(type);
    final Unmarshaller unmarshaller = ctx.createUnmarshaller();
    unmarshaller.setEventHandler(new ValidationEventHandler() {

        public boolean handleEvent(final ValidationEvent validationEvent) {
            System.out.println(validationEvent);
            return false;
        }
    });
    final JaxbJavaee.TaglibNamespaceFilter xmlFilter = new JaxbJavaee.TaglibNamespaceFilter(parser.getXMLReader());
    xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler());
    final SAXSource source = new SAXSource(xmlFilter, inputSource);
    currentPublicId.set(new TreeSet<String>());
    try {
        return unmarshaller.unmarshal(source);
    } finally {
        currentPublicId.set(null);
    }
}
Also used : InputSource(org.xml.sax.InputSource) ValidationEventHandler(javax.xml.bind.ValidationEventHandler) JAXBContext(javax.xml.bind.JAXBContext) SAXSource(javax.xml.transform.sax.SAXSource) ValidationEvent(javax.xml.bind.ValidationEvent) SAXParser(javax.xml.parsers.SAXParser) Unmarshaller(javax.xml.bind.Unmarshaller) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 28 with ValidationEvent

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

the class JaxbJavaee method unmarshalHandlerChains.

/**
 * @param type Class of object to be read in
 * @param in   input stream to read
 * @param <T>  class of object to be returned
 * @return a T read from the input stream
 * @throws ParserConfigurationException is the SAX parser can not be configured
 * @throws SAXException                 if there is an xml problem
 * @throws JAXBException                if the xml cannot be marshalled into a T.
 */
public static <T> Object unmarshalHandlerChains(final Class<T> type, final InputStream in) throws ParserConfigurationException, SAXException, JAXBException {
    final InputSource inputSource = new InputSource(in);
    final SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(false);
    final SAXParser parser = factory.newSAXParser();
    final JAXBContext ctx = JaxbJavaee.getContext(type);
    final Unmarshaller unmarshaller = ctx.createUnmarshaller();
    unmarshaller.setEventHandler(new ValidationEventHandler() {

        public boolean handleEvent(final ValidationEvent validationEvent) {
            System.out.println(validationEvent);
            return false;
        }
    });
    final JaxbJavaee.HandlerChainsNamespaceFilter xmlFilter = new JaxbJavaee.HandlerChainsNamespaceFilter(parser.getXMLReader());
    xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler());
    final HandlerChainsStringQNameAdapter adapter = new HandlerChainsStringQNameAdapter();
    adapter.setHandlerChainsNamespaceFilter(xmlFilter);
    unmarshaller.setAdapter(HandlerChainsStringQNameAdapter.class, adapter);
    final SAXSource source = new SAXSource(xmlFilter, inputSource);
    currentPublicId.set(new TreeSet<String>());
    try {
        return unmarshaller.unmarshal(source);
    } finally {
        currentPublicId.set(null);
    }
}
Also used : InputSource(org.xml.sax.InputSource) ValidationEventHandler(javax.xml.bind.ValidationEventHandler) JAXBContext(javax.xml.bind.JAXBContext) SAXSource(javax.xml.transform.sax.SAXSource) ValidationEvent(javax.xml.bind.ValidationEvent) SAXParser(javax.xml.parsers.SAXParser) Unmarshaller(javax.xml.bind.Unmarshaller) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 29 with ValidationEvent

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

the class JpaJaxbUtil method unmarshal.

public static <T> Object unmarshal(final Class<T> type, final InputStream in) throws ParserConfigurationException, SAXException, JAXBException {
    final InputSource inputSource = new InputSource(in);
    final SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(false);
    final JAXBContext ctx = JAXBContextFactory.newInstance(type);
    final Unmarshaller unmarshaller = ctx.createUnmarshaller();
    unmarshaller.setEventHandler(new ValidationEventHandler() {

        public boolean handleEvent(final ValidationEvent validationEvent) {
            System.out.println(validationEvent);
            return false;
        }
    });
    return unmarshaller.unmarshal(inputSource);
}
Also used : InputSource(org.xml.sax.InputSource) ValidationEventHandler(javax.xml.bind.ValidationEventHandler) ValidationEvent(javax.xml.bind.ValidationEvent) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 30 with ValidationEvent

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

the class JaxbOpenejbJar2 method unmarshal.

public static <T> Object unmarshal(final Class<T> type, final InputStream in, final boolean logErrors) throws ParserConfigurationException, SAXException, JAXBException {
    final InputSource inputSource = new InputSource(in);
    final SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(false);
    final SAXParser parser = factory.newSAXParser();
    final JAXBContext ctx = getContext(type);
    final Unmarshaller unmarshaller = ctx.createUnmarshaller();
    unmarshaller.setEventHandler(new ValidationEventHandler() {

        public boolean handleEvent(final ValidationEvent validationEvent) {
            if (logErrors) {
                System.out.println(validationEvent);
            }
            return false;
        }
    });
    unmarshaller.setListener(new Unmarshaller.Listener() {

        public void afterUnmarshal(final Object object, final Object object1) {
            super.afterUnmarshal(object, object1);
        }

        public void beforeUnmarshal(final Object target, final Object parent) {
            super.beforeUnmarshal(target, parent);
        }
    });
    final NamespaceFilter xmlFilter = new NamespaceFilter(parser.getXMLReader());
    xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler());
    final SAXSource source = new SAXSource(xmlFilter, inputSource);
    return unmarshaller.unmarshal(source, type);
}
Also used : InputSource(org.xml.sax.InputSource) ValidationEventHandler(javax.xml.bind.ValidationEventHandler) SAXSource(javax.xml.transform.sax.SAXSource) ValidationEvent(javax.xml.bind.ValidationEvent) SAXParser(javax.xml.parsers.SAXParser) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

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