axis-java-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ami...@apache.org
Subject svn commit: r558841 - in /webservices/axis2/trunk/java/modules/adb: src/org/apache/axis2/databinding/ src/org/apache/axis2/databinding/utils/writer/ test/org/apache/axis2/databinding/utils/writer/
Date Mon, 23 Jul 2007 19:31:13 GMT
Author: amilas
Date: Mon Jul 23 12:31:12 2007
New Revision: 558841

URL: http://svn.apache.org/viewvc?view=rev&rev=558841
Log:
add a new OMElement builder to produce the xmlStream reader

Added:
    webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/utils/writer/
    webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/utils/writer/OMElementStreamWriter.java
    webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/utils/writer/OMStreamNamespaceContext.java
    webservices/axis2/trunk/java/modules/adb/test/org/apache/axis2/databinding/utils/writer/
    webservices/axis2/trunk/java/modules/adb/test/org/apache/axis2/databinding/utils/writer/OMElementStreamWriterTest.java
Modified:
    webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/ADBDataSource.java

Modified: webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/ADBDataSource.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/ADBDataSource.java?view=diff&rev=558841&r1=558840&r2=558841
==============================================================================
--- webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/ADBDataSource.java
(original)
+++ webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/ADBDataSource.java
Mon Jul 23 12:31:12 2007
@@ -21,6 +21,7 @@
 import org.apache.axiom.om.OMDataSource;
 import org.apache.axiom.om.OMOutputFormat;
 import org.apache.axiom.om.util.StAXUtils;
+import org.apache.axis2.databinding.utils.writer.OMElementStreamWriter;
 
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLStreamException;
@@ -83,8 +84,10 @@
      */
     public XMLStreamReader getReader() throws XMLStreamException {
         // since only ADBBeans related to elements can be serialized
-        // we are safe in passing null here. 
-        return bean.getPullParser(parentQName);
+        // we are safe in passing null here.
+        OMElementStreamWriter omElementStreamWriter = new OMElementStreamWriter();
+        serialize(omElementStreamWriter);
+        return omElementStreamWriter.getOMElement().getXMLStreamReader();
     }
 
 }

Added: webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/utils/writer/OMElementStreamWriter.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/utils/writer/OMElementStreamWriter.java?view=auto&rev=558841
==============================================================================
--- webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/utils/writer/OMElementStreamWriter.java
(added)
+++ webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/utils/writer/OMElementStreamWriter.java
Mon Jul 23 12:31:12 2007
@@ -0,0 +1,229 @@
+/*
+ * Copyright 2004,2005 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.
+ */
+package org.apache.axis2.databinding.utils.writer;
+
+import org.apache.axiom.om.*;
+
+import javax.xml.stream.XMLStreamWriter;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.namespace.NamespaceContext;
+import java.util.Stack;
+import java.util.Map;
+import java.util.HashMap;
+
+
+public class OMElementStreamWriter implements XMLStreamWriter {
+
+    // this is the om Element we are going to create
+    private OMElement rootElement;
+    private OMFactory omFactory;
+
+    private OMElement currentOMElement;
+
+    private Stack omElementStack;
+
+    // this map contains the namespace key and OMNamespace
+    private Map namespaceOMNamesapceMap;
+
+    private int prefixNum;
+
+    private OMStreamNamespaceContext omStreamNamespaceContext;
+
+
+    public OMElementStreamWriter() {
+        omFactory = OMAbstractFactory.getOMFactory();
+        omElementStack = new Stack();
+        currentOMElement = null;
+        omStreamNamespaceContext = new OMStreamNamespaceContext();
+        namespaceOMNamesapceMap = new HashMap();
+        prefixNum = 0;
+
+    }
+
+    // serailizer must have finish serializing when we call this method.
+    public OMElement getOMElement() throws XMLStreamException {
+        if (!omElementStack.isEmpty()) {
+            throw new XMLStreamException("This is an invalid Xml ");
+        }
+        return rootElement;
+    }
+
+    private OMNamespace getOMNamespace(String namespace, String prefix) throws XMLStreamException
{
+        OMNamespace omNamespace = null;
+        if (namespace != null) {
+            if (namespaceOMNamesapceMap.containsKey(namespace)) {
+                omNamespace = (OMNamespace) namespaceOMNamesapceMap.get(namespace);
+            } else {
+                if (prefix == null) {
+                    prefix = "ns" + ++prefixNum;
+                } else if (this.omStreamNamespaceContext.getNamespaceURI(prefix) != null)
{
+                    throw new XMLStreamException("the prefix ==> " + prefix +
+                            " Already exists for namespace ==> " + namespace);
+                }
+                omNamespace = omFactory.createOMNamespace(namespace, prefix);
+                this.omStreamNamespaceContext.registerNamespace(namespace, prefix);
+                namespaceOMNamesapceMap.put(namespace, omNamespace);
+            }
+        }
+        return omNamespace;
+    }
+
+    public void writeStartElement(String localName) throws XMLStreamException {
+        writeStartElement(null, localName, null);
+    }
+
+    public void writeStartElement(String namespace, String localName) throws XMLStreamException
{
+        writeStartElement(null, localName, namespace);
+    }
+
+    public void writeStartElement(String prefix, String localName, String namespace) throws
XMLStreamException {
+        OMNamespace omNamespace = getOMNamespace(namespace, prefix);
+        currentOMElement = omFactory.createOMElement(localName, omNamespace);
+        if (!omElementStack.isEmpty()) {
+            // we always keep the parent at the top of the stack
+            OMElement parent = (OMElement) omElementStack.peek();
+            parent.addChild(currentOMElement);
+        } else {
+            // i.e this must be an start root element
+            rootElement = currentOMElement;
+        }
+        // set this as the top element
+        omElementStack.push(currentOMElement);
+    }
+
+    public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException
{
+        writeEmptyElement(null, localName, namespaceURI);
+    }
+
+    public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws
XMLStreamException {
+        writeStartElement(prefix, localName, namespaceURI);
+        writeEndElement();
+    }
+
+    public void writeEmptyElement(String localName) throws XMLStreamException {
+        writeEmptyElement(null, localName, null);
+    }
+
+    public void writeEndElement() throws XMLStreamException {
+        omElementStack.pop();
+    }
+
+    public void writeEndDocument() throws XMLStreamException {
+        // nothing to do
+    }
+
+    public void close() throws XMLStreamException {
+        // nothing to do
+    }
+
+    public void flush() throws XMLStreamException {
+        // nothing to do
+    }
+
+    public void writeAttribute(String attributeName, String attributeValue) throws XMLStreamException
{
+        writeAttribute(null, null, attributeName, attributeValue);
+    }
+
+    public void writeAttribute(String prefix, String namespace, String attributeName, String
attributeValue) throws XMLStreamException {
+        currentOMElement.addAttribute(attributeName, attributeValue, getOMNamespace(namespace,
prefix));
+    }
+
+    public void writeAttribute(String namespace, String attributeName, String attributeValue)
throws XMLStreamException {
+        writeAttribute(null, namespace, attributeName, attributeValue);
+    }
+
+    public void writeNamespace(String prefix, String namespaceURI) throws XMLStreamException
{
+        if (namespaceURI != null) {
+            OMNamespace omNamespace = getOMNamespace(namespaceURI, prefix);
+            currentOMElement.declareNamespace(omNamespace);
+        }
+    }
+
+    public void writeDefaultNamespace(String namespace) throws XMLStreamException {
+        rootElement.declareDefaultNamespace(namespace);
+        getOMNamespace(namespace, "");
+    }
+
+    public void writeComment(String string) throws XMLStreamException {
+        omFactory.createOMComment(currentOMElement,string);
+    }
+
+    public void writeProcessingInstruction(String string) throws XMLStreamException {
+        throw new UnsupportedOperationException("this method has not yet been implemented");
+    }
+
+    public void writeProcessingInstruction(String string, String string1) throws XMLStreamException
{
+        throw new UnsupportedOperationException("this method has not yet been implemented");
+    }
+
+    public void writeCData(String string) throws XMLStreamException {
+        throw new UnsupportedOperationException("this method has not yet been implemented");
+    }
+
+    public void writeDTD(String string) throws XMLStreamException {
+        throw new UnsupportedOperationException("this method has not yet been implemented");
+    }
+
+    public void writeEntityRef(String string) throws XMLStreamException {
+        throw new UnsupportedOperationException("this method has not yet been implemented");
+    }
+
+    public void writeStartDocument() throws XMLStreamException {
+        // nothing to do
+    }
+
+    public void writeStartDocument(String string) throws XMLStreamException {
+        // nothing to do
+    }
+
+    public void writeStartDocument(String string, String string1) throws XMLStreamException
{
+        // nothing to do
+    }
+
+    public void writeCharacters(String string) throws XMLStreamException {
+        currentOMElement.setText(string);
+    }
+
+    public void writeCharacters(char[] chars, int i, int i1) throws XMLStreamException {
+        writeCharacters(new String(chars, i, i1));
+    }
+
+    public String getPrefix(String namespace) throws XMLStreamException {
+        return this.omStreamNamespaceContext.getPrefix(namespace);
+    }
+
+    public void setPrefix(String prefix, String uri) throws XMLStreamException {
+        // this method will add the namespace correctly.
+        getOMNamespace(uri, prefix);
+    }
+
+    public void setDefaultNamespace(String namespace) throws XMLStreamException {
+        rootElement.declareDefaultNamespace(namespace);
+        getOMNamespace(namespace,"");
+    }
+
+    public void setNamespaceContext(NamespaceContext namespaceContext) throws XMLStreamException
{
+        throw new UnsupportedOperationException("this method has not yet been implemented");
+    }
+
+    public NamespaceContext getNamespaceContext() {
+        return this.omStreamNamespaceContext;
+    }
+
+    public Object getProperty(String string) throws IllegalArgumentException {
+        throw new UnsupportedOperationException("this method has not yet been implemented");
+    }
+}

Added: webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/utils/writer/OMStreamNamespaceContext.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/utils/writer/OMStreamNamespaceContext.java?view=auto&rev=558841
==============================================================================
--- webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/utils/writer/OMStreamNamespaceContext.java
(added)
+++ webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/utils/writer/OMStreamNamespaceContext.java
Mon Jul 23 12:31:12 2007
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2004,2005 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.
+ */
+package org.apache.axis2.databinding.utils.writer;
+
+import javax.xml.namespace.NamespaceContext;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.HashMap;
+
+
+public class OMStreamNamespaceContext implements NamespaceContext {
+
+    private Map namespaceToPrefixMap;
+    private Map prefixToNamespaceMap;
+
+    public OMStreamNamespaceContext() {
+        this.namespaceToPrefixMap = new HashMap();
+        this.prefixToNamespaceMap = new HashMap();
+    }
+
+    public void registerNamespace(String namespace,String prefix){
+        this.namespaceToPrefixMap.put(namespace,prefix);
+        this.prefixToNamespaceMap.put(prefix,namespace);
+    }
+
+    public String getNamespaceURI(String prefix) {
+        return (String) prefixToNamespaceMap.get(prefix);
+    }
+
+    public String getPrefix(String namespaceURI) {
+        return (String) namespaceToPrefixMap.get(namespaceURI);
+    }
+
+    public Iterator getPrefixes(String namespaceURI) {
+        return prefixToNamespaceMap.keySet().iterator();
+    }
+
+
+}

Added: webservices/axis2/trunk/java/modules/adb/test/org/apache/axis2/databinding/utils/writer/OMElementStreamWriterTest.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/adb/test/org/apache/axis2/databinding/utils/writer/OMElementStreamWriterTest.java?view=auto&rev=558841
==============================================================================
--- webservices/axis2/trunk/java/modules/adb/test/org/apache/axis2/databinding/utils/writer/OMElementStreamWriterTest.java
(added)
+++ webservices/axis2/trunk/java/modules/adb/test/org/apache/axis2/databinding/utils/writer/OMElementStreamWriterTest.java
Mon Jul 23 12:31:12 2007
@@ -0,0 +1,218 @@
+/*
+ * Copyright 2004,2005 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.
+ */
+package org.apache.axis2.databinding.utils.writer;
+
+import junit.framework.TestCase;
+
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.namespace.QName;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.impl.builder.StAXOMBuilder;
+import org.apache.axis2.databinding.utils.ConverterUtil;
+import org.apache.axis2.databinding.utils.BeanUtil;
+
+
+public class OMElementStreamWriterTest extends TestCase {
+
+    private int prefixNum;
+
+    public void testGetOMElement() {
+
+        OMElementStreamWriter omElementStreamWriter = new OMElementStreamWriter();
+
+        try {
+
+            writeStartElement(null, "test1", omElementStreamWriter);
+            writeStartElement("http://apach.temp.uri", "test2", omElementStreamWriter);
+            writeStartElement("http://apach.temp.uri1", "test3", omElementStreamWriter);
+            omElementStreamWriter.writeCharacters("test string3");
+            writeAttribute("http://apach.temp.uri", "attribute1", "attrubteValue1", omElementStreamWriter);
+            writeQNames(new QName[]{new QName("http://axis2.apach.org1", "testQName"),
+                    new QName("http://axis2.apach.org2", "testQName2")}, omElementStreamWriter);
+            omElementStreamWriter.writeEndElement();
+            writeStartElement("http://apach.temp.uri1", "test4", omElementStreamWriter);
+            writeAttribute(null, "attribute1", "attrubteValue1", omElementStreamWriter);
+            writeAttribute("ns1", "http://apach.temp.uri", "attribute1", "attrubteValue1",
omElementStreamWriter);
+            omElementStreamWriter.writeCharacters("test string4");
+            omElementStreamWriter.writeEndElement();
+            writeStartElement("http://temp.new.org", "testattributeElement", omElementStreamWriter);
+            writeQNameAttribute(null, "testQname", new QName("http://temp.new.org", "testQName"),
omElementStreamWriter);
+            writeQName(new QName("http://axis2.apach.org", "testQName"), omElementStreamWriter);
+            omElementStreamWriter.writeEndElement();
+            omElementStreamWriter.writeEndElement();
+            omElementStreamWriter.writeEndElement();
+
+            OMElement omElement = omElementStreamWriter.getOMElement();
+            System.out.println("OM String ==> " + omElement.toString());
+
+            XMLStreamReader xmlReader = omElement.getXMLStreamReader();
+            StAXOMBuilder stAXOMBuilder = new StAXOMBuilder(xmlReader);
+            OMElement result = stAXOMBuilder.getDocumentElement();
+
+            System.out.println("OM result ==> " + result.toString());
+        } catch (XMLStreamException e) {
+            fail();
+        }
+
+    }
+
+    private String generatePrefix(String string) {
+        return "ns" + ++prefixNum;
+    }
+
+    private void writeStartElement(String namespace, String localName, XMLStreamWriter xmlWriter)
throws XMLStreamException {
+
+        if ((namespace != null) && ! namespace.equals("")) {
+            java.lang.String prefix = xmlWriter.getPrefix(namespace);
+
+            if (prefix == null) {
+                prefix = "ns" + ++prefixNum;
+                xmlWriter.writeStartElement(prefix, localName, namespace);
+                xmlWriter.writeNamespace(prefix, namespace);
+                xmlWriter.setPrefix(prefix, namespace);
+            } else {
+                xmlWriter.writeStartElement(namespace, localName);
+            }
+        } else {
+            xmlWriter.writeStartElement(localName);
+        }
+    }
+
+    private void writeAttribute(String prefix, String namespace, String attName,
+                                String attValue, XMLStreamWriter xmlWriter)
+            throws XMLStreamException {
+        if (xmlWriter.getPrefix(namespace) == null) {
+            xmlWriter.writeNamespace(prefix, namespace);
+            xmlWriter.setPrefix(prefix, namespace);
+
+        }
+        xmlWriter.writeAttribute(namespace, attName, attValue);
+    }
+
+    private void writeAttribute(String namespace, String attName,
+                                String attValue, XMLStreamWriter xmlWriter) throws XMLStreamException
{
+        if ((namespace == null) || namespace.equals("")) {
+            xmlWriter.writeAttribute(attName, attValue);
+        } else {
+            registerPrefix(xmlWriter, namespace);
+            xmlWriter.writeAttribute(namespace, attName, attValue);
+        }
+    }
+
+    private void writeQNameAttribute(String namespace, String attName,
+                                     QName qname, XMLStreamWriter xmlWriter) throws javax.xml.stream.XMLStreamException
{
+
+        String attributeNamespace = qname.getNamespaceURI();
+        String attributePrefix = xmlWriter.getPrefix(attributeNamespace);
+        if (attributePrefix == null) {
+            attributePrefix = registerPrefix(xmlWriter, attributeNamespace);
+        }
+        java.lang.String attributeValue;
+        if (attributePrefix.trim().length() > 0) {
+            attributeValue = attributePrefix + ":" + qname.getLocalPart();
+        } else {
+            attributeValue = qname.getLocalPart();
+        }
+
+        if ((namespace != null) && namespace.equals("")) {
+            xmlWriter.writeAttribute(attName, attributeValue);
+        } else {
+            if (namespace != null) {
+                registerPrefix(xmlWriter, namespace);
+            }
+            xmlWriter.writeAttribute(namespace, attName, attributeValue);
+        }
+    }
+
+    private void writeQName(QName qname,
+                            XMLStreamWriter xmlWriter) throws javax.xml.stream.XMLStreamException
{
+        String namespaceURI = qname.getNamespaceURI();
+        if (namespaceURI != null) {
+            String prefix = xmlWriter.getPrefix(namespaceURI);
+            if (prefix == null) {
+                prefix = generatePrefix(namespaceURI);
+                xmlWriter.writeNamespace(prefix, namespaceURI);
+                xmlWriter.setPrefix(prefix, namespaceURI);
+            }
+
+            if (prefix.trim().length() > 0) {
+                xmlWriter.writeCharacters(prefix + ":" + ConverterUtil.convertToString(qname));
+            } else {
+                // i.e this is the default namespace
+                xmlWriter.writeCharacters(ConverterUtil.convertToString(qname));
+            }
+
+        } else {
+            xmlWriter.writeCharacters(ConverterUtil.convertToString(qname));
+        }
+    }
+
+    private void writeQNames(QName[] qnames,
+                             XMLStreamWriter xmlWriter) throws XMLStreamException {
+
+        if (qnames != null) {
+            // we have to store this data until last moment since it is not possible to write
any
+            // namespace data after writing the charactor data
+            StringBuffer stringToWrite = new StringBuffer();
+            String namespaceURI = null;
+            String prefix = null;
+
+            for (int i = 0; i < qnames.length; i++) {
+                if (i > 0) {
+                    stringToWrite.append(" ");
+                }
+                namespaceURI = qnames[i].getNamespaceURI();
+                if (namespaceURI != null) {
+                    prefix = xmlWriter.getPrefix(namespaceURI);
+                    if ((prefix == null) || (prefix.length() == 0)) {
+                        prefix = generatePrefix(namespaceURI);
+                        xmlWriter.writeNamespace(prefix, namespaceURI);
+                        xmlWriter.setPrefix(prefix, namespaceURI);
+                    }
+
+                    if (prefix.trim().length() > 0) {
+                        stringToWrite.append(prefix).append(":").append(ConverterUtil.convertToString(qnames[i]));
+                    } else {
+                        stringToWrite.append(ConverterUtil.convertToString(qnames[i]));
+                    }
+                } else {
+                    stringToWrite.append(ConverterUtil.convertToString(qnames[i]));
+                }
+            }
+            xmlWriter.writeCharacters(stringToWrite.toString());
+        }
+
+    }
+
+
+    private String registerPrefix(XMLStreamWriter xmlWriter, String namespace) throws XMLStreamException
{
+        String prefix = xmlWriter.getPrefix(namespace);
+
+        if (prefix == null) {
+            prefix = generatePrefix(namespace);
+            while (xmlWriter.getNamespaceContext().getNamespaceURI(prefix) != null) {
+                prefix = BeanUtil.getUniquePrefix();
+            }
+            xmlWriter.writeNamespace(prefix, namespace);
+            xmlWriter.setPrefix(prefix, namespace);
+        }
+        return prefix;
+    }
+
+}



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


Mime
View raw message