You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by me...@apache.org on 2007/01/27 12:50:44 UTC

svn commit: r500514 - in /incubator/tuscany/java/sca/services/discovery/jxta: ./ src/main/java/org/apache/tuscany/service/discovery/jxta/stax/ src/test/java/org/apache/tuscany/service/discovery/jxta/stax/ src/test/resources/

Author: meerajk
Date: Sat Jan 27 03:50:43 2007
New Revision: 500514

URL: http://svn.apache.org/viewvc?view=rev&rev=500514
Log:
added stax helper test cases

Added:
    incubator/tuscany/java/sca/services/discovery/jxta/src/test/java/org/apache/tuscany/service/discovery/jxta/stax/
    incubator/tuscany/java/sca/services/discovery/jxta/src/test/java/org/apache/tuscany/service/discovery/jxta/stax/StaxHelperTestCase.java   (with props)
    incubator/tuscany/java/sca/services/discovery/jxta/src/test/resources/test.scdl   (with props)
Modified:
    incubator/tuscany/java/sca/services/discovery/jxta/pom.xml
    incubator/tuscany/java/sca/services/discovery/jxta/src/main/java/org/apache/tuscany/service/discovery/jxta/stax/StaxHelper.java

Modified: incubator/tuscany/java/sca/services/discovery/jxta/pom.xml
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/discovery/jxta/pom.xml?view=diff&rev=500514&r1=500513&r2=500514
==============================================================================
--- incubator/tuscany/java/sca/services/discovery/jxta/pom.xml (original)
+++ incubator/tuscany/java/sca/services/discovery/jxta/pom.xml Sat Jan 27 03:50:43 2007
@@ -59,6 +59,12 @@
             <version>1.2.13</version>
             <scope>compile</scope>
         </dependency>
+
+        <dependency>
+            <groupId>woodstox</groupId>
+            <artifactId>wstx-asl</artifactId>
+            <scope>compile</scope>
+        </dependency>
 		
         <dependency>
             <groupId>junit</groupId>

Modified: incubator/tuscany/java/sca/services/discovery/jxta/src/main/java/org/apache/tuscany/service/discovery/jxta/stax/StaxHelper.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/discovery/jxta/src/main/java/org/apache/tuscany/service/discovery/jxta/stax/StaxHelper.java?view=diff&rev=500514&r1=500513&r2=500514
==============================================================================
--- incubator/tuscany/java/sca/services/discovery/jxta/src/main/java/org/apache/tuscany/service/discovery/jxta/stax/StaxHelper.java (original)
+++ incubator/tuscany/java/sca/services/discovery/jxta/src/main/java/org/apache/tuscany/service/discovery/jxta/stax/StaxHelper.java Sat Jan 27 03:50:43 2007
@@ -23,6 +23,7 @@
 
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamConstants;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 
@@ -35,16 +36,17 @@
  *
  */
 public abstract class StaxHelper {
-    
+
     /** XML input factory. */
-    private static final XMLInputFactory xmlFactory = XMLInputFactory.newInstance("javax.xml.stream.XMLInputFactory", StaxHelper.class.getClassLoader());;
-    
+    private static final XMLInputFactory xmlFactory =
+        XMLInputFactory.newInstance("javax.xml.stream.XMLInputFactory", StaxHelper.class.getClassLoader());;
+
     /**
      * Utility constructor.
      */
     private StaxHelper() {
     }
-    
+
     /**
      * Serializes the infoset in the stream reader.
      * 
@@ -52,9 +54,50 @@
      * @return Serialized XML.
      */
     public static final String serialize(XMLStreamReader reader) {
-        return null;
+
+        try {
+
+            StringBuffer xml = new StringBuffer();
+
+            int event = reader.getEventType();
+            while (reader.hasNext()) {
+
+                switch (event) {
+                    case XMLStreamConstants.START_ELEMENT:
+                        onStartElement(reader, xml);
+                        onNsMappings(reader, xml);
+                        onAttributes(reader, xml);
+                        xml.append(">");
+                        break;
+                    case XMLStreamConstants.CHARACTERS:
+                        if (reader.isWhiteSpace()) {
+                            break;
+                        }
+                        xml.append(reader.getText());
+                        break;
+                    case XMLStreamConstants.END_ELEMENT:
+                        onEndElement(reader, xml);
+                        break;
+                }
+
+                event = reader.next();
+
+            }
+
+            return xml.toString();
+
+        } catch (XMLStreamException ex) {
+            throw new JxtaException(ex);
+        } finally {
+            try {
+                reader.close();
+            } catch (XMLStreamException ex) {
+                throw new JxtaException(ex);
+            }
+        }
+
     }
-    
+
     /**
      * Creates a stream reader to the serialized XML.
      * 
@@ -62,16 +105,32 @@
      * @return XML stream reader instance.
      */
     public static final XMLStreamReader createReader(String xml) {
-        
+
         try {
             InputStream in = new ByteArrayInputStream(xml.getBytes());
             return xmlFactory.createXMLStreamReader(in);
         } catch (XMLStreamException ex) {
             throw new JxtaException(ex);
         }
-        
+
+    }
+
+    /**
+     * Creates a stream reader to the serialized XML.
+     * 
+     * @param xml XML stream to which reader is to be created.
+     * @return XML stream reader instance.
+     */
+    public static final XMLStreamReader createReader(InputStream xml) {
+
+        try {
+            return xmlFactory.createXMLStreamReader(xml);
+        } catch (XMLStreamException ex) {
+            throw new JxtaException(ex);
+        }
+
     }
-    
+
     /**
      * Returns the qualified name of the document element.
      * 
@@ -79,15 +138,84 @@
      * @return Qualified name of the document element.
      */
     public static final QName getDocumentElementQName(String xml) {
-        
+
+        XMLStreamReader reader = null;
         try {
-            XMLStreamReader reader = createReader(xml);
+            reader = createReader(xml);
             reader.next();
             return reader.getName();
         } catch (XMLStreamException ex) {
             throw new JxtaException(ex);
+        } finally {
+            try {
+                reader.close();
+            } catch (XMLStreamException ex) {
+                throw new JxtaException(ex);
+            }
+        }
+
+    }
+
+    /*
+     * Renders end element markup.
+     */
+    private static void onEndElement(XMLStreamReader reader, StringBuffer xml) {
+        String name = getName(reader);
+        xml.append("<");
+        xml.append(name);
+        xml.append("/>");
+    }
+
+    /*
+     * Gets the fully-qualified name of the element.
+     */
+    private static String getName(XMLStreamReader reader) {
+        QName qname = reader.getName();
+        String namePrefix = qname.getPrefix();
+        String localPart = qname.getLocalPart();
+        String name =
+            namePrefix == null || "".equals(namePrefix) ? localPart : namePrefix + ":"
+                + localPart;
+        return name;
+    }
+
+    /*
+     * Render the attributes.
+     */
+    private static void onAttributes(XMLStreamReader reader, StringBuffer xml) {
+        for (int i = 0, n = reader.getAttributeCount(); i < n; ++i) {
+            xml.append(" ");
+            xml.append(reader.getAttributeLocalName(i));
+            xml.append("=");
+            xml.append("'");
+            xml.append(reader.getAttributeValue(i));
+            xml.append("'");
         }
-        
+    }
+
+    /*
+     * Renedr namespace mappings.
+     */
+    private static void onNsMappings(XMLStreamReader reader, StringBuffer xml) {
+        for (int i = 0, n = reader.getNamespaceCount(); i < n; ++i) {
+            String prefix = reader.getNamespacePrefix(i);
+            prefix = prefix == null ? prefix = "xmlns" : "xmlns:" + prefix;
+            xml.append(" ");
+            xml.append(prefix);
+            xml.append("=");
+            xml.append("'");
+            xml.append(reader.getNamespaceURI(i));
+            xml.append("'");
+        }
+    }
+
+    /*
+     * Render start element.
+     */
+    private static void onStartElement(XMLStreamReader reader, StringBuffer xml) {
+        xml.append("<");
+        String name = getName(reader);
+        xml.append(name);
     }
 
 }

Added: incubator/tuscany/java/sca/services/discovery/jxta/src/test/java/org/apache/tuscany/service/discovery/jxta/stax/StaxHelperTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/discovery/jxta/src/test/java/org/apache/tuscany/service/discovery/jxta/stax/StaxHelperTestCase.java?view=auto&rev=500514
==============================================================================
--- incubator/tuscany/java/sca/services/discovery/jxta/src/test/java/org/apache/tuscany/service/discovery/jxta/stax/StaxHelperTestCase.java (added)
+++ incubator/tuscany/java/sca/services/discovery/jxta/src/test/java/org/apache/tuscany/service/discovery/jxta/stax/StaxHelperTestCase.java Sat Jan 27 03:50:43 2007
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.tuscany.service.discovery.jxta.stax;
+
+import java.io.InputStream;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamReader;
+
+import junit.framework.TestCase;
+
+/**
+ * Test case for StaxHelper
+ * 
+ * @version $Revision$ $Date$
+ *
+ */
+public class StaxHelperTestCase extends TestCase {
+
+    public StaxHelperTestCase(String name) {
+        super(name);
+    }
+
+    public void testSerialize() throws Exception {
+        
+        InputStream in = getClass().getClassLoader().getResourceAsStream("test.scdl");
+        XMLStreamReader reader = StaxHelper.createReader(in);
+        StaxHelper.serialize(reader);
+        // TODO Do assertions
+    }
+
+    public void testGetDocumentElementQName() {
+        InputStream in = getClass().getClassLoader().getResourceAsStream("test.scdl");
+        XMLStreamReader reader = StaxHelper.createReader(in);
+        String xml = StaxHelper.serialize(reader);
+        QName qname = StaxHelper.getDocumentElementQName(xml);
+        assertEquals("http://www.osoa.org/xmlns/sca/1.0", qname.getNamespaceURI());
+        assertEquals("composite", qname.getLocalPart());
+    }
+
+}

Propchange: incubator/tuscany/java/sca/services/discovery/jxta/src/test/java/org/apache/tuscany/service/discovery/jxta/stax/StaxHelperTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/services/discovery/jxta/src/test/java/org/apache/tuscany/service/discovery/jxta/stax/StaxHelperTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/tuscany/java/sca/services/discovery/jxta/src/test/resources/test.scdl
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/discovery/jxta/src/test/resources/test.scdl?view=auto&rev=500514
==============================================================================
--- incubator/tuscany/java/sca/services/discovery/jxta/src/test/resources/test.scdl (added)
+++ incubator/tuscany/java/sca/services/discovery/jxta/src/test/resources/test.scdl Sat Jan 27 03:50:43 2007
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.    
+-->
+<!--
+    Test for stax.
+    
+    $Rev$ $Date$
+-->
+<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
+           xmlns:system="http://tuscany.apache.org/xmlns/system/1.0-SNAPSHOT"
+           name="test.scdl">
+
+    <component name="test.component">
+        <system:implementation.system class="test.class"/>
+        <property name="testProperty">123</property>
+    </component>
+
+</composite>

Propchange: incubator/tuscany/java/sca/services/discovery/jxta/src/test/resources/test.scdl
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/services/discovery/jxta/src/test/resources/test.scdl
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: incubator/tuscany/java/sca/services/discovery/jxta/src/test/resources/test.scdl
------------------------------------------------------------------------------
    svn:mime-type = text/xml



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