Package org.jboss.xb.binding.sunday.unmarshalling

Examples of org.jboss.xb.binding.sunday.unmarshalling.TermBinding


      }
   }

   public static void printParticle(StringBuilder builder, int pad, ParticleBinding particle)
   {
      TermBinding term = particle.getTerm();
      if (term instanceof ElementBinding)
         printElement(builder, pad, particle);
      else if (term instanceof WildcardBinding)
         printWildcard(builder, pad, particle);
      else if (term instanceof SequenceBinding)
View Full Code Here


   /** The singleton instance */
   public static final BuilderParticleHandler INSTANCE = new BuilderParticleHandler();
  
   public Object startParticle(Object parent, QName elementName, ParticleBinding particle, Attributes attrs, NamespaceContext nsCtx)
   {
      TermBinding term = particle.getTerm();
      if (term instanceof ArraySequenceBinding)
         return new ArrayWrapper(parent, elementName);
      return parent;
   }
View Full Code Here

      return parent;
   }

   public void setParent(Object parent, Object o, QName elementName, ParticleBinding particle, ParticleBinding parentParticle)
   {
      TermBinding term = particle.getTerm();
      TermBinding parentTerm = parentParticle.getTerm();
      if (term.isModelGroup() == false && parentTerm instanceof ElementBinding)
      {
         ElementBinding elementBinding = (ElementBinding) parentTerm;
         if (parent != null && parent instanceof ArrayWrapper)
         {
View Full Code Here

      }
   }

   public Object endParticle(Object o, QName elementName, ParticleBinding particle)
   {
      TermBinding term = particle.getTerm();
      if (term instanceof ArraySequenceBinding)
      {
         ArrayWrapper wrapper = (ArrayWrapper) o;
         Object parent = wrapper.getParent();
         ParticleBinding childParticle = wrapper.getChildParticle();
View Full Code Here

   public void setParent(Object parent, Object o, QName qName,  ParticleBinding particle, ParticleBinding parentParticle)
   {
      if (trace)
         log.trace("setParent " + qName + " parent=" + BuilderUtil.toDebugString(parent) + " child=" + BuilderUtil.toDebugString(o));

      TermBinding term = particle.getTerm();
      ValueAdapter valueAdapter = null;
      if(term.isModelGroup())
      {
         QName modelGroupName = ((ModelGroupBinding)term).getQName();
         if(modelGroupName != null)
         {
            qName = modelGroupName;
         }
      }
      else if(term.isElement())
      {
         valueAdapter = ((ElementBinding)term).getValueAdapter();
      }

      if (parent != null && parent instanceof ArrayWrapper)
      {
         ArrayWrapper wrapper = (ArrayWrapper) parent;
         wrapper.add(o);
         wrapper.setChildParticle(particle);
         wrapper.setParentParticle(parentParticle);
         return;
      }

      BeanAdapter beanAdapter;
      if (parent instanceof ArrayWrapper)
         beanAdapter = (BeanAdapter) ((ArrayWrapper) parent).getParent();
      else
         beanAdapter = (BeanAdapter) parent;
      AbstractPropertyHandler propertyHandler = beanAdapter.getPropertyHandler(qName);
      if (propertyHandler == null)
      {
         AbstractPropertyHandler wildcardHandler = beanAdapter.getWildcardHandler();
         if (wildcardHandler != null)
         {
            if (o != null && o instanceof ArrayWrapper)
            {
               ArrayWrapper wrapper = (ArrayWrapper) o;
               wildcardHandler.doHandle(beanAdapter, wrapper, wrapper.getElementName());
               return;
            }
         }
         TermBinding element = term;
         if (element.getSchema().isStrictSchema())
         {
            throw new RuntimeException("QName " + qName + " unknown property parent=" + BuilderUtil.toDebugString(parent) + " child=" + BuilderUtil.toDebugString(o) + " available=" + beanAdapter.getAvailable());
         }
         if (trace)
            log.trace("QName " + qName + " unknown property parent=" + BuilderUtil.toDebugString(parent) + " child=" + BuilderUtil.toDebugString(o));
View Full Code Here

                               QName elementName,
                               ParticleBinding particle,
                               Attributes attrs,
                               NamespaceContext nsCtx)
   {
      TermBinding term = particle.getTerm();
      Object o = startElement(parent, elementName, particle);
      if(!term.isModelGroup())
      {
         ElementBinding element = (ElementBinding)term;
         if(o != null)
         {
            attrs = element.getType().expandWithDefaultAttributes(attrs);
View Full Code Here

                         Object o,
                         QName qName,
                         ParticleBinding particle,
                         ParticleBinding parentParticle)
   {
      TermBinding term = particle.getTerm();
      if(term.isSkip())
      {
         return;
      }

      boolean trace = log.isTraceEnabled();
      if(trace)
      {
         log.trace("setParent " + qName + " parent=" + parent + " object=" + o + " term=" + term);
      }

      TermBinding parentTerm = parentParticle.getTerm();

      if(term.isMapEntryKey())
      {
         if(trace)
         {
            log.trace("setParent " + qName + " mapKey");
         }

         if(parent instanceof MapEntry)
         {
            MapEntry mapEntry = (MapEntry)parent;
            mapEntry.setKey(o);
         }
         else if(parentTerm != null)
         {
            MapEntryMetaData mapEntryMetaData = getMapEntryMetaData(parentTerm, qName);

            String getKeyMethodName = mapEntryMetaData.getGetKeyMethod();
            if(getKeyMethodName == null)
            {
               getKeyMethodName = "getKey";
            }

            String setKeyMethodName = mapEntryMetaData.getSetKeyMethod();
            if(setKeyMethodName == null)
            {
               setKeyMethodName = "setKey";
            }

            Class parentCls = parent.getClass();
            Method setKeyMethod = getSetMethod(parentCls, getKeyMethodName, setKeyMethodName);
            invokeSetter(setKeyMethod, parent, o, setKeyMethodName);
         }
         else
         {
            throw new JBossXBRuntimeException(
               "Element " +
               qName +
               " bound as map entry key but parent element is not recognized as map entry and its metadata is not available."
            );
         }
      }
      else if(term.isMapEntryValue())
      {
         if(trace)
         {
            log.trace("setParent " + qName + " mapValue");
         }

         if(parent instanceof MapEntry)
         {
            MapEntry mapEntry = (MapEntry)parent;
            mapEntry.setValue(o);
         }
         else if(parentTerm != null)
         {
            MapEntryMetaData mapEntryMetaData = getMapEntryMetaData(parentTerm, qName);
            setMapEntryValue(mapEntryMetaData, parent, o);
         }
         else
         {
            throw new JBossXBRuntimeException(
               "Element " +
               qName +
               " bound as map entry key but parent element is not recognized as map entry and its metadata is not available."
            );
         }
      }
      else
      {
         Object owner = parent;
         if(parent instanceof MapEntry)
         {
            if(trace)
            {
               log.trace("setParent " + qName + " mapEntry");
            }

            MapEntry mapEntry = (MapEntry)parent;
            owner = mapEntry.getValue();
            if(owner == null)
            {
               if(parentTerm == null)
               {
                  throw new JBossXBRuntimeException("Binding metadata needed for lazy map entry value instantiation is not available " +
                     "for parent element of element " +
                     qName
                  );
               }

               MapEntryMetaData mapEntryMetaData = getMapEntryMetaData(parentTerm, qName);
               String valueType = mapEntryMetaData.getValueType();
               if(valueType == null)
               {
                  throw new JBossXBRuntimeException("Element " +
                     qName +
                     " is supposed to be bound as map entry value with lazy value instantiation " +
                     "but value type is not specified in its map entry metadata."
                  );
               }

               Class valueCls;
               try
               {
                  valueCls = Thread.currentThread().getContextClassLoader().loadClass(valueType);
               }
               catch(ClassNotFoundException e)
               {
                  throw new JBossXBRuntimeException(
                     "Failed to load value type specified in the map entry metadata: " + valueType
                  );
               }

               try
               {
                  owner = valueCls.newInstance();
               }
               catch(Exception e)
               {
                  throw new JBossXBRuntimeException(
                     "Failed to create an instance of value type " + valueType + ": " + e.getMessage()
                  );
               }

               setMapEntryValue(mapEntryMetaData, parent, owner);
            }
         }

         // the wildcard this element is a content of
         WildcardBinding wildcard = null;
         if(parentTerm != null && !parentTerm.isModelGroup())
         {
            ElementBinding parentElement = (ElementBinding)parentTerm;
            TypeBinding parentType = parentElement.getType();
            wildcard = parentType.getWildcard();
            // there should be a better way of checking this
View Full Code Here

      }
   }

   public Object endParticle(Object o, QName elementName, ParticleBinding particle)
   {
      TermBinding term = particle.getTerm();
      if(term.isSkip())
      {
         return o;
      }

      boolean trace = log.isTraceEnabled();
View Full Code Here

   // Private

   private Object startElement(Object parent, QName elementName, ParticleBinding particle)
   {
      TermBinding term = particle.getTerm();
      if(term.isSkip())
      {
         return parent;
      }

      boolean trace = log.isTraceEnabled();
      if(trace)
      {
         log.trace("startElement " + elementName + " parent=" + parent + " term=" + term);
      }

      ClassMetaData classMetaData = term.getClassMetaData();
      MapEntryMetaData mapEntryMetaData = term.getMapEntryMetaData();

      if(!term.isModelGroup())
      {
         TypeBinding type = ((ElementBinding)term).getType();
         if(type.isSimple() ||
               classMetaData == null && mapEntryMetaData == null &&
               (!type.isStartElementCreatesObject() ||
                     Constants.QNAME_ANYTYPE.equals(type.getQName())))
         {
            if(trace)
            {
               log.trace("startElement " + elementName + " does not create an object");
            }
            return null;
         }
      }

      // if addMethod is specified, it's probably some collection field
      // but should not be set as a property. Instead, items are added to it using the addMethod
      ElementBinding arrayItem = null;
      if(!term.isModelGroup())
      {
         TypeBinding type = ((ElementBinding)term).getType();
         if(type.getAttributes().isEmpty())
         {
            ParticleBinding typeParticle = type.getParticle();
            ModelGroupBinding modelGroup = (ModelGroupBinding)(typeParticle == null ? null : typeParticle.getTerm());
            arrayItem = modelGroup == null ? null : modelGroup.getArrayItem();

            // todo refactor later (move it to modelGroup.getArrayItem()?)
            if(arrayItem != null &&
               (arrayItem.isSkip() ||
               arrayItem.getMapEntryMetaData() != null ||
               arrayItem.getPutMethodMetaData() != null ||
               arrayItem.getAddMethodMetaData() != null
               ))
            {
               arrayItem = null;
            }
         }
      }

      if(arrayItem != null)
      {
         Class wrapperType = null;
         if(classMetaData != null)
         {
            wrapperType = loadClassForTerm(classMetaData.getImpl(),
               term.getSchema().isIgnoreUnresolvedFieldOrClass(),
               elementName
            );

            if(GenericValueContainer.class.isAssignableFrom(wrapperType) ||
               Collection.class.isAssignableFrom(wrapperType) ||
               Map.class.isAssignableFrom(wrapperType))
            {
               return newInstance(wrapperType, elementName, term.getSchema().isUseNoArgCtorIfFound());
            }
         }

         if(wrapperType == null && parent == null)
         {
            Class itemType = classForElement(arrayItem, null);
            if(itemType != null)
            {
               if(trace)
               {
                  log.trace("startElement " + elementName + " new array " + itemType.getName());
               }
               return GenericValueContainer.FACTORY.array(itemType);
            }
         }
         else
         {
            PropertyMetaData propertyMetaData = wrapperType == null ?
               term.getPropertyMetaData() : arrayItem.getPropertyMetaData();

            String propName;
            if(propertyMetaData == null)
            {
               propName = Util.xmlNameToFieldName(
                  wrapperType == null ? elementName.getLocalPart() : arrayItem.getQName().getLocalPart(),
                  term.getSchema().isIgnoreLowLine()
               );
            }
            else
            {
               propName = propertyMetaData.getName();
            }

            if(trace)
            {
               log.trace("startElement " + elementName + " property=" + propName + " wrapper=" + wrapperType);
            }

            Class parentClass = wrapperType;
            if(wrapperType == null)
            {
               if(parent instanceof GenericValueContainer)
               {
                  parentClass = ((GenericValueContainer)parent).getTargetClass();
               }
               else if(parent instanceof ValueList)
               {
                  parentClass = ((ValueList)parent).getTargetClass();
               }
               else
               {
                  parentClass = parent.getClass();
               }
            }

            Class fieldType = null;
            if(parentClass.isArray())
            {
               fieldType = parentClass.getComponentType();
            }
            else
            {
               //fieldType = FieldInfo.getFieldInfo(parentClass, propName, true).getType();
               // this was changed to false because allow overriding of handler.setParent()
               // with an interceptor.add(). See CollectionOverridePropertyUnitTestCase
               // In other words, don't treat it as an array wrapper.
               FieldInfo fieldInfo = FieldInfo.getFieldInfo(parentClass, propName, false);
               if(fieldInfo != null)
               {
                  fieldType = fieldInfo.getType();
                  if (particle.isRepeatable() && fieldType.isArray())
                  {
                     fieldType = fieldType.getComponentType();
                  }
               }
               else if(((ElementBinding)term).getType().getInterceptors(arrayItem.getQName()).isEmpty() &&
                     arrayItem.getInterceptors().isEmpty())
               {
                  QName typeName = ((ElementBinding)term).getType().getQName();
                  throw new JBossXBRuntimeException(
                        "Couldn't apply 'array wrapper' pattern for element " +
                        elementName + " of type " +
                        (typeName == null ? "anonymous" : typeName.toString()) +
                        ": failed to resolve property " + propName +
                        " and no interceptors applied to override handler.setParent(...)");
               }
            }

            if(fieldType != null)
            {
               // TODO: review the logic for cases when wrapperType == null
               if (fieldType.isArray())
               {
                  return GenericValueContainer.FACTORY.array(wrapperType, propName, fieldType.getComponentType());
               }
               else if (Collection.class.isAssignableFrom(fieldType))
               {
                  if (wrapperType == null)
                  {
                     return new ValueListInitializer().newValueList(ValueListHandler.FACTORY.child(), Collection.class);
                     //o = new ArrayList();
                  }
               }
               else
               {
                  return GenericValueContainer.FACTORY.array(wrapperType, propName, fieldType);
               }
            }
         }
      }

      Object o = null;
      if (mapEntryMetaData != null)
      {
         if (mapEntryMetaData.getImpl() != null)
         {
            Class cls = loadClassForTerm(mapEntryMetaData.getImpl(), term.getSchema().isIgnoreUnresolvedFieldOrClass(), elementName);

            if (trace)
            {
               log.trace("startElement " + elementName + " new map entry " + cls.getName());
            }

            o = newInstance(cls, elementName, term.getSchema().isUseNoArgCtorIfFound());
         }
         else
         {
            o = new MapEntry();
            if (trace)
            {
               log.trace("startElement " + elementName + " new map entry");
            }
         }

         if (mapEntryMetaData.isNonNullValue() && mapEntryMetaData.getValueType() != null)
         {
            Class mapValueType;
            try
            {
               mapValueType = Thread.currentThread().getContextClassLoader().loadClass(mapEntryMetaData.getValueType());
            }
            catch (ClassNotFoundException e)
            {
               throw new JBossXBRuntimeException("startElement failed for " + elementName + ": failed to load class "
                     + mapEntryMetaData.getValueType() + " for map entry value.");
            }

            Object value;
            try
            {
               if (trace)
               {
                  log.trace("startElement " + elementName + " map value type " + mapEntryMetaData.getValueType());
               }
               value = mapValueType.newInstance();
            }
            catch (Exception e)
            {
               throw new JBossXBRuntimeException("startElement failed for " + elementName
                     + ": failed to create an instance of " + mapValueType + " for map entry value.");
            }

            if (o instanceof MapEntry)
            {
               ((MapEntry) o).setValue(value);
            }
            else
            {
               String getValueMethodName = mapEntryMetaData.getGetValueMethod();
               if (getValueMethodName == null)
               {
                  getValueMethodName = "getValue";
               }

               String setValueMethodName = mapEntryMetaData.getSetValueMethod();
               if (setValueMethodName == null)
               {
                  setValueMethodName = "setValue";
               }

               Method getValueMethod;
               try
               {
                  getValueMethod = o.getClass().getMethod(getValueMethodName, null);
               }
               catch (NoSuchMethodException e)
               {
                  throw new JBossXBRuntimeException("getValueMethod=" + getValueMethodName
                        + " is not found in map entry " + o.getClass());
               }

               Method setValueMethod;
               try
               {
                  setValueMethod = o.getClass().getMethod(setValueMethodName, new Class[]{getValueMethod.getReturnType()});
               }
               catch (NoSuchMethodException e)
               {
                  throw new JBossXBRuntimeException("setValueMethod=" + setValueMethodName + "("
                        + getValueMethod.getReturnType().getName() + " value) is not found in map entry "
                        + o.getClass());
               }

               try
               {
                  setValueMethod.invoke(o, new Object[]
                  {value});
               }
               catch (Exception e)
               {
                  throw new JBossXBRuntimeException("setValueMethod=" + setValueMethodName + " failed: owner=" + o
                        + ", value=" + value + ", msg=" + e.getMessage(), e);
               }
            }
         }
      }
      else
      {
         // todo: for now we require metadata for model groups to be bound
         // todo 2: parent.getClass() is not going to work for containers
         Class parentClass = null;
         if (parent != null)
         {
            if (parent instanceof GenericValueContainer)
            {
               parentClass = ((GenericValueContainer) parent).getTargetClass();
            }
            else if (parent instanceof ValueList)
            {
               parentClass = ((ValueList) parent).getTargetClass();
            }
            else
            {
               parentClass = parent.getClass();
            }
         }

         Class cls;
         if (term.isModelGroup())
         {
            if (classMetaData == null)
            {
               throw new JBossXBRuntimeException(
                     "Model groups should be annotated with 'class' annotation to be bound.");
            }
            cls = loadClassForTerm(classMetaData.getImpl(), term.getSchema().isIgnoreUnresolvedFieldOrClass(), elementName);
         }
         else
         {
            ElementBinding element = (ElementBinding) term;
            cls = classForNonArrayItem(element, parentClass);
            if (cls != null)
            {
               // todo: before that, the type should be checked for required attributes and elements
               TypeBinding simpleType = element.getType().getSimpleType();
               if (simpleType != null)
               {
                  Class simpleCls = classForSimpleType(simpleType, element.isNillable());
                  if (cls.equals(simpleCls) || cls.isPrimitive() && Classes.getPrimitiveWrapper(cls) == simpleCls
                        || simpleCls.isPrimitive() && Classes.getPrimitiveWrapper(simpleCls) == cls)
                  {
                     cls = null;
                  }
               }
            }
         }

         if (cls != null)
         {
            boolean noArgCtor;
            if (classMetaData == null)
            {
               noArgCtor = term.getSchema().isUseNoArgCtorIfFound();
            }
            else
            {
               Boolean termUsesNoArgCtor = classMetaData.isUseNoArgCtor();
               noArgCtor = termUsesNoArgCtor == null ?
                     term.getSchema().isUseNoArgCtorIfFound() : termUsesNoArgCtor.booleanValue();
            }

            if (trace)
            {
               log.trace("startElement " + elementName + " new " + cls.getName() + ", noArgCtor=" + noArgCtor);
View Full Code Here

      }
   }

   public static void printParticle(StringBuilder builder, int pad, ParticleBinding particle)
   {
      TermBinding term = particle.getTerm();
      if (term instanceof ElementBinding)
         printElement(builder, pad, particle);
      else if (term instanceof WildcardBinding)
         printWildcard(builder, pad, particle);
      else if (term instanceof SequenceBinding)
View Full Code Here

TOP

Related Classes of org.jboss.xb.binding.sunday.unmarshalling.TermBinding

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.