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

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


         // 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
            if(wildcard != null && parentType.getElement(qName) != null)
            {
               wildcard = null;
            }
         }
View Full Code Here


      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);
            }

            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(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.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 = 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 ||
View Full Code Here

                           QName elementName,
                           ElementBinding element,
                           Attributes attrs,
                           NamespaceContext nsCtx)
   {
      TypeBinding type = element.getType();
      for(int i = 0; i < attrs.getLength(); ++i)
      {
         QName attrName = new QName(attrs.getURI(i), attrs.getLocalName(i));
         AttributeBinding binding = type.getAttribute(attrName);
         if(binding != null)
         {
            AttributeHandler handler = binding.getHandler();
            if(handler != null)
            {
               Object value = handler.unmarshal(elementName, attrName, binding, nsCtx, attrs.getValue(i));
               handler.attribute(elementName, attrName, binding, o, value);
            }
            else
            {
               throw new JBossXBRuntimeException(
                  "Attribute binding present but has no handler: element=" + elementName + ", attrinute=" + attrName
               );
            }
         }
         else
         {
            if(!Constants.NS_XML_SCHEMA_INSTANCE.equals(attrs.getURI(i)))
            {
               CharactersHandler simpleType = type.getCharactersHandler();
               Object value;
               if(simpleType == null)
               {
                  value = attrs.getValue(i);
                  RtUtil.set(o, attrName, value, element.getSchema().isIgnoreLowLine());
View Full Code Here

   }

   private Class classForElement(ElementBinding element, Class parentClass)
   {
      Class cls;
      TypeBinding type = element.getType();
      QName typeQName = type.getQName();
      if(typeQName != null && Constants.NS_XML_SCHEMA.equals(typeQName.getNamespaceURI()))
      {
         cls = SimpleTypeBindings.classForType(type.getQName().getLocalPart(), element.isNillable());
      }
      else
      {
         ElementBinding arrayItem = null;
         if(!type.isSimple() && type.getAttributes().isEmpty())
         {
            ParticleBinding typeParticle = type.getParticle();
            ModelGroupBinding modelGroup = (ModelGroupBinding)(typeParticle == null ? null : typeParticle.getTerm());
            arrayItem = modelGroup == null ? null : modelGroup.getArrayItem();
         }

         if(arrayItem != null)
View Full Code Here

      else if(type.getClassMetaData() != null && type.getClassMetaData().getImpl() != null)
      {
         return RtUtil.loadClass(type.getClassMetaData().getImpl(), true);
      }

      TypeBinding itemType = type.getItemType();
      if(itemType != null)
      {
         if(type.getSchemaBinding().isUnmarshalListsToArrays())
         {
            // todo: nillable not always should be propagated to the item
            Class itemClass = classForSimpleType(itemType, nillable);
            return Array.newInstance(itemClass, 0).getClass();
         }
         else
         {
            return java.util.List.class;
         }
      }
      else
      {
         QName qName = type.getQName();
         if(qName != null && Constants.NS_XML_SCHEMA.equals(qName.getNamespaceURI()))
         {
            return SimpleTypeBindings.classForType(qName.getLocalPart(), nillable);
         }
         else
         {
            TypeBinding baseType = type.getBaseType();
            if(baseType == null)
            {
               throw new JBossXBRuntimeException("Expected a base type here.");
            }
View Full Code Here

      String javaType = exceptionMapping.getExceptionType();
      log.trace("processExceptionMapping: [xmlType=" + xmlType + ",javaType=" + javaType + "]");
     
      if (schemaBinding.getType(xmlType) == null)
      {
         TypeBinding typeBinding = new TypeBinding(xmlType);
         ClassMetaData cmd = new ClassMetaData();
         cmd.setUseNoArgCtor(Boolean.FALSE);
         cmd.setImpl(javaType);
         typeBinding.setClassMetaData(cmd);
         typeBinding.setSimple(false);
         schemaBinding.addType(typeBinding);
      }
   }
View Full Code Here

   {
      QName xmlType = getXmlType(typeMapping);
      String javaType = typeMapping.getJavaType();
      log.trace("processNonArrayType: [xmlType=" + xmlType + ",javaType=" + javaType + "]");

      TypeBinding typeBinding = getTypeBinding(schemaBinding, typeMapping);
      if (typeBinding != null)
      {
         // Set the java type, but skip SimpleTypes
         boolean isSimpleTypeBinding = (typeBinding instanceof SimpleTypeBinding);
         if(isSimpleTypeBinding == false)
         {
         ClassMetaData classMetaData = typeBinding.getClassMetaData();
         if (classMetaData == null)
         {
            classMetaData = new ClassMetaData();
            typeBinding.setClassMetaData(classMetaData);
         }
         classMetaData.setImpl(javaType);

         // exception mapping drives whether we should use the noarg ctor
         JavaWsdlMapping wsdlMapping = typeMapping.getJavaWsdlMapping();
         for (ExceptionMapping aux : wsdlMapping.getExceptionMappings())
         {
            if (javaType.equals(aux.getExceptionType()))
            {
               classMetaData.setUseNoArgCtor(false);
               break;
            }
         }

         if (log.isTraceEnabled())
         {
            QName typeQName = typeBinding.getQName();
            log.trace("Bound: [xmlType=" + typeQName + ",javaType=" + javaType + "]");
            }
         }

         VariableMapping[] variableMappings = typeMapping.getVariableMappings();
View Full Code Here

         return getAnonymousTypeBinding(schemaBinding, anonymousTypeQName);
      }

      QName xmlType = typeMapping.getRootTypeQName();

      TypeBinding typeBinding = null;
      if ("complexType".equals(qnameScope) || "simpleType".equals(qnameScope))
      {
         typeBinding = schemaBinding.getType(xmlType);
         if (typeBinding == null)
         {
View Full Code Here

      return element.getType();
   }

   public void bindParameterToElement(SchemaBinding schemaBinding, QName xmlName, QName xmlType)
   {
      TypeBinding typeBinding;
      boolean isAnonymousType = xmlType.getLocalPart().startsWith(">");
      if (isAnonymousType)
      {
         typeBinding = getAnonymousTypeBinding(schemaBinding, xmlType);
      }
View Full Code Here

   private ElementBinding findLocalPathElementInTypes(Iterator types, String[] path)
   {
      while (types.hasNext())
      {
         TypeBinding type = (TypeBinding)types.next();
         if (type.getQName().getLocalPart().equals(path[0]))
         {
            ParticleBinding particle = type.getParticle();
            if (particle == null)
               continue;

            TermBinding term = particle.getTerm();
            if (!term.isModelGroup())
View Full Code Here

TOP

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

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.