Package org.jboss.xb.binding.metadata

Examples of org.jboss.xb.binding.metadata.ClassMetaData


               XSAnnotation an = (XSAnnotation)annotations.item(i);
               XsdAnnotation xsdAn = XsdAnnotation.unmarshal(an.getAnnotationString());
               XsdAppInfo appInfo = xsdAn.getAppInfo();
               if(appInfo != null)
               {
                  ClassMetaData classMetaData = appInfo.getClassMetaData();
                  if(classMetaData != null)
                  {
                     if (trace)
                     {
                        log.trace("complex type " +
                           type.getName() +
                           ": impl=" +
                           classMetaData.getImpl()
                        );
                     }
                     binding.setClassMetaData(classMetaData);
                  }
View Full Code Here


      XsdAppInfo appInfo = xsdAn.getAppInfo();
      if(appInfo != null)
      {
         Boolean skip = null;

         ClassMetaData classMetaData = appInfo.getClassMetaData();
         if(classMetaData != null)
         {
            if (trace)
            {
               String msg;
               if(term.isModelGroup())
               {
                  msg = term + " bound to ";
               }
               else if(term.isWildcard())
               {
                  msg = " wildcard bound to ";
               }
               else
               {
                  msg = "element: name=" + ((ElementBinding)term).getQName() + ", class=";
               }

               msg += classMetaData.getImpl();
               log.trace(msg);
            }
            term.setClassMetaData(classMetaData);
            skip = Boolean.FALSE;
         }
View Full Code Here

   private static final QName valueQName = new QName(NS, "value");

   public SchemaBinding init(SchemaBinding schema)
   {
      ElementBinding element = schema.getElement(propertyQName);
      ClassMetaData classMetaData = new ClassMetaData();
      classMetaData.setImpl(AbstractPropertyMetaData.class.getName());
      element.setClassMetaData(classMetaData);
      element.getType().getWildcard().setWildcardHandler(new DefaultWildcardHandler()
      {
         public void setParent(Object parent, Object o, QName elementName, ElementBinding element, ElementBinding parentElement)
         {
            AbstractPropertyMetaData property = (AbstractPropertyMetaData) parent;
            if (o instanceof ValueMetaData == false)
               o = new AbstractValueMetaData(o);
            property.setValue((ValueMetaData) o);
         }
      });

      element = schema.getElement(valueQName);
      classMetaData = new ClassMetaData();
      classMetaData.setImpl(StringValueMetaData.class.getName());
      element.setClassMetaData(classMetaData);

      return schema;
   }
View Full Code Here

   public void testGlobalGroupSchemaBindingModel() throws Exception
   {
      SchemaBinding schema = bind("GlobalGroup.xsd");
      schema.setIgnoreUnresolvedFieldOrClass(false);

      ClassMetaData classMetaData = new ClassMetaData();
      classMetaData.setImpl(Parent.class.getName());
      ElementBinding element = schema.getElement(new QName(NS, "parent"));
      assertNotNull(element);
      element.setClassMetaData(classMetaData);

      classMetaData = new ClassMetaData();
      classMetaData.setImpl(Global1.class.getName());
      ModelGroupBinding group = schema.getGroup(new QName(NS, "global1"));
      assertNotNull(group);
      group.setClassMetaData(classMetaData);
      group.setSkip(Boolean.FALSE);
View Full Code Here

               XSAnnotation an = (XSAnnotation)annotations.item(i);
               XsdAnnotation xsdAn = XsdAnnotation.unmarshal(an.getAnnotationString());
               XsdAppInfo appInfo = xsdAn.getAppInfo();
               if(appInfo != null)
               {
                  ClassMetaData classMetaData = appInfo.getClassMetaData();
                  if(classMetaData != null)
                  {
                     if(ctx.trace)
                     {
                        log.trace("simple type " +
                           type.getName() +
                           ": impl=" +
                           classMetaData.getImpl());
                     }
                     binding.setClassMetaData(classMetaData);
                  }

                  ValueMetaData valueMetaData = appInfo.getValueMetaData();
View Full Code Here

               XSAnnotation an = (XSAnnotation)annotations.item(i);
               XsdAnnotation xsdAn = XsdAnnotation.unmarshal(an.getAnnotationString());
               XsdAppInfo appInfo = xsdAn.getAppInfo();
               if(appInfo != null)
               {
                  ClassMetaData classMetaData = appInfo.getClassMetaData();
                  if(classMetaData != null)
                  {
                     if (ctx.trace)
                     {
                        log.trace("complex type " +
                           type.getName() +
                           ": impl=" +
                           classMetaData.getImpl()
                        );
                     }
                     binding.setClassMetaData(classMetaData);
                  }
View Full Code Here

      XsdAppInfo appInfo = xsdAn.getAppInfo();
      if(appInfo != null)
      {
         Boolean skip = null;

         ClassMetaData classMetaData = appInfo.getClassMetaData();
         if(classMetaData != null)
         {
            if (trace)
            {
               String msg;
               if(term.isModelGroup())
               {
                  msg = term + " bound to ";
               }
               else if(term.isWildcard())
               {
                  msg = " wildcard bound to ";
               }
               else
               {
                  msg = "element: name=" + ((ElementBinding)term).getQName() + ", class=";
               }

               msg += classMetaData.getImpl();
               log.trace(msg);
            }
            term.setClassMetaData(classMetaData);
            skip = Boolean.FALSE;
         }
View Full Code Here

      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.isStartElementCreatesObject() ||
            classMetaData == null && mapEntryMetaData == null && Constants.QNAME_ANYTYPE.equals(type.getQName()))
         {
            if(trace)
            {
               log.trace("startElement " + elementName + " does not create an object");
            }
            return null;
         }
      }

      Object o = 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());
               }
               o = 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;
            if(parentClass.isArray())
            {
               fieldType = parentClass.getComponentType();
            }
            else
            {
               fieldType = FieldInfo.getFieldInfo(parentClass, propName, true).getType();
               if(particle.isRepeatable() && fieldType.isArray())
               {
                  fieldType = fieldType.getComponentType();
               }
            }

            if(fieldType.isArray())
            {
               o = GenericValueContainer.FACTORY.array(wrapperType, propName, fieldType.getComponentType());
            }
            else if(Collection.class.isAssignableFrom(fieldType))
            {
               //System.out.println("GeenericValueContainer.child: " + elementName);
               o = new ValueListInitializer().newValueList(ValueListHandler.FACTORY.child(), Collection.class);
               //o = new ArrayList();
            }
            else
            {
               o = GenericValueContainer.FACTORY.array(wrapperType, propName, fieldType);
            }
         }
      }
      else
      {
         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)
               {
View Full Code Here

   private Class classForNonArrayItem(ElementBinding element, Class parentClass)
   {
      String clsName;

      // first, class metadata and map entry metadata
      ClassMetaData clsMetaData = element.getClassMetaData();
      clsName = clsMetaData == null ? null : clsMetaData.getImpl();
      if(clsName == null)
      {
         MapEntryMetaData mapEntryMetaData = element.getMapEntryMetaData();
         if(mapEntryMetaData != null)
         {
View Full Code Here

      }
   }

   public ClassMetaData getClassMetaData()
   {
      ClassMetaData result = classMetaData;
      if(result == null && mapEntryMetaData == null)
      {
         result = typeBinding.getClassMetaData();
      }
      return result;
View Full Code Here

TOP

Related Classes of org.jboss.xb.binding.metadata.ClassMetaData

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.