Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, JBoss Inc., and individual contributors as indicated
    * by the @authors tag. See the copyright.txt in the distribution for a
    * full listing of individual contributors.
    *
    * This is free software; you can redistribute it and/or modify it
    * under the terms of the GNU Lesser General Public License as
    * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this software; if not, write to the Free
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
 package org.jboss.xb.binding;
 
 import  org.apache.xerces.xs.XSTypeDefinition;
 
 import java.util.Map;
An ObjectModelFactory that uses mappings

Author(s):
Alexey Loubyansky
Adrian Brock
Version:
$Revision: 2913 $
 
    implements GenericObjectModelFactory
 {
    private final static Logger log = Logger.getLogger(MappingObjectModelFactory.class);

   
The class mappings
 
The field mappings
 
 
    // Public
 
   
Map an element to a class

Parameters:
element the element name
cls the class
 
    public void mapElementToClass(String elementClass<?> cls)
    {
       ElementToClassMapping mapping = new ElementToClassMapping(elementcls);
       addElementToClassMapping(mapping);
       if(.isTraceEnabled())
       {
          .trace(mapping);
       }
    }

   
Map an element to a field

Parameters:
element the element name
cls the class
field the field name
converter the type convertor
 
    public void mapElementToField(String elementClass<?> clsString fieldTypeBinding converter)
    {
       ElementToFieldMapping mapping = new ElementToFieldMapping(elementclsfieldconverter);
       addElementToFieldMapping(mapping);
       if(.isTraceEnabled())
       {
          .trace(mapping);
       }
    }
 
    // ObjectModelFactory implementation
 
    public Object newRoot(Object root,
                          UnmarshallingContext ctx,
                          String namespaceURI,
                          String localName,
                         Attributes attrs)
   {
      if(.isTraceEnabled())
      {
         .trace("newRoot root=" +
            root +
            " namespaceURI=" +
            namespaceURI +
            " localName=" +
            localName +
            " attributes=" +
            attrs
         );
      }
      if(root == null)
      {
         ElementToClassMapping mapping = .get(localName);
         if(mapping != null)
         {
            if(.isTraceEnabled())
            {
               .trace("creating root using " + mapping);
            }
            root = newInstance(mapping.cls);
         }
         else
         {
            root = create(namespaceURIlocalNamectx.getType());
         }
         if(root == null)
         {
            throw new IllegalStateException(
               "Failed to resolve Java type binding for root element: ns=" + namespaceURI + ", local=" + localName
            );
         }
      }
      if(attrs != null)
      {
         for(int i = 0; i < attrs.getLength(); ++i)
         {
            try
            {
               if(attrs.getLocalName(i).length() > 0)
               {
                  if(!attrs.getQName(i).startsWith("xsi:")) //todo horrible
                  {
                     setAttribute(rootattrs.getLocalName(i), attrs.getValue(i), ctx);
                  }
               }
            }
            catch(Exception e)
            {
               String msg = "Failed to set attribute " + attrs.getQName(i) + "=" + attrs.getValue(i);
               .error(msge);
               throw new IllegalStateException(msg + ": " + e.getMessage());
            }
         }
      }
      return root;
   }
   // GenericObjectModelFactory implementation
   public Object newChild(Object o,
                          UnmarshallingContext ctx,
                          String namespaceURI,
                          String localName,
                          Attributes attrs)
   {
      if(.isTraceEnabled())
      {
         .trace("newChild object=" +
            o +
            " namespaceURI=" +
            namespaceURI +
            " localName=" +
            localName +
            " attributes=" +
            attrs
         );
      }
      if(o == null)
      {
         throw new RuntimeException("Attempt to add a new child to a null parent localName=" + localName);
      }
      Object child = null;
      ElementToClassMapping mapping = .get(localName);
      XSTypeDefinition type = ctx.getType();
      if(mapping != null)
      {
         if(.isTraceEnabled())
         {
            .trace("newChild using mapping " + mapping);
         }
         try
         {
            if(!(o instanceof Collection))
            {
               ElementToFieldMapping fieldMapping = .get(
                  new ElementToFieldMappingKey(localNameo.getClass())
               );
               FieldInfo fieldInfo;
               if(fieldMapping != null)
               {
                  fieldInfo = fieldMapping.fieldInfo;
               }
               else
               {
                  String fieldName = Util.xmlNameToFieldName(localNametrue);
                  fieldInfo = FieldInfo.getFieldInfo(o.getClass(), fieldNametrue);
               }
               child = get(olocalNamefieldInfo);
            }
            if(child == null)
            {
               child = newInstance(mapping.cls);
            }
            if(attrs != null)
            {
               for(int i = 0; i < attrs.getLength(); ++i)
               {
                  if(attrs.getLocalName(i).length() > 0)
                  {
                     if(!attrs.getQName(i).startsWith("xsi:")) //todo horrible
                     {
                        setAttribute(childattrs.getLocalName(i), attrs.getValue(i), ctx);
                     }
                  }
               }
            }
         }
         catch(RuntimeException e)
         {
            throw e;
         }
         catch(Exception e)
         {
            throw new NestedRuntimeException("newChild failed for o=" +
               o +
               ", uri=" +
               namespaceURI +
               ", local="
               + localName + ", attrs=" + attrse
            );
         }
      }
      else
      {
         if(o instanceof Collection)
         {
            child = create(namespaceURIlocalNametype);
         }
         else
         {
            Class<?> oCls;
            if(o instanceof Immutable)
            {
               oCls = ((Immutable)o).;
            }
            else
            {
               oCls = o.getClass();
            }
            String fieldName = Util.xmlNameToFieldName(localNametrue);
            FieldInfo fieldInfo = FieldInfo.getFieldInfo(oClsfieldNametrue);
            if(Collection.class.isAssignableFrom(fieldInfo.getType()))
            {
               child = get(olocalNamefieldInfo);
               // now does this element really represent a Java collection or is it an element that can appear more than once?
               // try to load the class and create an instance
               Object item = null;
               if(type == null || type != null && type.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE)
               {
                  item = create(namespaceURIlocalNametype);
               }
               if(item != null)
               {
                  if(child == null)
                  {
                     setChild(new ArrayList(), olocalName);
                  }
                  child = item;
               }
               else
               {
                  if(child == null)
                  {
                     child = new ArrayList<Object>();
                  }
               }
            }
            else if(!Util.isAttributeType(fieldInfo.getType()))
            {
               // id there is no field mapping
               ElementToFieldMapping fieldMapping = .get(
                  new ElementToFieldMappingKey(localNameo.getClass())
               );
               TypeBinding converter = fieldMapping == null ? null : fieldMapping.converter;
               // if converter != null it will be used in setValue
               if(converter == null)
               {
                  child = newInstance(fieldInfo.getType());
               }
            }
         }
      }
      return child;
   }
   public void addChild(Object parent,
                        Object child,
                        UnmarshallingContext ctx,
                        String namespaceURI,
                        String localName)
   {
      if(.isTraceEnabled())
      {
         .trace("addChild parent=" +
            parent +
            " child=" +
            child +
            " namespaceURI=" +
            namespaceURI +
            " localName=" +
            localName
         );
      }
      if(child instanceof Immutable)
      {
         child = ((Immutable)child).newInstance();
      }
      setChild(childparentlocalName);
   }
   public void setValue(Object oUnmarshallingContext ctxString namespaceURIString localNameString value)
   {
      if(.isTraceEnabled())
      {
         .trace("setValue object=" +
            o +
            " ctx=" +
            ctx +
            " namespaceURI=" +
            namespaceURI +
            " localName=" +
            localName +
            " value=" +
            value
         );
      }
      setAttribute(olocalNamevaluectx);
   }
   public Object completeRoot(Object rootUnmarshallingContext navigatorString namespaceURIString localName)
   {
      if(.isTraceEnabled())
      {
         .trace("completeRoot root=" +
            root +
            " navigator=" +
            navigator +
            " namespaceURI=" +
            namespaceURI +
            " localName=" +
            localName
         );
      }
      if(root instanceof Immutable)
      {
         root = ((Immutable)root).newInstance();
      }
      return root;
   }
   // Private
   private void addElementToClassMapping(ElementToClassMapping mapping)
   {
      .put(mapping.elementmapping);
   }
   private void addElementToFieldMapping(ElementToFieldMapping mapping)
   {
      .put(mapping.keymapping);
   }
   private void setChild(Object childObject parentString localName)
   {
      boolean trace = .isTraceEnabled();
      Object value = child;
      if(parent instanceof Collection)
      {
         if(trace)
         {
            .trace("Add " + value + " to collection " + parent);
         }
         ((Collection<Object>)parent).add(value);
      }
      else
      {
         final ElementToFieldMapping fieldMapping = .get(
            new ElementToFieldMappingKey(localNameparent.getClass())
         );
         if(fieldMapping != null)
         {
            if(trace)
            {
               .trace("Add " + value + " to " + parent + " using field mapping " + fieldMapping);
            }
            set(parentvaluelocalNamefieldMapping.fieldInfo);
         }
         else
         {
            Class<?> parentCls = parent instanceof Immutable ?
               ((Immutable)parent). :
               parent.getClass();
            String fieldName = Util.xmlNameToFieldName(localNametrue);
            FieldInfo fieldInfo = FieldInfo.getFieldInfo(parentClsfieldNamefalse);
            if(trace)
            {
               .trace("Add " + value + " to property " + fieldName + " of " + parentCls);
            }
            if(fieldInfo != null)
            {
               if(!(child instanceof Collection) && Collection.class.isAssignableFrom(fieldInfo.getType()))
               {
                  Object o = get(parentlocalNamefieldInfo);
                  Collection<Objectcol = (Collection<Object>)o;
                  if(trace)
                  {
                     .trace("Add " + value + " to collection " + col + " retrieved from " + fieldName);
                  }
                  col.add(child);
               }
               else
               {
                  set(parentvaluelocalNamefieldInfo);
               }
            }
         }
      }
   }
   private void setAttribute(Object oString localNameString valueUnmarshallingContext ctx)
   {
      if(o instanceof Collection)
      {
         XSTypeDefinition type = ctx.getType();
         if(type == null)
         {
            .warn("Type is not available for collection item " + localName + "=" + value + " -> adding as string.");
            ((Collection<String>)o).add(value);
         }
         else
         {
            if(type.getName() == null)
            {
               throw new IllegalStateException("Name is null for simple type?!");
            }
            Object trgValue = SimpleTypeBindings.unmarshal(type.getName(), valuectx.getNamespaceContext());
            ((Collection<Object>)o).add(trgValue);
         }
      }
      else
      {
         Object fieldValue = null;
         final ElementToFieldMapping fieldMapping = .get(
            new ElementToFieldMappingKey(localNameo.getClass())
         );
         if(fieldMapping != null)
         {
            fieldValue = fieldMapping.converter.unmarshal(value);
            set(ofieldValuelocalNamefieldMapping.fieldInfo);
         }
         else
         {
            Class<?> oCls;
            if(o instanceof Immutable)
            {
               oCls = ((Immutable)o).;
            }
            else
            {
               oCls = o.getClass();
            }
            final String fieldName = Util.xmlNameToFieldName(localNametrue);
            FieldInfo fieldInfo = FieldInfo.getFieldInfo(oClsfieldNametrue);
            fieldValue = SimpleTypeBindings.unmarshal(valuefieldInfo.getType());
            set(ofieldValuelocalNamefieldInfo);
         }
      }
   }

   
Converts namspace URI and local name into a class name, tries to load the class, create an instance and return it.

Parameters:
namespaceURI element's namespace URI
localName element's local name
Returns:
null if the class could not be loaded, otherwise an instance of the loaded class
   private static Object create(String namespaceURIString localName, XSTypeDefinition type)
   {
      Object o = null;
      String clsName = type != null && type.getName() != null ?
         Util.xmlNameToClassName(namespaceURItype.getName(), true) :
         Util.xmlNameToClassName(namespaceURIlocalNametrue);
      Class<?> cls = null;
      try
      {
         cls = Thread.currentThread().getContextClassLoader().loadClass(clsName);
      }
      catch(ClassNotFoundException e)
      {
         if(.isTraceEnabled())
         {
            .trace("create: failed to load class " + clsName);
         }
      }
      if(cls != null)
      {
         o = newInstance(cls);
      }
      return o;
   }
   private static Object get(Object oString localNameFieldInfo fieldInfo)
   {
      if(.isTraceEnabled())
      {
         .trace("get object=" + o + " localName=" + localName);
      }
      Object value;
      if(o instanceof Immutable)
      {
         Immutable con = ((Immutable)o);
         value = con.getChild(localName);
      }
      else
      {
         value = fieldInfo.getValue(o);
      }
      return value;
   }
   private static void set(Object parentObject childString localNameFieldInfo fieldInfo)
   {
      if(.isTraceEnabled())
      {
         .trace("set parent=" + parent + " child=" + child + " localName=" + localName);
      }
      if(fieldInfo.isWritable())
      {
         fieldInfo.setValue(parentchild);
      }
      else if(parent instanceof Immutable)
      {
         ((Immutable)parent).addChild(localNamechild);
      }
      else
      {
         throw new IllegalStateException("Neither write method nor field were found for " + fieldInfo.getName() +
            " and the parent object is not an immutable container: parent=" +
            parent.getClass() +
            ", localName=" + localName + ", parent=" + parent + ", child=" + child
         );
      }
   }
   private static Object newInstance(Class<?> cls)
   {
      if(.isTraceEnabled())
      {
         .trace("new " + cls.getName());
      }
      Object instance;
      try
      {
         Constructor<?> ctor = cls.getConstructor(null);
         instance = ctor.newInstance(null);
      }
      catch(NoSuchMethodException e)
      {
         .warn("No no-arg constructor in " + cls);
         instance = new Immutable(cls);
      }
      catch(Exception e)
      {
         throw new IllegalStateException("Failed to create an instance of " +
            cls +
            " with the no-arg constructor: "
            + e.getMessage()
         );
      }
      return instance;
   }
   // Inner classes
   private class ElementToClassMapping
   {
      public final String element;
      public final Class<?> cls;
      public ElementToClassMapping(String elementClass<?> cls)
      {
         this. = element;
         this. = cls;
      }
      public String toString()
      {
         StringBuffer buffer = new StringBuffer();
         buffer.append("ElementToClass@").append(System.identityHashCode(this));
         buffer.append("{element=").append();
         if( != null)
         {
            buffer.append(" class=").append(.getName());
         }
         buffer.append("}");
         return buffer.toString();
      }
      public boolean equals(Object o)
      {
         if(this == o)
         {
            return true;
         }
         if(!(o instanceof ElementToClassMapping))
         {
            return false;
         }
         final ElementToClassMapping classMapping = (ElementToClassMapping)o;
         if( != null ? !.equals(classMapping.cls) : classMapping.cls != null)
         {
            return false;
         }
         return true;
      }
      public int hashCode()
      {
         return ( != null ? .hashCode() : 0);
      }
   }
   private class ElementToFieldMappingKey
   {
      public final String element;
      public final Class<?> cls;
      public ElementToFieldMappingKey(String elementClass<?> cls)
      {
         this. = element;
         this. = cls;
      }
      public boolean equals(Object o)
      {
         if(this == o)
         {
            return true;
         }
         if(!(o instanceof ElementToFieldMappingKey))
         {
            return false;
         }
         final ElementToFieldMappingKey elementToFieldMappingKey = (ElementToFieldMappingKey)o;
         if( != null ? !.equals(elementToFieldMappingKey.cls) : elementToFieldMappingKey.cls != null)
         {
            return false;
         }
         if( != null ?
            !.equals(elementToFieldMappingKey.element) :
            elementToFieldMappingKey.element != null)
         {
            return false;
         }
         return true;
      }
      public int hashCode()
      {
         int result;
         result = ( != null ? .hashCode() : 0);
         result = 29 * result + ( != null ? .hashCode() : 0);
         return result;
      }
   }
   private class ElementToFieldMapping
   {
      public final String element;
      public final Class<?> cls;
      public final TypeBinding converter;
      public final ElementToFieldMappingKey key;
      public final FieldInfo fieldInfo;
      public ElementToFieldMapping(String elementClass<?> clsString fieldNameTypeBinding converter)
      {
         this. = element;
         this. = cls;
         this. = converter;
          = new ElementToFieldMappingKey(elementcls);
          = FieldInfo.getFieldInfo(clsfieldNametrue);
      }
      public String toString()
      {
         StringBuffer buffer = new StringBuffer();
         buffer.append("ElementToField@").append(System.identityHashCode(this));
         buffer.append("{element=").append();
         if( != null)
         {
            buffer.append(" class=").append(.getName());
         }
         buffer.append(" field=").append(.getName());
         if( != null)
         {
            buffer.append(" convertor=").append(.getClass().getName());
         }
         buffer.append("}");
         return buffer.toString();
      }
      public boolean equals(Object o)
      {
         if(this == o)
         {
            return true;
         }
         if(!(o instanceof ElementToFieldMapping))
         {
            return false;
         }
         final ElementToFieldMapping elementToFieldMapping = (ElementToFieldMapping)o;
         if( != null ? !.equals(elementToFieldMapping.cls) : elementToFieldMapping.cls != null)
         {
            return false;
         }
         if( != null ? !.equals(elementToFieldMapping.element) : elementToFieldMapping.element != null)
         {
            return false;
         }
         if(!.getName().equals(elementToFieldMapping.fieldInfo.getName()))
         {
            return false;
         }
         return true;
      }
      public int hashCode()
      {
         int result;
         result = ( != null ? .hashCode() : 0);
         result = 29 * result + ( != null ? .hashCode() : 0);
         result = 29 * result + .getName().hashCode();
         return result;
      }
   }
New to GrepCode? Check out our FAQ X