You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by ch...@apache.org on 2005/10/21 05:11:53 UTC

svn commit: r327079 - in /webservices/axis2/trunk/java/modules/codegen: src/org/apache/axis2/databinding/utils/ADBPullParser.java test/org/apache/axis2/databinding/utils/ADBPullParserTest.java

Author: chinthaka
Date: Thu Oct 20 20:11:36 2005
New Revision: 327079

URL: http://svn.apache.org/viewcvs?rev=327079&view=rev
Log:
Adding more test cases. Fixing no namespace case.

Modified:
    webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/utils/ADBPullParser.java
    webservices/axis2/trunk/java/modules/codegen/test/org/apache/axis2/databinding/utils/ADBPullParserTest.java

Modified: webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/utils/ADBPullParser.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/utils/ADBPullParser.java?rev=327079&r1=327078&r2=327079&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/utils/ADBPullParser.java (original)
+++ webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/utils/ADBPullParser.java Thu Oct 20 20:11:36 2005
@@ -97,52 +97,52 @@
      * @param properties - this should contain all the stuff that stax events should be generated.
      *                   Lets take an example of a bean.
      *                   <pre>
-     *                       <Person>
-     *                              <DependentOne>
-     *                                  <Name>FooTwo</Name>
-     *                                  <Age>25</Age>
-     *                                  <Sex>Male</Sex>
-     *                              </DependentOne>
-     *                      </Person>
-     *                   <p/>
-     *                   <p/>
-     *                   so the mapping bean for this is
-     *                   class Person {
-     *                      String Name;
-     *                      Dependent dependentOne;
-     *                   }
-     *                   <p/>
-     *                   <p/>
-     *                   class Dependent {
-     *                      String name;
-     *                      int age;
-     *                      String sex;
-     *                   }
-     *                   <p/>
-     *                   <p/>
-     *                   So if one needs to generate pull events out of a Person bean, the array he needs
-     *                   to pass is like this.
-     *                   ---------------------------------------------------------------------------------------------------
-     *                   | "Name" | "FooOne" | QName("DependentOne") | Dependent object| null | Array of Dependent objects |
-     *                   ---------------------------------------------------------------------------------------------------
-     *                   This DependentObject can either be an ADBBean, OMElement or a POJO. If its an ADBBean
-     *                   We directly get the pull parser from that. If not we create a reflection based
-     *                   pull parser for that java bean.
-     *                   <p/>
-     *                   <p/>
-     *                                     This is the how the parsed array should look like
-     *                                                       Key             Value
-     *                                                       String              String
-     *                                                       QName               ADBBean, OMElement, Bean
-     *                                                       String              String[]
-     *                                                       QName               Object[] - this contains only one type of objects
-     *                   <p/>
-     *                   <p/>
-     *                                       This is how the passed attribute array should look like
-     *                                                       Key             Value
-     *                                                       null            OMAttribute[]
-     *                                                       QName           String
-     *                                     </pre>
+     *                                                                                               <Person>
+     *                                                                                                      <DependentOne>
+     *                                                                                                          <Name>FooTwo</Name>
+     *                                                                                                          <Age>25</Age>
+     *                                                                                                          <Sex>Male</Sex>
+     *                                                                                                      </DependentOne>
+     *                                                                                              </Person>
+     *                   <p/>
+     *                   <p/>
+     *                                                                                           so the mapping bean for this is
+     *                                                                                           class Person {
+     *                                                                                              String Name;
+     *                                                                                              Dependent dependentOne;
+     *                                                                                           }
+     *                   <p/>
+     *                   <p/>
+     *                                                                                           class Dependent {
+     *                                                                                              String name;
+     *                                                                                              int age;
+     *                                                                                              String sex;
+     *                                                                                           }
+     *                   <p/>
+     *                   <p/>
+     *                                                                                           So if one needs to generate pull events out of a Person bean, the array he needs
+     *                                                                                           to pass is like this.
+     *                                                                                           ---------------------------------------------------------------------------------------------------
+     *                                                                                           | "Name" | "FooOne" | QName("DependentOne") | Dependent object| null | Array of Dependent objects |
+     *                                                                                           ---------------------------------------------------------------------------------------------------
+     *                                                                                           This DependentObject can either be an ADBBean, OMElement or a POJO. If its an ADBBean
+     *                                                                                           We directly get the pull parser from that. If not we create a reflection based
+     *                                                                                           pull parser for that java bean.
+     *                   <p/>
+     *                   <p/>
+     *                                                                                                             This is the how the parsed array should look like
+     *                                                                                                                               Key             Value
+     *                                                                                                                               String              String
+     *                                                                                                                               QName               ADBBean, OMElement, Bean
+     *                                                                                                                               String              String[]
+     *                                                                                                                               QName               Object[] - this contains only one type of objects
+     *                   <p/>
+     *                   <p/>
+     *                                                                                                               This is how the passed attribute array should look like
+     *                                                                                                                               Key             Value
+     *                                                                                                                               null            OMAttribute[]
+     *                                                                                                                               QName           String
+     *                                                                                                             </pre>
      * @return XMLStreamReader
      */
     public static XMLStreamReader createPullParser(QName adbBeansQName, Object[] properties, Object[] attributes) {
@@ -183,6 +183,7 @@
             }
         }
 
+
         if (processingADBNameValuePair && nameValuePairEndElementProcessed) {
             processingADBNameValuePair = false;
             currentIndex = currentIndex + 2;
@@ -201,6 +202,10 @@
             isEndElementFinished = true;
             return XMLStreamConstants.END_ELEMENT;
         } else {
+
+            // first remove all the attributes from the current element
+            attributesList = null;
+
             if (processingADBNameValuePair) {
                 return processADBNameValuePair(null, null);
             }
@@ -220,10 +225,25 @@
                 accessingChildPullParser = true;
                 return this.next();
             } else if (o instanceof String) {
+                Object property = properties[currentIndex];
                 String simplePropertyName = (String) o;
-                String simplePropertyValue = (String) properties[currentIndex];
-                processingADBNameValuePair = true;
-                return processADBNameValuePair(simplePropertyName, simplePropertyValue);
+
+                if (property instanceof String[]) {
+                    String[] stringArray = (String[]) property;
+
+                    // lets create an array out of this and create a pull parser out of this
+                    String[] propertyArray = new String[stringArray.length * 2];
+                    for (int i = 0; i < stringArray.length; i++) {
+                        propertyArray[i] = simplePropertyName;
+                        propertyArray[i + 1] = stringArray[i];
+                    }
+
+                } else if (property instanceof String) {
+                    String simplePropertyValue = (String) properties[currentIndex];
+                    processingADBNameValuePair = true;
+                    return processADBNameValuePair(simplePropertyName, simplePropertyValue);
+                }
+                throw new XMLStreamException("Only String and String[] are accepted as the values when the key is a String");
             } else {
                 throw new XMLStreamException("Sorry !! We only support QNames and Strings as the keys of the properties list");
             }
@@ -235,7 +255,7 @@
         // by this time all the attributes related methods can be called.
         // now need to extract namespace from them and attach them to the element itself.
         String elementNSUri = this.elementQName.getNamespaceURI();
-        if (namespaceMap.get(elementNSUri) == null) {
+        if (!"".equals(elementNSUri) && elementNSUri != null && namespaceMap.get(elementNSUri) == null) {
             String prefix = this.elementQName.getPrefix();
             namespaceMap.put(elementNSUri, prefix);
             if (declaredNamespaces == null) declaredNamespaces = new HashMap();
@@ -288,7 +308,7 @@
 
     public int getAttributeCount() {
         if (accessingChildPullParser) return childPullParser.getAttributeCount();
-        if (attributes != null) return attributesList.size() / 2;
+        if (attributesList != null) return attributesList.size() / 2;
         return 0;
     }
 

Modified: webservices/axis2/trunk/java/modules/codegen/test/org/apache/axis2/databinding/utils/ADBPullParserTest.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/codegen/test/org/apache/axis2/databinding/utils/ADBPullParserTest.java?rev=327079&r1=327078&r2=327079&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/test/org/apache/axis2/databinding/utils/ADBPullParserTest.java (original)
+++ webservices/axis2/trunk/java/modules/codegen/test/org/apache/axis2/databinding/utils/ADBPullParserTest.java Thu Oct 20 20:11:36 2005
@@ -349,6 +349,54 @@
 
     }
 
+    public void testAttributeArray(){
+
+        String expectedXML = "<ns1:TestAttributeArray xmlns:ns1=\"http://testAttributeArray.org\" " +
+                "xmlns:attrNS=\"mailto:whoever@whatever.com\" xmlns:myAttr=\"http://www.axis2.net\" " +
+                "myAttr:Axis2Attr=\"SomeValue\" attrNS:Attr3=\"Value 3\" attrNS:Attr1=\"Value 1\" " +
+                "attrNS:Attr4=\"Value 4\" attrNS:Attr5=\"Value 5\" attrNS:Attr2=\"Value 2\">" +
+                "<Foo>Some Text</Foo><Dependent><Name>FooTwo</Name><Age>25</Age><Sex>Male</Sex>" +
+                "</Dependent></ns1:TestAttributeArray>";
+
+
+        // lets first have some properties
+        ArrayList propertyList = new ArrayList();
+        propertyList.add("Foo");
+        propertyList.add("Some Text");
+        propertyList.add(new QName("Dependent"));
+        DummyADBBean dummyBean = new DummyADBBean();
+        propertyList.add(dummyBean);
+
+        ArrayList attributes = new ArrayList();
+        OMFactory factory = OMAbstractFactory.getOMFactory();
+        QName elementQName = new QName("http://ec.org/software", "Employee", "emp");
+        OMNamespace attrNS = factory.createOMNamespace("mailto:whoever@whatever.com", "attrNS");
+
+        // add some attributes with namespaces
+        OMAttribute[] attribute = new OMAttribute[5];
+        for (int i = 0; i < 5; i++) {
+            attribute[i] = factory.createOMAttribute("Attr" + (i + 1), attrNS, "Value " + (i + 1));
+        }
+        attributes.add(null);
+        attributes.add(attribute);
+        attributes.add(new QName("http://www.axis2.net", "Axis2Attr", "myAttr"));
+        attributes.add("SomeValue");
+
+        XMLStreamReader pullParser = ADBPullParser.createPullParser(new QName("http://testAttributeArray.org", "TestAttributeArray", "ns1"), propertyList.toArray(), attributes.toArray());
+        String actualXML = getStringXML(pullParser);
+
+        try {
+            assertXMLEqual(newDocument(expectedXML), newDocument(actualXML));
+        } catch (ParserConfigurationException e) {
+            fail("Error has occurred "+ e);
+        } catch (SAXException e) {
+            fail("Error has occurred "+ e);
+        } catch (IOException e) {
+            fail("Error has occurred "+ e);
+        }
+
+    }
+
     private String getStringXML(XMLStreamReader reader) {
         OMElement omelement = new StAXOMBuilder(reader).getDocumentElement();
         return omelement.toString();