You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xmlbeans-cvs@xml.apache.org by zi...@apache.org on 2004/01/29 04:48:28 UTC

cvs commit: xml-xmlbeans/v2/test/cases/marshal doc2.xml

zieg        2004/01/28 19:48:28

  Modified:    v2/src/marshal/org/apache/xmlbeans/impl/marshal
                        ByNameRuntimeBindingType.java
                        ByNameUnmarshaller.java MarshalStreamUtils.java
                        UnmarshalResult.java UnmarshallerImpl.java
               v2/test/cases/marshal doc2.xml
  Log:
  implement attribute defaulting
  fix whitespace bug at beginning of document.
  
  Revision  Changes    Path
  1.23      +91 -5     xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/ByNameRuntimeBindingType.java
  
  Index: ByNameRuntimeBindingType.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/ByNameRuntimeBindingType.java,v
  retrieving revision 1.22
  retrieving revision 1.23
  diff -u -r1.22 -r1.23
  --- ByNameRuntimeBindingType.java	27 Jan 2004 01:30:16 -0000	1.22
  +++ ByNameRuntimeBindingType.java	29 Jan 2004 03:48:28 -0000	1.23
  @@ -56,6 +56,7 @@
   
   package org.apache.xmlbeans.impl.marshal;
   
  +import org.apache.xmlbeans.XmlException;
   import org.apache.xmlbeans.XmlRuntimeException;
   import org.apache.xmlbeans.impl.binding.bts.BindingLoader;
   import org.apache.xmlbeans.impl.binding.bts.BindingProperty;
  @@ -70,10 +71,13 @@
   import org.apache.xmlbeans.impl.marshal.util.collections.AccumulatorFactory;
   
   import javax.xml.namespace.QName;
  +import javax.xml.stream.XMLStreamException;
  +import javax.xml.stream.XMLStreamReader;
  +import java.io.StringReader;
   import java.lang.reflect.InvocationTargetException;
   import java.lang.reflect.Method;
  -import java.util.Iterator;
   import java.util.Collection;
  +import java.util.Iterator;
   
   
   final class ByNameRuntimeBindingType
  @@ -84,6 +88,7 @@
       private final Property[] attributeProperties;
       private final Property[] elementProperties;
       private final boolean hasMulti;  //has any multi properties
  +    private final boolean hasDefaultAttributes;  //has any attributes with defaults
   
       //is this a subtype of something besides the ultimate parent type?
       //(XmlObject or java.lang.Object, though only the latter
  @@ -106,19 +111,25 @@
           int elem_prop_cnt = 0;
           int att_prop_cnt = 0;
           boolean has_multi = false;
  +        boolean has_attribute_defaults = false;
           final Collection type_props = btype.getProperties();
           for (Iterator itr = type_props.iterator(); itr.hasNext();) {
               QNameProperty p = (QNameProperty)itr.next();
               if (p.isMultiple()) has_multi = true;
  -            if (p.isAttribute())
  +            if (p.isAttribute()) {
                   att_prop_cnt++;
  -            else
  +                if (p.getDefault() != null) {
  +                    has_attribute_defaults = true;
  +                }
  +            } else {
                   elem_prop_cnt++;
  +            }
           }
   
           attributeProperties = new Property[att_prop_cnt];
           elementProperties = new Property[elem_prop_cnt];
           hasMulti = has_multi;
  +        hasDefaultAttributes = has_attribute_defaults;
   
           isSubType = determineIsSubType(javaClass);
       }
  @@ -208,13 +219,18 @@
   
       //TODO: optimize this linear scan
       RuntimeBindingProperty getMatchingAttributeProperty(String uri,
  -                                                        String localname)
  +                                                        String localname,
  +                                                        UnmarshalResult context)
       {
           for (int i = 0, len = attributeProperties.length; i < len; i++) {
               final Property prop = attributeProperties[i];
   
  -            if (doesPropMatch(uri, localname, prop))
  +            if (doesPropMatch(uri, localname, prop)) {
  +                if (hasDefaultAttributes && (prop.defaultValue != null)) {
  +                    context.attributePresent(i);
  +                }
                   return prop;
  +            }
           }
           return null;
       }
  @@ -250,11 +266,30 @@
           return isSubType;
       }
   
  +    public boolean hasDefaultAttributes()
  +    {
  +        return hasDefaultAttributes;
  +    }
  +
       public QName getSchemaTypeName()
       {
           return byNameBean.getName().getXmlName().getQName();
       }
   
  +    public void fillDefaultAttributes(Object inter, UnmarshalResult context)
  +    {
  +        if (!hasDefaultAttributes) return;
  +
  +        for (int aidx = 0, alen = attributeProperties.length; aidx < alen; aidx++) {
  +            final Property p = attributeProperties[aidx];
  +
  +            if (p.defaultValue == null) continue;
  +            if (context.isAttributePresent(aidx)) continue;
  +
  +            p.fillDefaultValue(inter);
  +        }
  +    }
  +
   
       private static final class Property implements RuntimeBindingProperty
       {
  @@ -270,6 +305,7 @@
           private final Method getMethod;
           private final Method setMethod;
           private final boolean javaPrimitive;
  +        private final Object defaultValue;
   
           private static final Object[] EMPTY_OBJECT_ARRAY = new Object[]{};
   
  @@ -298,6 +334,47 @@
               getMethod = getGetterMethod(prop, beanClass);
               setMethod = getSetterMethod(prop, beanClass);
               javaPrimitive = propertyClass.isPrimitive();
  +
  +            String def = bindingProperty.getDefault();
  +            if (def != null) {
  +                defaultValue = extractDefaultObject(def, bindingType,
  +                                                    typeTable, loader);
  +                if (!prop.isAttribute()) {
  +                    //TODO: deal with defaulting elements!
  +                    System.out.println("Default elements not supported: " + this);
  +                }
  +            } else {
  +                defaultValue = null;
  +            }
  +        }
  +
  +
  +        //REVIEW: find a shorter path to our goal.
  +        private static Object extractDefaultObject(String value,
  +                                                   BindingType bindingType,
  +                                                   RuntimeBindingTypeTable typeTable,
  +                                                   BindingLoader loader)
  +        {
  +            final String xmldoc = "<a>" + value + "</a>";
  +            try {
  +                final UnmarshallerImpl um = new UnmarshallerImpl(loader, typeTable);
  +                final StringReader sr = new StringReader(xmldoc);
  +                final XMLStreamReader reader =
  +                    um.getXmlInputFactory().createXMLStreamReader(sr);
  +                final BindingTypeName btname = bindingType.getName();
  +                final Object obj =
  +                    um.unmarshalType(reader, btname.getXmlName().getQName(),
  +                                     btname.getJavaName().toString());
  +                reader.close();
  +                sr.close();
  +                return obj;
  +            }
  +            catch (XmlException e) {
  +                throw new XmlRuntimeException(e);
  +            }
  +            catch (XMLStreamException e) {
  +                throw new XmlRuntimeException(e);
  +            }
           }
   
           private Class getPropertyClass(QNameProperty prop, BindingType btype)
  @@ -468,6 +545,14 @@
               }
           }
   
  +
  +        public void fillDefaultValue(Object inter)
  +        {
  +            assert (defaultValue != null);
  +
  +            this.fill(inter, defaultValue);
  +        }
  +
           public void fillCollection(final Object inter, final Object prop_obj)
           {
               assert isMultiple();
  @@ -584,6 +669,7 @@
           {
               return bindingProperty.getQName();
           }
  +
   
       }
   
  
  
  
  1.12      +3 -1      xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/ByNameUnmarshaller.java
  
  Index: ByNameUnmarshaller.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/ByNameUnmarshaller.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- ByNameUnmarshaller.java	26 Jan 2004 03:34:36 -0000	1.11
  +++ ByNameUnmarshaller.java	29 Jan 2004 03:48:28 -0000	1.12
  @@ -162,6 +162,8 @@
   
               context.advanceAttribute();
           }
  +
  +       type.fillDefaultAttributes(inter, context);
       }
   
       private RuntimeBindingProperty findMatchingAttributeProperty(UnmarshalResult context)
  @@ -169,7 +171,7 @@
           String uri = context.getCurrentAttributeNamespaceURI();
           String lname = context.getCurrentAttributeLocalName();
   
  -        return type.getMatchingAttributeProperty(uri, lname);
  +        return type.getMatchingAttributeProperty(uri, lname, context);
       }
   
       private RuntimeBindingProperty findMatchingElementProperty(UnmarshalResult context)
  
  
  
  1.12      +3 -1      xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/MarshalStreamUtils.java
  
  Index: MarshalStreamUtils.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/MarshalStreamUtils.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- MarshalStreamUtils.java	26 Jan 2004 03:34:36 -0000	1.11
  +++ MarshalStreamUtils.java	29 Jan 2004 03:48:28 -0000	1.12
  @@ -273,8 +273,10 @@
   
                           //eventually we'll handle these...
                       case XMLStreamReader.ATTRIBUTE:
  +                        throw new AssertionError("NAKED ATTRIBUTE UNIMPLEMENTED");
                       case XMLStreamReader.CHARACTERS:
  -                        throw new AssertionError("UNIMPLEMENTED TYPE: " + state);
  +                        if (rdr.isWhiteSpace()) break;
  +                        throw new AssertionError("NAKED CHARDATA UNIMPLEMENTED");
   
                           //bad news in the xml stream
                       case XMLStreamReader.END_DOCUMENT:
  
  
  
  1.3       +23 -0     xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/UnmarshalResult.java
  
  Index: UnmarshalResult.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/UnmarshalResult.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- UnmarshalResult.java	26 Jan 2004 03:34:36 -0000	1.2
  +++ UnmarshalResult.java	29 Jan 2004 03:48:28 -0000	1.3
  @@ -79,6 +79,7 @@
   import java.io.InputStream;
   import java.math.BigDecimal;
   import java.math.BigInteger;
  +import java.util.BitSet;
   import java.util.Collection;
   import java.util.Date;
   
  @@ -103,6 +104,7 @@
       private final XsiAttributeHolder xsiAttributeHolder =
           new XsiAttributeHolder();
       private boolean gotXsiAttributes;
  +    private BitSet defaultAttributeBits;
       private int currentAttributeIndex = INVALID;
       private int currentAttributeCount = INVALID;
   
  @@ -722,6 +724,9 @@
       {
           xsiAttributeHolder.reset();
           gotXsiAttributes = false;
  +        if (defaultAttributeBits != null) {
  +            defaultAttributeBits.clear();
  +        }
           if (baseReader.isStartElement()) {
               currentAttributeCount = baseReader.getAttributeCount();
               currentAttributeIndex = 0;
  @@ -801,6 +806,24 @@
           assert currentAttributeIndex != INVALID;
   
           return baseReader.getAttributeLocalName(currentAttributeIndex);
  +    }
  +
  +    public void attributePresent(int att_idx)
  +    {
  +        if (defaultAttributeBits == null) {
  +            int bits_size = getAttributeCount();
  +            defaultAttributeBits = new BitSet(bits_size);
  +        }
  +
  +        defaultAttributeBits.set(att_idx);
  +    }
  +
  +    public boolean isAttributePresent(int att_idx)
  +    {
  +        if (defaultAttributeBits == null)
  +            return false;
  +
  +        return defaultAttributeBits.get(att_idx);
       }
   
   }
  
  
  
  1.17      +5 -0      xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/UnmarshallerImpl.java
  
  Index: UnmarshallerImpl.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/UnmarshallerImpl.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- UnmarshallerImpl.java	26 Jan 2004 03:34:36 -0000	1.16
  +++ UnmarshallerImpl.java	29 Jan 2004 03:48:28 -0000	1.17
  @@ -143,4 +143,9 @@
   
           return result.unmarshalType(reader, schemaType, javaType);
       }
  +
  +    XMLInputFactory getXmlInputFactory()
  +    {
  +        return xmlInputFactory;
  +    }
   }
  
  
  
  1.6       +3 -1      xml-xmlbeans/v2/test/cases/marshal/doc2.xml
  
  Index: doc2.xml
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/test/cases/marshal/doc2.xml,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- doc2.xml	15 Dec 2003 05:03:32 -0000	1.5
  +++ doc2.xml	29 Jan 2004 03:48:28 -0000	1.6
  @@ -7,7 +7,9 @@
   <!--    xsi:type="n1:MyClass"-->
   
   
  -    <n1:Myelt n1:Attrib="  5555.54321">
  +<!--    <n1:Myelt n1:Attrib="  5555.54321">-->
  +
  +    <n1:Myelt>
   
           <SomeBool xmlns="java:com.mytest">1</SomeBool>
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xmlbeans-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-cvs-help@xml.apache.org