[jboss-svn-commits] JBoss Common SVN: r2366 - in jbossxb/trunk/src: main/java/org/jboss/xb/binding/sunday/unmarshalling and 3 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Apr 5 11:12:51 EDT 2007


Author: alex.loubyansky at jboss.com
Date: 2007-04-05 11:12:51 -0400 (Thu, 05 Apr 2007)
New Revision: 2366

Added:
   jbossxb/trunk/src/main/java/org/jboss/xb/util/DomCharactersHandler.java
   jbossxb/trunk/src/main/java/org/jboss/xb/util/DomLocalMarshaller.java
   jbossxb/trunk/src/main/java/org/jboss/xb/util/DomParticleHandler.java
   jbossxb/trunk/src/test/java/org/jboss/test/xml/AnyTypeDomBindingUnitTestCase.java
   jbossxb/trunk/src/test/resources/org/jboss/test/xml/AnyTypeDomBindingUnitTestCase.xsd
   jbossxb/trunk/src/test/resources/org/jboss/test/xml/AnyTypeDomBindingUnitTestCase_complexContent.xml
   jbossxb/trunk/src/test/resources/org/jboss/test/xml/AnyTypeDomBindingUnitTestCase_simpleContent.xml
Modified:
   jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/marshalling/MarshallerImpl.java
   jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SchemaBinding.java
   jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/WildcardBinding.java
   jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/XsdBinder.java
   jbossxb/trunk/src/test/java/org/jboss/test/xml/AbstractJBossXBTest.java
   jbossxb/trunk/src/test/java/org/jboss/test/xml/WildcardUnresolvedElementsUnitTestCase.java
Log:
JBXB-97

Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/marshalling/MarshallerImpl.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/marshalling/MarshallerImpl.java	2007-04-05 00:27:36 UTC (rev 2365)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/marshalling/MarshallerImpl.java	2007-04-05 15:12:51 UTC (rev 2366)
@@ -575,7 +575,7 @@
 
       String characters = null;
       TypeBinding simpleType = type.getSimpleType();
-      if(simpleType != null)
+      if(simpleType != null && !Constants.QNAME_ANYTYPE.equals(type.getQName()))
       {
          String fieldName = ctx.getSimpleContentProperty();
          CharactersMetaData charactersMetaData = type.getCharactersMetaData();
@@ -812,24 +812,25 @@
          }
          else
          {
+            ObjectLocalMarshaller marshaller = wildcard.getUnresolvedMarshaller();
+            if(marshaller != null)
+            {
+               marshaller.marshal(ctx, o);
+               return true;
+            }
+            
+            String msg = "Failed to marshal wildcard: neither class mapping was found for "
+               + o.getClass() + "@" + o.hashCode()
+               + " (toString: " + o
+               + ") nor marshaller for unresolved classes was setup.";
             if(ignoreUnresolvedWildcard)
             {
-               log.warn("Failed to marshal wildcard. Class mapping not found for " +
-                   o.getClass() +
-                   "@" +
-                   o.hashCode() +
-                   ": " + o
-               );
+               log.warn(msg);
                return true;
             }
             else
             {
-               throw new IllegalStateException("Failed to marshal wildcard. Class mapping not found for " +
-                   o.getClass() +
-                   "@" +
-                   o.hashCode() +
-                   ": " + o
-               );
+               throw new JBossXBRuntimeException(msg);
             }
          }
       }

Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SchemaBinding.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SchemaBinding.java	2007-04-05 00:27:36 UTC (rev 2365)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SchemaBinding.java	2007-04-05 15:12:51 UTC (rev 2366)
@@ -34,6 +34,9 @@
 import org.jboss.xb.binding.sunday.xop.XOPUnmarshaller;
 import org.jboss.xb.binding.sunday.xop.XOPMarshaller;
 import org.jboss.xb.binding.metadata.PackageMetaData;
+import org.jboss.xb.util.DomCharactersHandler;
+import org.jboss.xb.util.DomLocalMarshaller;
+import org.jboss.xb.util.DomParticleHandler;
 
 /**
  * A SchemaBinding is a collection of binding objects (TypeBinding,
@@ -401,6 +404,47 @@
       this.xopMarshaller = xopMarshaller;
    }
 
+   public void setUnresolvedContentBoundToDOM(boolean toDom)
+   {
+      TypeBinding type = getType(Constants.QNAME_ANYTYPE);
+      if(type == null)
+      {
+         // ignore, there is no use of the anyType in the schema
+         return;
+         //throw new JBossXBRuntimeException("anyType is not bound.");
+      }
+
+      WildcardBinding wildcard = type.getWildcard();
+      if(toDom)
+      {
+         wildcard.setUnresolvedCharactersHandler(DomCharactersHandler.INSTANCE);
+         wildcard.setUnresolvedElementHandler(DomParticleHandler.INSTANCE);
+         wildcard.setUnresolvedMarshaller(DomLocalMarshaller.INSTANCE);
+      }
+      else
+      {
+         wildcard.setUnresolvedCharactersHandler(null);
+         wildcard.setUnresolvedElementHandler(null);
+         wildcard.setUnresolvedMarshaller(null);
+      }
+   }
+   
+   public boolean isUnresolvedContentBoundToDOM()
+   {
+      TypeBinding type = getType(Constants.QNAME_ANYTYPE);
+      if(type == null)
+      {
+         // there is no use of the anyType in the schema
+         return false;
+         //throw new JBossXBRuntimeException("anyType is not bound.");
+      }
+
+      WildcardBinding wildcard = type.getWildcard();
+      return wildcard.getUnresolvedCharactersHandler() instanceof DomCharactersHandler
+      && wildcard.getUnresolvedElementHandler() instanceof DomParticleHandler
+      && wildcard.getUnresolvedMarshaller() instanceof DomLocalMarshaller;
+   }
+   
    void addElementParticle(ParticleBinding particle)
    {
       ElementBinding element = (ElementBinding)particle.getTerm();

Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/WildcardBinding.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/WildcardBinding.java	2007-04-05 00:27:36 UTC (rev 2365)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/WildcardBinding.java	2007-04-05 15:12:51 UTC (rev 2366)
@@ -24,6 +24,7 @@
 import javax.xml.namespace.QName;
 import org.jboss.logging.Logger;
 import org.jboss.xb.binding.JBossXBRuntimeException;
+import org.jboss.xb.binding.ObjectLocalMarshaller;
 import org.jboss.xb.binding.Util;
 import org.xml.sax.Attributes;
 
@@ -46,8 +47,10 @@
 
    private ParticleHandler unresolvedElementHandler;
    private CharactersHandler unresolvedCharactersHandler;
+   private ObjectLocalMarshaller unresolvedMarshaller;
    private ParticleHandler wildcardHandler;
 
+
    public WildcardBinding(SchemaBinding schema)
    {
       super(schema);
@@ -132,6 +135,16 @@
       this.unresolvedCharactersHandler = unresolvedCharactersHandler;
    }
 
+   public ObjectLocalMarshaller getUnresolvedMarshaller()
+   {
+      return unresolvedMarshaller;
+   }
+   
+   public void setUnresolvedMarshaller(ObjectLocalMarshaller marshaller)
+   {
+      this.unresolvedMarshaller = marshaller;
+   }
+   
    public ElementBinding getElement(QName qName, Attributes attrs)
    {
       if(pc == PC_SKIP)

Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/XsdBinder.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/XsdBinder.java	2007-04-05 00:27:36 UTC (rev 2365)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/XsdBinder.java	2007-04-05 15:12:51 UTC (rev 2366)
@@ -246,7 +246,7 @@
    {
       XsdBinder binder = new XsdBinder();
       binder.setProcessAnnotations(processAnnotations);
-      binder.setSchemaResolver(resolver);      
+      binder.setSchemaResolver(resolver);
       return binder.parse(model);
    }
 
@@ -298,6 +298,7 @@
    private boolean processAnnotations = true;
    private SchemaBindingResolver resolver;
    private boolean simpleContentWithIdAsSimpleType = true;
+   private boolean unresolvedContentBoundToDOM = true;
    
    // Internal attributes
    
@@ -350,6 +351,16 @@
       return simpleContentWithIdAsSimpleType;
    }
 
+   public void setUnresolvedContentBoundToDOM(boolean toDOM)
+   {
+      this.unresolvedContentBoundToDOM = toDOM;      
+   }
+   
+   public boolean isUnresolvedContentBoundToDOM()
+   {
+      return this.unresolvedContentBoundToDOM;
+   }
+   
    public SchemaBinding parse(String xsdUrl)
    {
       if(resolver == null)
@@ -470,6 +481,11 @@
          bindElement(element, 1, 0, false);
       }
 
+      if(unresolvedContentBoundToDOM)
+      {
+         schema.setUnresolvedContentBoundToDOM(unresolvedContentBoundToDOM);
+      }
+      
       if (trace)
       {
          log.trace("finished binding schema " + schema);

Added: jbossxb/trunk/src/main/java/org/jboss/xb/util/DomCharactersHandler.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/util/DomCharactersHandler.java	                        (rev 0)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/util/DomCharactersHandler.java	2007-04-05 15:12:51 UTC (rev 2366)
@@ -0,0 +1,76 @@
+/*
+  * 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.util;
+
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
+
+import org.jboss.xb.binding.JBossXBRuntimeException;
+import org.jboss.xb.binding.metadata.ValueMetaData;
+import org.jboss.xb.binding.sunday.unmarshalling.CharactersHandler;
+import org.jboss.xb.binding.sunday.unmarshalling.ElementBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.TypeBinding;
+import org.w3c.dom.Element;
+import org.w3c.dom.Text;
+
+
+/**
+ * CharactersHandler that unmarshals into org.w3c.dom.Element.
+ * 
+ * @author <a href="mailto:alex at jboss.org">Alexey Loubyansky</a>
+ * @version <tt>$Revision: 46112 $</tt>
+ */
+public class DomCharactersHandler
+   extends CharactersHandler
+{
+   public static final DomCharactersHandler INSTANCE = new DomCharactersHandler();
+   
+   public Object unmarshalEmpty(QName qName,
+                                TypeBinding typeBinding,
+                                NamespaceContext nsCtx,
+                                ValueMetaData valueMetaData)
+   {
+      return "";
+   }
+
+   public Object unmarshal(QName qName,
+                           TypeBinding typeBinding,
+                           NamespaceContext nsCtx,
+                           ValueMetaData valueMetaData,
+                           String value)
+   {
+      return value;
+   }
+
+   public void setValue(QName qName, ElementBinding element, Object owner, Object value)
+   {
+      if(!(owner instanceof Element))
+      {
+         throw new JBossXBRuntimeException("The parent must be an instance of "
+               + Element.class + ": parent=" + owner);
+      }
+
+      Element e = (Element)owner;
+      Text textNode = e.getOwnerDocument().createTextNode((String)value);
+      e.appendChild(textNode);
+   }
+}

Added: jbossxb/trunk/src/main/java/org/jboss/xb/util/DomLocalMarshaller.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/util/DomLocalMarshaller.java	                        (rev 0)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/util/DomLocalMarshaller.java	2007-04-05 15:12:51 UTC (rev 2366)
@@ -0,0 +1,62 @@
+/*
+  * 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.util;
+
+import org.jboss.xb.binding.JBossXBRuntimeException;
+import org.jboss.xb.binding.MarshallingContext;
+import org.jboss.xb.binding.ObjectLocalMarshaller;
+import org.w3c.dom.Element;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+
+
+/**
+ * ObjectLocalMarshaller that marshals org.w3c.dom.Element.
+ * 
+ * @author <a href="mailto:alex at jboss.org">Alexey Loubyansky</a>
+ * @version <tt>$Revision: 46112 $</tt>
+ */
+public class DomLocalMarshaller
+   implements ObjectLocalMarshaller
+{
+   public static final DomLocalMarshaller INSTANCE = new DomLocalMarshaller();
+   
+   public void marshal(MarshallingContext ctx, Object o)
+   {
+      if(!(o instanceof Element))
+      {
+         throw new JBossXBRuntimeException("The argument must be an instance of " + Element.class + ": arg=" + o);
+      }
+      
+      Element e = (Element)o;
+      
+      ContentHandler ch = ctx.getContentHandler();
+      try
+      {
+         Dom2Sax.dom2sax(e, ch);
+      }
+      catch (SAXException e1)
+      {
+         throw new JBossXBRuntimeException("Failed to SAX the DOM");
+      }
+   }
+}

Added: jbossxb/trunk/src/main/java/org/jboss/xb/util/DomParticleHandler.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/util/DomParticleHandler.java	                        (rev 0)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/util/DomParticleHandler.java	2007-04-05 15:12:51 UTC (rev 2366)
@@ -0,0 +1,107 @@
+/*
+  * 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.util;
+
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.jboss.xb.binding.JBossXBRuntimeException;
+import org.jboss.xb.binding.sunday.unmarshalling.ParticleBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.ParticleHandler;
+import org.jboss.xb.binding.sunday.unmarshalling.impl.runtime.RtElementHandler;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.xml.sax.Attributes;
+
+
+/**
+ * ParticleHandler that unmarshals into org.w3c.dom.Element.
+ * 
+ * @author <a href="mailto:alex at jboss.org">Alexey Loubyansky</a>
+ * @version <tt>$Revision: 46112 $</tt>
+ */
+public class DomParticleHandler extends RtElementHandler implements ParticleHandler
+{
+   public static final DomParticleHandler INSTANCE = new DomParticleHandler();
+   
+   private Document doc;
+
+   public Object startParticle(Object parent,
+         QName elementName,
+         ParticleBinding particle,
+         Attributes attrs,
+         NamespaceContext nsCtx)
+   {
+      Document doc = getDocument();
+      Element element = doc.createElementNS(elementName.getNamespaceURI(), elementName.getLocalPart());
+
+      if (attrs != null)
+      {
+         for (int i = 0; i < attrs.getLength(); ++i)
+         {
+            element.setAttribute(attrs.getLocalName(i), attrs.getValue(i));
+         }
+      }
+
+      return element;
+   }
+
+   public Object endParticle(Object o, QName elementName, ParticleBinding particle)
+   {
+      return o;
+   }
+
+   public void setParent(Object parent, Object o, QName elementName, ParticleBinding particle,
+         ParticleBinding parentParticle)
+   {
+      if (parent instanceof Element)
+      {
+         ((Element) parent).appendChild((Element) o);
+      }
+      else
+      {
+         super.setParent(parent, o, elementName, particle, parentParticle);
+      }
+   }
+
+   private Document getDocument()
+   {
+      if (doc == null)
+      {
+         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+         DocumentBuilder domBuilder = null;
+         try
+         {
+            domBuilder = factory.newDocumentBuilder();
+         }
+         catch (ParserConfigurationException e)
+         {
+            throw new JBossXBRuntimeException("Failed to create document builder instance", e);
+         }
+         doc = domBuilder.newDocument();
+      }
+      return doc;
+   }
+}

Modified: jbossxb/trunk/src/test/java/org/jboss/test/xml/AbstractJBossXBTest.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xml/AbstractJBossXBTest.java	2007-04-05 00:27:36 UTC (rev 2365)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xml/AbstractJBossXBTest.java	2007-04-05 15:12:51 UTC (rev 2366)
@@ -51,6 +51,58 @@
       super(name);
    }
 
+   /**
+    * Validates XML content passed in the second argument
+    * against the XML content in the file.
+    * 
+    * @param xmlFileName  the name of the XML file
+    * @param content  the content to validate
+    */
+   public void assertXmlFileContent(String xmlFileName, String content)
+   {
+      URL expectedUrl = getResource(xmlFileName);
+      if(expectedUrl == null)
+      {
+         fail("File not found: " + xmlFileName);
+      }
+      
+      java.io.InputStream is = null;
+      String expectedContent = null;
+      try
+      {
+         is = expectedUrl.openStream();
+         byte[] bytes = new byte[is.available()];
+         is.read(bytes);
+         expectedContent = new String(bytes);
+      }
+      catch(java.io.IOException e)
+      {
+         fail("Failed to read file " + xmlFileName + ": " + e.getMessage());
+      }
+      finally
+      {
+         if(is != null)
+         {
+            try
+            {
+               is.close();
+            }
+            catch(Exception ignore)
+            {
+            }
+         }
+      }
+
+      assertXmlEqual(expectedContent, content);
+   }
+
+   /**
+    * Validates XML content passed in the second argument
+    * against the expected XML content passed in the first argument.
+    * 
+    * @param expected  expected content
+    * @param was  the content to validate
+    */
    public void assertXmlEqual(String expected, String was)
    {
       String diff = DIFF.diff(expected, was);

Added: jbossxb/trunk/src/test/java/org/jboss/test/xml/AnyTypeDomBindingUnitTestCase.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xml/AnyTypeDomBindingUnitTestCase.java	                        (rev 0)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xml/AnyTypeDomBindingUnitTestCase.java	2007-04-05 15:12:51 UTC (rev 2366)
@@ -0,0 +1,146 @@
+/*
+  * 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.test.xml;
+
+import java.io.StringWriter;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import org.jboss.xb.binding.sunday.marshalling.MarshallerImpl;
+import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding;
+import org.w3c.dom.Element;
+
+
+/**
+ * @author <a href="mailto:alex at jboss.org">Alexey Loubyansky</a>
+ * @version <tt>$Revision: 46112 $</tt>
+ */
+public class AnyTypeDomBindingUnitTestCase
+   extends AbstractJBossXBTest
+{
+   public AnyTypeDomBindingUnitTestCase(String name)
+   {
+      super(name);
+   }
+
+   public void testComplexContentUnmarshalling() throws Exception
+   {
+      SchemaBinding schema = bindSchema();
+
+      Object unmarshalled = unmarshal(rootName + "_complexContent.xml", schema);
+      
+      assertNotNull(unmarshalled);
+      assertTrue(unmarshalled instanceof MyType);
+      
+      MyType mt = (MyType) unmarshalled;
+      
+      assertNotNull(mt.anything);
+      assertTrue(mt.anything.toString(), mt.anything instanceof Element);
+      
+      Element dom = (Element) mt.anything;
+      assertEquals("dom", dom.getLocalName());
+      assertEquals(null, dom.getNamespaceURI());
+      assertEquals("test", dom.getTextContent());
+   }
+
+   public void testSimpleContentUnmarshalling() throws Exception
+   {
+      SchemaBinding schema = bindSchema();
+
+      Object unmarshalled = unmarshal(rootName + "_simpleContent.xml", schema);
+      
+      assertNotNull(unmarshalled);
+      assertTrue(unmarshalled instanceof MyType);
+      
+      MyType mt = (MyType) unmarshalled;
+      
+      assertNotNull(mt.anything);
+      assertTrue(mt.anything instanceof String);
+      assertEquals("test", mt.anything);
+   }
+
+   public void testComplexContentMarshalling() throws Exception
+   {
+      SchemaBinding schema = bindSchema();
+      
+      DocumentBuilder domBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+      Element dom = domBuilder.newDocument().createElement("dom");
+      dom.setTextContent("test");
+
+      MyType root = new MyType();
+      root.anything = dom;
+      
+      MarshallerImpl marshaller = new MarshallerImpl();
+      StringWriter writer = new StringWriter();
+      marshaller.marshal(schema, null, root, writer);
+      
+      assertXmlFileContent(rootName + "_complexContent.xml", writer.getBuffer().toString());
+   }
+
+   public void testSimpleContentMarshalling() throws Exception
+   {
+      SchemaBinding schema = bindSchema();
+      
+      DocumentBuilder domBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+      Element dom = domBuilder.newDocument().createElementNS("http://ns1", "dom");
+      dom.setTextContent("test");
+
+      MyType root = new MyType();
+      root.anything = "test";
+      
+      MarshallerImpl marshaller = new MarshallerImpl();
+      StringWriter writer = new StringWriter();
+      marshaller.marshal(schema, null, root, writer);
+      
+      assertXmlFileContent(rootName + "_simpleContent.xml", writer.getBuffer().toString());
+   }
+
+   private SchemaBinding bindSchema() throws Exception
+   {
+      SchemaBinding schema = bind(rootName + ".xsd");
+      schema.setIgnoreUnresolvedFieldOrClass(false);
+      
+      //schema.setUnresolvedContentBoundToDOM(true);
+      // is true by default and equivalent to
+      /*
+      TypeBinding myType = schema.getType(new QName("http://www.jboss.org/xb/test/any", "myType"));
+      SequenceBinding seq = (SequenceBinding)myType.getParticle().getTerm();
+      ElementBinding anything = (ElementBinding)((ParticleBinding)seq.getParticles().iterator().next()).getTerm();
+      TypeBinding anyType = anything.getType();
+      WildcardBinding wc = anyType.getWildcard();
+      
+      wc.setUnresolvedElementHandler(DomParticleHandler.INSTANCE);
+      wc.setUnresolvedCharactersHandler(DomCharactersHandler.INSTANCE);
+      wc.setUnresolvedMarshaller(DomLocalMarshaller.INSTANCE);
+      */
+
+      assertTrue(schema.isUnresolvedContentBoundToDOM());
+
+      return schema;
+   }
+
+   public static class MyType
+   {
+      public Object anything;
+   }
+}

Modified: jbossxb/trunk/src/test/java/org/jboss/test/xml/WildcardUnresolvedElementsUnitTestCase.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xml/WildcardUnresolvedElementsUnitTestCase.java	2007-04-05 00:27:36 UTC (rev 2365)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xml/WildcardUnresolvedElementsUnitTestCase.java	2007-04-05 15:12:51 UTC (rev 2366)
@@ -58,18 +58,19 @@
 import org.jboss.xb.binding.sunday.unmarshalling.WildcardBinding;
 import org.jboss.xb.binding.sunday.unmarshalling.XsdBinder;
 import org.jboss.xb.binding.sunday.unmarshalling.impl.runtime.RtElementHandler;
-import org.jboss.xb.util.Dom2Sax;
+import org.jboss.xb.util.DomCharactersHandler;
+import org.jboss.xb.util.DomLocalMarshaller;
+import org.jboss.xb.util.DomParticleHandler;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
-import org.w3c.dom.Text;
 import org.w3c.dom.ls.LSInput;
 import org.xml.sax.Attributes;
 import org.xml.sax.ContentHandler;
-import org.xml.sax.SAXException;
 
+
 /**
  * @author <a href="mailto:alex at jboss.org">Alexey Loubyansky</a>
  * @version <tt>$Revision: 46112 $</tt>
@@ -190,25 +191,7 @@
    private void setupDomMarshaller(AbstractMarshaller marshaller)
    {
       marshaller.mapClassToGlobalElement(ArrayOfAny.class, "e", "http://org.jboss.ws/jbws434/types", null, null);
-      marshaller.mapFieldToWildcard(ArrayOfAny.class, "_any",
-         new ObjectLocalMarshaller()
-         {
-            public void marshal(MarshallingContext ctx, Object o)
-            {
-               Element e = (Element)o;
-               
-               ContentHandler ch = ctx.getContentHandler();
-               try
-               {
-                  Dom2Sax.dom2sax(e, ch);
-               }
-               catch (SAXException e1)
-               {
-                  throw new JBossXBRuntimeException("Failed to SAX the DOM");
-               }
-            }
-         }
-      );
+      marshaller.mapFieldToWildcard(ArrayOfAny.class, "_any", DomLocalMarshaller.INSTANCE);
    }
 
    private void setupGeMarshaller(AbstractMarshaller marshaller)
@@ -291,8 +274,8 @@
       }
       else
       {
-         unresolvedElementHandler = new DomElementHandler();
-         unresolvedCharactersHandler = new DomCharactersHandler();
+         unresolvedElementHandler = DomParticleHandler.INSTANCE;
+         unresolvedCharactersHandler = DomCharactersHandler.INSTANCE;
       }
 
       wildcard.setUnresolvedElementHandler(unresolvedElementHandler);
@@ -926,99 +909,4 @@
          }
       }
    }
-
-   public static class DomCharactersHandler
-      extends CharactersHandler
-   {
-      public Object unmarshalEmpty(QName qName,
-                                   TypeBinding typeBinding,
-                                   NamespaceContext nsCtx,
-                                   ValueMetaData valueMetaData)
-      {
-         return "";
-      }
-
-      public Object unmarshal(QName qName,
-                              TypeBinding typeBinding,
-                              NamespaceContext nsCtx,
-                              ValueMetaData valueMetaData,
-                              String value)
-      {
-         return value;
-      }
-
-      public void setValue(QName qName, ElementBinding element, Object owner, Object value)
-      {
-         Element e = (Element)owner;
-         Text textNode = e.getOwnerDocument().createTextNode((String)value);
-         e.appendChild(textNode);
-      }
-   }
-
-   public static class DomElementHandler
-      extends RtElementHandler
-      implements ParticleHandler
-   {
-      private Document doc;
-
-      public Object startParticle(Object parent,
-                                  QName elementName,
-                                  ParticleBinding particle,
-                                  Attributes attrs,
-                                  NamespaceContext nsCtx)
-      {
-         Document doc = getDocument();
-         Element element = doc.createElementNS(elementName.getNamespaceURI(), elementName.getLocalPart());
-
-         if(attrs != null)
-         {
-            for(int i = 0; i < attrs.getLength(); ++i)
-            {
-               element.setAttribute(attrs.getLocalName(i), attrs.getValue(i));
-            }
-         }
-         
-         return element;
-      }
-
-      public Object endParticle(Object o, QName elementName, ParticleBinding particle)
-      {
-         return o;
-      }
-
-      public void setParent(Object parent,
-                            Object o,
-                            QName elementName,
-                            ParticleBinding particle,
-                            ParticleBinding parentParticle)
-      {
-         if(parent instanceof Element)
-         {
-            ((Element)parent).appendChild((Element)o);
-         }
-         else
-         {
-            super.setParent(parent, o, elementName, particle, parentParticle);
-         }
-      }
-
-      private Document getDocument()
-      {
-         if(doc == null)
-         {
-            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
-            DocumentBuilder domBuilder = null;
-            try
-            {
-               domBuilder = factory.newDocumentBuilder();
-            }
-            catch(ParserConfigurationException e)
-            {
-               throw new JBossXBRuntimeException("Failed to create document builder instance", e);
-            }
-            doc = domBuilder.newDocument();
-         }
-         return doc;
-      }
-   }
 }

Added: jbossxb/trunk/src/test/resources/org/jboss/test/xml/AnyTypeDomBindingUnitTestCase.xsd
===================================================================
--- jbossxb/trunk/src/test/resources/org/jboss/test/xml/AnyTypeDomBindingUnitTestCase.xsd	                        (rev 0)
+++ jbossxb/trunk/src/test/resources/org/jboss/test/xml/AnyTypeDomBindingUnitTestCase.xsd	2007-04-05 15:12:51 UTC (rev 2366)
@@ -0,0 +1,17 @@
+<schema
+ targetNamespace='http://www.jboss.org/xb/test/any'
+ xmlns='http://www.w3.org/2001/XMLSchema'
+ xmlns:jbxb='http://www.jboss.org/xml/ns/jbxb'
+ xmlns:tns='http://www.jboss.org/xb/test/any'>
+  <complexType name='myType'>
+    <annotation>
+      <appinfo>
+        <jbxb:class impl='org.jboss.test.xml.AnyTypeDomBindingUnitTestCase$MyType'/>
+      </appinfo>
+    </annotation>
+    <sequence>
+      <element name='anything' nillable='true' type='anyType'/>
+    </sequence>
+  </complexType>
+  <element name='e' type='tns:myType'/>
+</schema>

Added: jbossxb/trunk/src/test/resources/org/jboss/test/xml/AnyTypeDomBindingUnitTestCase_complexContent.xml
===================================================================
--- jbossxb/trunk/src/test/resources/org/jboss/test/xml/AnyTypeDomBindingUnitTestCase_complexContent.xml	                        (rev 0)
+++ jbossxb/trunk/src/test/resources/org/jboss/test/xml/AnyTypeDomBindingUnitTestCase_complexContent.xml	2007-04-05 15:12:51 UTC (rev 2366)
@@ -0,0 +1,3 @@
+<e xmlns='http://www.jboss.org/xb/test/any'>
+  <anything xmlns=""><dom>test</dom></anything>
+</e>

Added: jbossxb/trunk/src/test/resources/org/jboss/test/xml/AnyTypeDomBindingUnitTestCase_simpleContent.xml
===================================================================
--- jbossxb/trunk/src/test/resources/org/jboss/test/xml/AnyTypeDomBindingUnitTestCase_simpleContent.xml	                        (rev 0)
+++ jbossxb/trunk/src/test/resources/org/jboss/test/xml/AnyTypeDomBindingUnitTestCase_simpleContent.xml	2007-04-05 15:12:51 UTC (rev 2366)
@@ -0,0 +1,3 @@
+<e xmlns='http://www.jboss.org/xb/test/any'>
+  <anything xmlns="">test</anything>
+</e>




More information about the jboss-svn-commits mailing list