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

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Sep 8 08:12:23 EDT 2008


Author: alex.loubyansky at jboss.com
Date: 2008-09-08 08:12:23 -0400 (Mon, 08 Sep 2008)
New Revision: 2911

Added:
   jbossxb/trunk/src/main/java/org/jboss/xb/annotations/JBossXmlValue.java
   jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/jbossxmlvalue/
   jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/jbossxmlvalue/support/
   jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/jbossxmlvalue/support/MixedType.java
   jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/jbossxmlvalue/support/MixedTypeIgnoreEmptyStringFalse.java
   jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/jbossxmlvalue/support/MixedTypeIgnoreEmptyStringTrue.java
   jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/jbossxmlvalue/test/
   jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/jbossxmlvalue/test/MixedTypeIgnoreEmptyStringUnitTestCase.java
   jbossxb/trunk/src/test/resources/org/jboss/test/xb/builder/object/jbossxmlvalue/
   jbossxb/trunk/src/test/resources/org/jboss/test/xb/builder/object/jbossxmlvalue/test/
   jbossxb/trunk/src/test/resources/org/jboss/test/xb/builder/object/jbossxmlvalue/test/MixedTypeIgnoreEmptyString.xml
Modified:
   jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/CharactersHandler.java
   jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SundayContentHandler.java
   jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/TypeBinding.java
   jbossxb/trunk/src/main/java/org/jboss/xb/builder/JBossXBNoSchemaBuilder.java
Log:
JBXB-151

Added: jbossxb/trunk/src/main/java/org/jboss/xb/annotations/JBossXmlValue.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/annotations/JBossXmlValue.java	                        (rev 0)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/annotations/JBossXmlValue.java	2008-09-08 12:12:23 UTC (rev 2911)
@@ -0,0 +1,40 @@
+/*
+ * 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.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * A JBossXmlValue.
+ * 
+ * @author <a href="alex at jboss.com">Alexey Loubyansky</a>
+ * @version $Revision: 1.1 $
+ */
+ at Target({ElementType.TYPE})
+ at Retention(RetentionPolicy.RUNTIME)
+public @interface JBossXmlValue
+{
+   boolean ignoreEmptyString() default true;
+}

Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/CharactersHandler.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/CharactersHandler.java	2008-09-04 18:27:57 UTC (rev 2910)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/CharactersHandler.java	2008-09-08 12:12:23 UTC (rev 2911)
@@ -56,16 +56,20 @@
 
    public Object unmarshalEmpty(QName qName, TypeBinding typeBinding, NamespaceContext nsCtx, ValueMetaData valueMetaData)
    {
-      Object result = null;
+      if(typeBinding.isIgnoreEmptyString())
+         return null;
+      
+      Object result = "";
       QName typeQName = typeBinding.getQName();
-      if(Constants.QNAME_STRING.equals(typeQName))
+      if(Constants.QNAME_BASE64BINARY.equals(typeQName))
       {
-         result = "";
-      }
-      else if(Constants.QNAME_BASE64BINARY.equals(typeQName))
-      {
          result = new byte[0];
       }
+      else if(Constants.QNAME_BOOLEAN.equals(typeQName))
+      {// this should be an error but this hack is still here
+       // for backwards compatibility in handling empty elements bound to boolean types
+         result = null;
+      }
       return result;
    }
 

Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SundayContentHandler.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SundayContentHandler.java	2008-09-04 18:27:57 UTC (rev 2910)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SundayContentHandler.java	2008-09-08 12:12:23 UTC (rev 2911)
@@ -1038,7 +1038,7 @@
           * (in case of simple types that's not always true and depends on nillable attribute).
           */
          String textContent = item.textContent == null ? "" : item.textContent.toString();
-         if(textContent.length() > 0 || charHandler != null && type.isSimple())
+         if(textContent.length() > 0 || charHandler != null && !type.isIgnoreEmptyString())
          {
             String dataContent;
             SchemaBinding schema = element.getSchema();

Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/TypeBinding.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/TypeBinding.java	2008-09-04 18:27:57 UTC (rev 2910)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/TypeBinding.java	2008-09-08 12:12:23 UTC (rev 2911)
@@ -70,6 +70,7 @@
    
    private Boolean startElementCreatesObject;
    private Boolean simple;
+   private Boolean ignoreEmptyString;
 
    private WildcardBinding wildcard;
    private ParticleBinding particle;
@@ -682,6 +683,16 @@
       return beforeSetParentCallback;
    }
 
+   public boolean isIgnoreEmptyString()
+   {
+      return ignoreEmptyString == null ? !isSimple() : ignoreEmptyString;
+   }
+   
+   public void setIgnoreEmptyString(boolean value)
+   {
+      this.ignoreEmptyString = value;
+   }
+   
    public String toString()
    {
       return super.toString() + "[" + qName + "]";

Modified: jbossxb/trunk/src/main/java/org/jboss/xb/builder/JBossXBNoSchemaBuilder.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/builder/JBossXBNoSchemaBuilder.java	2008-09-04 18:27:57 UTC (rev 2910)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/builder/JBossXBNoSchemaBuilder.java	2008-09-08 12:12:23 UTC (rev 2911)
@@ -88,6 +88,7 @@
 import org.jboss.xb.annotations.JBossXmlPreserveWhitespace;
 import org.jboss.xb.annotations.JBossXmlSchema;
 import org.jboss.xb.annotations.JBossXmlType;
+import org.jboss.xb.annotations.JBossXmlValue;
 import org.jboss.xb.binding.JBossXBRuntimeException;
 import org.jboss.xb.binding.SimpleTypeBindings;
 import org.jboss.xb.binding.sunday.unmarshalling.AllBinding;
@@ -736,7 +737,7 @@
     */
    public TypeBinding generateType(ClassInfo typeInfo, boolean root)
    {
-      // Determine the paremeters
+      // Determine the parameters
       String overrideNamespace = null;
       String overrideName = null;
       ClassInfo factoryClassInfo = typeInfo;
@@ -977,6 +978,14 @@
       {
          CharactersHandler charactersHandler = new ValueHandler(valueProperty);
          typeBinding.setSimpleType(charactersHandler);
+         
+         JBossXmlValue jbossXmlValue = typeInfo.getUnderlyingAnnotation(JBossXmlValue.class);
+         if(jbossXmlValue != null)
+         {
+            if(trace)
+               log.trace("Type " + typeInfo.getName() + " is annotated with @JBossXmlValue.ignoreEmptyString=" + jbossXmlValue.ignoreEmptyString());
+            typeBinding.setIgnoreEmptyString(jbossXmlValue.ignoreEmptyString());
+         }
       }
       else if (trace)
          log.trace("No value for type=" + beanInfo.getName());

Added: jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/jbossxmlvalue/support/MixedType.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/jbossxmlvalue/support/MixedType.java	                        (rev 0)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/jbossxmlvalue/support/MixedType.java	2008-09-08 12:12:23 UTC (rev 2911)
@@ -0,0 +1,59 @@
+/*
+ * 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.xb.builder.object.jbossxmlvalue.support;
+
+
+import javax.xml.bind.annotation.XmlValue;
+
+
+/**
+ * A MixedType.
+ * 
+ * @author <a href="alex at jboss.com">Alexey Loubyansky</a>
+ * @version $Revision: 1.1 $
+ */
+public class MixedType
+{
+   private String child;
+   private String value;
+   
+   public String getChild()
+   {
+      return child;
+   }
+   
+   public void setChild(String child)
+   {
+      this.child = child;
+   }
+   
+   @XmlValue
+   public String getValue()
+   {
+      return value;
+   }
+   
+   public void setValue(String value)
+   {
+      this.value = value;
+   }   
+}

Added: jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/jbossxmlvalue/support/MixedTypeIgnoreEmptyStringFalse.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/jbossxmlvalue/support/MixedTypeIgnoreEmptyStringFalse.java	                        (rev 0)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/jbossxmlvalue/support/MixedTypeIgnoreEmptyStringFalse.java	2008-09-08 12:12:23 UTC (rev 2911)
@@ -0,0 +1,51 @@
+/*
+ * 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.xb.builder.object.jbossxmlvalue.support;
+
+import java.util.List;
+
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.jboss.xb.annotations.JBossXmlValue;
+
+/**
+ * A MixedTypeIgnoreEmptyStringFalse.
+ * 
+ * @author <a href="alex at jboss.com">Alexey Loubyansky</a>
+ * @version $Revision: 1.1 $
+ */
+ at XmlRootElement(name="root")
+ at JBossXmlValue(ignoreEmptyString=false)
+public class MixedTypeIgnoreEmptyStringFalse extends MixedType
+{
+   private List<MixedTypeIgnoreEmptyStringFalse> children;
+
+   public List<MixedTypeIgnoreEmptyStringFalse> getChildren()
+   {
+      return children;
+   }
+
+   public void setChildren(List<MixedTypeIgnoreEmptyStringFalse> children)
+   {
+      this.children = children;
+   }
+}

Added: jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/jbossxmlvalue/support/MixedTypeIgnoreEmptyStringTrue.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/jbossxmlvalue/support/MixedTypeIgnoreEmptyStringTrue.java	                        (rev 0)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/jbossxmlvalue/support/MixedTypeIgnoreEmptyStringTrue.java	2008-09-08 12:12:23 UTC (rev 2911)
@@ -0,0 +1,52 @@
+/*
+ * 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.xb.builder.object.jbossxmlvalue.support;
+
+
+import java.util.List;
+
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.jboss.xb.annotations.JBossXmlValue;
+
+/**
+ * A MixedTypeIgnoreEmptyStringTrue.
+ * 
+ * @author <a href="alex at jboss.com">Alexey Loubyansky</a>
+ * @version $Revision: 1.1 $
+ */
+ at XmlRootElement(name="root")
+ at JBossXmlValue(ignoreEmptyString=true)
+public class MixedTypeIgnoreEmptyStringTrue extends MixedType
+{
+   private List<MixedTypeIgnoreEmptyStringTrue> children;
+
+   public List<MixedTypeIgnoreEmptyStringTrue> getChildren()
+   {
+      return children;
+   }
+
+   public void setChildren(List<MixedTypeIgnoreEmptyStringTrue> children)
+   {
+      this.children = children;
+   }
+}

Added: jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/jbossxmlvalue/test/MixedTypeIgnoreEmptyStringUnitTestCase.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/jbossxmlvalue/test/MixedTypeIgnoreEmptyStringUnitTestCase.java	                        (rev 0)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/jbossxmlvalue/test/MixedTypeIgnoreEmptyStringUnitTestCase.java	2008-09-08 12:12:23 UTC (rev 2911)
@@ -0,0 +1,75 @@
+/*
+ * 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.xb.builder.object.jbossxmlvalue.test;
+
+import java.util.List;
+
+import org.jboss.test.xb.builder.AbstractBuilderTest;
+import org.jboss.test.xb.builder.object.jbossxmlvalue.support.MixedType;
+import org.jboss.test.xb.builder.object.jbossxmlvalue.support.MixedTypeIgnoreEmptyStringFalse;
+import org.jboss.test.xb.builder.object.jbossxmlvalue.support.MixedTypeIgnoreEmptyStringTrue;
+
+/**
+ * A MixedTypeIgnoreEmptyStringUnitTestCase.
+ * 
+ * @author <a href="alex at jboss.com">Alexey Loubyansky</a>
+ * @version $Revision: 1.1 $
+ */
+public class MixedTypeIgnoreEmptyStringUnitTestCase extends AbstractBuilderTest
+{
+   public MixedTypeIgnoreEmptyStringUnitTestCase(String name)
+   {
+      super(name);
+   }
+
+   public void testIgnoreEmptyStringTrue() throws Exception
+   {
+      MixedTypeIgnoreEmptyStringTrue o = unmarshalObject(MixedTypeIgnoreEmptyStringTrue.class);
+      assertEquals("empty string test", o.getChild());
+      assertNull(o.getValue());
+      List<MixedTypeIgnoreEmptyStringTrue> children = o.getChildren();
+      assertNotNull(children);
+      assertEquals(2, children.size());
+      MixedType c = children.get(0);
+      assertNotNull(c);
+      assertNull(c.getValue());
+      c = children.get(1);
+      assertNotNull(c);
+      assertEquals("txt", c.getValue());
+   }
+
+   public void testIgnoreEmptyStringFalse() throws Exception
+   {
+      MixedTypeIgnoreEmptyStringFalse o = unmarshalObject(MixedTypeIgnoreEmptyStringFalse.class);
+      assertEquals("empty string test", o.getChild());
+      assertEquals("", o.getValue());
+      List<MixedTypeIgnoreEmptyStringFalse> children = o.getChildren();
+      assertNotNull(children);
+      assertEquals(2, children.size());
+      MixedType c = children.get(0);
+      assertNotNull(c);
+      assertEquals("", c.getValue());
+      c = children.get(1);
+      assertNotNull(c);
+      assertEquals("txt", c.getValue());
+   }
+}

Added: jbossxb/trunk/src/test/resources/org/jboss/test/xb/builder/object/jbossxmlvalue/test/MixedTypeIgnoreEmptyString.xml
===================================================================
--- jbossxb/trunk/src/test/resources/org/jboss/test/xb/builder/object/jbossxmlvalue/test/MixedTypeIgnoreEmptyString.xml	                        (rev 0)
+++ jbossxb/trunk/src/test/resources/org/jboss/test/xb/builder/object/jbossxmlvalue/test/MixedTypeIgnoreEmptyString.xml	2008-09-08 12:12:23 UTC (rev 2911)
@@ -0,0 +1,5 @@
+<root>
+   <child>empty string test</child>
+   <children/>
+   <children>txt</children>
+</root>




More information about the jboss-svn-commits mailing list