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

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


         XmlRootElement xmlRootElement = classInfo.getUnderlyingAnnotation(XmlRootElement.class);
         if (xmlRootElement != null)
            root = true;
      }

      ElementBinding elementBinding = new ElementBinding(schemaBinding, qName, typeBinding);
      if (trace)
         log.trace("created  element " + qName + " element=" + elementBinding + " rootElement=" + root);

      // If we are a root element bind it
      if (root)
View Full Code Here


                     memberTypeInfo = ((ClassInfo) memberTypeInfo).getComponentType();
                     isCol = true;
                  }

                  TypeBinding memberTypeBinding = resolveTypeBinding(memberTypeInfo);
                  ElementBinding memberElement = createElementBinding(memberTypeInfo, memberTypeBinding, memberQName, false);
                  memberElement.setNillable(true);
                  ParticleBinding memberParticle = new ParticleBinding(memberElement, 0, 1, isCol);
                  choiceGroup.addParticle(memberParticle);

                  typeBinding.pushInterceptor(memberQName, ChildCollectionInterceptor.SINGLETON);
               }
            }
            else
            {
               ParticleBinding choiceParticle = new ParticleBinding(choiceGroup, 0, 1, true);
               model.addParticle(choiceParticle);
            }
           
            if (trace)
               log.trace("choices for " + typeBinding.getQName() + ": " + choiceGroup.getParticles());
         }
      }

      // Determine the wildcard handler
      AbstractPropertyHandler wildcardHandler = null;
      if (wildcardProperty != null)
      {
         TypeInfo wildcardType = wildcardProperty.getType();
         if (wildcardType.isCollection())
            wildcardHandler = new CollectionPropertyWildcardHandler(wildcardProperty, wildcardType);
         else
            wildcardHandler = new PropertyWildcardHandler(wildcardProperty, wildcardType);
      }

      // Look through the properties
      for (String name : propertyOrder)
      {
         // Setup the error stack
         push(typeInfo, name);
         // Get the property
         PropertyInfo property = beanInfo.getProperty(name);
         bindProperty(property, typeBinding, model, beanAdapterFactory, propertyOrder);
         pop();
      }

      // Bind the children
      JBossXmlChild[] children = null;
      JBossXmlChildren jbossXmlChildren = typeInfo.getUnderlyingAnnotation(JBossXmlChildren.class);
      if (jbossXmlChildren != null)
         children = jbossXmlChildren.value();
      else
      {
         JBossXmlChild jbossXmlChild = typeInfo.getUnderlyingAnnotation(JBossXmlChild.class);
         if (jbossXmlChild != null)
            children = new JBossXmlChild[] { jbossXmlChild };
      }

      if (children != null && children.length > 0)
      {
         for (JBossXmlChild child : children)
         {
            QName qName = generateXmlName(child.name(), elementForm, child.namespace(), child.name());
            TypeInfo childType = JBossXBBuilder.configuration.getTypeInfo(child.type());

            TypeBinding elementTypeBinding = resolveTypeBinding(childType);
            ElementBinding elementBinding = createElementBinding(childType, elementTypeBinding, qName, false);

            // Bind it to the model
            ParticleBinding particle = new ParticleBinding(elementBinding, child.minOccurs(), child.maxOccurs(), child.unbounded());
            model.addParticle(particle);
View Full Code Here

                     valueAdapter = new XBValueAdapter(xmlTypeAdapter.value(), memberTypeInfo.getTypeInfoFactory());
                     memberTypeInfo = valueAdapter.getAdaptedTypeInfo();
                  }

                  TypeBinding memberTypeBinding = resolveTypeBinding(memberTypeInfo);
                  ElementBinding memberElement = createElementBinding(memberTypeInfo, memberTypeBinding, memberQName,
                        false);
                  memberElement.setNillable(true);
                  memberElement.setValueAdapter(valueAdapter);
                  ParticleBinding memberParticle = new ParticleBinding(memberElement, 0, 1, isCol);
                  propertyGroup.addParticle(memberParticle);

                  if (trace)
                     log.trace("added " + memberParticle + " to " + xmlModelGroup.kind() + ", property "
                           + property.getName());
               }
            }

            model.addParticle(new ParticleBinding(propertyGroup));

            // model group value handler based on the model group name
            // TODO what if it doesn't have a name?
            AbstractPropertyHandler propertyHandler = null;
            if (propertyType.isCollection())
               propertyHandler = new CollectionPropertyHandler(property, propClassInfo);
            else
            {
               propertyHandler = new PropertyHandler(property, propClassInfo);
            }
            beanAdapterFactory.addProperty(propertyGroup.getQName(), propertyHandler);
            return;
         }
      }

      // So this is element(s)
      XmlElement[] elements = null;
      XmlElement xmlElement = property.getUnderlyingAnnotation(XmlElement.class);
      if (xmlElement != null)
      {
         // A single element annotated
         elements = new XmlElement[]
         {xmlElement};
      }
      else
      {
         // Mutlple elements
         XmlElements xmlElements = property.getUnderlyingAnnotation(XmlElements.class);
         if (xmlElements != null)
            elements = xmlElements.value();
      }

      // A single element not annotated
      if (elements == null || elements.length == 0)
         elements = new XmlElement[1];

      // for now support just one JBossXmlNsPrefix
      JBossXmlNsPrefix xmlNsPrefix = property.getUnderlyingAnnotation(JBossXmlNsPrefix.class);

      // support for @XmlElementWrapper
      // the wrapping element is ignored in this case
      XmlElementWrapper xmlWrapper = property.getUnderlyingAnnotation(XmlElementWrapper.class);
      if (xmlWrapper != null)
      {        
         String wrapperNamespace = xmlWrapper.namespace();
         String wrapperName = xmlWrapper.name();
         QName wrapperQName = generateXmlName(property.getName(), elementForm, wrapperNamespace, wrapperName);
         localModel = bindXmlElementWrapper(propertyType, localModel, xmlWrapper.nillable(), wrapperQName);
         if (trace)
            log.trace("Added property " + wrapperQName + " for type=" + property.getBeanInfo().getName() + " property="
                  + property.getName() + " as a wrapper element");
      }

      // Setup a choice
      if (elements.length > 1)
      {
         ChoiceBinding choice = new ChoiceBinding(schemaBinding);
         choice.setHandler(BuilderParticleHandler.INSTANCE);
         ParticleBinding particleBinding = new ParticleBinding(choice);
         particleBinding.setMinOccurs(0);
         particleBinding.setMaxOccurs(1);
         localModel.addParticle(particleBinding);
         localModel = choice;
         if (trace)
            log.trace("XmlElements seen adding choice for type=" + property.getBeanInfo().getName() + " property="
                  + property.getName());
      }

      for (int i = 0; i < elements.length; ++i)
      {
         XmlElement element = elements[i];
         if (trace)
            log.trace("Processing " + element + " for type=" + property.getBeanInfo().getName() + " property="
                  + property.getName());

         // Determine the parameters
         String overrideNamespace = null;
         String overrideName = null;
         boolean nillable = false;
         boolean required = false;

         TypeInfo localPropertyType = propertyType;

         if (element != null)
         {
            overrideNamespace = element.namespace();
            overrideName = element.name();
            nillable = element.nillable();
            required = element.required();
            Class<?> elementType = element.type();
            if (elementType != XmlElement.DEFAULT.class)
               localPropertyType = propertyType.getTypeInfoFactory().getTypeInfo(elementType);
         }

         if (xmlNsPrefix != null)
         {
            overrideNamespace = schemaBinding.getNamespace(xmlNsPrefix.prefix());
            if (overrideNamespace == null)
            {
               if (xmlNsPrefix.schemaTargetIfNotMapped())
               {
                  overrideNamespace = defaultNamespace;
               }
               else
               {
                  throw new IllegalStateException("Prefix '" + xmlNsPrefix.prefix()
                        + "' is not mapped to any namespace!");
               }
            }
         }

         // Determine the name
         QName propertyQName = generateXmlName(property.getName(), elementForm, overrideNamespace, overrideName);

         // Create the element
         JBossXmlGroup jbossXmlGroup = null;
         if (!propertyType.isPrimitive())
            jbossXmlGroup = ((ClassInfo) propertyType).getUnderlyingAnnotation(JBossXmlGroup.class);
         if (element == null && jbossXmlGroup != null)
         {
            if (trace)
               log.trace("Processing group for property " + property.getName() + " in "
                     + property.getBeanInfo().getName() + " " + jbossXmlGroup);
            JBossXmlChild[] children = jbossXmlGroup.value();
            if (children != null && children.length > 0)
            {
               TypeBinding elementTypeBinding = new TypeBinding();
               elementTypeBinding.setSchemaBinding(schemaBinding);
               elementTypeBinding.setHandler(BuilderParticleHandler.INSTANCE);
               ElementBinding elementBinding = createElementBinding(localPropertyType, elementTypeBinding, propertyQName, false);

               // Bind it to the model
               ParticleBinding particle = new ParticleBinding(elementBinding, 1, 1, false);
               if (required == false)
                  particle.setMinOccurs(0);
               localModel.addParticle(particle);

               // Can it take text?
               JBossXmlGroupText groupText = ((ClassInfo) propertyType)
                     .getUnderlyingAnnotation(JBossXmlGroupText.class);
               if (groupText != null)
               {
                  CharactersHandler textHandler;
                  if (groupText.wrapper() != Object.class)
                  {
                     BeanInfo wrapperInfo = JBossXBBuilder.configuration.getBeanInfo(groupText.wrapper());
                     textHandler = new ValueHandler(property, wrapperInfo, groupText.property());
                  }
                  else
                     textHandler = new ValueHandler(property);
                  elementTypeBinding.setSimpleType(textHandler);
               }

               // Setup the child model
               ChoiceBinding childModel = new ChoiceBinding(schemaBinding);
               childModel.setHandler(BuilderParticleHandler.INSTANCE);
               ParticleBinding particleBinding = new ParticleBinding(childModel);
               particleBinding.setMinOccurs(0);
               particleBinding.setMaxOccurs(1);
               elementTypeBinding.setParticle(particleBinding);

               for (JBossXmlChild child : children)
               {
                  QName childName = generateXmlName(child.name(), elementForm, child.namespace(), child.name());
                  TypeInfo childType = JBossXBBuilder.configuration.getTypeInfo(child.type());

                  TypeBinding childTypeBinding = resolveTypeBinding(childType);
                  ElementBinding childBinding = createElementBinding(childType, childTypeBinding, childName, false);
                  childBinding.setNillable(nillable);

                  // Bind it to the model
                  particle = new ParticleBinding(childBinding, child.minOccurs(), child.maxOccurs(), child.unbounded());
                  particle.setMinOccurs(0);
                  childModel.addParticle(particle);

                  if(childType.isMap())
                     bindMapProperty(property, (ClassInfo) childType, childName, (ModelGroupBinding) childTypeBinding.getParticle().getTerm());
                                   
                  DefaultElementInterceptor interceptor = new PropertyInterceptor(property, propertyType);
                  elementTypeBinding.pushInterceptor(childName, interceptor);
                  if (trace)
                     log.trace("Added interceptor " + childName + " for type=" + property.getBeanInfo().getName()
                           + " property=" + property.getName() + " interceptor=" + interceptor + " " + childType.getName());

                  beanAdapterFactory.addProperty(propertyQName, new NoopPropertyHandler(property, propertyType));

                  JBossXmlGroupWildcard groupWildcard = ((ClassInfo) propertyType)
                        .getUnderlyingAnnotation(JBossXmlGroupWildcard.class);

                  if (groupWildcard != null)
                  {
                     ChildWildcardHandler groupWildcardHandler;
                     if (groupWildcard.wrapper() != Object.class)
                     {
                        BeanInfo wrapperInfo = JBossXBBuilder.configuration.getBeanInfo(groupWildcard.wrapper());
                        groupWildcardHandler = new ChildWildcardHandler(property, wrapperInfo, groupWildcard.property());
                     }
                     else
                        groupWildcardHandler = new ChildWildcardHandler(property);

                     WildcardBinding wildcard = new WildcardBinding(schemaBinding);
                     if (groupWildcard.lax())
                        wildcard.setProcessContents((short) 3); // Lax
                     else
                        wildcard.setProcessContents((short) 1); // Strict

                     particleBinding = new ParticleBinding(wildcard);
                     particleBinding.setMinOccurs(0);
                     particleBinding.setMaxOccurs(1);
                     childModel.addParticle(particleBinding);

                     elementTypeBinding.getWildcard().setWildcardHandler(groupWildcardHandler);
                  }
               }
            }
         }
         else
         {
            XBValueAdapter valueAdapter = null;
            XmlJavaTypeAdapter xmlTypeAdapter = property.getUnderlyingAnnotation(XmlJavaTypeAdapter.class);
            if (xmlTypeAdapter != null)
            {
               valueAdapter = new XBValueAdapter(xmlTypeAdapter.value(), propertyType.getTypeInfoFactory());
               localPropertyType = valueAdapter.getAdaptedTypeInfo();
            }

            ModelGroupBinding targetGroup = localModel;
            boolean isCol = false;
            boolean isMap = false;
            AbstractPropertyHandler propertyHandler = null;

            // a collection may be bound as a value of a complex type
            // and this is checked with the XmlType annotation
            if (propertyType.isCollection() && ((ClassInfo) propertyType).getUnderlyingAnnotation(XmlType.class) == null)
            {
               isCol = true;
               propertyHandler = new CollectionPropertyHandler(property, propertyType);
               // here we get the comp type based on the non-overriden property type...
               // which feels like a weak point
               TypeInfo typeArg = ((ClassInfo)property.getType()).getComponentType();

               if (typeArg != null && ((ClassInfo)typeArg).getUnderlyingAnnotation(JBossXmlModelGroup.class) == null)
               {// it may be a model group in which case we don't want to change the type
                  // TODO yes, this is another hack with collections
                  JBossXmlChild xmlChild = ((ClassInfo) propertyType).getUnderlyingAnnotation(JBossXmlChild.class);
                  if (xmlChild == null && localPropertyType.equals(propertyType))
                  { // the localPropertyType was not overriden previously so use the collection parameter type
                     localPropertyType = typeArg;
                  }
               }
            }
            // TODO this shouldn't be here (because localPropertyType should specify an item?)
            // this is to support the Descriptions.class -> DescriptionsImpl.class
            else if (localPropertyType.isCollection()
                  && ((ClassInfo) localPropertyType).getUnderlyingAnnotation(XmlType.class) == null)
            {
               if (valueAdapter != null)
               {
                  propertyHandler = new PropertyHandler(property, localPropertyType);
               }
               else
               {
                  propertyHandler = new CollectionPropertyHandler(property, localPropertyType);
               }
               isCol = true;
               localPropertyType = ((ClassInfo)localPropertyType).getComponentType();
            }
            else if (localPropertyType.isMap())
            {
               TypeBinding wrapperType = null;
               if(elements.length > 1)
               {
                  wrapperType = resolveTypeBinding(localPropertyType);
                  ElementBinding elementBinding = createElementBinding(localPropertyType, wrapperType, propertyQName, false);
                  elementBinding.setNillable(nillable);
                  elementBinding.setValueAdapter(valueAdapter);

                  // Bind it to the model
                  ParticleBinding particle = new ParticleBinding(elementBinding, 0, 1, isCol);
                  if (required == false)
                     particle.setMinOccurs(0);

                  targetGroup.addParticle(particle);
                  targetGroup = (ModelGroupBinding) wrapperType.getParticle().getTerm();
               }
              
               QName boundQName = bindMapProperty(property, (ClassInfo) localPropertyType, propertyQName, targetGroup);
               if(boundQName != null)
               {
                  if(wrapperType != null)
                  {
                     BeanAdapterFactory wrapperBeanFactory = ((BeanHandler)wrapperType.getHandler()).getBeanAdapterFactory();
                     Map<QName, AbstractPropertyHandler> properties = wrapperBeanFactory.getProperties();
                     if(!properties.containsKey(boundQName))
                     {
                        propertyHandler = new MapPropertyHandler(JBossXBBuilder.configuration, property, localPropertyType, true);
                        wrapperBeanFactory.addProperty(boundQName, propertyHandler);
                     }
                     propertyHandler = new PropertyHandler(property, localPropertyType);
                  }
                  else
                  {
                     propertyQName = boundQName;
                     propertyHandler = new MapPropertyHandler(JBossXBBuilder.configuration, property, localPropertyType, false);
                  }
                  isMap = true;
               }
               else
                  propertyHandler = new PropertyHandler(property, localPropertyType);
            }
            else
            {
               propertyHandler = new PropertyHandler(property, localPropertyType);
            }

            ParticleBinding particle;
            // DOM elements are going to be treated as unresolved
            // however having the property registered
            if (!isMap && !Element.class.getName().equals(propertyType.getName()))
            {              
               TypeBinding elementTypeBinding = resolveTypeBinding(localPropertyType);
               ElementBinding elementBinding = createElementBinding(localPropertyType, elementTypeBinding, propertyQName, false);
               elementBinding.setNillable(nillable);
               elementBinding.setValueAdapter(valueAdapter);

               // Bind it to the model
               particle = new ParticleBinding(elementBinding, 1, 1, isCol);
               if (required == false)
                  particle.setMinOccurs(0);
View Full Code Here

      seq.setHandler(BuilderParticleHandler.INSTANCE);
      ParticleBinding particle = new ParticleBinding(seq);
      wrapperType.setParticle(particle);
      wrapperType.setHandler(new DefaultElementHandler());

      ElementBinding wrapperElement = createElementBinding(propertyType, wrapperType, wrapperQName, false);
      wrapperElement.setNillable(wrapperNillable);
      wrapperElement.setSkip(Boolean.TRUE);
      particle = new ParticleBinding(wrapperElement, 1, 1, false);
      parentModel.addParticle(particle);
      return seq;
   }
View Full Code Here

            entryType = new TypeBinding();
            entryType.setSchemaBinding(schemaBinding);
            entryType.setHandler(entryHandler);

            entryTypeInfo = JBossXBBuilder.configuration.getTypeInfo(DefaultMapEntry.class);                    
            ElementBinding entryElementBinding = createElementBinding(entryTypeInfo, entryType, entryName, false);
            ParticleBinding entryParticle = new ParticleBinding(entryElementBinding, 0, -1, true);
            targetGroup.addParticle(entryParticle);
              
            propertyQName = entryName;
              
            if(keyAttribute != null)
            {
               TypeBinding attributeType = resolveTypeBinding(keyType);
               AttributeHandler attributeHandler = new PropertyHandler(entryInfo.getProperty("key"), keyType);
               String attrNs = keyAttribute.namespace();
               if(JBossXmlConstants.DEFAULT.equals(attrNs))
                  attrNs = defaultNamespace;
               AttributeBinding keyBinding = new AttributeBinding(schemaBinding, new QName(attrNs, keyAttribute.name()), attributeType, attributeHandler);
               keyBinding.setRequired(true);
               entryType.addAttribute(keyBinding);
            }

            if(valueAttribute != null)
            {
               TypeBinding attributeType = resolveTypeBinding(valueType);
               AttributeHandler attributeHandler = new PropertyHandler(entryInfo.getProperty("value"), valueType);
               String valueNs = valueAttribute.namespace();
               if(JBossXmlConstants.DEFAULT.equals(valueNs))
                  valueNs = defaultNamespace;
               AttributeBinding valueBinding = new AttributeBinding(schemaBinding, new QName(valueNs, valueAttribute.name()), attributeType, attributeHandler);
               valueBinding.setRequired(true);
               entryType.addAttribute(valueBinding);
            }
            else if(valueElement == null)
            {
               CharactersHandler charactersHandler = new ValueHandler(entryInfo.getProperty("value"), valueType);
               entryType.setSimpleType(charactersHandler);
            }
         }
        
         SequenceBinding keyValueSequence = null;
         if(keyElement != null)
         {
            keyValueSequence = new SequenceBinding(schemaBinding);                    
            if(entryType == null)
            {
               keyValueSequence.setSkip(Boolean.FALSE);
               keyValueSequence.setQName(propertyQName);
               schemaBinding.addGroup(keyValueSequence.getQName(), keyValueSequence);
               ParticleBinding keyValueParticle = new ParticleBinding(keyValueSequence, 0, -1, true);
               targetGroup.addParticle(keyValueParticle);
               keyValueSequence.setHandler(entryHandler);
            }
            else
            {
               ParticleBinding keyValueParticle = new ParticleBinding(keyValueSequence, 1, 1, false);
               entryType.setParticle(keyValueParticle);
            }
           
            // key element
            TypeBinding keyTypeBinding = resolveTypeBinding(keyType);                 
            String keyNs = keyElement.namespace();
            if(JBossXmlConstants.DEFAULT.equals(keyNs))
               keyNs = defaultNamespace;                 
            ElementBinding keyElementBinding = createElementBinding(keyType, keyTypeBinding, new QName(keyNs, keyElement.name()), false);
            ParticleBinding particle = new ParticleBinding(keyElementBinding, 1, 1, false);
            keyValueSequence.addParticle(particle);
            PropertyHandler keyHandler = new PropertyHandler(entryInfo.getProperty("key"), keyType);
            entryAdapterFactory.addProperty(keyElementBinding.getQName(), keyHandler);
         }
        
         if(valueElement != null)
         {
            TypeBinding valueTypeBinding = resolveTypeBinding(valueType);                 
            String valueNs = valueElement.namespace();
            if(JBossXmlConstants.DEFAULT.equals(valueNs))
               valueNs = defaultNamespace;                 
            ElementBinding valueElementBinding = createElementBinding(valueType, valueTypeBinding, new QName(valueNs, valueElement.name()), false);
            ParticleBinding particle = new ParticleBinding(valueElementBinding, 1, 1, false);
            keyValueSequence.addParticle(particle);
            PropertyHandler valueHandler = new PropertyHandler(entryInfo.getProperty("value"), valueType);
            entryAdapterFactory.addProperty(valueElementBinding.getQName(), valueHandler);
         }

         // TODO: need to verify correct binding before proceeding
         boundQName = propertyQName;
      }
      else if(entryElement != null && !JBossXmlMapEntry.DEFAULT.class.equals(entryElement.type()))
      {
         if(!JBossXmlConstants.DEFAULT.equals(entryElement.name()))
         {
            String ns = entryElement.namespace();
            if(JBossXmlConstants.DEFAULT.equals(ns))
               ns = propertyQName.getNamespaceURI();
            propertyQName = new QName(ns, entryElement.name());
         }

         TypeInfo entryTypeInfo = JBossXBBuilder.configuration.getTypeInfo(entryElement.type());
         ElementBinding entryElementBinding = createElementBinding(entryTypeInfo, propertyQName.getLocalPart(), false);
         ParticleBinding entryParticle = new ParticleBinding(entryElementBinding, 0, -1, true);
         targetGroup.addParticle(entryParticle);
         boundQName = propertyQName;
      }
      return boundQName;
View Full Code Here

      SchemaBinding schema = XsdBinder.bind(new StringReader(XSD), null);

      // get bound components
      TypeBinding type = schema.getType(new QName("http://org.jboss.ws/jbws434/types", "ArrayOfAny"));
      WildcardBinding wildcard = type.getWildcard();
      ElementBinding e = schema.getElement(new QName("http://org.jboss.ws/jbws434/types", "e"));

      // adjust binding
      ParticleHandler unresolvedElementHandler;
      CharactersHandler unresolvedCharactersHandler;
      if(genericElement)
      {
         unresolvedElementHandler = new GenericElementHandler();
         unresolvedCharactersHandler = new GenericCharactersHandler();
      }
      else
      {
         unresolvedElementHandler = new DomElementHandler();
         unresolvedCharactersHandler = new DomCharactersHandler();
      }

      wildcard.setUnresolvedElementHandler(unresolvedElementHandler);
      wildcard.setUnresolvedCharactersHandler(unresolvedCharactersHandler);
      PropertyMetaData property = new PropertyMetaData();
      property.setName("_any");
      wildcard.setPropertyMetaData(property);

      ClassMetaData clsBinding = new ClassMetaData();
      clsBinding.setImpl(ArrayOfAny.class.getName());
      e.setClassMetaData(clsBinding);

      // to fail the resolution quickly
      schema.setSchemaResolver(new SchemaBindingResolver()
      {
         public String getBaseURI()
View Full Code Here

      }
   }
  
   public void testAttributesInSchema()
   {
      ElementBinding eBinding = SCHEMA.getElement(new QName(NS_1, "e"));
      TypeBinding eType = eBinding.getType();
      Collection attributes = eType.getAttributes();
      assertNotNull(attributes);
      assertEquals(3, attributes.size());
      assertNotNull(eType.getAttribute(UNQUALIFIED_QNAME));
      assertNotNull(eType.getAttribute(MATE_QNAME));
View Full Code Here

         TermBeforeSetParentCallback callback = new TermBeforeSetParentCallback()
         {
            public Object beforeSetParent(Object o, UnmarshallingContext ctx)
            {
               ElementBinding e = (ElementBinding) ctx.getParticle().getTerm();
               Class propType = ctx.resolvePropertyType();

               String localPart = e.getQName().getLocalPart();
               if("image".equals(localPart) ||
                   "sig".equals(localPart) ||
                   "imageWithContentType".equals(localPart) ||
                   "octets".equals(localPart) ||
                   "jpeg".equals(localPart))
               {
                  assertEquals("expected " + byte[].class + " for " + localPart, byte[].class, propType);
               }
               else if("awtImage".equals(localPart))
               {
                  assertEquals(java.awt.Image.class, propType);
               }
               else if("string".equals(localPart))
               {
                  assertEquals(String.class, propType);
               }
               else if("source".equals(localPart))
               {
                  assertEquals(javax.xml.transform.Source.class, propType);
               }
               else
               {
                  fail("unexpected element: " + e.getQName());
               }
               return o;
            }
         };

         ElementBinding e = SCHEMA.getElement(new javax.xml.namespace.QName("http://www.jboss.org/xml/test/xop", "e"));
         SequenceBinding seq = (SequenceBinding) e.getType().getParticle().getTerm();
         for(Iterator i = seq.getParticles().iterator(); i.hasNext();)
         {
            ParticleBinding particle = (ParticleBinding) i.next();
            ElementBinding child = (ElementBinding) particle.getTerm();
            if(! "xopContent".equals( child.getQName().getLocalPart()))
               child.setBeforeSetParentCallback(callback);
         }

         TermBeforeSetParentCallback xmimeBase64Callback = new TermBeforeSetParentCallback()
         {
            public Object beforeSetParent(Object o, UnmarshallingContext ctx)
            {
               ElementBinding e = (ElementBinding) ctx.getParticle().getTerm();
               Class propType = ctx.resolvePropertyType();

               assertNotNull("Failed to resolve property type for "+e.getQName(), propType);

               String localPart = e.getQName().getLocalPart();
               if("xopContent".equals(localPart))
               {
                  assertEquals(String.class, propType);
                  if(propType.equals(String.class))
                  {
View Full Code Here

               {
                  ParticleBinding particle = ctx.getParticleBinding();
                  assertNotNull(particle);
                  TermBinding term = particle.getTerm();
                  assertTrue(term.isElement());
                  ElementBinding element = (ElementBinding) term;
                  String localPart = element.getQName().getLocalPart();
                  assertTrue("stringType".endsWith(localPart) || "item".equals(localPart));
                  TypeBinding type = element.getType();
                  assertEquals(Constants.QNAME_STRING, type.getQName());
                 
                  o = ((GlobalElement.StringType)o).data;
               }
               return o;
            }
         });        
         stringType.setBeforeSetParentCallback(new TermBeforeSetParentCallback()
         {
            public Object beforeSetParent(Object o, UnmarshallingContext ctx)
            {
               ParticleBinding particle = ctx.getParticle();
               assertNotNull(particle);
               assertTrue(particle.getTerm().isElement());
               ElementBinding element = (ElementBinding) particle.getTerm();
              
               String eName = element.getQName().getLocalPart();
               if(eName.equals("stringType"))
               {
                  assertTrue(ctx.getParentValue() instanceof GlobalElement);
                  TermBinding parentTerm = ctx.getParentParticle().getTerm();
                  assertTrue(parentTerm.isElement());
                  assertEquals("global", ((ElementBinding)parentTerm).getQName().getLocalPart());

                  assertEquals("stringType", ctx.resolvePropertyName());
                  assertEquals(StringType.class, ctx.resolvePropertyType());
               }
               else if(eName.equals("item"))
               {
                  assertTrue(ctx.getParentValue() instanceof GlobalElement.Sequence);
                  TermBinding parentTerm = ctx.getParentParticle().getTerm();
                  assertTrue(parentTerm instanceof SequenceBinding);
                 
                  assertEquals("item", ctx.resolvePropertyName());
                  assertEquals(StringType.class, ctx.resolvePropertyType());
               }
               else
               {
                  fail("Expected stringType or item but got " + eName);
               }
               return o == null ? null : new GlobalElement.StringType((String)o);
            }
         });
        
         // have to override string type's handlers
         ElementBinding global = SCHEMA.getElement(new QName("http://jboss.org/ns/test", "global"));
         SequenceBinding sequence = (SequenceBinding) global.getType().getParticle().getTerm();
         Iterator iter = sequence.getParticles().iterator();
         iter.next(); // stringType element
         ElementBinding stringElement = (ElementBinding) ((ParticleBinding)iter.next()).getTerm();
         stringElement.setBeforeMarshallingCallback(new TermBeforeMarshallingCallback()
         {
            public Object beforeMarshalling(Object o, MarshallingContext ctx)
            {
               ParticleBinding particle = ctx.getParticleBinding();
               assertNotNull(particle);
               assertTrue(particle.getTerm().isElement());
               ElementBinding element = (ElementBinding) particle.getTerm();
               assertEquals(new QName("http://jboss.org/ns/test", "stringElement"), element.getQName());
              
               TypeBinding type = element.getType();
               assertEquals(Constants.QNAME_STRING, type.getQName());
              
               return o == null ? null : (String)o;
            }
         });        
         stringElement.setBeforeSetParentCallback(new TermBeforeSetParentCallback()
         {
            public Object beforeSetParent(Object o, UnmarshallingContext ctx)
            {
               ParticleBinding particle = ctx.getParticle();
               assertNotNull(particle);
               assertTrue(particle.getTerm().isElement());
               ElementBinding element = (ElementBinding) particle.getTerm();
               assertEquals(new QName("http://jboss.org/ns/test", "stringElement"), element.getQName());
              
               assertTrue(ctx.getParentValue() instanceof GlobalElement);
               TermBinding parentTerm = ctx.getParentParticle().getTerm();
               assertTrue(parentTerm.isElement());
               assertEquals("global", ((ElementBinding)parentTerm).getQName().getLocalPart());
View Full Code Here

      mapType.addElement(QNAME_ENTRY3, entry3Type, 0, true);
      mapType.addElement(QNAME_ENTRY4, entry4Type, 0, true);

      // sequence as entry
      SequenceBinding entrySeq = new SequenceBinding(schema);
      ElementBinding keyElement = new ElementBinding(schema, new QName(NS, "key"), new TypeBinding());
      entrySeq.addParticle(new ParticleBinding(keyElement));
      ElementBinding valueElement = new ElementBinding(schema, new QName(NS, "value"), new TypeBinding());
      entrySeq.addParticle(new ParticleBinding(valueElement));

      ParticleBinding entryParticle = new ParticleBinding(entrySeq);
      entryParticle.setMinOccurs(0);
      entryParticle.setMaxOccursUnbounded(true);
      mapType.addParticle(entryParticle);

      mapType.addElement(QNAME_SUBMAP, mapType, 0, true);
      schema.addElement(QNAME_MAP, mapType);

      // Handler implementations
      mapType.setHandler(new DefaultElementHandler()
      {
         public Object startElement(Object parent, QName name, ElementBinding element)
         {
            return new HashMap();
         }
      }
      );

      ElementInterceptor addMapEntryInterceptor = new DefaultElementInterceptor()
      {
         public void add(Object parent, Object child, QName name)
         {
            Map map = (Map)parent;
            Object[] arr = (Object[])child;
            map.put(arr[0], arr[1]);
         }
      };
      mapType.pushInterceptor(QNAME_ENTRY1, addMapEntryInterceptor);
      mapType.pushInterceptor(QNAME_ENTRY2, addMapEntryInterceptor);
      mapType.pushInterceptor(QNAME_ENTRY3, addMapEntryInterceptor);
      mapType.pushInterceptor(QNAME_ENTRY4, addMapEntryInterceptor);
      mapType.pushInterceptor(QNAME_SUBMAP, addMapEntryInterceptor);

      entry1Type.setHandler(new DefaultElementHandler()
      {
         public Object startElement(Object parent, QName name, ElementBinding element)
         {
            return new Object[2];
         }
      }
      );
      entry1Type.pushInterceptor(QNAME_KEY1, new DefaultElementInterceptor()
      {
         public void characters(Object o, QName name, TypeBinding type, NamespaceContext nsCtx, String text)
         {
            Object[] arr = (Object[])o;
            arr[0] = text;
         }
      }
      );
      entry1Type.pushInterceptor(QNAME_VALUE1, new DefaultElementInterceptor()
      {
         public void characters(Object o, QName name, TypeBinding type, NamespaceContext nsCtx, String text)
         {
            Object[] arr = (Object[])o;
            arr[1] = text;
         }
      }
      );

      entry2Type.setHandler(new DefaultElementHandler()
      {
         public Object startElement(Object parent, QName name, ElementBinding element)
         {
            return new Object[2];
         }

         public void attributes(Object o,
                                QName elementName,
                                ElementBinding element,
                                Attributes attrs,
                                NamespaceContext nsCtx)
         {
            Object[] arr = (Object[])o;
            for(int i = 0; i < attrs.getLength(); ++i)
            {
               String lName = attrs.getLocalName(i);
               if("key2".equals(lName))
               {
                  arr[0] = attrs.getValue(i);
               }
            }
         }
      }
      );
      entry2Type.pushInterceptor(QNAME_VALUE2, new DefaultElementInterceptor()
      {
         public void characters(Object o, QName name, TypeBinding type, NamespaceContext nsCtx, String text)
         {
            Object[] arr = (Object[])o;
            arr[1] = text;
         }
      }
      );

      entry3Type.setHandler(new DefaultElementHandler()
      {
         public Object startElement(Object parent, QName name, ElementBinding element)
         {
            return new Object[2];
         }

         public void attributes(Object o,
                                QName elementName,
                                ElementBinding element,
                                Attributes attrs,
                                NamespaceContext nsCtx)
         {
            Object[] arr = (Object[])o;
            for(int i = 0; i < attrs.getLength(); ++i)
            {
               String lName = attrs.getLocalName(i);
               if("key3".equals(lName))
               {
                  arr[0] = attrs.getValue(i);
               }
               else if("value3".equals(lName))
               {
                  arr[1] = attrs.getValue(i);
               }
            }
         }
      }
      );

      entry4Type.setHandler(new DefaultElementHandler()
      {
         public Object startElement(Object parent, QName name, ElementBinding element)
         {
            return new Object[2];
         }

         public void attributes(Object o,
                                QName elementName,
                                ElementBinding element,
                                Attributes attrs,
                                NamespaceContext nsCtx)
         {
            Object[] arr = (Object[])o;
            for(int i = 0; i < attrs.getLength(); ++i)
            {
               String lName = attrs.getLocalName(i);
               if("key4".equals(lName))
               {
                  arr[0] = attrs.getValue(i);
               }
            }
         }

      }
      );
      entry4Type.setSimpleType(new CharactersHandler()
      {
         public void setValue(QName qName, ElementBinding element, Object owner, Object value)
         {
            Object[] arr = (Object[])owner;
            arr[1] = value;
         }
      }
      );

      mapType.pushInterceptor(QNAME_SUBMAP, new DefaultElementInterceptor()
      {
         public Object startElement(Object parent, QName name, TypeBinding type)
         {
            return new Object[2];
         }

         public void attributes(Object o,
                                QName elementName,
                                TypeBinding type,
                                Attributes attrs,
                                NamespaceContext nsCtx)
         {
            Object[] arr = (Object[])o;
            for(int i = 0; i < attrs.getLength(); ++i)
            {
               String lName = attrs.getLocalName(i);
               if("key".equals(lName))
               {
                  arr[0] = attrs.getValue(i);
               }
            }
         }

         public void add(Object parent, Object child, QName qName)
         {
            Object[] arr = (Object[])parent;
            Map submap = (Map)child;
            arr[1] = submap;
         }
      }
      );

      // sequence entry
      entrySeq.setHandler(new ParticleHandler()
      {
         public Object startParticle(Object parent,
                                     QName elementName,
                                     ParticleBinding particle,
                                     Attributes attrs,
                                     NamespaceContext nsCtx)
         {
            return new Object[2];
         }

         public Object endParticle(Object o, QName elementName, ParticleBinding particle)
         {
            return o;
         }

         public void setParent(Object parent,
                               Object o,
                               QName elementName,
                               ParticleBinding particle,
                               ParticleBinding parentParticle)
         {
            Map map = (Map)parent;
            Object[] arr = (Object[])o;
            map.put(arr[0], arr[1]);
         }
      }
      );

      keyElement.pushInterceptor(new DefaultElementInterceptor()
      {
         public void characters(Object o, QName name, TypeBinding type, NamespaceContext nsCtx, String text)
         {
            Object[] arr = (Object[])o;
            arr[0] = text;
         }
      });

      valueElement.pushInterceptor(new DefaultElementInterceptor()
      {
         public void characters(Object o, QName name, TypeBinding type, NamespaceContext nsCtx, String text)
         {
            Object[] arr = (Object[])o;
            arr[1] = text;
View Full Code Here

TOP

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

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.