Package org.jboss.xb.binding

Examples of org.jboss.xb.binding.JBossXBRuntimeException


            if(propertyMetaData != null)
            {
               String msg = "A term can be bound either as a property or as a map" +
                  " entry but not both: " +
                  (term.isModelGroup() ? term.toString() : ((ElementBinding)term).getQName().toString());
               throw new JBossXBRuntimeException(msg);
            }

            if (trace)
            {
               String msg = term.isWildcard() || term.isModelGroup() ? term.toString() : "element name=" +
                  ((ElementBinding)term).getQName();

               msg += " is bound to a map entry: impl=" +
                  mapEntryMetaData.getImpl() +
                  ", getKeyMethod=" +
                  mapEntryMetaData.getGetKeyMethod() +
                  ", setKeyMethod=" +
                  mapEntryMetaData.getSetKeyMethod() +
                  ", getValueMethod=" +
                  mapEntryMetaData.getGetValueMethod() +
                  ", setValueMethod=" +
                  mapEntryMetaData.getSetValueMethod() +
                  ", valueType=" +
                  mapEntryMetaData.getValueType() +
                  ", nonNullValue=" + mapEntryMetaData.isNonNullValue();
               log.trace(msg);
            }

            if(classMetaData != null)
            {
               String msg = "Invalid customization: both jbxb:class and jbxb:mapEntry are specified for term " +
                  (term.isWildcard() || term.isModelGroup() ? term.toString() : ((ElementBinding)term).getQName().toString());
               throw new JBossXBRuntimeException(msg);
            }
            term.setMapEntryMetaData(mapEntryMetaData);
            skip = Boolean.FALSE;
         }
View Full Code Here


         }
      }

      if(elementBinding == null)
      {
         throw new JBossXBRuntimeException("Failed to endElement " + qName + ": binding not found");
      }

      if(!elementBinding.getQName().equals(endName))
      {
         throw new JBossXBRuntimeException("Failed to end element " +
            new QName(namespaceURI, localName) +
            ": element on the stack is " + elementBinding.getQName()
         );
      }
View Full Code Here

            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);
View Full Code Here

         {
            throw e;
         }
         catch(RuntimeException e)
         {
            throw new JBossXBRuntimeException("Container failed to create an instance for " +
               elementName +
               ": " + e.getMessage(), e
            );
         }
      }
View Full Code Here

            {
               particle = schemaBinding.getElementParticle(startName);
            }
            else
            {
               throw new JBossXBRuntimeException("Failed to resolve schema nsURI=" + namespaceURI + " location=" + schemaLocation);
            }
         }
         else
         {
            throw new JBossXBRuntimeException("Neither schema binding nor schema binding resolver is available!");
         }
      }
      else
      {
         while(!stack.isEmpty())
         {
            item = stack.peek();
            if(item.cursor == null)
            {
               TermBinding term = item.particle.getTerm();
               ElementBinding element = (ElementBinding)term;
               if(item.ended)
               {
                  if(element.getQName().equals(startName))
                  {
                     particle = item.particle;
                     repeated = true;
                     item.reset();

                     if(!particle.isRepeatable())
                     {
                        endRepeatableParent(startName);
                     }
                  }
                  else
                  {
                     pop();                    
                     if(item.particle.isRepeatable())
                     {
                        endRepeatableParticle(item.particle);
                     }
                     continue;
                  }
               }
               else
               {
                  ParticleBinding typeParticle = element.getType().getParticle();
                  ModelGroupBinding modelGroup = typeParticle == null ?
                     null :
                     (ModelGroupBinding)typeParticle.getTerm();
                  if(modelGroup == null)
                  {
                     if(startName.equals(Constants.QNAME_XOP_INCLUDE))
                     {
                        TypeBinding anyUriType = schema.getType(Constants.QNAME_ANYURI);
                        if(anyUriType == null)
                        {
                           log.warn("Type " + Constants.QNAME_ANYURI + " not bound.");
                        }

                        TypeBinding xopIncludeType = new TypeBinding(new QName(Constants.NS_XOP_INCLUDE, "Include"));
                        xopIncludeType.setSchemaBinding(schema);
                        xopIncludeType.addAttribute(new QName("href"), anyUriType, DefaultHandlers.ATTRIBUTE_HANDLER);
                        xopIncludeType.setHandler(new XOPIncludeHandler(element.getType(), schema.getXopUnmarshaller()));

                        ElementBinding xopInclude = new ElementBinding(schema, Constants.QNAME_XOP_INCLUDE, xopIncludeType);

                        particle = new ParticleBinding(xopInclude);

                        ElementBinding parentElement = (ElementBinding) item.particle.getTerm();
                        parentElement.setXopUnmarshaller(schema.getXopUnmarshaller());

                        flushIgnorableCharacters();
                        item.handler = DefaultHandlers.XOP_HANDLER;
                        item.ignoreCharacters = true;
                        item.o = item.handler.startParticle(stack.peek().o, startName, stack.peek().particle, null, nsRegistry);
                        break;
                     }

                     QName typeName = element.getType().getQName();
                     throw new JBossXBRuntimeException((typeName == null ? "Anonymous" : typeName.toString()) +
                        " type of element " +
                        element.getQName() +
                        " should be complex and contain " + startName + " as a child element."
                     );
                  }

                  cursor = modelGroup.newCursor(typeParticle);
                  List newCursors = cursor.startElement(startName, atts);
                  if(newCursors.isEmpty())
                  {
                     throw new JBossXBRuntimeException(startName +
                        " not found as a child of " +
                        ((ElementBinding)term).getQName()
                     );
                  }
                  else
                  {
                     flushIgnorableCharacters();

                     Object o = item.o;
                     // push all except the last one
                     for(int i = newCursors.size() - 1; i >= 0; --i)
                     {
                        cursor = (ModelGroupBinding.Cursor)newCursors.get(i);

                        ParticleBinding modelGroupParticle = cursor.getParticle();
                        if(modelGroupParticle.isRepeatable())
                        {
                           startRepeatableParticle(startName, modelGroupParticle);
                        }

                        handler = getHandler(modelGroupParticle);
                        o = handler.startParticle(o, startName, modelGroupParticle, atts, nsRegistry);
                        push(cursor, o, handler);
                     }
                     particle = cursor.getCurrentParticle();
                  }
               }
               break;
            }
            else
            {
               cursor = item.cursor;
               if(cursor == null)
               {
                  throw new JBossXBRuntimeException("No cursor for " + startName);
               }

               // todo review
               if(!item.ended && cursor.isPositioned() && cursor.getParticle().getTerm() instanceof ChoiceBinding)
               {
                  endParticle(item, startName, 1);
                  if(!item.particle.isRepeatable()) // this is for repeatable choices that should stay on the stack
                  {
                     pop();
                  }
                  continue;
               }

               //int prevOccurence = cursor.getOccurence();
               ParticleBinding prevParticle = cursor.isPositioned() ? cursor.getCurrentParticle() : null;
               List newCursors = cursor.startElement(startName, atts);
               if(newCursors.isEmpty())
               {
                  if(!item.ended) // this is for choices
                  {
                     endParticle(item, startName, 1);
                  }
                  pop();
               }
               else
               {
                  if(item.ended) // for repeatable choices
                  {
                     if(!item.particle.isRepeatable())
                     {
                        throw new JBossXBRuntimeException("The particle expected to be repeatable but it's not: " + item.particle.getTerm());
                     }
                     item.reset();
                    
                     handler = getHandler(item.particle);
                     item.o = handler.startParticle(stack.peek(1).o, startName, item.particle, atts, nsRegistry);
                  }
                 
                  ParticleBinding curParticle = cursor.getCurrentParticle();
                  if(curParticle != prevParticle)
                  {
                     if(prevParticle != null && prevParticle.isRepeatable() && prevParticle.getTerm().isModelGroup())
                     {
                        endRepeatableParticle(prevParticle);
                     }

                     if(newCursors.size() > 1 && curParticle.isRepeatable())
                     {
                        startRepeatableParticle(startName, curParticle);
                     }
                  }
                  else
                  {
                     repeatedParticle = true;
                  }

                  // push all except the last one
                  Object o = item.o;
                  for(int i = newCursors.size() - 2; i >= 0; --i)
                  {
                     cursor = (ModelGroupBinding.Cursor)newCursors.get(i);

                     ParticleBinding modelGroupParticle = cursor.getParticle();
                     handler = getHandler(modelGroupParticle);
                     o = handler.startParticle(o, startName, modelGroupParticle, atts, nsRegistry);
                     push(cursor, o, handler);
                  }
                  cursor = (ModelGroupBinding.Cursor)newCursors.get(0);
                  particle = cursor.getCurrentParticle();
                  break;
               }
            }
         }
      }

      Object o = null;
      if(particle != null)
      {
         Object parent = stack.isEmpty() ? null :
            (repeated ? stack.peek(1).o : stack.peek().o);
         if(particle.getTerm() instanceof WildcardBinding)
         {
            /*
            WildcardBinding wildcard = (WildcardBinding)particle.getTerm();
            ElementBinding element = wildcard.getElement(startName, atts);
            */
            ElementBinding element = cursor.getElement();
            if(element == null)
            {
               throw new JBossXBRuntimeException("Failed to resolve element " +
                  startName + " for wildcard."
               );
            }

            if(!repeatedParticle && particle.isRepeatable())
            {
               startRepeatableParticle(startName, particle);
            }
            particle = new ParticleBinding(element/*, particle.getMinOccurs(), particle.getMaxOccurs(), particle.getMaxOccursUnbounded()*/);
         }

         ElementBinding element = (ElementBinding)particle.getTerm();

         // todo xsi:type support should be implemented in a better way
         String xsiType = atts.getValue("xsi:type");
         if(xsiType != null)
         {
            if(trace)
            {
               log.trace(element.getQName() + " uses xsi:type " + xsiType);
            }

            String xsiTypePrefix;
            String xsiTypeLocal;
            int colon = xsiType.indexOf(':');
            if(colon == -1)
            {
               xsiTypePrefix = "";
               xsiTypeLocal = xsiType;
            }
            else
            {
               xsiTypePrefix = xsiType.substring(0, colon);
               xsiTypeLocal = xsiType.substring(colon + 1);
            }

            String xsiTypeNs = nsRegistry.getNamespaceURI(xsiTypePrefix);
            QName xsiTypeQName = new QName(xsiTypeNs, xsiTypeLocal);

            TypeBinding xsiTypeBinding = schemaBinding.getType(xsiTypeQName);
            if(xsiTypeBinding == null)
            {
               throw new JBossXBRuntimeException("Type binding not found for type " +
                  xsiTypeQName +
                  " specified with xsi:type for element " + startName
               );
            }

            element = new ElementBinding(schemaBinding, startName, xsiTypeBinding);
            particle =
               new ParticleBinding(element,
                  particle.getMinOccurs(),
                  particle.getMaxOccurs(),
                  particle.getMaxOccursUnbounded()
               );
         }

         if(!repeated && particle.isRepeatable())
         {
            startRepeatableParticle(startName, particle);
         }

         TypeBinding type = element.getType();
         if(type == null)
         {
            throw new JBossXBRuntimeException("No type for element " + element);
         }

         handler = type.getHandler();
         if(handler == null)
         {
            handler = defParticleHandler;
         }

         List interceptors = element.getInterceptors();
         if(!interceptors.isEmpty())
         {
            if (repeated)
            {
               pop();
            }

            for (int i = 0; i < interceptors.size(); ++i)
            {
               ElementInterceptor interceptor = (ElementInterceptor) interceptors.get(i);
               parent = interceptor.startElement(parent, startName, type);
               push(startName, particle, parent, handler);
               interceptor.attributes(parent, startName, type, atts, nsRegistry);
            }

            if (repeated)
            {
               // to have correct endRepeatableParticle calls
               stack.push(item);
            }
         }

         String nil = atts.getValue("xsi:nil");
         if(nil == null || !("1".equals(nil) || "true".equals(nil)))
         {
            o = handler.startParticle(parent, startName, particle, atts, nsRegistry);
         }
         else
         {
            o = NIL;
         }
      }
      else
      {
         ElementBinding parentBinding = null;
         if(!stack.isEmpty())
         {
            ParticleBinding stackParticle = repeated ? stack.peek(1).particle : stack.peek().particle;
            if(stackParticle != null)
            {
               parentBinding = (ElementBinding)stackParticle.getTerm();
            }
         }

         if(parentBinding != null && parentBinding.getSchema() != null)
         {
            schemaBinding = parentBinding.getSchema();
         }

         String msg = "Element " +
            startName +
            " is not bound " +
            (parentBinding == null ? "as a global element." : "in type " + parentBinding.getType().getQName());
         if(schemaBinding != null && schemaBinding.isStrictSchema())
         {
            throw new JBossXBRuntimeException(msg);
         }
         else if(trace)
         {
            log.trace(msg);
         }
View Full Code Here

                  }
               }
               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(),
View Full Code Here

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

      while(true)
      {
         parentItem = stack.peek(parentPos);
         if(parentItem.cursor == null)
         {
            throw new JBossXBRuntimeException(
               "Failed to start " + startName +
               ": the element is not repeatable, repeatable parent expected to be a model group but got element " +
               ((ElementBinding)parentItem.particle.getTerm()).getQName()
            );
         }

         parentParticle = parentItem.particle;
         if(parentParticle.isRepeatable())
         {
            break;
         }

         endParticle(parentItem, startName, ++parentPos);
      }

      if(!parentParticle.isRepeatable())
      {
         StringBuffer msg = new StringBuffer();

         StackItem item = stack.peek();
         ParticleBinding currentParticle = item.particle;
         msg.append("Failed to start ").append(startName).append(": ")
            .append(currentParticle.getTerm())
            .append(" is not repeatable.")
            .append(" Its parent ")
            .append(parentParticle.getTerm())
            .append(" expected to be repeatable!")
            .append("\ncurrent stack: ");

         for(int i = stack.size() - 1; i >= 0; --i)
         {
            item = stack.peek(i);
            ParticleBinding particle = item.particle;
            TermBinding term = particle.getTerm();
            if(term.isModelGroup())
            {
               if(term instanceof SequenceBinding)
               {
                  msg.append("sequence");
               }
               else if(term instanceof ChoiceBinding)
               {
                  msg.append("choice");
               }
               else
               {
                  msg.append("all");
               }
            }
            else if(term.isWildcard())
            {
               msg.append("wildcard");
            }
            else
            {
               msg.append(((ElementBinding)term).getQName());
            }
            msg.append("\\");
         }

         throw new JBossXBRuntimeException(msg.toString());
      }

      // todo startName is wrong here
      endParticle(parentItem, startName, parentPos + 1);
View Full Code Here

   private void endParticle(StackItem item, QName qName, int parentStackPos)
   {
      if(item.ended)
      {
         throw new JBossXBRuntimeException(item.particle.getTerm() + " has already been ended.");
      }

      ParticleBinding modelGroupParticle = item.particle;
      ParticleHandler handler = item.handler;//getHandler(modelGroupParticle);
View Full Code Here

      {
         setValueMethod.invoke(parent, new Object[]{o});
      }
      catch(Exception e)
      {
         throw new JBossXBRuntimeException("Failed to invoke " +
            setValueMethodName +
            " on " +
            parent +
            " with parameter " +
            o +
View Full Code Here

TOP

Related Classes of org.jboss.xb.binding.JBossXBRuntimeException

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.