You are viewing a plain text version of this content. The canonical link for it is here.
Posted to axis-cvs@ws.apache.org by ch...@apache.org on 2005/10/12 08:56:54 UTC

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

Author: chinthaka
Date: Tue Oct 11 23:56:40 2005
New Revision: 314805

URL: http://svn.apache.org/viewcvs?rev=314805&view=rev
Log:
Improving the parser logic, now the recursions works well. Generalizing the pull parser to generate pull events from a bean.

Added:
    webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/util/BeanSeriliazerUtil.java
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=314805&r1=314804&r2=314805&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 Tue Oct 11 23:56:40 2005
@@ -63,38 +63,6 @@
         return isEndElementFinished;
     }
 
-
-    public class ParserInformation {
-        String text;
-        QName name;
-
-        public ParserInformation(QName name, String text) {
-            this.text = text;
-            this.name = name;
-        }
-
-        public ParserInformation(QName name) {
-            this.name = name;
-        }
-
-
-        public String getText() {
-            return text;
-        }
-
-        public void setText(String text) {
-            this.text = text;
-        }
-
-        public QName getName() {
-            return name;
-        }
-
-        public void setName(QName name) {
-            this.name = name;
-        }
-    }
-
     // ----------- XMLStreamReader Methods -------------------------------------------//
     public Object getProperty(String string) throws IllegalArgumentException {
         return null;  //To change body of implemented methods use File | Settings | File Templates.
@@ -115,7 +83,7 @@
             }
         }
 
-        if(processingADBNameValuePair && nameValuePairEndElementProcessed){
+        if (processingADBNameValuePair && nameValuePairEndElementProcessed) {
             processingADBNameValuePair = false;
             currentIndex = currentIndex + 2;
             parserInformation = tempParserInfo;
@@ -137,53 +105,33 @@
             }
             Object o = properties[currentIndex - 1];
             if (o instanceof QName) {
-                ADBBean adbBean = (ADBBean) properties[currentIndex];
-                childPullParser = (ADBPullParser) adbBean.getPullParser((QName) o);
+
+                Object object = properties[currentIndex];
+                if (object instanceof ADBBean) {
+                    ADBBean adbBean = (ADBBean) object;
+                    childPullParser = (ADBPullParser) adbBean.getPullParser((QName) o);
+                } else {
+                    
+                }
                 accessingChildPullParser = true;
                 return this.next();
-            } else {
+            } else if (o instanceof String) {
                 String simplePropertyName = (String) o;
                 String simplePropertyValue = (String) properties[currentIndex];
                 processingADBNameValuePair = true;
                 return processADBNameValuePair(simplePropertyName, simplePropertyValue);
+            } else {
+                throw new XMLStreamException("Sorry !! We only support QNames and Strings as the keys of the properties list");
             }
         }
 
     }
 
-    private int processADBNameValuePair(String simplePropertyName, String simplePropertyValue) {
-        int event = 0;
-        if (!nameValuePairStartElementProcessed) {
-            event = XMLStreamConstants.START_ELEMENT;
-            tempParserInfo = parserInformation;
-            parserInformation = new ParserInformation(new QName(simplePropertyName), simplePropertyValue);
-            nameValuePairStartElementProcessed = true;
-            nameValuePairEndElementProcessed = false;
-        } else if (nameValuePairStartElementProcessed && !nameValuePairTextProcessed) {
-            event = XMLStreamConstants.CHARACTERS;
-            nameValuePairTextProcessed = true;
-        } else if (nameValuePairTextProcessed) {
-            event = XMLStreamConstants.END_ELEMENT;
-            nameValuePairEndElementProcessed = true;
-            nameValuePairStartElementProcessed = false;
-            nameValuePairTextProcessed = false;
-        }
-
-        return event;
-    }
-
-    public ParserInformation getParserInformation() {
-        return parserInformation;
-    }
 
     public boolean hasNext() throws XMLStreamException {
         return !isEndElementFinished;
     }
 
-    private ADBPullParser.ParserInformation getCorrectParserInformation() {
-        return accessingChildPullParser ? childPullParser.getParserInformation() : this.parserInformation;
-    }
-
     public String getElementText() throws XMLStreamException {
         ParserInformation parserInfo = getCorrectParserInformation();
         return parserInfo != null ? parserInfo.getText() : "";
@@ -364,6 +312,77 @@
         throw new UnsupportedOperationException("Yet to be implemented !!");
     }
 
+    // =============================================================================
+    // Utill methods inside this class
+    // =============================================================================
+    private int processADBNameValuePair(String simplePropertyName, String simplePropertyValue) {
+        int event = 0;
+        if (!nameValuePairStartElementProcessed) {
+            event = XMLStreamConstants.START_ELEMENT;
+            tempParserInfo = parserInformation;
+            parserInformation = new ParserInformation(new QName(simplePropertyName), simplePropertyValue);
+            nameValuePairStartElementProcessed = true;
+            nameValuePairEndElementProcessed = false;
+        } else if (nameValuePairStartElementProcessed && !nameValuePairTextProcessed) {
+            event = XMLStreamConstants.CHARACTERS;
+            nameValuePairTextProcessed = true;
+        } else if (nameValuePairTextProcessed) {
+            event = XMLStreamConstants.END_ELEMENT;
+            nameValuePairEndElementProcessed = true;
+            nameValuePairStartElementProcessed = false;
+            nameValuePairTextProcessed = false;
+        }
+        return event;
+    }
+
+    /**
+     * This will returns the parser information
+     */
+    public ParserInformation getParserInformation() {
+        return accessingChildPullParser ? childPullParser.getParserInformation() : this.parserInformation;
+    }
+
+    private ADBPullParser.ParserInformation getCorrectParserInformation() {
+        return accessingChildPullParser ? childPullParser.getParserInformation() : this.parserInformation;
+    }
+
     // --------------------------------------------------------------------------------------------------//
 
+    /**
+     * Inner class which holds stuff for the parser to pick data
+     * This hold the information the parser will hold when user request for data. Every ADBPullParser
+     * hold this kind of object inside it and within the methods of ADBPullParser, they refer to the
+     * fields inside this class. So if user needs to change what parser returns, he just need to
+     * change parser information object.
+     */
+    public class ParserInformation {
+        String text;
+        QName name;
+
+        public ParserInformation(QName name, String text) {
+            this.text = text;
+            this.name = name;
+        }
+
+        public ParserInformation(QName name) {
+            this.name = name;
+        }
+
+
+        public String getText() {
+            return text;
+        }
+
+        public void setText(String text) {
+            this.text = text;
+        }
+
+        public QName getName() {
+            return name;
+        }
+
+        public void setName(QName name) {
+            this.name = name;
+        }
+    }
 }

Added: webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/util/BeanSeriliazerUtil.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/util/BeanSeriliazerUtil.java?rev=314805&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/util/BeanSeriliazerUtil.java (added)
+++ webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/util/BeanSeriliazerUtil.java Tue Oct 11 23:56:40 2005
@@ -0,0 +1,37 @@
+package org.apache.axis2.util;
+
+import org.apache.axis2.databinding.utils.ADBPullParser;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamReader;
+import java.util.ArrayList;
+
+/*
+ * Copyright 2001-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @author : Eran Chinthaka (chinthaka@apache.org)
+ */
+
+public class BeanSeriliazerUtil {
+
+    public static XMLStreamReader getPullParserForObject(Object pojo){
+        // TODO : Please fix this with your magical reflection code
+        ArrayList propertList = new ArrayList();
+        QName elementQName = null;
+
+        return ADBPullParser.createPullParser(propertList.toArray(), elementQName);
+    }
+
+}

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=314805&r1=314804&r2=314805&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 Tue Oct 11 23:56:40 2005
@@ -41,23 +41,39 @@
     public void testSimpleArrayList() {
         try {
 
-            String exptectedXML = "<Project><Name>Axis2</Name><Releases><FirstRelease>0.90</FirstRelease>" +
-                    "<SecondRelease>0.91</SecondRelease><ThirdRelease>0.92</ThirdRelease></Releases>" +
-                    "<Releases><FirstRelease>0.90</FirstRelease><SecondRelease>0.91</SecondRelease>" +
-                    "<ThirdRelease>0.92</ThirdRelease></Releases><Organization>Apache</Organization>" +
-                    "</Project>";
+            /*
+            This is what I expect :
+            <Person>
+                <Name>FooOne</Name>
+                <DependentOne>
+                    <Name>FooTwo</Name>
+                    <Age>25</Age>
+                    <Sex>Male</Sex>
+                </DependentOne>
+                <DependentTwo>
+                    <Name>FooTwo</Name>
+                    <Age>25</Age>
+                    <Sex>Male</Sex>
+                </DependentTwo>
+                <Organization>Apache</Organization>
+            </Person>
+            */
+            String exptectedXML = "<Person><Name>FooOne</Name><DependentOne><Name>FooTwo</Name>" +
+                    "<Age>25</Age><Sex>Male</Sex></DependentOne><DependentTwo><Name>FooTwo</Name>" +
+                    "<Age>25</Age><Sex>Male</Sex></DependentTwo><Organization>Apache</Organization>" +
+                    "</Person>";
 
             ArrayList propertyList = new ArrayList();
             propertyList.add("Name");
-            propertyList.add("Axis2");
-            propertyList.add(new QName("Releases"));
+            propertyList.add("FooOne");
+            propertyList.add(new QName("DependentOne"));
             propertyList.add(new DummyADBBean());
-            propertyList.add(new QName("Releases"));
+            propertyList.add(new QName("DependentTwo"));
             propertyList.add(new DummyADBBean());
             propertyList.add("Organization");
             propertyList.add("Apache");
 
-            QName projectQName = new QName("Project");
+            QName projectQName = new QName("Person");
             XMLStreamReader pullParser = ADBPullParser.createPullParser(propertyList.toArray(), projectQName);
 //            while (pullParser.hasNext()) {
 //                int eventCode = pullParser.next();
@@ -88,76 +104,108 @@
             }
 
 
-            String s = buff.toString();
-            System.out.println("s = " + s);
-            assertEquals(exptectedXML, s);
+            assertEquals(exptectedXML, buff.toString());
         } catch (XMLStreamException e) {
             log.error("Parser Error " + e);
         }
 
     }
 
-//    public void testComplexArrayList() {
-//        try {
-//
-//            String exptectedXML = "<Project><Name>Axis2</Name><Releases><FirstRelease>0.90</FirstRelease>" +
-//                    "<SecondRelease>0.91</SecondRelease><ThirdRelease>0.92</ThirdRelease></Releases>" +
-//                    "<Releases><FirstRelease>0.90</FirstRelease><SecondRelease>0.91</SecondRelease>" +
-//                    "<ThirdRelease>0.92</ThirdRelease></Releases><Organization>Apache</Organization>" +
-//                    "</Project>";
-//
-//            ArrayList propertyList = new ArrayList();
-//            propertyList.add("Name");
-//            propertyList.add("Axis2");
-//            propertyList.add(null);
-//            DummyADBBean dummyBean = new DummyADBBean();
-//            dummyBean.addAnotherBean();
-//            propertyList.add(dummyBean);
-//            propertyList.add("Organization");
-//            propertyList.add("Apache");
-//
-//            QName projectQName = new QName("Project");
-//            XMLStreamReader pullParser = ADBPullParser.createPullParser(propertyList.toArray(), projectQName);
-////            while (pullParser.hasNext()) {
-////                int eventCode = pullParser.next();
-////                System.out.println(eventCode + ":" + getEventString(eventCode));
-////            }
-//
-//            StringBuffer buff = new StringBuffer();
-//            while (pullParser.hasNext()) {
-//                int eventCode = pullParser.next();
-//
-//                switch (eventCode) {
-//                    case XMLStreamConstants.START_ELEMENT :
-//                        System.out.println("<" + pullParser.getLocalName() + ">");
-//                        buff.append("<");
-//                        buff.append(pullParser.getLocalName());
-//                        buff.append(">");
-//                        break;
-//                    case XMLStreamConstants.CHARACTERS :
-//                        System.out.println(pullParser.getText());
-//                        buff.append(pullParser.getText());
-//                        break;
-//                    case XMLStreamConstants.END_ELEMENT :
-//                        System.out.println("</" + pullParser.getLocalName() + ">");
-//                        buff.append("</");
-//                        buff.append(pullParser.getLocalName());
-//                        buff.append(">");
-//                        break;
-//                    default:
-//                        System.out.println("No Other event can be trown here");
-//                }
-//            }
-//
-//
-//            String s = buff.toString();
-//            System.out.println("s = " + s);
-////            assertEquals(exptectedXML, s);
-//        } catch (XMLStreamException e) {
-//            log.error("Parser Error " + e);
-//        }
-//
-//    }
+    public void testComplexArrayList() {
+        try {
+
+            /*
+            This is what I expect :
+
+            <Person>
+                <Name>FooOne</Name>
+                <Organization>Apache</Organization>
+                <Dependent>
+                    <Name>FooTwo</Name>
+                    <Age>25</Age>
+                    <Sex>Male</Sex>
+                    <Depemdent>
+                        <Name>FooTwo</Name>
+                        <Age>25</Age>
+                        <Sex>Male</Sex>
+                            <Depemdent>
+                            <Name>FooTwo</Name>
+                            <Age>25</Age>
+                            <Sex>Male</Sex>
+                    </Depemdent>
+                </Depemdent>
+            </Dependent>
+            <Dependent>
+                <Name>FooTwo</Name>
+                <Age>25</Age>
+                <Sex>Male</Sex>
+                <Depemdent>
+                    <Name>FooTwo</Name>
+                    <Age>25</Age>
+                    <Sex>Male</Sex>
+                </Depemdent>
+            </Dependent>
+        </Person>
+            */
+            String exptectedXML = "<Person><Name>FooOne</Name><Organization>Apache</Organization>" +
+                    "<Dependent><Name>FooTwo</Name><Age>25</Age><Sex>Male</Sex><Depemdent>" +
+                    "<Name>FooTwo</Name><Age>25</Age><Sex>Male</Sex><Depemdent><Name>FooTwo</Name>" +
+                    "<Age>25</Age><Sex>Male</Sex></Depemdent></Depemdent></Dependent><Dependent>" +
+                    "<Name>FooTwo</Name><Age>25</Age><Sex>Male</Sex><Depemdent><Name>FooTwo</Name>" +
+                    "<Age>25</Age><Sex>Male</Sex></Depemdent></Dependent></Person>";
+
+
+            ArrayList propertyList = new ArrayList();
+            propertyList.add("Name");
+            propertyList.add("FooOne");
+
+            propertyList.add("Organization");
+            propertyList.add("Apache");
+
+            propertyList.add(new QName("Dependent"));
+            DummyADBBean dummyBean = new DummyADBBean();
+            ADBPullParserTest.DummyADBBean nextdummyBean = dummyBean.addAnotherBean();
+            nextdummyBean.addAnotherBean();
+            propertyList.add(dummyBean);
+
+            propertyList.add(new QName("Dependent"));
+            dummyBean = new DummyADBBean();
+            dummyBean.addAnotherBean();
+            propertyList.add(dummyBean);
+
+            QName projectQName = new QName("Person");
+            XMLStreamReader pullParser = ADBPullParser.createPullParser(propertyList.toArray(), projectQName);
+
+            StringBuffer buff = new StringBuffer();
+            while (pullParser.hasNext()) {
+                int eventCode = pullParser.next();
+
+                switch (eventCode) {
+                    case XMLStreamConstants.START_ELEMENT :
+                        buff.append("<");
+                        buff.append(pullParser.getLocalName());
+                        buff.append(">");
+                        break;
+                    case XMLStreamConstants.CHARACTERS :
+                        buff.append(pullParser.getText());
+                        break;
+                    case XMLStreamConstants.END_ELEMENT :
+                        buff.append("</");
+                        buff.append(pullParser.getLocalName());
+                        buff.append(">");
+                        break;
+                    default:
+                        System.out.println("No Other event can be trown here");
+                }
+            }
+
+
+            assertEquals(exptectedXML, buff.toString());
+        } catch (XMLStreamException e) {
+            log.error("Parser Error " + e);
+        }
+
+    }
 
     private String getEventString(int eventCode) {
         String event = "";
@@ -216,16 +264,16 @@
         ArrayList propertyList = new ArrayList();
 
         public DummyADBBean() {
-            propertyList.add("FirstRelease");
-            propertyList.add("0.90");
-            propertyList.add("SecondRelease");
-            propertyList.add("0.91");
-            propertyList.add("ThirdRelease");
-            propertyList.add("0.92");
+            propertyList.add("Name");
+            propertyList.add("FooTwo");
+            propertyList.add("Age");
+            propertyList.add("25");
+            propertyList.add("Sex");
+            propertyList.add("Male");
         }
 
         public DummyADBBean addAnotherBean() {
-            propertyList.add(null);
+            propertyList.add(new QName("Depemdent"));
             DummyADBBean dummyBean = new DummyADBBean();
             propertyList.add(dummyBean);
             return dummyBean;