You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by js...@apache.org on 2007/03/14 09:26:11 UTC

svn commit: r518061 - in /incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly: reader/ reader/util/ writer/ writer/util/

Author: jsdelfino
Date: Wed Mar 14 01:26:10 2007
New Revision: 518061

URL: http://svn.apache.org/viewvc?view=rev&rev=518061
Log:
Added test cases to write .componentTypes. Simplified the code used to read SCDL a bit. Moved util classes to a util package.

Added:
    incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/util/
    incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/util/BaseHandler.java   (with props)
    incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/util/NamespaceStack.java   (with props)
    incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/
    incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/ComponentTypeWriter.java   (with props)
    incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/WriteTestCase.java   (with props)
    incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/util/
    incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/util/Attr.java   (with props)
    incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/util/BaseWriter.java   (with props)
Removed:
    incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/BaseHandler.java
    incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/NamespaceStack.java
Modified:
    incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/ComponentTypeHandler.java
    incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/CompositeHandler.java
    incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/ConstrainingTypeHandler.java

Modified: incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/ComponentTypeHandler.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/ComponentTypeHandler.java?view=diff&rev=518061&r1=518060&r2=518061
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/ComponentTypeHandler.java (original)
+++ incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/ComponentTypeHandler.java Wed Mar 14 01:26:10 2007
@@ -24,6 +24,7 @@
 import org.apache.tuscany.assembly.model.Property;
 import org.apache.tuscany.assembly.model.Reference;
 import org.apache.tuscany.assembly.model.Service;
+import org.apache.tuscany.assembly.reader.util.BaseHandler;
 import org.xml.sax.Attributes;
 import org.xml.sax.ContentHandler;
 import org.xml.sax.SAXException;

Modified: incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/CompositeHandler.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/CompositeHandler.java?view=diff&rev=518061&r1=518060&r2=518061
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/CompositeHandler.java (original)
+++ incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/CompositeHandler.java Wed Mar 14 01:26:10 2007
@@ -28,6 +28,7 @@
 import org.apache.tuscany.assembly.model.CompositeReference;
 import org.apache.tuscany.assembly.model.CompositeService;
 import org.apache.tuscany.assembly.model.Property;
+import org.apache.tuscany.assembly.reader.util.BaseHandler;
 import org.xml.sax.Attributes;
 import org.xml.sax.ContentHandler;
 import org.xml.sax.SAXException;

Modified: incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/ConstrainingTypeHandler.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/ConstrainingTypeHandler.java?view=diff&rev=518061&r1=518060&r2=518061
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/ConstrainingTypeHandler.java (original)
+++ incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/ConstrainingTypeHandler.java Wed Mar 14 01:26:10 2007
@@ -24,6 +24,7 @@
 import org.apache.tuscany.assembly.model.AbstractService;
 import org.apache.tuscany.assembly.model.AssemblyFactory;
 import org.apache.tuscany.assembly.model.ConstrainingType;
+import org.apache.tuscany.assembly.reader.util.BaseHandler;
 import org.xml.sax.Attributes;
 import org.xml.sax.ContentHandler;
 import org.xml.sax.SAXException;

Added: incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/util/BaseHandler.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/util/BaseHandler.java?view=auto&rev=518061
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/util/BaseHandler.java (added)
+++ incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/util/BaseHandler.java Wed Mar 14 01:26:10 2007
@@ -0,0 +1,112 @@
+/*
+ * 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.assembly.reader.util;
+
+import javax.xml.namespace.QName;
+
+import org.apache.tuscany.assembly.model.AbstractProperty;
+import org.apache.tuscany.assembly.model.AssemblyFactory;
+import org.apache.tuscany.assembly.model.ConstrainingType;
+import org.apache.tuscany.assembly.model.Property;
+import org.xml.sax.Attributes;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.DefaultHandler;
+
+/**
+ * A test handler to test the usability of the assembly model API when loading SCDL
+ * 
+ * @version $Rev$ $Date$
+ */
+public abstract class BaseHandler extends DefaultHandler implements ContentHandler {
+
+    protected final static String sca10 = "http://www.osoa.org/xmlns/sca/1.0";
+
+    protected NamespaceStack nsStack = new NamespaceStack();
+    protected AssemblyFactory factory;
+    protected XMLReader reader;
+
+    public BaseHandler(AssemblyFactory factory, XMLReader reader) {
+        this.factory = factory;
+        this.reader = reader;
+    }
+
+    protected String getString(Attributes attr, String name) {
+        return attr.getValue(name);
+    }
+
+    protected QName getQName(Attributes attr, String name) {
+        String qName = attr.getValue(name);
+        int index = qName.indexOf(':');
+        String prefix = index == -1 ? "" : qName.substring(0, index);
+        String localName = index == -1 ? qName : qName.substring(index);
+        String ns = nsStack.getNamespaceURI(prefix);
+        if (ns == null) {
+            ns = "";
+        }
+        return new QName(ns, localName, prefix);
+    }
+
+    protected boolean getBoolean(Attributes attr, String name) {
+        return Boolean.valueOf(attr.getValue(name));
+    }
+
+    protected ConstrainingType getConstrainingType(Attributes attr) {
+        String constrainingTypeName = attr.getValue(sca10, "constrainingType");
+        if (constrainingTypeName != null) {
+            ConstrainingType constrainingType = factory.createConstrainingType();
+            constrainingType.setName(new QName(constrainingTypeName));
+            constrainingType.setUndefined(true);
+            return constrainingType;
+        } else {
+            return null;
+        }
+    }
+
+    protected void readAbstractProperty(AbstractProperty prop, Attributes attr) {
+        prop.setName(getString(attr, "name"));
+        prop.setMany(getBoolean(attr, "many"));
+        prop.setMustSupply(getBoolean(attr, "mustSupply"));
+        String xsdElement = getString(attr, "element");
+        if (xsdElement != null) {
+            prop.setXSDElement(new QName(xsdElement));
+        }
+        String xsdType = getString(attr, "type");
+        if (xsdType != null) {
+            prop.setXSDType(new QName(xsdType));
+        }
+        // TODO handle default value
+    }
+
+    protected void readProperty(Property prop, Attributes attr) {
+        readAbstractProperty(prop, attr);
+        // TODO handle property value
+    }
+
+    public void endPrefixMapping(String prefix) throws SAXException {
+        nsStack.endPrefixMapping(prefix);
+    }
+
+    public void startPrefixMapping(String prefix, String uri) throws SAXException {
+        nsStack.startPrefixMapping(prefix, uri);
+    }
+
+}

Propchange: incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/util/BaseHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/util/BaseHandler.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/util/NamespaceStack.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/util/NamespaceStack.java?view=auto&rev=518061
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/util/NamespaceStack.java (added)
+++ incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/util/NamespaceStack.java Wed Mar 14 01:26:10 2007
@@ -0,0 +1,225 @@
+/*
+ * 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.assembly.reader.util;
+
+import java.util.ArrayList;
+import java.util.EmptyStackException;
+
+public class NamespaceStack {
+    private FastStack<String> prefixStack = new FastStack<String>();
+
+    // Keep two arraylists for the prefixes and namespaces. They should be in
+    // sync
+    // since the index of the entry will be used to relate them
+    // use the minimum initial capacity to let things handle memory better
+
+    private FastStack<String> uriStack = new FastStack<String>();
+
+    public String getNamespaceURI(String prefix) {
+        int index = prefixStack.search(prefix);
+        return index == -1 ? null : (String)uriStack.get(index);
+    }
+
+    public String getPrefix(String uri) {
+        int index = uriStack.search(uri);
+        if (index != -1) {
+            return (String)prefixStack.get(index);
+        }
+        return null;
+    }
+
+    /**
+     * Pop a namespace
+     */
+    public void endPrefixMapping(String prefix) {
+        int index = prefixStack.search(prefix);
+        if (index != -1) {
+            prefixStack.remove(index);
+            uriStack.remove(index);
+        }
+    }
+
+    /**
+     * Register a namespace in this context
+     * 
+     * @param prefix
+     * @param uri
+     */
+    public void startPrefixMapping(String prefix, String uri) {
+        prefixStack.push(prefix);
+        uriStack.push(uri);
+
+    }
+
+    /**
+     * An implementation of the {@link java.util.Stack} API that is based on an <code>ArrayList</code> instead of a
+     * <code>Vector</code>, so it is not synchronized to protect against multi-threaded access. The implementation is
+     * therefore operates faster in environments where you do not need to worry about multiple thread contention.
+     * <p>
+     * The removal order of an <code>ArrayStack</code> is based on insertion order: The most recently added element is
+     * removed first. The iteration order is <i>not</i> the same as the removal order. The iterator returns elements
+     * from the bottom up, whereas the {@link #remove()} method removes them from the top down.
+     * <p>
+     * Unlike <code>Stack</code>, <code>ArrayStack</code> accepts null entries.
+     */
+    private static class FastStack<T> extends ArrayList<T> {
+
+        /** Ensure serialization compatibility */
+        private static final long serialVersionUID = 2130079159931574599L;
+
+        /**
+         * Constructs a new empty <code>ArrayStack</code>. The initial size is controlled by <code>ArrayList</code>
+         * and is currently 10.
+         */
+        public FastStack() {
+            super();
+        }
+
+        /**
+         * Constructs a new empty <code>ArrayStack</code> with an initial size.
+         * 
+         * @param initialSize the initial size to use
+         * @throws IllegalArgumentException if the specified initial size is negative
+         */
+        public FastStack(int initialSize) {
+            super(initialSize);
+        }
+
+        /**
+         * Return <code>true</code> if this stack is currently empty.
+         * <p>
+         * This method exists for compatibility with <code>java.util.Stack</code>. New users of this class should use
+         * <code>isEmpty</code> instead.
+         * 
+         * @return true if the stack is currently empty
+         */
+        public boolean empty() {
+            return isEmpty();
+        }
+
+        /**
+         * Returns the top item off of this stack without removing it.
+         * 
+         * @return the top item on the stack
+         * @throws EmptyStackException if the stack is empty
+         */
+        public T peek() throws EmptyStackException {
+            int n = size();
+            if (n <= 0) {
+                throw new EmptyStackException();
+            } else {
+                return get(n - 1);
+            }
+        }
+
+        /**
+         * Returns the n'th item down (zero-relative) from the top of this stack without removing it.
+         * 
+         * @param n the number of items down to go
+         * @return the n'th item on the stack, zero relative
+         * @throws EmptyStackException if there are not enough items on the stack to satisfy this request
+         */
+        public T peek(int n) throws EmptyStackException {
+            int m = (size() - n) - 1;
+            if (m < 0) {
+                throw new EmptyStackException();
+            } else {
+                return get(m);
+            }
+        }
+
+        /**
+         * Pops the top item off of this stack and return it.
+         * 
+         * @return the top item on the stack
+         * @throws EmptyStackException if the stack is empty
+         */
+        public T pop() throws EmptyStackException {
+            int n = size();
+            if (n <= 0) {
+                throw new EmptyStackException();
+            } else {
+                return remove(n - 1);
+            }
+        }
+
+        /**
+         * Pushes a new item onto the top of this stack. The pushed item is also returned. This is equivalent to calling
+         * <code>add</code>.
+         * 
+         * @param item the item to be added
+         * @return the item just pushed
+         */
+        public Object push(T item) {
+            add(item);
+            return item;
+        }
+
+        /**
+         * Returns the one-based position of the distance from the top that the specified object exists on this stack,
+         * where the top-most element is considered to be at distance <code>1</code>. If the object is not present on
+         * the stack, return <code>-1</code> instead. The <code>equals()</code> method is used to compare to the
+         * items in this stack.
+         * 
+         * @param object the object to be searched for
+         * @return index of the stack for the object, or -1 if not found
+         */
+        public int search(T object) {
+            int i = size() - 1; // Current index
+            while (i >= 0) {
+                T current = get(i);
+                if ((object == null && current == null) || (object != null && object.equals(current))) {
+                    return i;
+                }
+                i--;
+            }
+            return -1;
+        }
+
+        /**
+         * Returns the element on the top of the stack.
+         * 
+         * @return the element on the top of the stack
+         * @throws EmptyStackException if the stack is empty
+         */
+        public T get() {
+            int size = size();
+            if (size == 0) {
+                throw new EmptyStackException();
+            }
+            return get(size - 1);
+        }
+
+        /**
+         * Removes the element on the top of the stack.
+         * 
+         * @return the removed element
+         * @throws EmptyStackException if the stack is empty
+         */
+        public T remove() {
+            int size = size();
+            if (size == 0) {
+                throw new EmptyStackException();
+            }
+            return remove(size - 1);
+        }
+
+    }
+
+}

Propchange: incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/util/NamespaceStack.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/reader/util/NamespaceStack.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/ComponentTypeWriter.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/ComponentTypeWriter.java?view=auto&rev=518061
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/ComponentTypeWriter.java (added)
+++ incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/ComponentTypeWriter.java Wed Mar 14 01:26:10 2007
@@ -0,0 +1,71 @@
+/*
+ * 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.assembly.writer;
+
+import org.apache.tuscany.assembly.model.ComponentType;
+import org.apache.tuscany.assembly.model.Property;
+import org.apache.tuscany.assembly.model.Reference;
+import org.apache.tuscany.assembly.model.Service;
+import org.apache.tuscany.assembly.writer.util.Attr;
+import org.apache.tuscany.assembly.writer.util.BaseWriter;
+import org.xml.sax.SAXException;
+
+/**
+ * A test handler to test the usability of the assembly model API when writing SCDL
+ * 
+ * @version $Rev$ $Date$
+ */
+public class ComponentTypeWriter extends BaseWriter {
+
+    private ComponentType componentType;
+
+    public ComponentTypeWriter(ComponentType componentType) {
+        this.componentType = componentType;
+    }
+
+    protected void write() throws SAXException {
+    	
+    	out.startElement(sca10, "componentType", "componentType",
+    		attrs(
+    			new Attr("constrainingType", getConstrainingType(componentType))
+    		));
+    	
+    	for (Service service: componentType.getServices()) {
+    		out.startElement(sca10, "service", "service", attrs(
+    			new Attr("name", service.getName())
+    		));
+    	}
+    	
+    	for (Reference reference: componentType.getReferences()) {
+    		out.startElement(sca10, "reference", "reference", attrs(
+    			new Attr("name", reference.getName())
+    		));
+    	}
+    	
+    	for (Property property: componentType.getProperties()) {
+    		out.startElement(sca10, "property", "property", attrs(
+    			new Attr("name", property.getName())
+    		));
+    	}
+    	
+    	out.endElement(sca10, "componentType", "componentType");
+    }
+    
+}

Propchange: incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/ComponentTypeWriter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/ComponentTypeWriter.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/WriteTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/WriteTestCase.java?view=auto&rev=518061
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/WriteTestCase.java (added)
+++ incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/WriteTestCase.java Wed Mar 14 01:26:10 2007
@@ -0,0 +1,103 @@
+/*
+ * 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.assembly.writer;
+
+import java.io.InputStream;
+
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.stream.StreamResult;
+
+import junit.framework.TestCase;
+
+import org.apache.tuscany.assembly.model.AssemblyFactory;
+import org.apache.tuscany.assembly.model.impl.AssemblyFactoryImpl;
+import org.apache.tuscany.assembly.reader.ComponentTypeHandler;
+import org.apache.tuscany.assembly.reader.CompositeHandler;
+import org.apache.tuscany.assembly.reader.ConstrainingTypeHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.XMLReaderFactory;
+
+/**
+ * Test the usability of the assembly model API when loading SCDL
+ * 
+ * @version $Rev$ $Date$
+ */
+public class WriteTestCase extends TestCase {
+
+    AssemblyFactory factory;
+
+    XMLReader reader;
+    Transformer transformer;
+
+    public void setUp() throws Exception {
+        factory = new AssemblyFactoryImpl();
+
+        reader = XMLReaderFactory.createXMLReader();
+        reader.setFeature("http://xml.org/sax/features/namespaces", true);
+        reader.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
+        
+        transformer = TransformerFactory.newInstance().newTransformer();
+    }
+
+    public void tearDown() throws Exception {
+        factory = null;
+        reader = null;
+        transformer = null;
+    }
+    
+    public static void main(String[] args) throws Exception {
+    	WriteTestCase tc = new WriteTestCase();
+    	tc.setUp();
+		tc.testWriteComponentType();
+	}
+
+    public void testWriteComponentType() throws Exception {
+        InputStream is = getClass().getClassLoader().getResourceAsStream("CalculatorImpl.componentType");
+        ComponentTypeHandler handler = new ComponentTypeHandler(factory, reader);
+        reader.setContentHandler(handler);
+        reader.parse(new InputSource(is));
+        assertNotNull(handler.getComponentType());
+
+        ComponentTypeWriter writer = new ComponentTypeWriter(handler.getComponentType());
+        System.out.println();
+        transformer.transform(new SAXSource(writer, null), new StreamResult(System.out));
+        System.out.println();
+    }
+
+    public void testWriteConstrainingType() throws Exception {
+        InputStream is = getClass().getClassLoader().getResourceAsStream("Calculator.composite");
+        CompositeHandler handler = new CompositeHandler(factory, reader);
+        reader.setContentHandler(handler);
+        reader.parse(new InputSource(is));
+        assertNotNull(handler.getComposite());
+    }
+
+    public void testWriteComposite() throws Exception {
+        InputStream is = getClass().getClassLoader().getResourceAsStream("CalculatorComponent.constrainingType");
+        ConstrainingTypeHandler handler = new ConstrainingTypeHandler(factory, reader);
+        reader.setContentHandler(handler);
+        reader.parse(new InputSource(is));
+        assertNotNull(handler.getConstrainingType());
+    }
+
+}

Propchange: incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/WriteTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/WriteTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/util/Attr.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/util/Attr.java?view=auto&rev=518061
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/util/Attr.java (added)
+++ incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/util/Attr.java Wed Mar 14 01:26:10 2007
@@ -0,0 +1,71 @@
+/*
+ * 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.assembly.writer.util;
+
+import javax.xml.namespace.QName;
+
+import org.xml.sax.helpers.AttributesImpl;
+
+public class Attr {
+	
+	String uri;
+	String name;
+	Object value;
+	
+	public Attr(String uri, String name, String value) {
+		this.uri = uri;
+		this.name = name;
+		this.value = value;
+	}
+
+	public Attr(String name, String value) {
+		this.name = name;
+		this.value = value;
+	}
+
+	public Attr(String uri, String name, boolean value) {
+		this.uri = uri;
+		this.name = name;
+		this.value = value;
+	}
+
+	public Attr(String name, boolean value) {
+		this.name = name;
+		this.value = value;
+	}
+	
+	public Attr(String uri, String name, QName value) {
+		this.uri = uri;
+		this.name = name;
+		this.value = value;
+	}
+
+	public Attr(String name, QName value) {
+		this.name = name;
+		this.value = value;
+	}
+	
+	void write(AttributesImpl attrs) {
+		if (value != null) {
+			attrs.addAttribute(uri, name, name, "CDATA", String.valueOf(value));
+		}
+	}
+	
+}

Propchange: incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/util/Attr.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/util/Attr.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/util/BaseWriter.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/util/BaseWriter.java?view=auto&rev=518061
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/util/BaseWriter.java (added)
+++ incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/util/BaseWriter.java Wed Mar 14 01:26:10 2007
@@ -0,0 +1,97 @@
+/*
+ * 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.assembly.writer.util;
+
+import java.io.IOException;
+
+import javax.xml.namespace.QName;
+
+import org.apache.tuscany.assembly.model.AbstractProperty;
+import org.apache.tuscany.assembly.model.ComponentType;
+import org.apache.tuscany.assembly.model.ConstrainingType;
+import org.apache.tuscany.assembly.model.Property;
+import org.xml.sax.Attributes;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.AttributesImpl;
+import org.xml.sax.helpers.XMLFilterImpl;
+
+/**
+ * A test writer to test the usability of the assembly model API when writing SCDL
+ * 
+ * @version $Rev$ $Date$
+ */
+public abstract class BaseWriter extends XMLFilterImpl {
+
+    protected final static String sca10 = "http://www.osoa.org/xmlns/sca/1.0";
+    protected ContentHandler out;
+
+    /**
+     * This is where you write the logic to produce SCDL.
+     * @throws SAXException
+     */
+    abstract protected void write() throws SAXException;
+    
+    public void parse(InputSource input) throws SAXException, IOException {
+    	out.startDocument();
+    	write();
+    	out.endDocument();
+    }
+    
+    public void setContentHandler(ContentHandler handler) {
+    	super.setContentHandler(handler);
+    	out = handler;
+    }
+    
+    protected Attributes attrs(Attr... attrs) {
+    	AttributesImpl attributes = new AttributesImpl();
+    	for (Attr attr: attrs) {
+    		if (attr != null)
+    			attr.write(attributes);
+    	}
+    	return attributes;
+    }
+
+    protected QName getConstrainingType(ComponentType componentType) {
+    	ConstrainingType constrainingType = componentType.getConstrainingType();
+    	if (constrainingType!=null)
+    		return constrainingType.getName();
+    	else
+    		return null;
+    }
+
+    protected Attributes abstractPropertyAttributes(AbstractProperty prop) {
+    	Attributes attributes = attrs(
+	        new Attr("name", prop.getName()),
+	        new Attr("many", prop.isMany()),
+	        new Attr("mustSupply", prop.isMustSupply()),
+	        new Attr("element", prop.getXSDElement()),
+	        new Attr("type", prop.getXSDType())
+    	);
+        // TODO handle default value
+    	return attributes;
+    }
+
+    protected Attributes propertyAttributes(Property prop) {
+        // TODO handle property value
+        return abstractPropertyAttributes(prop);
+    }
+}

Propchange: incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/util/BaseWriter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/branches/sca-java-integration/sca/scdl/src/test/java/org/apache/tuscany/assembly/writer/util/BaseWriter.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



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