Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
     * JBoss, Home of Professional Open Source
     * Copyright 2005, JBoss Inc., and individual contributors as indicated
     * by the @authors tag. See the copyright.txt in the distribution for a
     * full listing of individual contributors.
     *
     * This is free software; you can redistribute it and/or modify it
     * under the terms of the GNU Lesser General Public License as
     * published by the Free Software Foundation; either version 2.1 of
    * the License, or (at your option) any later version.
    *
    * This software is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public
    * License along with this software; if not, write to the Free
    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
    */
  package org.jboss.xb.binding;
  
  import java.io.Reader;
  import java.io.Writer;
  import java.util.Arrays;
  import java.util.List;
  import  org.apache.xerces.xs.StringList;
  import  org.apache.xerces.xs.XSAttributeDeclaration;
  import  org.apache.xerces.xs.XSAttributeUse;
  import  org.apache.xerces.xs.XSComplexTypeDefinition;
  import  org.apache.xerces.xs.XSConstants;
  import  org.apache.xerces.xs.XSElementDeclaration;
  import  org.apache.xerces.xs.XSModel;
  import  org.apache.xerces.xs.XSModelGroup;
  import  org.apache.xerces.xs.XSNamedMap;
  import  org.apache.xerces.xs.XSObject;
  import  org.apache.xerces.xs.XSObjectList;
  import  org.apache.xerces.xs.XSParticle;
  import  org.apache.xerces.xs.XSSimpleTypeDefinition;
  import  org.apache.xerces.xs.XSTerm;
  import  org.apache.xerces.xs.XSTypeDefinition;
  import  org.apache.xerces.xs.XSWildcard;

Author(s):
Alexey Loubyansky
Version:
$Revision: 1958 $
  
  public class XercesXsMarshaller
     extends AbstractMarshaller
  {
     private Stack stack = new StackImpl();

   
ObjectModelProvider for this marshaller
  
  
     private Object root;

   
Whether NULL values should be ignored or marshalled as xsi:nil='1'
  
     private boolean supportNil = true;
  
     private QName rootTypeQName;
  
  
     private XSModel model;
  
     private boolean ignoreUnresolvedWildcard;
  
     private XSAttributeUse currentAttribute;
     private XSTypeDefinition currentElementType;
  
     private String simpleContentProperty = "value";
     
     private MarshallingContext ctx = new MarshallingContext()
     {
        private ContentHandler ch;
  
        public boolean isAttributeRequired()
        {
           if( == null)
           {
              throw new JBossXBRuntimeException("There is no current attribute!");
          }
          return .getRequired();
       }
 
       public boolean isTypeComplex()
       {
          if( == null)
          {
             throw new JBossXBRuntimeException("There is no current element!");
          }
          return .getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE;
       }
 
       public String getSimpleContentProperty()
       {
          return ;
       }
 
       public ContentHandler getContentHandler()
       {
          if( == null)
          {
              = new ContentHandlerAdaptor();
          }
          return ;
       }
 
       public NamespaceRegistry getNamespaceContext()
       {
          return ;
       }
    };
 
    {
       return ;
    }
 
    public void setSimpleContentProperty(String simpleContentProperty)
    {
       this. = simpleContentProperty;
    }
 
    public boolean isIgnoreUnresolvedWildcard()
    {
       return ;
    }
 
    public void setIgnoreUnresolvedWildcard(boolean ignoreUnresolvedWildcard)
    {
       this. = ignoreUnresolvedWildcard;
    }
 
    {
       return ;
    }
 
    public void setSchemaResolver(SchemaBindingResolver schemaResolver)
    {
       this. = schemaResolver;
    }
 
    public QName getRootTypeQName()
    {
       return ;
    }
 
    public void setRootTypeQName(QName rootTypeQName)
    {
       this. = rootTypeQName;
    }
 
    public boolean isSupportNil()
    {
       return ;
    }
 
    public void setSupportNil(boolean supportNil)
    {
       this. = supportNil;
    }

   
Adds an attribute to the top most elements. First, we check whether there is a namespace associated with the passed in prefix. If the prefix was not declared, an exception is thrown.

Parameters:
prefix the prefix of the attribute to be declared
localName local name of the attribute
type the type of the attribute
value the value of the attribute
 
    public void addAttribute(String prefixString localNameString typeString value)
    {
       // todo addAttribute(String prefix, String localName, String type, String value)
    }
 
    // AbstractMarshaller implementation
 
    public void marshal(Reader xsdReaderObjectModelProvider providerObject rootWriter writer)
    {
       XSModel model = Util.loadSchema(xsdReadernull);
       marshallInternal(providerrootmodelwriter);
    }
 
    public void marshal(String xsdURLObjectModelProvider providerObject rootWriter writerthrows IOException,
       SAXException
    {
       XSModel model = Util.loadSchema(xsdURL);
       marshallInternal(providerrootmodelwriter);
    }
 
    public void marshal(XSModel modelObjectModelProvider providerObject rootWriter writerthrows IOException,
       SAXException
    {
       marshallInternal(providerrootmodelwriter);
    }
 
    private void marshallInternal(ObjectModelProvider providerObject root, XSModel modelWriter writer)
       throws IOExceptionSAXException
    {
       if(model == null)
       {
          throw new JBossXBRuntimeException("XSModel is not available!");
       }
 
       this. = model;
       this. = provider instanceof GenericObjectModelProvider ?
          (GenericObjectModelProvider)provider : new DelegatingObjectModelProvider(provider);
 
       this. = root;
 
       .startDocument();
 
       if( != null)
       {
          if(.isEmpty())
          {
             throw new JBossXBRuntimeException("If type name (" +
                 +
                ") for the root element is specified then the name for the root element is required!"
             );
          }
          QName rootQName = (QName).get(0);
 
          XSTypeDefinition type = model.getTypeDefinition(.getLocalPart(),
             .getNamespaceURI()
          );
          if(type == null)
          {
             throw new JBossXBRuntimeException("Global type definition is not found: " + );
          }
 
          if(isArrayWrapper(type))
          {
             Object o = provider.getRoot(rootnullrootQName.getNamespaceURI(), rootQName.getLocalPart());
             .push(o);
             marshalComplexType(rootQName.getNamespaceURI(),
                rootQName.getLocalPart(),
                (XSComplexTypeDefinition)type,
                true,
                false
             );
             .pop();
          }
          else
          {
             Object o = provider.getRoot(rootnullrootQName.getNamespaceURI(), rootQName.getLocalPart());
             marshalElementOccurence(rootQName.getNamespaceURI(),
                rootQName.getLocalPart(),
                type,
                o,
                false,
                false,
                true
             );
          }
       }
       else if(.isEmpty())
       {
          XSNamedMap components = model.getComponents(XSConstants.ELEMENT_DECLARATION);
          if(components.getLength() == 0)
          {
             throw new JBossXBRuntimeException("The schema doesn't contain global element declarations.");
          }
 
          for(int i = 0; i < components.getLength(); ++i)
          {
             XSElementDeclaration element = (XSElementDeclaration)components.item(i);
             Object o = provider.getRoot(rootnullelement.getNamespace(), element.getName());
             marshalElementOccurence(element.getNamespace(),
                element.getName(),
                element.getTypeDefinition(),
                o,
                element.getNillable(),
                false,
                true
             );
          }
       }
       else
       {
          for(int i = 0; i < .size(); ++i)
          {
             QName qName = (QName).get(i);
             XSElementDeclaration element = model.getElementDeclaration(qName.getLocalPart(), qName.getNamespaceURI());
             if(element == null)
             {
                XSNamedMap components = model.getComponents(XSConstants.ELEMENT_DECLARATION);
                String roots = "";
                for(int j = 0; j < components.getLength(); ++j)
                {
                   XSObject xsObject = components.item(j);
                   if(j > 0)
                   {
                      roots += ", ";
                   }
                   roots += "{" + xsObject.getNamespace() + "}" + xsObject.getName();
                }
                throw new IllegalStateException("Root element not found: " + qName + " among " + roots);
             }
 
             Object o = provider.getRoot(rootnullelement.getNamespace(), element.getName());
             marshalElementOccurence(element.getNamespace(),
                element.getName(),
                element.getTypeDefinition(),
                o,
                element.getNillable(),
                false,
                true
             );
          }
       }
 
       .endDocument();
 
       // version & encoding
       writeXmlVersion(writer);
 
       ContentWriter contentWriter = new ContentWriter(writer,
       );
       .handleContent(contentWriter);
 
       if(.isTraceEnabled())
       {
          java.io.StringWriter traceWriter = new java.io.StringWriter();
          contentWriter = new ContentWriter(traceWriter,
          );
          .handleContent(contentWriter);
          .trace("marshalled:\n" + traceWriter.getBuffer().toString());
       }
    }
 
    private boolean marshalElement(String elementNsString elementLocal,
                                   XSTypeDefinition type,
                                   boolean optional,
                                   boolean nillable,
                                   boolean declareNs,
                                   boolean declareXsiType)
    {
       Object value = .peek();
       boolean result = value != null || value == null && (optional || nillable);
       boolean trace = .isTraceEnabled() && result;
       if(trace)
       {
          String prefix = getPrefix(elementNs);
          .trace("started element ns=" + elementNs + ", local=" + elementLocal + ", prefix=" + prefix);
       }
 
       if(value != null)
       {
          marshalElementType(elementNselementLocaltypedeclareNsnillabledeclareXsiType);
       }
       else if(nillable)
       {
          writeNillable(elementNselementLocalnillable);
       }
 
       if(trace)
       {
          .trace("finished element ns=" + elementNs + ", local=" + elementLocal);
       }
 
       return result;
    }
 
    private void marshalElementType(String elementNs,
                                    String elementLocal,
                                    XSTypeDefinition type,
                                    boolean declareNs,
                                    boolean nillable,
                                    boolean declareXsiType)
    {
       switch(type.getTypeCategory())
       {
          case XSTypeDefinition.SIMPLE_TYPE:
             marshalSimpleType(elementNs,
                elementLocal,
                (XSSimpleTypeDefinition)type,
                declareNs,
                nillable,
                declareXsiType
             );
             break;
          case XSTypeDefinition.COMPLEX_TYPE:
             marshalComplexType(elementNselementLocal, (XSComplexTypeDefinition)typedeclareNsdeclareXsiType);
             break;
          default:
             throw new IllegalStateException("Unexpected type category: " + type.getTypeCategory());
       }
    }
 
    private void marshalSimpleType(String elementUri,
                                   String elementLocal,
                                   XSSimpleTypeDefinition type,
                                   boolean declareNs,
                                   boolean nillable,
                                   boolean declareXsiType)
    {
       Object value = .peek();
       if(value != null)
       {
          String prefix = getPrefix(elementUri);
          boolean genPrefix = prefix == null && elementUri != null && elementUri.length() > 0;
          if(genPrefix)
          {
             prefix = "ns_" + elementLocal;
          }
 
          AttributesImpl attrs = null;
          String typeName = type.getName();
          if(..equals(typeName) ||
             ..equals(typeName) ||
             type.getItemType() != null &&
             (..equals(type.getItemType().getName()) ||
             ..equals(type.getItemType().getName())
             )
          )
          {
             attrs = new AttributesImpl(5);
          }
 
          String marshalled = marshalCharacters(elementUriprefixtypevalueattrs);
 
          if((declareNs || declareXsiType) && .size() > 0)
          {
             if(attrs == null)
             {
                attrs = new AttributesImpl(.size() + 1);
             }
             declareNs(attrs);
          }
 
          if(declareXsiType)
          {
             declareXsiType(typeattrs);
          }
 
          if(genPrefix)
          {
             if(attrs == null)
             {
                attrs = new AttributesImpl(1);
             }
             attrs.add(nullprefix"xmlns:" + prefixnull, (String)elementUri);
          }
 
          String qName = prefixLocalName(prefixelementLocal);
 
          .startElement(elementUrielementLocalqNameattrs);
          .characters(marshalled.toCharArray(), 0, marshalled.length());
          .endElement(elementUrielementLocalqName);
       }
       else
       {
          writeNillable(elementUrielementLocalnillable);
       }
    }
 
    private void marshalComplexType(String elementNsUri,
                                    String elementLocalName,
                                    XSComplexTypeDefinition type,
                                    boolean declareNs,
                                    boolean declareXsiType)
    {
       Object o = .peek();
       XSParticle particle = type.getParticle();
 
       XSObjectList attributeUses = type.getAttributeUses();
       int attrsTotal = declareNs || declareXsiType ?
          .size() + attributeUses.getLength() + 1 :
          attributeUses.getLength();
       AttributesImpl attrs = attrsTotal > 0 ? new AttributesImpl(attrsTotal) : null;
 
       if(declareNs && .size() > 0)
       {
          declareNs(attrs);
       }
 
       String generatedPrefix = null;
       if(declareXsiType)
       {
          generatedPrefix = declareXsiType(typeattrs);
          if(generatedPrefix != null)
          {
             String typeNsWithGeneratedPrefix = type.getNamespace();
             declareNs(attrsgeneratedPrefixtypeNsWithGeneratedPrefix);
             declareNamespace(generatedPrefixtypeNsWithGeneratedPrefix);
          }
       }
 
       String prefix = getPrefix(elementNsUri);
       boolean genPrefix = prefix == null && elementNsUri != null && elementNsUri.length() > 0;
       if(genPrefix)
       {
          // todo: it's possible that the generated prefix already mapped. this should be fixed
          prefix = "ns_" + elementLocalName;
          declareNamespace(prefixelementNsUri);
          if(attrs == null)
          {
             attrs = new AttributesImpl(1);
          }
          attrs.add(nullprefix"xmlns:" + prefixnullelementNsUri);
       }
 
       for(int i = 0; i < attributeUses.getLength(); ++i)
       {
           = (XSAttributeUse)attributeUses.item(i);
          XSAttributeDeclaration attrDec = .getAttrDeclaration();
          String attrNs = attrDec.getNamespace();
          String attrLocal = attrDec.getName();
          Object attrValue = .getAttributeValue(oattrNsattrLocal);
 
          if(attrValue != null)
          {
             if(attrs == null)
             {
                attrs = new AttributesImpl(5);
             }
 
             String attrPrefix = null;
             if(attrNs != null)
             {
                attrPrefix = getPrefix(attrNs);
                if(attrPrefix == null && attrNs != null && attrNs.length() > 0)
                {
                   attrPrefix = "ns_" + attrLocal;
                   attrs.add(nullattrPrefix"xmlns:" + attrPrefixnullattrNs);
                }
             }
 
             String qName = attrPrefix == null || attrPrefix.length() == 0 ? attrLocal : attrPrefix + ":" + attrLocal;
 
             // todo: this is a quick fix for boolean pattern (0|1 or true|false) should be refactored
             XSSimpleTypeDefinition attrType = attrDec.getTypeDefinition();
             if(attrType.getItemType() != null)
             {
                XSSimpleTypeDefinition itemType = attrType.getItemType();
                if(..equals(itemType.getNamespace()))
                {
                   List list;
                   if(attrValue instanceof List)
                   {
                      list = (List)attrValue;
                   }
                   else if(attrValue.getClass().isArray())
                   {
                      list = Arrays.asList((Object[])attrValue);
                   }
                   else
                   {
                      throw new JBossXBRuntimeException("Expected value for list type is an array or " +
                         List.class.getName() +
                         " but got: " +
                         attrValue
                      );
                   }
 
                   if(..getLocalPart().equals(itemType.getName()))
                   {
                      for(int listInd = 0; listInd < list.size(); ++listInd)
                      {
                         QName item = (QName)list.get(listInd);
                         String itemNs = item.getNamespaceURI();
                         if(itemNs != null && itemNs.length() > 0)
                         {
                            String itemPrefix;
                            if(itemNs.equals(elementNsUri))
                            {
                               itemPrefix = prefix;
                            }
                            else
                            {
                               itemPrefix = getPrefix(itemNs);
                               if(itemPrefix == null)
                               {
                                  itemPrefix = attrLocal + listInd;
                                  declareNs(attrsitemPrefixitemNs);
                               }
                            }
                            item = new QName(item.getNamespaceURI(), item.getLocalPart(), itemPrefix);
                            list.set(listInditem);
                         }
                      }
                   }
 
                   attrValue = SimpleTypeBindings.marshalList(itemType.getName(), listnull);
                }
                else
                {
                   throw new JBossXBRuntimeException("Marshalling of list types with item types not from " +
                      . + " is not supported."
                   );
                }
             }
             else if(attrType.getLexicalPattern().item(0) != null
                &&
                attrType.derivedFrom(.,
                   ..getLocalPart(),
                   XSConstants.DERIVATION_RESTRICTION
                ))
             {
                String item = attrType.getLexicalPattern().item(0);
                if(item.indexOf('0') != -1 && item.indexOf('1') != -1)
                {
                   attrValue = ((Boolean)attrValue).booleanValue() ? "1" : "0";
                }
                else
                {
                   attrValue = ((Boolean)attrValue).booleanValue() ? "true" : "false";
                }
             }
             else if(..getNamespaceURI().equals(attrType.getNamespace()) &&
                ..getLocalPart().equals(attrType.getName()))
             {
                QName qNameValue = (QName)attrValue;
 
                String qNamePrefix = null;
                boolean declarePrefix = false;
                String ns = qNameValue.getNamespaceURI();
                if(ns != null && ns.length() > 0)
                {
                   qNamePrefix = getPrefix(ns);
                   if(qNamePrefix == null)
                   {
                      qNamePrefix = qNameValue.getPrefix();
                      if(qNamePrefix == null || qNamePrefix.length() == 0)
                      {
                         qNamePrefix = "ns_" + qNameValue.getLocalPart();
                      }
                      declareNs(attrsqNamePrefixns);
                      .addPrefixMapping(qNamePrefixns);
                      declarePrefix = true;
                   }
                }
 
                attrValue = SimpleTypeBindings.marshalQName(qNameValue);
 
                if(declarePrefix)
                {
                   .removePrefixMapping(qNamePrefix);
                }
             }
             else
             {
                attrValue = attrValue.toString();
             }
 
             attrs.add(attrNs,
                attrLocal,
                qName,
                attrDec.getTypeDefinition().getName(),
                attrValue.toString()
             );
          }
       }
        = null;
 
       String characters = null;
       if(type.getSimpleType() != null)
       {
          Object value = getSimpleContentValue(elementNsUrielementLocalNametype);
          if(value != null)
          {
             XSSimpleTypeDefinition simpleType = type.getSimpleType();
             String typeName = simpleType.getName();
             if(attrs == null && (..equals(typeName) ||
                ..equals(typeName) ||
                simpleType.getItemType() != null &&
                (..equals(simpleType.getItemType().getName()) ||
                ..equals(simpleType.getItemType().getName())
                )
                )
             )
             {
                attrs = new AttributesImpl(5);
             }
 
             characters = marshalCharacters(elementNsUriprefixsimpleTypevalueattrs);
          }
       }
 
       String qName = prefixLocalName(prefixelementLocalName);
       .startElement(elementNsUrielementLocalNameqNameattrs);
 
       if(particle != null)
       {
          marshalParticle(particlefalse);
       }
 
       if(characters != null)
       {
          .characters(characters.toCharArray(), 0, characters.length());
       }
       .endElement(elementNsUrielementLocalNameqName);
 
       if(genPrefix)
       {
          removePrefixMapping(prefix);
       }
 
       if(generatedPrefix != null)
       {
          removePrefixMapping(generatedPrefix);
       }
    }
 
    private boolean marshalParticle(XSParticle particleboolean declareNs)
    {
       boolean marshalled;
       XSTerm term = particle.getTerm();
       Object o;
       Iterator i;
       switch(term.getType())
       {
          case XSConstants.MODEL_GROUP:
             o = .peek();
             i = o != null && isRepeatable(particle) ? getIterator(o) : null;
             if(i != null)
             {
                marshalled = true;
                while(i.hasNext() && marshalled)
                {
                   Object value = i.next();
                   .push(value);
                   marshalled = marshalModelGroup(particledeclareNs);
                   .pop();
                }
             }
             else
             {
                marshalled = marshalModelGroup(particledeclareNs);
             }
             break;
          case XSConstants.WILDCARD:
             o = .peek();
 
             boolean popWildcardValue = false;
             ObjectLocalMarshaller marshaller = null;
             FieldToWildcardMapping mapping = (FieldToWildcardMapping).get(o.getClass());
             if(mapping != null)
             {
                marshaller = mapping.marshaller;
                o = mapping.fieldInfo.getValue(o);
                .push(o);
                popWildcardValue = true;
             }
 
             i = o != null && isRepeatable(particle) ? getIterator(o) : null;
             if(i != null)
             {
                marshalled = true;
                while(i.hasNext() && marshalled)
                {
                   Object value = i.next();
                   marshalled = marshalWildcardOccurence(particlemarshallervaluedeclareNs);
                }
             }
             else
             {
                marshalled = marshalWildcardOccurence(particlemarshallerodeclareNs);
             }
 
             if(popWildcardValue)
             {
                .pop();
             }
 
             break;
          case XSConstants.ELEMENT_DECLARATION:
             XSElementDeclaration element = (XSElementDeclaration)term;
             XSTypeDefinition type = element.getTypeDefinition();
             o = getElementValue(element.getNamespace(), element.getName(), type);
 
             i = o != null && isRepeatable(particle) ? getIterator(o) : null;
             if(i != null)
             {
                marshalled = true;
                while(i.hasNext() && marshalled)
                {
                   Object value = i.next();
                   marshalled =
                      marshalElementOccurence(element.getNamespace(),
                         element.getName(),
                         type,
                         value,
                         element.getNillable(),
                         particle.getMinOccurs() == 0,
                         declareNs
                      );
                }
             }
             else
             {
                marshalled =
                   marshalElementOccurence(element.getNamespace(),
                      element.getName(),
                      type,
                      o,
                      element.getNillable(),
                      particle.getMinOccurs() == 0,
                      declareNs
                   );
             }
             break;
          default:
             throw new IllegalStateException("Unexpected term type: " + term.getType());
       }
       return marshalled;
    }
 
    private boolean marshalElementOccurence(String elementNs,
                                            String elementLocal,
                                            XSTypeDefinition type,
                                            Object value,
                                            boolean nillable,
                                            boolean optional,
                                            boolean declareNs)
    {
       boolean declareXsiType = false;
       QName xsiTypeQName = null;
       if(value != null)
       {
          xsiTypeQName = (QName).get(value.getClass());
          if(xsiTypeQName != null &&
             !(type.getName().equals(xsiTypeQName.getLocalPart()) &&
             type.getNamespace().equals(xsiTypeQName.getNamespaceURI())
             ))
          {
             declareXsiType = true;
             if(.isTraceEnabled())
             {
                .trace(value.getClass() + " is mapped to xsi:type " + xsiTypeQName);
             }
 
             XSTypeDefinition xsiType = .getTypeDefinition(xsiTypeQName.getLocalPart(),
                xsiTypeQName.getNamespaceURI()
             );
 
             if(xsiType == null)
             {
                .warn("Class " +
                   value.getClass() +
                   " is mapped to type " +
                   xsiTypeQName +
                   " but the type is not found in schema."
                );
             }
             // todo should check derivation also, i.e. if(xsiType.derivedFrom())
             else
             {
                type = xsiType;
             }
          }
       }
 
       .push(value);
       boolean marshalled = marshalElement(elementNs,
          elementLocal,
          type,
          optional,
          nillable,
          declareNs,
          declareXsiType
       );
       .pop();
 
       return marshalled;
    }
 
    private boolean marshalWildcardOccurence(XSParticle particle,
                                             ObjectLocalMarshaller marshaller,
                                             Object value,
                                             boolean declareNs)
    {
       boolean marshalled = true;
       if(marshaller != null)
       {
          marshaller.marshal(value);
       }
       else
       {
          .push(value);
          marshalled = marshalWildcard(particledeclareNs);
          .pop();
       }
       return marshalled;
    }
 
    private boolean marshalWildcard(XSParticle particleboolean declareNs)
    {
       XSWildcard wildcard = (XSWildcard)particle.getTerm();
       Object o = .peek();
       ClassMapping mapping = getClassMapping(o.getClass());
       if(mapping == null)
       {
          // todo: YAH (yet another hack)
          QName autoType = SimpleTypeBindings.typeQName(o.getClass());
          if(autoType != null)
          {
             String marshalled = SimpleTypeBindings.marshal(autoType.getLocalPart(), onull);
             .characters(marshalled.toCharArray(), 0, marshalled.length());
             return true;
          }
          else
          {
             if()
             {
                .warn("Failed to marshal wildcard. Class mapping not found for " +
                   o.getClass() +
                   "@" +
                   o.hashCode() +
                   ": " + o
                );
                return true;
             }
             else
             {
                throw new IllegalStateException("Failed to marshal wildcard. Class mapping not found for " +
                   o.getClass() +
                   "@" +
                   o.hashCode() +
                   ": " + o
                );
             }
          }
       }
 
       GenericObjectModelProvider parentProvider = this.;
       Object parentRoot = this.;
       Stack parentStack = this.;
       XSModel parentModel = this.;
 
       this. = o;
       this. = new StackImpl();
       this. = mapping.schemaUrl == null ? this. : Util.loadSchema(mapping.schemaUrl);
       if(mapping.provider != null)
       {
          this. = mapping.provider;
       }
 
       boolean marshalled;
       if(mapping.elementName != null)
       {
          XSElementDeclaration elDec = .getElementDeclaration(mapping.elementName.getLocalPart(),
             mapping.elementName.getNamespaceURI()
          );
 
          if(elDec == null)
          {
             throw new JBossXBRuntimeException("Element " + mapping.elementName + " is not declared in the schema.");
          }
 
          Object elementValue = .getRoot(nullelDec.getNamespace(), elDec.getName());
          marshalled = marshalElementOccurence(elDec.getNamespace(),
             elDec.getName(),
             elDec.getTypeDefinition(),
             elementValue,
             elDec.getNillable(),
             particle.getMinOccurs() == 0,
             declareNs
          );
       }
       else if(mapping.typeName != null)
       {
          XSTypeDefinition typeDef = .getTypeDefinition(mapping.typeName.getLocalPart(),
             mapping.typeName.getNamespaceURI()
          );
 
          if(typeDef == null)
          {
             List typeNames = new ArrayList();
             XSNamedMap types = .getComponents(XSConstants.TYPE_DEFINITION);
             for(int i = 0; i < types.getLength(); ++i)
             {
                XSObject type = types.item(i);
               if(!..equals(type.getNamespace()))
               {
                  typeNames.add(new QName(type.getNamespace(), type.getName()));
               }
            }
            throw new JBossXBRuntimeException("Type " +
               mapping.typeName +
               " is not defined in the schema." +
               " Defined types are: " + typeNames
            );
         }
         Object elementValue = .getRoot(nullwildcard.getNamespace(), wildcard.getName());
         marshalled =
            marshalElementOccurence(wildcard.getNamespace(),
               wildcard.getName(),
               typeDef,
               elementValue,
               true,
               particle.getMinOccurs() == 0,
               declareNs
            );
      }
      else
      {
         throw new JBossXBRuntimeException("Class mapping for " +
            mapping.cls +
            " is associated with neither global element name nor global type name."
         );
      }
      this. = parentRoot;
      this. = parentProvider;
      this. = parentStack;
      this. = parentModel;
      return marshalled;
   }
   private boolean marshalModelGroup(XSParticle particleboolean declareNs)
   {
      XSModelGroup modelGroup = (XSModelGroup)particle.getTerm();
      boolean marshalled;
      switch(modelGroup.getCompositor())
      {
         case XSModelGroup.COMPOSITOR_ALL:
            marshalled = marshalModelGroupAll(modelGroup.getParticles(), declareNs);
            break;
         case XSModelGroup.COMPOSITOR_CHOICE:
            marshalled = marshalModelGroupChoice(modelGroup.getParticles(), declareNs);
            break;
         case XSModelGroup.COMPOSITOR_SEQUENCE:
            marshalled = marshalModelGroupSequence(modelGroup.getParticles(), declareNs);
            break;
         default:
            throw new IllegalStateException("Unexpected compsitor: " + modelGroup.getCompositor());
      }
      return marshalled;
   }
   private boolean marshalModelGroupAll(XSObjectList particlesboolean declareNs)
   {
      boolean marshalled = false;
      for(int i = 0; i < particles.getLength(); ++i)
      {
         XSParticle particle = (XSParticle)particles.item(i);
         marshalled |= marshalParticle(particledeclareNs);
      }
      return marshalled;
   }
   private boolean marshalModelGroupChoice(XSObjectList particlesboolean declareNs)
   {
      boolean marshalled = false;
      Content mainContent = this.;
      for(int i = 0; i < particles.getLength() && !marshalled; ++i)
      {
         XSParticle particle = (XSParticle)particles.item(i);
         this. = new Content();
         marshalled = marshalParticle(particledeclareNs);
      }
      if(marshalled)
      {
         mainContent.append(this.);
      }
      this. = mainContent;
      return marshalled;
   }
   private boolean marshalModelGroupSequence(XSObjectList particlesboolean declareNs)
   {
      boolean marshalled = true;
      for(int i = 0; i < particles.getLength(); ++i)
      {
         XSParticle particle = (XSParticle)particles.item(i);
         marshalled &= marshalParticle(particledeclareNs);
      }
      return marshalled;
   }
   private String marshalCharacters(String elementUri,
                                    String elementPrefix,
                                    XSSimpleTypeDefinition type,
                                    Object value,
                                    AttributesImpl attrs)
   {
      String marshalled;
      if(type.getItemType() != null)
      {
         XSSimpleTypeDefinition itemType = type.getItemType();
         if(..equals(itemType.getNamespace()))
         {
            List list;
            if(value instanceof List)
            {
               list = (List)value;
            }
            else if(value.getClass().isArray())
            {
               list = asList(value);
            }
            else
            {
               // todo: qname are also not yet supported
               throw new JBossXBRuntimeException(
                  "Expected value for list type is an array or " + List.class.getName() + " but got: " + value
               );
            }
            marshalled = SimpleTypeBindings.marshalList(itemType.getName(), listnull);