You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xmlbeans.apache.org by Wesley Leggette <wl...@cleversafe.com> on 2009/05/03 06:25:04 UTC

Schema aware comparison

In my application code, I've attempted to implement a somewhat generic
content aware comparison. Would anyone care to review this and let me know
if it seems correct? If it is a sound approach, should I look to make a
patch adding this to xmlbeans proper?


Thanks,
Wesley


----------


   private boolean objectEquals(final XmlObject a, final XmlObject b)
   {
      if (a == null)
      {
         if (b == null)
            return true;
         else
            return false;
      }

      if (b == null)
         return false;

      if (a instanceof SimpleValue)
      {
         if (b instanceof SimpleValue)
            return a.valueEquals(b);
         else
            return false;
      }

      final SchemaType sa = a.schemaType();
      final SchemaType sb = b.schemaType();

      if (!sa.equals(sb))
         return false;

      // compare attributes
      for (final SchemaProperty property : sa.getAttributeProperties())
      {
         final XmlObject pa = a.selectAttribute(property.getName());
         final XmlObject pb = b.selectAttribute(property.getName());
         if (!objectEquals(pa, pb))
            return false;
      }

      // compare elements
      for (final SchemaProperty property : sa.getElementProperties())
      {
         final XmlObject[] ea = a.selectChildren(property.getName());
         final XmlObject[] eb = b.selectChildren(property.getName());

         if (ea.length != eb.length)
            return false;
         for (int i = 0; i < ea.length; i++)
            objectEquals(ea[i], eb[i]);
      }

      return a.valueEquals(b);
   }







---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: dev-help@xmlbeans.apache.org


Re: Schema aware comparison

Posted by Radu Preotiuc <ra...@oracle.com>.
Hi Wesley,

Being able to compare two XmlObjects for content is something that we
have seen significant interest in. The trouble is that there are
multiple ways of comparing XML, taking or not taking different things
into account (for example, your patch does not take into account
wildcards).

I have been thinking about it and what I believe we should do is start
adding some code under src/tools, say a new package
org.apache.xmlbeans.impl.xmlcompare. Then, we can add one class or
multiple classes to implement different kinds of comparisons. This way,
we can get the effort off the ground.

The code you suggest looks like a good start. One thing that I think
should be taken into account is the case where the schema types sa and
sb are derived from one another: if their common content matches and the
extra content is absent, they should still be considered equal. Also,
there's the question about the relative order of children elements with
different QNames.

Thanks,
Radu

On Sat, 2009-05-02 at 23:38 -0500, Wesley Leggette wrote:
> 
> And below, a corrected version:
> 
> 
> 
>    protected static boolean objectEquals(final XmlObject a, final XmlObject
> b)
>    {
>       if (a == null)
>       {
>          if (b == null)
>             return true;
>          else
>             return false;
>       }
> 
>       if (b == null)
>          return false;
> 
>       final SchemaType sa = a.schemaType();
>       final SchemaType sb = b.schemaType();
> 
>       if (sa.getContentType() == SchemaType.NOT_COMPLEX_TYPE)
>       {
>          if (sb.getContentType() == SchemaType.NOT_COMPLEX_TYPE)
>             return a.valueEquals(b);
>          else
>             return false;
>       }
> 
>       if (!sa.equals(sb))
>          return false;
> 
>       // compare attributes
>       for (final SchemaProperty property : sa.getAttributeProperties())
>       {
>          final XmlObject pa = a.selectAttribute(property.getName());
>          final XmlObject pb = b.selectAttribute(property.getName());
>          if (!RegistryBase.objectEquals(pa, pb))
>             return false;
>       }
> 
>       // compare elements
>       for (final SchemaProperty property : sa.getElementProperties())
>       {
>          final XmlObject[] ea = a.selectChildren(property.getName());
>          final XmlObject[] eb = b.selectChildren(property.getName());
> 
>          if (ea.length != eb.length)
>             return false;
>          for (int i = 0; i < ea.length; i++)
>          {
>             if (!RegistryBase.objectEquals(ea[i], eb[i]))
>                return false;
>          }
> 
>       }
> 
>       return a.valueEquals(b);
>    }
> 
> 
> On 5/2/09 23:25, "Wesley Leggette" <wl...@cleversafe.com> wrote:
> 
> > In my application code, I've attempted to implement a somewhat generic
> > content aware comparison. Would anyone care to review this and let me know
> > if it seems correct? If it is a sound approach, should I look to make a
> > patch adding this to xmlbeans proper?
> > 
> > 
> > Thanks,
> > Wesley
> > 
> > 
> > ----------
> > 
> > 
> >    private boolean objectEquals(final XmlObject a, final XmlObject b)
> >    {
> >       if (a == null)
> >       {
> >          if (b == null)
> >             return true;
> >          else
> >             return false;
> >       }
> > 
> >       if (b == null)
> >          return false;
> > 
> >       if (a instanceof SimpleValue)
> >       {
> >          if (b instanceof SimpleValue)
> >             return a.valueEquals(b);
> >          else
> >             return false;
> >       }
> > 
> >       final SchemaType sa = a.schemaType();
> >       final SchemaType sb = b.schemaType();
> > 
> >       if (!sa.equals(sb))
> >          return false;
> > 
> >       // compare attributes
> >       for (final SchemaProperty property : sa.getAttributeProperties())
> >       {
> >          final XmlObject pa = a.selectAttribute(property.getName());
> >          final XmlObject pb = b.selectAttribute(property.getName());
> >          if (!objectEquals(pa, pb))
> >             return false;
> >       }
> > 
> >       // compare elements
> >       for (final SchemaProperty property : sa.getElementProperties())
> >       {
> >          final XmlObject[] ea = a.selectChildren(property.getName());
> >          final XmlObject[] eb = b.selectChildren(property.getName());
> > 
> >          if (ea.length != eb.length)
> >             return false;
> >          for (int i = 0; i < ea.length; i++)
> >             objectEquals(ea[i], eb[i]);
> >       }
> > 
> >       return a.valueEquals(b);
> >    }
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: dev-unsubscribe@xmlbeans.apache.org
> > For additional commands, e-mail: dev-help@xmlbeans.apache.org
> > 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@xmlbeans.apache.org
> For additional commands, e-mail: dev-help@xmlbeans.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: dev-help@xmlbeans.apache.org


Re: Schema aware comparison

Posted by Wesley Leggette <wl...@cleversafe.com>.

And below, a corrected version:



   protected static boolean objectEquals(final XmlObject a, final XmlObject
b)
   {
      if (a == null)
      {
         if (b == null)
            return true;
         else
            return false;
      }

      if (b == null)
         return false;

      final SchemaType sa = a.schemaType();
      final SchemaType sb = b.schemaType();

      if (sa.getContentType() == SchemaType.NOT_COMPLEX_TYPE)
      {
         if (sb.getContentType() == SchemaType.NOT_COMPLEX_TYPE)
            return a.valueEquals(b);
         else
            return false;
      }

      if (!sa.equals(sb))
         return false;

      // compare attributes
      for (final SchemaProperty property : sa.getAttributeProperties())
      {
         final XmlObject pa = a.selectAttribute(property.getName());
         final XmlObject pb = b.selectAttribute(property.getName());
         if (!RegistryBase.objectEquals(pa, pb))
            return false;
      }

      // compare elements
      for (final SchemaProperty property : sa.getElementProperties())
      {
         final XmlObject[] ea = a.selectChildren(property.getName());
         final XmlObject[] eb = b.selectChildren(property.getName());

         if (ea.length != eb.length)
            return false;
         for (int i = 0; i < ea.length; i++)
         {
            if (!RegistryBase.objectEquals(ea[i], eb[i]))
               return false;
         }

      }

      return a.valueEquals(b);
   }


On 5/2/09 23:25, "Wesley Leggette" <wl...@cleversafe.com> wrote:

> In my application code, I've attempted to implement a somewhat generic
> content aware comparison. Would anyone care to review this and let me know
> if it seems correct? If it is a sound approach, should I look to make a
> patch adding this to xmlbeans proper?
> 
> 
> Thanks,
> Wesley
> 
> 
> ----------
> 
> 
>    private boolean objectEquals(final XmlObject a, final XmlObject b)
>    {
>       if (a == null)
>       {
>          if (b == null)
>             return true;
>          else
>             return false;
>       }
> 
>       if (b == null)
>          return false;
> 
>       if (a instanceof SimpleValue)
>       {
>          if (b instanceof SimpleValue)
>             return a.valueEquals(b);
>          else
>             return false;
>       }
> 
>       final SchemaType sa = a.schemaType();
>       final SchemaType sb = b.schemaType();
> 
>       if (!sa.equals(sb))
>          return false;
> 
>       // compare attributes
>       for (final SchemaProperty property : sa.getAttributeProperties())
>       {
>          final XmlObject pa = a.selectAttribute(property.getName());
>          final XmlObject pb = b.selectAttribute(property.getName());
>          if (!objectEquals(pa, pb))
>             return false;
>       }
> 
>       // compare elements
>       for (final SchemaProperty property : sa.getElementProperties())
>       {
>          final XmlObject[] ea = a.selectChildren(property.getName());
>          final XmlObject[] eb = b.selectChildren(property.getName());
> 
>          if (ea.length != eb.length)
>             return false;
>          for (int i = 0; i < ea.length; i++)
>             objectEquals(ea[i], eb[i]);
>       }
> 
>       return a.valueEquals(b);
>    }
> 
> 
> 
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@xmlbeans.apache.org
> For additional commands, e-mail: dev-help@xmlbeans.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: dev-help@xmlbeans.apache.org