Java Code Examples for javax.xml.bind.Unmarshaller#setListener()

The following examples show how to use javax.xml.bind.Unmarshaller#setListener() . 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: Jaxb2Marshaller.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * 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 defined properties}, the
 * {@link #setValidationEventHandler validation event handler}, the
 * {@link #setSchemas schemas}, {@link #setUnmarshallerListener listener},
 * and {@link #setAdapters 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 2
Source File: Jaxb2Marshaller.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * 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 3
Source File: Jaxb2Marshaller.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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: PageXmlUtils.java    From TranskribusCore with GNU General Public License v3.0 6 votes vote down vote up
public static Unmarshaller createUnmarshaller(ValidationEventCollector vec) throws JAXBException {
	JAXBContext jc = createPageJAXBContext();

	Unmarshaller u = jc.createUnmarshaller();
	try {
		u.setProperty("com.sun.xml.internal.bind.ObjectFactory", new TrpObjectFactory());
	} catch(PropertyException pe) {
		u.setProperty("com.sun.xml.bind.ObjectFactory", new TrpObjectFactory());
	}
	u.setListener(new TrpPageUnmarshalListener());

	if(vec != null) {
		u.setEventHandler(vec);
	}
	
	return u;
}
 
Example 5
Source File: AbstractJAXBProvider.java    From cxf with Apache License 2.0 6 votes vote down vote up
protected Unmarshaller createUnmarshaller(Class<?> cls, Type genericType, boolean isCollection)
    throws JAXBException {
    JAXBContext context = isCollection ? getCollectionContext(cls)
                                       : getJAXBContext(cls, genericType);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    if (validateInputIfPossible) {
        Schema theSchema = getSchema(cls);
        if (theSchema != null) {
            unmarshaller.setSchema(theSchema);
        }
    }
    if (eventHandler != null) {
        unmarshaller.setEventHandler(eventHandler);
    }
    if (unmarshallerListener != null) {
        unmarshaller.setListener(unmarshallerListener);
    }
    if (uProperties != null) {
        for (Map.Entry<String, Object> entry : uProperties.entrySet()) {
            unmarshaller.setProperty(entry.getKey(), entry.getValue());
        }
    }
    return unmarshaller;
}
 
Example 6
Source File: XmlTransformer.java    From recheck with GNU Affero General Public License v3.0 5 votes vote down vote up
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 7
Source File: Sheet.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Unmarshal the provided XML stream to allocate the corresponding sheet.
 *
 * @param in the input stream that contains the sheet in XML format.
 *           The stream is not closed by this method
 * @return the allocated sheet.
 * @exception JAXBException raised when unmarshalling goes wrong
 */
public static Sheet unmarshal (InputStream in)
        throws JAXBException
{
    Unmarshaller um = getJaxbContext().createUnmarshaller();

    if (constants.useUnmarshalLogger.isSet()) {
        um.setListener(new Jaxb.UnmarshalLogger());
    }

    Sheet sheet = (Sheet) um.unmarshal(in);
    logger.debug("Sheet unmarshalled");

    return sheet;
}
 
Example 8
Source File: JaxbOpenejbJar2.java    From tomee with Apache License 2.0 5 votes vote down vote up
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);
}