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/19 07:43:33 UTC
svn commit: r326406 - 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: Tue Oct 18 22:43:16 2005
New Revision: 326406
URL: http://svn.apache.org/viewcvs?rev=326406&view=rev
Log:
Adding the ability for attributes to have namespaces. Test case also added with this. But need to do some more testing as to how this works with a bean which has the same namespace.
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=326406&r1=326405&r2=326406&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 18 22:43:16 2005
@@ -11,7 +11,9 @@
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
+import java.util.ArrayList;
import java.util.HashMap;
+import java.util.Iterator;
/*
* Copyright 2004,2005 The Apache Software Foundation.
@@ -58,8 +60,14 @@
// a pointer to the children list of current location
private int currentIndex = 0;
+ // namespace handling
private HashMap declaredNamespaces;
+ // the following two are to improve the efficiency in code and should not be used by anyone as
+ // the values these holds may be out of date.
+ private ArrayList tempDeclaredNamespacesArray;
+ private ArrayList tempDeclaredNamespacePrefixesArray;
+
/**
* This namespace map will contain uri as the key and the prefix as the value, so that I can check
* whether a particular namespace is there or not easily.
@@ -84,35 +92,35 @@
* @param properties - this should contain all the stuff that stax events should be generated.
* Lets take an example of a bean.
* <pre> <Person>
- * <Name>FooOne</Name>
- * <DependentOne>
- * <Name>FooTwo</Name>
- * <Age>25</Age>
- * <Sex>Male</Sex>
- * </DependentOne>
- * </Person>
+ * <Name>FooOne</Name>
+ * <DependentOne>
+ * <Name>FooTwo</Name>
+ * <Age>25</Age>
+ * <Sex>Male</Sex>
+ * </DependentOne>
+ * </Person>
* <p/>
- * so the mapping bean for this is
- * class Person {
- * String Name;
- * Dependent dependentOne;
- * }
+ * so the mapping bean for this is
+ * class Person {
+ * String Name;
+ * Dependent dependentOne;
+ * }
* <p/>
- * class Dependent {
- * String name;
- * int age;
- * String sex;
- * }
+ * class Dependent {
+ * String name;
+ * int age;
+ * String sex;
+ * }
* <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.
- * </pre>
+ * 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.
+ * </pre>
* @param adbBeansQName
* @return XMLStreamReader
*/
@@ -249,9 +257,8 @@
}
public int getNamespaceCount() {
-// if (accessingChildPullParser) return childPullParser.getNamespaceCount();
-// return declaredNamespaces == null ? 0 : declaredNamespaces.size();
- return 0;
+ if (accessingChildPullParser) return childPullParser.getNamespaceCount();
+ return declaredNamespaces == null ? 0 : declaredNamespaces.size();
}
public String getText() {
@@ -385,12 +392,20 @@
throw new UnsupportedOperationException("Yet to be implemented !!");
}
- public String getNamespaceURI(String namespaceURI) {
-
-// if(accessingChildPullParser) return childPullParser.getNamespaceURI(namespaceURI);
-// if(declaredNamespaces != null) return declaredNamespaces.get(namespaceURI);
-// return this.elementQName.getNamespaceURI();
- return namespaceURI;
+ public String getNamespaceURI(String prefixParam) {
+ if (accessingChildPullParser) return childPullParser.getNamespaceURI(prefixParam);
+ if ("".equals(prefixParam) || prefixParam == null) return null;
+
+ if (declaredNamespaces != null) {
+ Iterator nsIter = declaredNamespaces.keySet().iterator();
+ while (nsIter.hasNext()) {
+ String nsURI = (String) nsIter.next();
+ if (prefixParam.equals(declaredNamespaces.get(nsURI))) {
+ return nsURI;
+ }
+ }
+ }
+ return null;
}
public boolean isStartElement() {
@@ -410,12 +425,50 @@
}
- public String getNamespacePrefix(int i) {
- throw new UnsupportedOperationException("Yet to be implemented !!");
+ public String getNamespacePrefix(int index) {
+ /* We are holding namespaces in a HashMap and there is no direct way to retrieve a prefix
+ by its index. So I need to call toArray and then get the index. Since this method will be
+ called recursively and if we call toArray again and again, this is a performance killer.
+ So I create a temp array, at the first time this method being called and use that to serve
+ future requests. But at the same time I check whether the temp array contains up-to-date
+ information or not.
+ */
+ if (accessingChildPullParser) return childPullParser.getNamespacePrefix(index);
+ if (declaredNamespaces != null && declaredNamespaces.size() >= index) {
+ if (tempDeclaredNamespacePrefixesArray == null || tempDeclaredNamespacePrefixesArray.size() != declaredNamespaces.size()) {
+ tempDeclaredNamespacePrefixesArray = new ArrayList();
+ Iterator iterator = declaredNamespaces.values().iterator();
+ while (iterator.hasNext()) {
+ String s = (String) iterator.next();
+ tempDeclaredNamespacePrefixesArray.add(s);
+ }
+ }
+ return (String) tempDeclaredNamespacePrefixesArray.get(index);
+ }
+ return null;
}
- public String getNamespaceURI(int i) {
- throw new UnsupportedOperationException("Yet to be implemented !!");
+ public String getNamespaceURI(int index) {
+ /* We are holding namespaces in a HashMap and there is no direct way to retrieve a namespace
+ by its index. So I need to call toArray and then get the index. Since this method will be
+ called recursively and if we call toArray again and again, this is a performance killer.
+ So I create a temp array, at the first time this method being called and use that to serve
+ future requests. But at the same time I check whether the temp array contains up-to-date
+ information or not.
+ */
+ if (accessingChildPullParser) return childPullParser.getNamespaceURI(index);
+ if (declaredNamespaces != null && declaredNamespaces.size() >= index) {
+ if (tempDeclaredNamespacesArray == null || tempDeclaredNamespacesArray.size() != declaredNamespaces.size()) {
+ tempDeclaredNamespacesArray = new ArrayList();
+ Iterator iterator = declaredNamespaces.keySet().iterator();
+ while (iterator.hasNext()) {
+ String s = (String) iterator.next();
+ tempDeclaredNamespacesArray.add(s);
+ }
+ }
+ return (String) tempDeclaredNamespacesArray.get(index);
+ }
+ return null;
}
public NamespaceContext getNamespaceContext() {
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=326406&r1=326405&r2=326406&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 18 22:43:16 2005
@@ -5,6 +5,7 @@
import org.apache.axis2.om.OMAttribute;
import org.apache.axis2.om.OMElement;
import org.apache.axis2.om.OMFactory;
+import org.apache.axis2.om.OMNamespace;
import org.apache.axis2.om.impl.llom.builder.StAXOMBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -87,30 +88,6 @@
QName projectQName = new QName("Person");
XMLStreamReader pullParser = ADBPullParser.createPullParser(projectQName, propertyList.toArray(), null);
-// 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");
-// }
-// }
-
-
Document actualDom = newDocument(getStringXML(pullParser));
Document expectedDocument = newDocument(exptectedXML);
assertXMLEqual(actualDom, expectedDocument);
@@ -190,29 +167,6 @@
QName projectQName = new QName("Person");
XMLStreamReader pullParser = ADBPullParser.createPullParser(projectQName, propertyList.toArray(), null, true);
-// 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");
-// }
-// }
-
Document actualDom = newDocument(getStringXML(pullParser));
Document expectedDocument = newDocument(exptectedXML);
assertXMLEqual(actualDom, expectedDocument);
@@ -318,6 +272,40 @@
}
String stringXML = getStringXML(ADBPullParser.createPullParser(elementQName, null, attribute, true));
+ try {
+ Document actualDom = newDocument(stringXML);
+ Document expectedDocument = newDocument(expectedXML);
+ assertXMLEqual(actualDom, expectedDocument);
+ } catch (ParserConfigurationException e) {
+ fail("Exception in parsing documents " + e);
+ } catch (SAXException e) {
+ fail("Exception in parsing documents " + e);
+ } catch (IOException e) {
+ fail("Exception in parsing documents " + e);
+ }
+
+
+ }
+
+ public void testAttributesWithNamespaces() throws XMLStreamException {
+
+ String expectedXML = "<emp:Employee xmlns:emp=\"http://ec.org/software\" " +
+ "xmlns:attrNS=\"mailto:whoever@whatever.com\" attrNS:Attr2=\"Value 2\" " +
+ "attrNS:Attr3=\"Value 3\" attrNS:Attr1=\"Value 1\"\n" +
+ " attrNS:Attr5=\"Value 5\" attrNS:Attr4=\"Value 4\"></emp:Employee>";
+
+ 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));
+ }
+
+ String stringXML = getStringXML(ADBPullParser.createPullParser(elementQName, null, attribute, true));
+ System.out.println("stringXML = " + stringXML);
try {
Document actualDom = newDocument(stringXML);
Document expectedDocument = newDocument(expectedXML);