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

The following examples show how to use javax.xml.bind.Marshaller#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 #setMarshallerProperties defined properties}, the
 * {@link #setValidationEventHandler validation event handler}, the
 * {@link #setSchemas schemas}, {@link #setMarshallerListener listener},
 * and {@link #setAdapters adapters}.
 */
protected void initJaxbMarshaller(Marshaller marshaller) throws JAXBException {
	if (this.marshallerProperties != null) {
		for (String name : this.marshallerProperties.keySet()) {
			marshaller.setProperty(name, this.marshallerProperties.get(name));
		}
	}
	if (this.marshallerListener != null) {
		marshaller.setListener(this.marshallerListener);
	}
	if (this.validationEventHandler != null) {
		marshaller.setEventHandler(this.validationEventHandler);
	}
	if (this.adapters != null) {
		for (XmlAdapter<?, ?> adapter : this.adapters) {
			marshaller.setAdapter(adapter);
		}
	}
	if (this.schema != null) {
		marshaller.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 #setMarshallerProperties(Map) defined properties}, the {@link
 * #setValidationEventHandler(ValidationEventHandler) validation event handler}, the {@link #setSchemas(Resource[])
 * schemas}, {@link #setMarshallerListener(javax.xml.bind.Marshaller.Listener) listener}, and
 * {@link #setAdapters(XmlAdapter[]) adapters}.
 */
protected void initJaxbMarshaller(Marshaller marshaller) throws JAXBException {
	if (this.marshallerProperties != null) {
		for (String name : this.marshallerProperties.keySet()) {
			marshaller.setProperty(name, this.marshallerProperties.get(name));
		}
	}
	if (this.marshallerListener != null) {
		marshaller.setListener(this.marshallerListener);
	}
	if (this.validationEventHandler != null) {
		marshaller.setEventHandler(this.validationEventHandler);
	}
	if (this.adapters != null) {
		for (XmlAdapter<?, ?> adapter : this.adapters) {
			marshaller.setAdapter(adapter);
		}
	}
	if (this.schema != null) {
		marshaller.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 #setMarshallerProperties(Map) defined properties}, the {@link
 * #setValidationEventHandler(ValidationEventHandler) validation event handler}, the {@link #setSchemas(Resource[])
 * schemas}, {@link #setMarshallerListener(javax.xml.bind.Marshaller.Listener) listener}, and
 * {@link #setAdapters(XmlAdapter[]) adapters}.
 */
protected void initJaxbMarshaller(Marshaller marshaller) throws JAXBException {
	if (this.marshallerProperties != null) {
		for (String name : this.marshallerProperties.keySet()) {
			marshaller.setProperty(name, this.marshallerProperties.get(name));
		}
	}
	if (this.marshallerListener != null) {
		marshaller.setListener(this.marshallerListener);
	}
	if (this.validationEventHandler != null) {
		marshaller.setEventHandler(this.validationEventHandler);
	}
	if (this.adapters != null) {
		for (XmlAdapter<?, ?> adapter : this.adapters) {
			marshaller.setAdapter(adapter);
		}
	}
	if (this.schema != null) {
		marshaller.setSchema(this.schema);
	}
}
 
Example 4
Source File: AbstractJAXBProvider.java    From cxf with Apache License 2.0 6 votes vote down vote up
protected Marshaller createMarshaller(Object obj, Class<?> cls, Type genericType, String enc)
    throws JAXBException {

    Class<?> objClazz = JAXBElement.class.isAssignableFrom(cls)
                        ? ((JAXBElement<?>)obj).getDeclaredType() : cls;

    JAXBContext context = getJAXBContext(objClazz, genericType);
    Marshaller marshaller = context.createMarshaller();
    if (enc != null) {
        marshaller.setProperty(Marshaller.JAXB_ENCODING, enc);
    }
    if (marshallerListener != null) {
        marshaller.setListener(marshallerListener);
    }
    validateObjectIfNeeded(marshaller, cls, obj);
    return marshaller;
}
 
Example 5
Source File: XmlTransformer.java    From recheck with GNU Affero General Public License v3.0 5 votes vote down vote up
public void toXML( final Object obj, final OutputStream out, final Marshaller.Listener listener ) {
	Marshaller marshaller = null;
	try {
		final JAXBContext jc = createJAXBContext( additionalClazzes );
		marshaller = jc.createMarshaller();
		marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true );
		marshaller.setProperty( MarshallerProperties.NAMESPACE_PREFIX_MAPPER,
				new MapNamespacePrefixMapper( NAMESPACE_MAPPINGS ) );
		marshaller.setProperty( MarshallerProperties.INDENT_STRING, "\t" );
		marshaller.setEventHandler( new DefaultValidationEventHandler() );
		marshaller.setListener( listener );
		final SessionLogDelegate sessionLog = new SessionLogDelegate( AbstractSessionLog.getLog() );
		AbstractSessionLog.setLog( sessionLog );

		if ( config.isOnlyFragment() ) {
			log.info( "Create only fragment for '{}'.", obj );
			marshaller.setProperty( Marshaller.JAXB_FRAGMENT, true );
		}

		if ( config.isLightweightXml() ) {
			log.info( "Use lightweight XML for '{}'.", obj );
			lightweightMarshallerSet.add( marshaller );
			XmlUtil.addLightWeightAdapter( marshaller );
		}

		marshaller.marshal( obj, out );

		if ( sessionLog.containsMessages() ) {
			throw new RuntimeException( "Error persisting XML: " + sessionLog.getLog() );
		}
	} catch ( final JAXBException e ) {
		throw new RuntimeException( e );
	} finally {
		if ( config.isLightweightXml() && marshaller != null ) {
			lightweightMarshallerSet.remove( marshaller );
		}
	}
}
 
Example 6
Source File: PageXmlUtils.java    From TranskribusCore with GNU General Public License v3.0 5 votes vote down vote up
private static Marshaller createMarshaller(ValidationEventCollector vec) throws JAXBException {
	JAXBContext jc = createPageJAXBContext();
	Marshaller m = jc.createMarshaller();
	m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
	m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
	m.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaLocStr);
	m.setEventHandler(vec);
	m.setListener(new TrpPageMarshalListener());
	
	return m;
}