Java Code Examples for javax.xml.bind.Unmarshaller#setEventHandler()
The following examples show how to use
javax.xml.bind.Unmarshaller#setEventHandler() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JAXBReaderBuilder.java From ph-commons with Apache License 2.0 | 6 votes |
@Nonnull protected Unmarshaller createUnmarshaller () throws JAXBException { final JAXBContext aJAXBContext = getJAXBContext (); // create an Unmarshaller final Unmarshaller aUnmarshaller = aJAXBContext.createUnmarshaller (); if (m_aEventHandler != null) aUnmarshaller.setEventHandler (m_aEventHandler); else aUnmarshaller.setEventHandler (new LoggingValidationEventHandler ().andThen (aUnmarshaller.getEventHandler ())); // Validating (if possible) final Schema aSchema = getSchema (); if (aSchema != null) aUnmarshaller.setSchema (aSchema); return aUnmarshaller; }
Example 2
Source File: GenericJAXBMarshaller.java From ph-commons with Apache License 2.0 | 6 votes |
/** * @param aClassLoader * The class loader to be used for XML schema resolving. May be * <code>null</code>. * @return The JAXB unmarshaller to use. Never <code>null</code>. * @throws JAXBException * In case the creation fails. */ @Nonnull private Unmarshaller _createUnmarshaller (@Nullable final ClassLoader aClassLoader) throws JAXBException { final JAXBContext aJAXBContext = getJAXBContext (aClassLoader); // create an Unmarshaller final Unmarshaller aUnmarshaller = aJAXBContext.createUnmarshaller (); if (m_aVEHFactory != null) { // Create and set a new event handler final ValidationEventHandler aEvHdl = m_aVEHFactory.apply (aUnmarshaller.getEventHandler ()); if (aEvHdl != null) aUnmarshaller.setEventHandler (aEvHdl); } // Set XSD (if any) final Schema aValidationSchema = createValidationSchema (); if (aValidationSchema != null) aUnmarshaller.setSchema (aValidationSchema); return aUnmarshaller; }
Example 3
Source File: Jaxb2Marshaller.java From spring4-understanding with Apache License 2.0 | 6 votes |
/** * Template method that can be overridden by concrete JAXB marshallers for custom initialization behavior. * Gets called after creation of JAXB {@code Marshaller}, and after the respective properties have been set. * <p>The default implementation sets the {@link #setUnmarshallerProperties(Map) defined properties}, the {@link * #setValidationEventHandler(ValidationEventHandler) validation event handler}, the {@link #setSchemas(Resource[]) * schemas}, {@link #setUnmarshallerListener(javax.xml.bind.Unmarshaller.Listener) listener}, and * {@link #setAdapters(XmlAdapter[]) adapters}. */ protected void initJaxbUnmarshaller(Unmarshaller unmarshaller) throws JAXBException { if (this.unmarshallerProperties != null) { for (String name : this.unmarshallerProperties.keySet()) { unmarshaller.setProperty(name, this.unmarshallerProperties.get(name)); } } if (this.unmarshallerListener != null) { unmarshaller.setListener(this.unmarshallerListener); } if (this.validationEventHandler != null) { unmarshaller.setEventHandler(this.validationEventHandler); } if (this.adapters != null) { for (XmlAdapter<?, ?> adapter : this.adapters) { unmarshaller.setAdapter(adapter); } } if (this.schema != null) { unmarshaller.setSchema(this.schema); } }
Example 4
Source File: JaxbWls.java From tomee with Apache License 2.0 | 5 votes |
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 SAXParser parser = factory.newSAXParser(); final JAXBContext ctx = JaxbWls.getContext(type); final Unmarshaller unmarshaller = ctx.createUnmarshaller(); unmarshaller.setEventHandler(new ValidationEventHandler() { public boolean handleEvent(final ValidationEvent validationEvent) { System.out.println(validationEvent); return false; } }); final JaxbWls.NamespaceFilter xmlFilter = new JaxbWls.NamespaceFilter(parser.getXMLReader()); xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler()); final SAXSource source = new SAXSource(xmlFilter, inputSource); JaxbWls.currentPublicId.set(new TreeSet<String>()); try { return unmarshaller.unmarshal(source, type); } finally { JaxbWls.currentPublicId.set(null); } }
Example 5
Source File: JAXBUtils.java From cxf with Apache License 2.0 | 5 votes |
public static <T> JAXBElement<T> unmarshall(JAXBContext c, Element e, Class<T> cls) throws JAXBException { Unmarshaller u = c.createUnmarshaller(); try { u.setEventHandler(null); return u.unmarshal(e, cls); } finally { closeUnmarshaller(u); } }
Example 6
Source File: XmlTransformer.java From nomulus with Apache License 2.0 | 5 votes |
/** Get a {@link Unmarshaller} instance with the default configuration. */ private Unmarshaller getUnmarshaller() throws JAXBException { Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); unmarshaller.setSchema(schema); // This handler was the default in JAXB 1.0. It fails on any exception thrown while // unmarshalling. In JAXB 2.0 some errors are considered recoverable and are ignored, which is // not what we want, so we have to set this explicitly. unmarshaller.setEventHandler(new DefaultValidationEventHandler()); return unmarshaller; }
Example 7
Source File: JAXBUtils.java From cxf with Apache License 2.0 | 5 votes |
public static Object unmarshall(JAXBContext c, XMLStreamReader reader) throws JAXBException { Unmarshaller u = c.createUnmarshaller(); try { u.setEventHandler(null); return u.unmarshal(reader); } finally { closeUnmarshaller(u); } }
Example 8
Source File: JaxbJavaee.java From tomee with Apache License 2.0 | 5 votes |
/** * Read in a T from the input stream. * * @param type Class of object to be read in * @param in input stream to read * @param validate whether to validate the input. * @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 unmarshal(final Class<T> type, final InputStream in, final boolean validate) throws ParserConfigurationException, SAXException, JAXBException { final InputSource inputSource = new InputSource(in); final SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(validate); 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.NoSourceFilter xmlFilter = new JaxbJavaee.NoSourceFilter(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); } }
Example 9
Source File: MessageSchemaUnmarshaller.java From java-cme-mdp3-handler with GNU Lesser General Public License v3.0 | 5 votes |
public static MessageSchema unmarshall(InputStream inputStream) throws SchemaUnmarshallingException { try { final JAXBContext jc = JAXBContext.newInstance(ObjectFactory.class.getPackage().getName()); final Unmarshaller unmarshaller = jc.createUnmarshaller(); final InputSource is = new InputSource(new InputStreamReader(inputStream)); final XMLReader reader = XMLReaderFactory.createXMLReader(); final NamespaceFilter filter = new NamespaceFilter(NAMESPACE, false); filter.setParent(reader); final SAXSource source = new SAXSource(filter, is); unmarshaller.setEventHandler(event -> false); return (MessageSchema) unmarshaller.unmarshal(source); } catch (Exception e) { throw new SchemaUnmarshallingException("Failed to parse MDP Schema: " + e.getMessage(), e); } }
Example 10
Source File: JaxbOpenejbJar3.java From tomee with Apache License 2.0 | 5 votes |
public static <T> T 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 SAXParser parser = factory.newSAXParser(); final JAXBContext ctx = getContext(type); final Unmarshaller unmarshaller = ctx.createUnmarshaller(); unmarshaller.setEventHandler(new ValidationEventHandler() { public boolean handleEvent(final ValidationEvent validationEvent) { // System.out.println(validationEvent); return false; } }); final NamespaceFilter xmlFilter = new NamespaceFilter(parser.getXMLReader()); xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler()); final SAXSource source = new SAXSource(xmlFilter, inputSource); final Object o = unmarshaller.unmarshal(source); if (o instanceof JAXBElement) { final JAXBElement element = (JAXBElement) o; return (T) element.getValue(); } return (T) o; }
Example 11
Source File: JaxbOpenejbJar2.java From tomee with Apache License 2.0 | 5 votes |
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); }
Example 12
Source File: JAXBUtils.java From cxf with Apache License 2.0 | 5 votes |
public static <T> JAXBElement<T> unmarshall(JAXBContext c, XMLStreamReader reader, Class<T> cls) throws JAXBException { Unmarshaller u = c.createUnmarshaller(); try { u.setEventHandler(null); return u.unmarshal(reader, cls); } finally { closeUnmarshaller(u); } }
Example 13
Source File: AgreementParser.java From SeaCloudsPlatform with Apache License 2.0 | 5 votes |
@Override public Agreement getWsagObject(String serializedData) throws ParserException{ Agreement agreementXML = null; try{ logger.info("Will parse {}", serializedData); JAXBContext jaxbContext = JAXBContext.newInstance(Agreement.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); jaxbUnmarshaller.setEventHandler(new ValidationHandler()); agreementXML = (Agreement)jaxbUnmarshaller.unmarshal(new StringReader(serializedData)); logger.info("Agreement parsed {}", agreementXML); }catch(JAXBException e){ throw new ParserException(e); } return agreementXML; }
Example 14
Source File: XmlTransformer.java From recheck with GNU Affero General Public License v3.0 | 5 votes |
public <T> T fromXML( final InputStream in, final Unmarshaller.Listener listener ) { try { final JAXBContext jc = createJAXBContext( additionalClazzes ); final Unmarshaller unmarshaller = jc.createUnmarshaller(); unmarshaller.setEventHandler( new DefaultValidationEventHandler() ); unmarshaller.setListener( listener ); @SuppressWarnings( "unchecked" ) final T result = (T) unmarshaller.unmarshal( in ); return result; } catch ( final JAXBException e ) { throw new RuntimeException( e ); } }
Example 15
Source File: FilemakerUtils.java From OpenEstate-IO with Apache License 2.0 | 2 votes |
/** * Creates a {@link Unmarshaller} to read JAXB objects from XML. * * @return created unmarshaller * @throws JAXBException if a problem with JAXB occurred */ public static Unmarshaller createUnmarshaller() throws JAXBException { Unmarshaller m = getContext().createUnmarshaller(); m.setEventHandler(new XmlValidationHandler()); return m; }
Example 16
Source File: KyeroUtils.java From OpenEstate-IO with Apache License 2.0 | 2 votes |
/** * Creates a {@link Unmarshaller} to read JAXB objects from XML. * * @return created unmarshaller * @throws JAXBException if a problem with JAXB occurred */ public static Unmarshaller createUnmarshaller() throws JAXBException { Unmarshaller m = getContext().createUnmarshaller(); m.setEventHandler(new XmlValidationHandler()); return m; }
Example 17
Source File: CasaItUtils.java From OpenEstate-IO with Apache License 2.0 | 2 votes |
/** * Creates a {@link Unmarshaller} to read JAXB objects from XML. * * @return created unmarshaller * @throws JAXBException if a problem with JAXB occurred */ public static Unmarshaller createUnmarshaller() throws JAXBException { Unmarshaller m = getContext().createUnmarshaller(); m.setEventHandler(new XmlValidationHandler()); return m; }
Example 18
Source File: Is24XmlUtils.java From OpenEstate-IO with Apache License 2.0 | 2 votes |
/** * Creates a {@link Unmarshaller} to read JAXB objects from XML. * * @return created unmarshaller * @throws JAXBException if a problem with JAXB occurred */ public static Unmarshaller createUnmarshaller() throws JAXBException { Unmarshaller m = getContext().createUnmarshaller(); m.setEventHandler(new XmlValidationHandler()); return m; }
Example 19
Source File: WisItUtils.java From OpenEstate-IO with Apache License 2.0 | 2 votes |
/** * Creates a {@link Unmarshaller} to read JAXB objects from XML. * * @return created unmarshaller * @throws JAXBException if a problem with JAXB occurred */ public static Unmarshaller createUnmarshaller() throws JAXBException { Unmarshaller m = getContext().createUnmarshaller(); m.setEventHandler(new XmlValidationHandler()); return m; }
Example 20
Source File: OpenImmoUtils.java From OpenEstate-IO with Apache License 2.0 | 2 votes |
/** * Creates a {@link Unmarshaller} to read JAXB objects from XML. * * @return created unmarshaller * @throws JAXBException if a problem with JAXB occurred */ public static Unmarshaller createUnmarshaller() throws JAXBException { Unmarshaller m = getContext().createUnmarshaller(); m.setEventHandler(new XmlValidationHandler()); return m; }