users@jaxb.java.net

Comparison by value vs. validatedObjects

From: Aleksei Valikov <valikov_at_gmx.net>
Date: Mon, 17 Jan 2005 18:16:56 +0100

Hi.

I've just found out that comparison by value conflicts with JAXB validation.

The problem arises in the validate(ValidateableObject vo) method of
runtime ValidationContext:

     private final HashSet validatedObjects = new HashSet();

     public void validate( ValidatableObject vo ) throws SAXException {
         if( validatedObjects.add(ProxyGroup.unwrap(vo)) ) {
             // setup a new validator for this object and validate it.
             MSVValidator.validate(jaxbContext,this,vo);
         } else {
             // this object has already been validated once.
             reportEvent( vo, Messages.format( Messages.CYCLE_DETECTED ) );
         }
     }

HashSet compares objects using equals(...) method. This means that in
case content tree has two different objects equal by value, JAXB
validator will throw cycle detected error for the second one.

This actually happens in my case. I get:

DefaultValidationEventHandler: [ERROR]: the content tree forms a cycle
      Location: obj:
org.dgiwg.metadata.smxml.impl.CIRoleCodePropertyTypeImpl_at_a674f395

javax.xml.bind.ValidationException
  - with linked exception:
[com.sun.xml.bind.serializer.AbortSerializationException: the content
tree forms a cycle]
com.sun.xml.bind.serializer.AbortSerializationException: the content
tree forms a cycle
        at
net.opengis.cat.csw.impl.runtime.ValidationContext.reportEvent(ValidationContext.java:199)
        at
net.opengis.cat.csw.impl.runtime.ValidationContext.reportEvent(ValidationContext.java:166)
        at
net.opengis.cat.csw.impl.runtime.ValidationContext.validate(ValidationContext.java:78)
        at
net.opengis.cat.csw.impl.runtime.MSVValidator.childAsElementBody(MSVValidator.java:366)
        at
net.opengis.cat.csw.impl.runtime.MSVValidator.childAsBody(MSVValidator.java:292)
        at
org.dgiwg.metadata.smxml.impl.CIResponsiblePartyTypeImpl.serializeBody(CIResponsiblePartyTypeImpl.java:117)

although there's no cycle.

My personal opinion is that JAXB validator should compare objects in the
tree by identity, not by value.

The simplest way to correct this would be to add objects to the
validatedObjects wrapped into an "identity-comparing" wrapper like the
one below.

class Wrapper
{
   protected Object object;

   public Wrapper(Object object)
   {
     this.object = object;
   }

   public boolean equals(Object target)
   {
      return object != null && object == target;
   }
}

Bye.
/lexi

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
For additional commands, e-mail: users-help_at_jaxb.dev.java.net