javax.xml.bind.Unmarshaller#setEventHandler ( )源码实例Demo

下面列出了javax.xml.bind.Unmarshaller#setEventHandler ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: ph-commons   文件: JAXBReaderBuilder.java
@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;
}
 
源代码2 项目: ph-commons   文件: GenericJAXBMarshaller.java
/**
 * @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;
}
 
源代码3 项目: spring4-understanding   文件: Jaxb2Marshaller.java
/**
 * 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);
	}
}
 
源代码4 项目: SeaCloudsPlatform   文件: AgreementParser.java
@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;
}
 
源代码5 项目: recheck   文件: XmlTransformer.java
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 );
	}
}
 
源代码6 项目: cxf   文件: JAXBUtils.java
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);
    }
}
 
源代码7 项目: tomee   文件: JaxbOpenejbJar2.java
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);
}
 
源代码8 项目: tomee   文件: JaxbOpenejbJar3.java
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;
    }
 
源代码9 项目: tomee   文件: JaxbWls.java
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);
    }
}
 
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);
    }
}
 
源代码11 项目: tomee   文件: JaxbJavaee.java
/**
 * 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);
    }
}
 
源代码12 项目: cxf   文件: JAXBUtils.java
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);
    }
}
 
源代码13 项目: nomulus   文件: XmlTransformer.java
/** 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;
}
 
源代码14 项目: cxf   文件: JAXBUtils.java
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);
    }
}
 
源代码15 项目: OpenEstate-IO   文件: OpenImmoUtils.java
/**
 * 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;
}
 
源代码16 项目: OpenEstate-IO   文件: WisItUtils.java
/**
 * 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;
}
 
源代码17 项目: OpenEstate-IO   文件: Is24XmlUtils.java
/**
 * 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;
}
 
源代码18 项目: OpenEstate-IO   文件: CasaItUtils.java
/**
 * 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;
}
 
源代码19 项目: OpenEstate-IO   文件: KyeroUtils.java
/**
 * 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;
}
 
源代码20 项目: OpenEstate-IO   文件: FilemakerUtils.java
/**
 * 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;
}