View Javadoc

1   /*   Copyright 2004 BEA Systems, Inc.
2    *
3    *   Licensed under the Apache License, Version 2.0 (the "License");
4    *   you may not use this file except in compliance with the License.
5    *   You may obtain a copy of the License at
6    *
7    *       http://www.apache.org/licenses/LICENSE-2.0
8    *
9    *   Unless required by applicable law or agreed to in writing, software
10   *   distributed under the License is distributed on an "AS IS" BASIS,
11   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   *   See the License for the specific language governing permissions and
13   *   limitations under the License.
14   */
15  
16  package com.bea.xml.stream;
17  
18  import java.util.Iterator;
19  import java.util.HashSet;
20  
21  import javax.xml.namespace.NamespaceContext;
22  
23  /***
24   * <p> This class provides a ReadOnlyNamespace context that 
25   * takes a snapshot of the current namespaces in scope </p>
26   */
27  
28  public class ReadOnlyNamespaceContextBase 
29    implements NamespaceContext
30  {
31    private String[] prefixes;
32    private String[] uris;
33  
34    public ReadOnlyNamespaceContextBase(String[] prefixArray,
35                                        String[] uriArray,
36                                        int size) 
37    {
38      prefixes = new String[size];
39      uris = new String[size];
40      System.arraycopy(prefixArray, 0, prefixes, 0, prefixes.length);
41      System.arraycopy(uriArray, 0, uris, 0, uris.length);
42     }
43  
44    public String getNamespaceURI(String prefix) {
45      if (prefix == null)
46        throw new IllegalArgumentException("Prefix may not be null.");
47      if(!"".equals(prefix)) {
48        for( int i = uris.length -1; i >= 0; i--) {
49          if( prefix.equals( prefixes[ i ] ) ) {
50            return uris[ i ];
51          }
52        }
53        if("xml".equals( prefix )) {
54          return MXParser.XML_URI;
55        } else if("xmlns".equals( prefix )) {
56          return MXParser.XMLNS_URI;
57        }
58      } else {
59        for( int i = uris.length -1; i >= 0; i--) {
60          if( prefixes[ i ]  == null ) {
61            return uris[ i ];
62          }
63        }
64      }
65      return null;
66    }
67    public String getPrefix(String uri) {
68      if (uri == null)
69        throw new IllegalArgumentException("uri may not be null");
70      if ("".equals(uri))
71        throw new IllegalArgumentException("uri may not be empty string");
72  
73      if(uri != null) {
74              for( int i = uris.length -1; i >= 0; i--) {
75                  if( uri.equals( uris[ i ] ) ) {
76                      return checkNull(prefixes[ i ]);
77                  }
78              }
79          } 
80      return null;
81    }
82  
83    public String getDefaultNameSpace() {
84      for( int i = uris.length -1; i >= 0; i--) {
85        if( prefixes[ i ]  == null ) {
86          return uris[ i ];
87        }
88      }
89      return null;
90    }
91    
92    private String checkNull(String s) {
93      if (s == null) return "";
94      return s;
95    }
96  
97    public Iterator getPrefixes(String uri) {
98      if (uri == null)
99        throw new IllegalArgumentException("uri may not be null");
100     if ("".equals(uri))
101       throw new IllegalArgumentException("uri may not be empty string");
102     HashSet s = new HashSet();
103     for( int i = uris.length -1; i >= 0; i--) {
104       String prefix = checkNull(prefixes[i]);
105       if( uri.equals( uris[ i ] ) && !s.contains(prefix)) {
106         s.add(prefix);
107       }
108     }
109     return s.iterator();
110   }
111 
112   public String toString() {
113     StringBuffer b = new StringBuffer();
114     for (int i=0; i < uris.length; i++) {
115       b.append("["+checkNull(prefixes[i])+"<->"+uris[i]+"]");
116     }
117     return b.toString();
118   }
119 
120   public static void main(String[] args) throws Exception {
121     MXParser p = new MXParser();
122     p.setInput(new java.io.FileReader(args[0]));
123     while (p.hasNext()) {
124       if (p.isStartElement()) {
125         System.out.println("context["+p.getNamespaceContext()+"]");
126         Iterator i = p.getNamespaceContext().getPrefixes("a");
127         while (i.hasNext())
128           System.out.println("Found prefix:"+i.next());
129       }
130       p.next();
131     }
132   }
133 }