Author: rfeng
Date: Tue Jan 13 14:07:48 2009
New Revision: 734254

URL: http://svn.apache.org/viewvc?rev=734254&view=rev
Log:
Merge the TuscanyNamespaceContext changes from 1.x

Removed:
    
tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ValidatingXMLStreamReader.java.orig
Modified:
    
tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyNamespaceContext.java
    
tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyXMLStreamReader.java
    
tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ValidatingXMLStreamReader.java

Modified: 
tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyNamespaceContext.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyNamespaceContext.java?rev=734254&r1=734253&r2=734254&view=diff
==============================================================================
--- 
tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyNamespaceContext.java
 (original)
+++ 
tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyNamespaceContext.java
 Tue Jan 13 14:07:48 2009
@@ -1,124 +1,93 @@
 package org.apache.tuscany.sca.contribution.processor;
 
-import java.util.List;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Stack;
 
-import javax.xml.XMLConstants;
 import javax.xml.namespace.NamespaceContext;
 
-...@suppresswarnings("unused")
 public class TuscanyNamespaceContext implements NamespaceContext {
 
-       private Stack<ArrayList<ArrayList<String>>> context = null;
+    private Stack<List<String>[]> context = null;
 
-       public TuscanyNamespaceContext(Stack<ArrayList<ArrayList<String>>> 
context){
-               this.context = context;
-       }
-
-       public String getNamespaceURI(String prefix) {
-               if (prefix == null) {
-                       throw new IllegalArgumentException();
-               }
-               return (String) getResult("getNSUri",prefix);
-       }
-
-       public String getPrefix(String namespaceURI) {
-               if (namespaceURI == null) {
-                       throw new IllegalArgumentException();
-               }
-               return (String) getResult("getPrefix",namespaceURI);
-       }
-
-       @SuppressWarnings("unchecked")
-       public Iterator<String> getPrefixes(String namespaceURI) {
-               if (namespaceURI == null) {
-                       throw new IllegalArgumentException();
-               }
-               
-               Iterator<String> iterator = new Itr<String>((Iterator<String>) 
getResult("getPrefixes",namespaceURI));
-               return iterator;
-       }
-       
-       /*
-        * Generic method to Iterate through the Stack and return required 
result(s) 
-        */
-       private Object getResult(String operation,String arg){
-               
-               ArrayList<ArrayList<String>> contextList = null;
-               Iterator<String> prefItr = null;
-               Iterator<String> uriItr = null;
-               
-               List<String> list = new ArrayList<String>();;
-               
-               String toCompare = null;
-               
-               String tempPrefix = null;
-               String tempUri = null ;
-               
-               for(int i  = context.size()-1; i>=0;i--){
-                       contextList = context.get(i);
-                       prefItr = 
((ArrayList<String>)contextList.get(0)).iterator();
-                       uriItr = 
((ArrayList<String>)contextList.get(1)).iterator();
-                       for(int j = 0;uriItr.hasNext();j++){
-                               tempPrefix = (String) prefItr.next();
-                               tempUri = (String) uriItr.next();
-                               if(operation.equalsIgnoreCase("getNSUri")){
-                                       toCompare = tempPrefix;
-                               }
-                               else 
if(operation.equalsIgnoreCase("getPrefix")){
-                                       toCompare = tempUri;
-                               }
-                               else 
if(operation.equalsIgnoreCase("getPrefixes")){
-                                       toCompare = tempUri;
-                               }
-                               if(toCompare != null && 
arg.equalsIgnoreCase(toCompare)){
-                                       
if(operation.equalsIgnoreCase("getNSUri")){
-                                               return tempUri;
-                                       }
-                                       else 
if(operation.equalsIgnoreCase("getPrefix")){
-                                               return tempPrefix;
-                                       }
-                                       else 
if(operation.equalsIgnoreCase("getPrefixes")){
-                                               list.add(tempPrefix);
-                                               
-                                       }
-                                       
-                               }
-                       }
-               }
-               
-               if(operation.equalsIgnoreCase("getPrefixes")){
-                       return list.iterator();
-               }
-               
-               return null;
-       }
-
-       /*
-        * Custom implementation of the Iterator interface to override the 
behavior of the remove() method.
-        * The iterator should not be modifiable and invocation of the remove() 
method should throw UnsupportedOperationException. 
-        */
-       private class Itr<E> implements Iterator<E>{
-               Iterator<E> iterator = null;
-
-               Itr(Iterator<E> inputItr){
-                       this.iterator = inputItr;
-               }
-               
-               public boolean hasNext() {
-                       return this.iterator.hasNext();
-               }
-
-               public E next() {
-                       return (E) this.iterator.next();
-               }
-
-               public void remove() {
-                       throw new  UnsupportedOperationException();
-               }
-       } //end of class Itr<E>
-       
-       
+    public TuscanyNamespaceContext(Stack<List<String>[]> context) {
+        this.context = context;
+    }
+
+    public String getNamespaceURI(String prefix) {
+        if (prefix == null) {
+            throw new IllegalArgumentException();
+        }
+        return (String)getResult("getNSUri", prefix);
+    }
+
+    public String getPrefix(String namespaceURI) {
+        if (namespaceURI == null) {
+            throw new IllegalArgumentException();
+        }
+        return (String)getResult("getPrefix", namespaceURI);
+    }
+
+    @SuppressWarnings("unchecked")
+    public Iterator<String> getPrefixes(String namespaceURI) {
+        if (namespaceURI == null) {
+            throw new IllegalArgumentException();
+        }
+
+        Iterator<String> iterator = (Iterator<String>)getResult("getPrefixes", 
namespaceURI);
+        return iterator;
+    }
+
+    /*
+     * Generic method to Iterate through the Stack and return required 
result(s) 
+     */
+    private Object getResult(String operation, String arg) {
+
+        List<String>[] contextList = null;
+        Iterator<String> prefItr = null;
+        Iterator<String> uriItr = null;
+
+        List<String> list = new ArrayList<String>();
+
+        String toCompare = null;
+
+        String tempPrefix = null;
+        String tempUri = null;
+
+        for (int i = context.size() - 1; i >= 0; i--) {
+            contextList = context.get(i);
+            prefItr = contextList[0].iterator();
+            uriItr = contextList[1].iterator();
+            while (uriItr.hasNext()) {
+                tempPrefix = prefItr.next();
+                tempUri = uriItr.next();
+                if (operation.equalsIgnoreCase("getNSUri")) {
+                    toCompare = tempPrefix;
+                } else if (operation.equalsIgnoreCase("getPrefix")) {
+                    toCompare = tempUri;
+                } else if (operation.equalsIgnoreCase("getPrefixes")) {
+                    toCompare = tempUri;
+                }
+                if (toCompare != null && arg.equalsIgnoreCase(toCompare)) {
+                    if (operation.equalsIgnoreCase("getNSUri")) {
+                        return tempUri;
+                    } else if (operation.equalsIgnoreCase("getPrefix")) {
+                        return tempPrefix;
+                    } else if (operation.equalsIgnoreCase("getPrefixes")) {
+                        list.add(tempPrefix);
+                    }
+
+                }
+            }
+        }
+
+        if (operation.equalsIgnoreCase("getPrefixes")) {
+            return Collections.unmodifiableList(list).iterator();
+        }
+
+        return null;
+    }
+
 } //end of Class

Modified: 
tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyXMLStreamReader.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyXMLStreamReader.java?rev=734254&r1=734253&r2=734254&view=diff
==============================================================================
--- 
tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyXMLStreamReader.java
 (original)
+++ 
tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyXMLStreamReader.java
 Tue Jan 13 14:07:48 2009
@@ -1,4 +1,5 @@
 package org.apache.tuscany.sca.contribution.processor;
+
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -21,92 +22,88 @@
 import java.util.List;
 import java.util.Stack;
 
+import javax.xml.XMLConstants;
 import javax.xml.namespace.NamespaceContext;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.util.StreamReaderDelegate;
-import javax.xml.XMLConstants;
 
 /*
  * Custom implementation of the XMLStreamReader to keep track of the namespace 
context for each element
  */
-public class TuscanyXMLStreamReader extends StreamReaderDelegate implements
-               XMLStreamReader {
+public class TuscanyXMLStreamReader extends StreamReaderDelegate implements 
XMLStreamReader {
+
+    Stack<List<String>[]> context = new Stack<List<String>[]>();
 
-       Stack<ArrayList<ArrayList<String>>> context = new 
Stack<ArrayList<ArrayList<String>>>();
-       
-       List contextList;
-       List<String> prefixList;
-       List<String> uriList;
-
-       public TuscanyXMLStreamReader(XMLStreamReader reader) {
-               super(reader);
-       }
-
-       public void pushContext() throws XMLStreamException {
-               contextList = new ArrayList<ArrayList<String>>();
-               prefixList = new ArrayList<String>();
-               uriList = new ArrayList<String>();
-               int namespaceCount = this.getNamespaceCount();
-               if (namespaceCount == 0) {
-                       prefixList.add(null);
-                       uriList.add(null);
-               }
-               for (int i = 0; i < namespaceCount; i++) {
-                       prefixList.add(checkString(this.getNamespacePrefix(i)));
-                       uriList.add(this.getNamespaceURI(i));
-               }
-               contextList.add(prefixList);
-               contextList.add(uriList);
-               context.push((ArrayList) contextList);
-       }
-
-       private String checkString(String namespacePrefix) {
-               if (namespacePrefix == null) {
-                       return XMLConstants.DEFAULT_NS_PREFIX;
-               } else {
-                       return namespacePrefix;
-               }
-       }
-
-       public void popContext() throws XMLStreamException {
-               context.pop();
-       }
-
-       /*
-        * Overriding the next() method to perform PUSH and POP operations 
-        * for the NamespaceContext for the current element
-        */
-                       
-       @Override
-       public int next() throws XMLStreamException {
-               // POP the context if the element ends
-               if (this.getEventType() == END_ELEMENT) {
-                       popContext();
-               }
-               
-               //get the next event 
-               int nextEvent = super.next();
-               //PUSH the events info onto the Stack 
-               if (nextEvent == START_ELEMENT) {
-                       pushContext();
-               }
-               return nextEvent;
-       }
-       
-       @Override
-       public int nextTag() throws XMLStreamException {
-               if (this.getEventType() == START_ELEMENT) {
-                       pushContext();
-               }
-               if (this.getEventType() == END_ELEMENT) {
-                       popContext();
-               }
-               return super.nextTag();
-       }
-       
-       @Override
-       public NamespaceContext getNamespaceContext(){
-               return new TuscanyNamespaceContext((Stack)context.clone());
-       }
+    List<String>[] contextList;
+    List<String> prefixList;
+    List<String> uriList;
+
+    public TuscanyXMLStreamReader(XMLStreamReader reader) {
+        super(reader);
+    }
+
+    public void pushContext() throws XMLStreamException {
+        contextList = new List[2];
+        prefixList = new ArrayList<String>();
+        uriList = new ArrayList<String>();
+        int namespaceCount = this.getNamespaceCount();
+        for (int i = 0; i < namespaceCount; i++) {
+            prefixList.add(checkString(this.getNamespacePrefix(i)));
+            uriList.add(this.getNamespaceURI(i));
+        }
+        contextList[0] = prefixList;
+        contextList[1] = uriList;
+        context.push(contextList);
+    }
+
+    private String checkString(String namespacePrefix) {
+        if (namespacePrefix == null) {
+            return XMLConstants.DEFAULT_NS_PREFIX;
+        } else {
+            return namespacePrefix;
+        }
+    }
+
+    public void popContext() throws XMLStreamException {
+        context.pop();
+    }
+
+    /*
+     * Overriding the next() method to perform PUSH and POP operations 
+     * for the NamespaceContext for the current element
+     */
+
+    @Override
+    public int next() throws XMLStreamException {
+        // POP the context if the element ends
+        if (this.getEventType() == END_ELEMENT) {
+            popContext();
+        }
+
+        //get the next event 
+        int nextEvent = super.next();
+        //PUSH the events info onto the Stack 
+        if (nextEvent == START_ELEMENT) {
+            pushContext();
+        }
+        return nextEvent;
+    }
+
+    @Override
+    public int nextTag() throws XMLStreamException {
+        int event = super.nextTag();
+        if (event == START_ELEMENT) {
+            pushContext();
+        }
+        if (event == END_ELEMENT) {
+            popContext();
+        }
+        return event;
+    }
+
+    @Override
+    public NamespaceContext getNamespaceContext() {
+        return new 
TuscanyNamespaceContext((Stack<List<String>[]>)context.clone());
+    }
 }

Modified: 
tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ValidatingXMLStreamReader.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ValidatingXMLStreamReader.java?rev=734254&r1=734253&r2=734254&view=diff
==============================================================================
--- 
tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ValidatingXMLStreamReader.java
 (original)
+++ 
tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ValidatingXMLStreamReader.java
 Tue Jan 13 14:07:48 2009
@@ -199,6 +199,7 @@
                     case XMLStreamConstants.START_ELEMENT:
                         level++;
                         handleStartElement();
+                        pushContext();
                         return event;
                     case XMLStreamConstants.PROCESSING_INSTRUCTION:
                         handler.processingInstruction(super.getPITarget(), 
super.getPIData());
@@ -212,6 +213,7 @@
                     case XMLStreamConstants.END_ELEMENT:
                         handleEndElement();
                         level--;
+                        popContext();
                         return event;
                     case XMLStreamConstants.END_DOCUMENT:
                         handler.endDocument();


Reply via email to