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/Release/2.4.0/JAXB RI Extensions/ID Resolver"

 
(13 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
<div style="margin:5px;float:right;border:1px solid #000000;padding:5px">__TOC__</div>
 
<div style="margin:5px;float:right;border:1px solid #000000;padding:5px">__TOC__</div>
  
= Design Documentation: ID Resolver =
+
= ID Resolver =
 
+
[http://bugs.eclipse.org/360249 ER 360249]
+
  
 
In the current JAXB RI, developed by Sun, there is a series of "proprietary" JAXB extensions which provide advanced functionality outside of the JAXB specification (these extension classes and properties reside in the '''com.sun.xml.bind''' package).
 
In the current JAXB RI, developed by Sun, there is a series of "proprietary" JAXB extensions which provide advanced functionality outside of the JAXB specification (these extension classes and properties reside in the '''com.sun.xml.bind''' package).
  
 
The abstract class '''IDResolver''' provided in the Sun JAXB implementation allows users to override the ID/IDREF processing of the JAXB runtime.
 
The abstract class '''IDResolver''' provided in the Sun JAXB implementation allows users to override the ID/IDREF processing of the JAXB runtime.
 
This document will outline the design for an EclipseLink equivalent to this extension.
 
  
  
Line 24: Line 20:
 
= Configuration =
 
= Configuration =
  
The user must extend the following abstract class:
+
The user must extend the '''org.eclipse.persistence.jaxb.IDResolver''' class and implement the following abstract methods (<tt>startDocument</tt> and <tt>endDocument</tt> are optional):
  
<div style="width:800px">
+
<div style="width:900px">
 
<source lang="java">
 
<source lang="java">
package org.eclipse.persistence.jaxb;
+
public abstract Callable<?> resolve(Object id, Class type) throws SAXException;
  
import java.util.concurrent.Callable;
+
public abstract Callable<?> resolve(Map<String, Object> id, final Class type) throws SAXException;
 
+
import javax.xml.bind.ValidationEventHandler;
+
 
+
import org.xml.sax.SAXException;
+
 
+
/**
+
* IDResolver can be subclassed to allow customization of the ID/IDREF processing of
+
* JAXBUnmarshaller.
+
*
+
* @see JAXBUnmarshaller
+
*/
+
public abstract class IDResolver {
+
 
+
    public abstract Callable<?> resolve(Object id, Class type) throws SAXException;
+
 
+
    public abstract Callable<?> resolve(Map<String, Object> id, final Class type) throws SAXException;
+
 
+
    public abstract void bind(Object id, Object obj) throws SAXException;
+
  
    public abstract void bind(Map<String, Object> id, Object obj) throws SAXException;
+
public abstract void bind(Object id, Object obj) throws SAXException;
  
    public void startDocument(ErrorHandler errorHandler) throws SAXException {}
+
public abstract void bind(Map<String, Object> id, Object obj) throws SAXException;
  
    public void endDocument() throws SAXException {}
+
public void startDocument(ValidationEventHandler errorHandler) throws SAXException {}
  
}
+
public void endDocument() throws SAXException {}
 
</source>
 
</source>
 
</div>
 
</div>
Line 62: Line 40:
 
The user's '''IDResolver''' class can then be passed to the '''Unmarshaller''' through the <tt>setProperty()</tt> method:
 
The user's '''IDResolver''' class can then be passed to the '''Unmarshaller''' through the <tt>setProperty()</tt> method:
  
<div style="width:800px">
+
<div style="width:900px">
 
<source lang="java">
 
<source lang="java">
 
...
 
...
Line 74: Line 52:
 
'''Note:''' EclipseLink also supports Sun's IDResolver property names:
 
'''Note:''' EclipseLink also supports Sun's IDResolver property names:
  
<div style="width:850px">
+
<div style="width:900px">
 
<source lang="java">
 
<source lang="java">
 
m.setProperty("com.sun.xml.bind.IDResolver", new MyResolver());
 
m.setProperty("com.sun.xml.bind.IDResolver", new MyResolver());
Line 81: Line 59:
 
</div>
 
</div>
  
Also note that if you are using a Sun IDResolver with EclipseLink, it will be unable to support EclipseLink's multiple XML IDs feature.  In this case, you should re-implement your IDResolver as a subclass of org.eclipse.persistence.jaxb.IDResolver.
+
Also note that if you are using a Sun IDResolver with EclipseLink, it will be unable to support EclipseLink's multiple XML IDs feature.  In this case, you should re-implement your IDResolver as a subclass of '''org.eclipse.persistence.jaxb.IDResolver'''.
  
  
 
= Appendix A - Example IDResolver =
 
= Appendix A - Example IDResolver =
  
<div style="width:900px">
+
<div style="width:925px">
 
<source lang="java">
 
<source lang="java">
 
import java.util.LinkedHashMap;
 
import java.util.LinkedHashMap;
Line 99: Line 77:
  
 
public class MyIDResolver extends IDResolver {
 
public class MyIDResolver extends IDResolver {
    Map<Map<String, Object>, Apple> apples = new LinkedHashMap();
+
  Map<Map<String, Object>, Apple> apples = new LinkedHashMap();
    Map<Map<String, Object>, Orange> oranges = new LinkedHashMap();
+
  Map<Map<String, Object>, Orange> oranges = new LinkedHashMap();
  
    @Override
+
  @Override
    public void startDocument(ValidationEventHandler eventHandler) throws SAXException {
+
  public void startDocument(ValidationEventHandler eventHandler) throws SAXException {
        apples.clear();
+
      apples.clear();
        oranges.clear();
+
      oranges.clear();
    }
+
  }
  
    @Override
+
  @Override
    public void endDocument() throws SAXException {
+
  public void endDocument() throws SAXException {
    }
+
  }
  
    @Override
+
  @Override
    public void bind(Map<String, Object> idWrapper, Object obj) throws SAXException {
+
  public void bind(Map<String, Object> idWrapper, Object obj) throws SAXException {
        if (obj instanceof Apple) {
+
      if (obj instanceof Apple) {
            ((Apple) obj).processed = true;
+
          ((Apple) obj).processed = true;
            apples.put(idWrapper, (Apple) obj);
+
          apples.put(idWrapper, (Apple) obj);
        } else {
+
      } else {
            ((Orange) obj).processed = true;
+
          ((Orange) obj).processed = true;
            oranges.put(idWrapper, (Orange) obj);
+
          oranges.put(idWrapper, (Orange) obj);
        }
+
      }
    }
+
  }
  
    @Override
+
  @Override
    public Callable<Object> resolve(final Map<String, Object> idWrapper, final Class type) throws SAXException {
+
  public Callable<Object> resolve(final Map<String, Object> idWrapper, final Class type) throws SAXException {
        return new Callable<Object>() {
+
      return new Callable<Object>() {
            public Object call() {
+
          public Object call() {
                if (type == Apple.class) {
+
              if (type == Apple.class) {
                    return apples.get(idWrapper);
+
                  return apples.get(idWrapper);
                } else {
+
              } else {
                    return oranges.get(idWrapper);
+
                  return oranges.get(idWrapper);
                }
+
              }
            }
+
          }
        };
+
      };
    }
+
  }
  
    @Override
+
  @Override
    public void bind(Object id, Object obj) throws SAXException {
+
  public void bind(Object id, Object obj) throws SAXException {
        Map<String, Object> idMap = new LinkedHashMap<String, Object>(1);
+
      Map<String, Object> idMap = new LinkedHashMap<String, Object>(1);
        idMap.put("stringId", id);
+
      idMap.put("stringId", id);
        bind(idMap, obj);
+
      bind(idMap, obj);
    }
+
  }
  
    @Override
+
  @Override
    public Callable<?> resolve(Object id, Class type) throws SAXException {
+
  public Callable<?> resolve(Object id, Class type) throws SAXException {
        Map<String, Object> idMap = new LinkedHashMap<String, Object>(1);
+
      Map<String, Object> idMap = new LinkedHashMap<String, Object>(1);
        idMap.put("stringId", id);
+
      idMap.put("stringId", id);
        return resolve(idMap, type);
+
      return resolve(idMap, type);
    }
+
  }
  
 
}
 
}
 
</source>
 
</source>
 
</div>
 
</div>

Latest revision as of 11:36, 18 June 2012

ID Resolver

In the current JAXB RI, developed by Sun, there is a series of "proprietary" JAXB extensions which provide advanced functionality outside of the JAXB specification (these extension classes and properties reside in the com.sun.xml.bind package).

The abstract class IDResolver provided in the Sun JAXB implementation allows users to override the ID/IDREF processing of the JAXB runtime.


Behaviour

If an IDResolver has been set on the Unmarshaller (via properties), then the following things must happen during unmarshal:

  • The IDResolver's startDocument() method must be called when unmarshalling starts.
  • When an ID value is encountered during unmarshal, the IDResolver's bind method must be called, to bind the object to the ID.
  • When an IDREF value is encountered during unmarshal, the IDResolver's resolve method must be used to obtain the object for the IDREF.
  • The IDResolver's endDocument() method must be called when unmarshalling completes.


Configuration

The user must extend the org.eclipse.persistence.jaxb.IDResolver class and implement the following abstract methods (startDocument and endDocument are optional):

public abstract Callable<?> resolve(Object id, Class type) throws SAXException;
 
public abstract Callable<?> resolve(Map<String, Object> id, final Class type) throws SAXException;
 
public abstract void bind(Object id, Object obj) throws SAXException;
 
public abstract void bind(Map<String, Object> id, Object obj) throws SAXException;
 
public void startDocument(ValidationEventHandler errorHandler) throws SAXException {}
 
public void endDocument() throws SAXException {}

The user's IDResolver class can then be passed to the Unmarshaller through the setProperty() method:

...
JAXBContext ctx = ...
Unmarshaller u = ctx.createUnmarshaller();
u.setProperty(UnmarshallerProperties.ID_RESOLVER, new MyIDResolver());
...

Note: EclipseLink also supports Sun's IDResolver property names:

m.setProperty("com.sun.xml.bind.IDResolver", new MyResolver());
m.setProperty("com.sun.xml.internal.bind.IDResolver", new MyResolver());

Also note that if you are using a Sun IDResolver with EclipseLink, it will be unable to support EclipseLink's multiple XML IDs feature. In this case, you should re-implement your IDResolver as a subclass of org.eclipse.persistence.jaxb.IDResolver.


Appendix A - Example IDResolver

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.Callable;
 
import javax.xml.bind.ValidationEventHandler;
 
import org.eclipse.persistence.jaxb.IDResolver;
 
import org.xml.sax.SAXException;
 
public class MyIDResolver extends IDResolver {
   Map<Map<String, Object>, Apple> apples = new LinkedHashMap();
   Map<Map<String, Object>, Orange> oranges = new LinkedHashMap();
 
   @Override
   public void startDocument(ValidationEventHandler eventHandler) throws SAXException {
       apples.clear();
       oranges.clear();
   }
 
   @Override
   public void endDocument() throws SAXException {
   }
 
   @Override
   public void bind(Map<String, Object> idWrapper, Object obj) throws SAXException {
       if (obj instanceof Apple) {
           ((Apple) obj).processed = true;
           apples.put(idWrapper, (Apple) obj);
       } else {
           ((Orange) obj).processed = true;
           oranges.put(idWrapper, (Orange) obj);
       }
   }
 
   @Override
   public Callable<Object> resolve(final Map<String, Object> idWrapper, final Class type) throws SAXException {
       return new Callable<Object>() {
           public Object call() {
               if (type == Apple.class) {
                   return apples.get(idWrapper);
               } else {
                   return oranges.get(idWrapper);
               }
           }
       };
   }
 
   @Override
   public void bind(Object id, Object obj) throws SAXException {
       Map<String, Object> idMap = new LinkedHashMap<String, Object>(1);
       idMap.put("stringId", id);
       bind(idMap, obj);
   }
 
   @Override
   public Callable<?> resolve(Object id, Class type) throws SAXException {
       Map<String, Object> idMap = new LinkedHashMap<String, Object>(1);
       idMap.put("stringId", id);
       return resolve(idMap, type);
   }
 
}

Back to the top