下面列出了javax.xml.bind.Marshaller#setListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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);
}
}
/**
* 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);
}
}
/**
* 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);
}
}
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;
}
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 );
}
}
}
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;
}