Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/MOXy/Runtime/Converting XML to Objects/Events"

Line 35: Line 35:
 
<source lang="java">
 
<source lang="java">
 
package example;
 
package example;
 +
 +
import java.util.Date;
  
 
import javax.xml.bind.annotation.*;
 
import javax.xml.bind.annotation.*;
  
 
@XmlRootElement
 
@XmlRootElement
 +
@XmlAccessorType(XmlAccessType.FIELD)
 
public class Company {
 
public class Company {
  
   ...
+
   @XmlAttribute
 +
  private String id;
  
 
   boolean beforeMarshal(Marshaller m) {
 
   boolean beforeMarshal(Marshaller m) {
       GlobalLogManager.logMessage(new java.util.Date() + " [COMPANY Pre-Marshal] " + Thread.currentThread())
+
       Logger.getLogger("example").info(new Date() + " [COMPANY Pre-Marshal] " + getId() + Thread.currentThread());
 
       return true;
 
       return true;
 
   }
 
   }
  
 
   void afterMarshal(Marshaller m) {
 
   void afterMarshal(Marshaller m) {
       GlobalLogManager.logMessage(java.util.Date() + " [COMPANY Post-Marshal] " + Thread.currentThread());
+
       Logger.getLogger("example").info(new Date() + " [COMPANY Post-Marshal] " + getId() + Thread.currentThread());
 
   }
 
   }
  
 
   void beforeUnmarshal(Unmarshaller u, Object parent) {
 
   void beforeUnmarshal(Unmarshaller u, Object parent) {
       GlobalLogManager.logMessage(new java.util.Date() + " [COMPANY Pre-Unmarshal] " + Thread.currentThread());
+
       Logger.getLogger("example").info(new Date() + " [COMPANY Pre-Unmarshal] " + getId() + Thread.currentThread());
 
   }
 
   }
  
 
   void afterUnmarshal(Unmarshaller u, Object parent) {
 
   void afterUnmarshal(Unmarshaller u, Object parent) {
       GlobalLogManager.logMessage(new java.util.Date() + " [COMPANY Pre-Unmarshal] " + Thread.currentThread());
+
       Logger.getLogger("example").info(new Date() + " [COMPANY Pre-Unmarshal] " + getId() + Thread.currentThread());
 
   }
 
   }
  
Line 134: Line 138:
 
   }
 
   }
 
}
 
}
 +
</source>
 +
 +
=== Example ===
 +
 +
This example performs the same logging as above, but using a generic '''Listener''' class.  This makes it easier to log all JAXB objects in the system.
 +
 +
<source>
 
</source>
 
</source>

Revision as of 18:00, 2 June 2011

EclipseLink MOXy

Eclipselink-logo.gif
EclipseLink
Website
Download
Community
Mailing ListForumsIRCmattermost
Issues
OpenHelp WantedBug Day
Contribute
Browse Source

Events

JAXB offers several mechanisms to get event callbacks during the marshalling and unmarshalling processes. You can specify callback methods directly on your mapped objects, or define separate Listener classes and register them with the JAXB runtime.

Event Listener Methods on JAXB Mapped Objects

On any of your objects you have mapped with JAXB, you have the option of specifying some special methods to allow you to recieve event notification when that object is marshalled or unmarshalled. The methods must have the following signatures:

// Invoked by Marshaller after it has created an instance of this object.
boolean beforeMarshal(Marshaller m);
 
// Invoked by Marshaller after it has marshalled all properties of this object.
void afterMarshal(Marshaller m);
 
// This method is called immediately after the object is created and before the unmarshalling of this 
// object begins. The callback provides an opportunity to initialize JavaBean properties prior to unmarshalling.
void beforeUnmarshal(Unmarshaller u, Object parent);
 
//This method is called after all the properties (except IDREF) are unmarshalled for this object, 
//but before this object is set to the parent object.
void afterUnmarshal(Unmarshaller u, Object parent);

Example

The following example shows how to add to a log file every time a Customer object is processed.

package example;
 
import java.util.Date;
 
import javax.xml.bind.annotation.*;
 
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Company {
 
   @XmlAttribute
   private String id;
 
   boolean beforeMarshal(Marshaller m) {
      Logger.getLogger("example").info(new Date() + " [COMPANY Pre-Marshal] " + getId() + Thread.currentThread());
      return true;
   }
 
   void afterMarshal(Marshaller m) {
      Logger.getLogger("example").info(new Date() + " [COMPANY Post-Marshal] " + getId() + Thread.currentThread());
   }
 
   void beforeUnmarshal(Unmarshaller u, Object parent) {
      Logger.getLogger("example").info(new Date() + " [COMPANY Pre-Unmarshal] " + getId() + Thread.currentThread());
   }
 
   void afterUnmarshal(Unmarshaller u, Object parent) {
      Logger.getLogger("example").info(new Date() + " [COMPANY Pre-Unmarshal] " + getId() + Thread.currentThread());
   }
 
}

Registering Listeners on Marshallers and Unmarshallers

JAXB's Marshaller and Unmarshaller interfaces both define a setListener() method, which allows you to set your own custom Listener to intercept marshal and unmarshal events. Marshaller.Listener and Unmarshaller.Listener

package javax.xml.bind;
 
public interface Marshaller {
   ...
   public static abstract class Listener {
     /**
      * Callback method invoked before marshalling from source to XML.
      *
      * This method is invoked just before marshalling process starts to marshal source.
      * Note that if the class of source defines its own beforeMarshal method,
      * the class specific callback method is invoked just before this method is invoked.
      *
      * @param source instance of JAXB mapped class prior to marshalling from it.
      */
      public void beforeMarshal(Object source) {}
 
     /**
      * Callback method invoked after marshalling source to XML.
      *
      * This method is invoked after source and all its descendants have been marshalled.
      * Note that if the class of source defines its own afterMarshal method,
      * the class specific callback method is invoked just before this method is invoked.
      *
      * @param source instance of JAXB mapped class after marshalling it.
      */
      public void afterMarshal(Object source) {}
   }
}
package javax.xml.bind;
 
public interface Unmarshaller {
   ...
   public static abstract class Listener {
 
     /**
      * Callback method invoked before unmarshalling into target.
      *
      * This method is invoked immediately after target was created and
      * before the unmarshalling of this object begins. Note that
      * if the class of target defines its own beforeUnmarsha method,
      * the class specific callback method is invoked before this method is invoked.
      *
      * @param target non-null instance of JAXB mapped class prior to unmarshalling into it.
      * @param parent instance of JAXB mapped class that will eventually reference target.
      *               null when target is root element.
      */
      public void beforeUnmarshal(Object target, Object parent) {}
 
     /**
      * Callback method invoked after unmarshalling XML data into target.
      *
      * This method is invoked after all the properties (except IDREF) are unmarshalled into target,
      * but before target is set into its parent object.
      * Note that if the class of target defines its own afterUnmarshal method,
      * the class specific callback method is invoked before this method is invoked.
      *
      * @param target non-null instance of JAXB mapped class prior to unmarshalling into it.
      * @param parent instance of JAXB mapped class that will reference target.
      *               null when target is root element.
      */
      public void afterUnmarshal(Object target, Object parent) {}
   }
}

Example

This example performs the same logging as above, but using a generic Listener class. This makes it easier to log all JAXB objects in the system.

Invalid language.

You need to specify a language like this: <source lang="html4strict">...</source>

Supported languages for syntax highlighting:

4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, arm, asm, asp, asymptote, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcl, dcpu16, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, freeswitch, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, haxe, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, ldif, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, nagios, netrexx, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, octave, oobas, oorexx, oracle11, oracle8, otj, oxygene, oz, parasail, parigp, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, pys60, python, q, qbasic, rails, rebol, reg, rexx, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, spark, sparql, sql, stonescript, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, upc, urbi, uscript, vala, vb, vbnet, vedit, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic







Back to the top