On Tue, Jan 13, 2009 at 7:09 AM, <[email protected]> wrote:

> Author: rfeng
> Date: Mon Jan 12 23:08:59 2009
> New Revision: 734056
>
> URL: http://svn.apache.org/viewvc?rev=734056&view=rev
> Log:
> Fix the StAX namespace context
>
> Removed:
>
>  
> tuscany/branches/sca-java-1.x/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ValidatingXMLStreamReader.java.orig
> Modified:
>
>  
> tuscany/branches/sca-java-1.x/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyNamespaceContext.java
>
>  
> tuscany/branches/sca-java-1.x/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyXMLStreamReader.java
>
>  
> tuscany/branches/sca-java-1.x/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ValidatingXMLStreamReader.java
>
> Modified:
> tuscany/branches/sca-java-1.x/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyNamespaceContext.java
> URL:
> http://svn.apache.org/viewvc/tuscany/branches/sca-java-1.x/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyNamespaceContext.java?rev=734056&r1=734055&r2=734056&view=diff
>
> ==============================================================================
> ---
> tuscany/branches/sca-java-1.x/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyNamespaceContext.java
> (original)
> +++
> tuscany/branches/sca-java-1.x/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyNamespaceContext.java
> Mon Jan 12 23:08:59 2009
> @@ -18,125 +18,94 @@
>  */
>  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<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;
> +    }
>
> -       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>
> -
> -
>  } //end of Class
>
> Modified:
> tuscany/branches/sca-java-1.x/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyXMLStreamReader.java
> URL:
> http://svn.apache.org/viewvc/tuscany/branches/sca-java-1.x/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyXMLStreamReader.java?rev=734056&r1=734055&r2=734056&view=diff
>
> ==============================================================================
> ---
> tuscany/branches/sca-java-1.x/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyXMLStreamReader.java
> (original)
> +++
> tuscany/branches/sca-java-1.x/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyXMLStreamReader.java
> Mon Jan 12 23:08:59 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 implementaion 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/branches/sca-java-1.x/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ValidatingXMLStreamReader.java
> URL:
> http://svn.apache.org/viewvc/tuscany/branches/sca-java-1.x/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ValidatingXMLStreamReader.java?rev=734056&r1=734055&r2=734056&view=diff
>
> ==============================================================================
> ---
> tuscany/branches/sca-java-1.x/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ValidatingXMLStreamReader.java
> (original)
> +++
> tuscany/branches/sca-java-1.x/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ValidatingXMLStreamReader.java
> Mon Jan 12 23:08:59 2009
> @@ -27,20 +27,19 @@
>  import javax.xml.stream.XMLStreamConstants;
>  import javax.xml.stream.XMLStreamException;
>  import javax.xml.stream.XMLStreamReader;
> -import javax.xml.stream.util.StreamReaderDelegate;
>  import javax.xml.validation.Schema;
>  import javax.xml.validation.ValidatorHandler;
>
> +import org.apache.tuscany.sca.assembly.builder.impl.ProblemImpl;
> +import org.apache.tuscany.sca.monitor.Monitor;
> +import org.apache.tuscany.sca.monitor.Problem;
> +import org.apache.tuscany.sca.monitor.Problem.Severity;
>  import org.xml.sax.Attributes;
>  import org.xml.sax.ErrorHandler;
>  import org.xml.sax.Locator;
>  import org.xml.sax.SAXException;
>  import org.xml.sax.SAXParseException;
>  import org.xml.sax.helpers.AttributesImpl;
> -import org.apache.tuscany.sca.assembly.builder.impl.ProblemImpl;
> -import org.apache.tuscany.sca.monitor.Monitor;
> -import org.apache.tuscany.sca.monitor.Problem;
> -import org.apache.tuscany.sca.monitor.Problem.Severity;
>
>  /**
>  *
> @@ -201,6 +200,7 @@
>                     case XMLStreamConstants.START_ELEMENT:
>                         level++;
>                         handleStartElement();
> +                        pushContext();
>                         return event;
>                     case XMLStreamConstants.PROCESSING_INSTRUCTION:
>                         handler.processingInstruction(super.getPITarget(),
> super.getPIData());
> @@ -214,6 +214,7 @@
>                     case XMLStreamConstants.END_ELEMENT:
>                         handleEndElement();
>                         level--;
> +                        popContext();
>                         return event;
>                     case XMLStreamConstants.END_DOCUMENT:
>                         handler.endDocument();
>
>
>
Hi Raymond

What was the actual change that fixed the stax problem? It's difficult to
tell here as some reformatting went on in this commit. I'm interested in why
it used to work in maven but not in Eclipse.

Simon

Reply via email to