View Javadoc

1   /*
2    * Copyright 2006 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.ws.commons.schema.utils;
18  
19  import org.apache.ws.commons.schema.constants.Constants;
20  import org.w3c.dom.Node;
21  
22  import javax.xml.namespace.NamespaceContext;
23  import java.util.*;
24  
25  /**
26   * Implementation of {@link NamespaceContext}, which is based on a DOM node.
27   */
28  public class NodeNamespaceContext implements NamespacePrefixList {
29      private static final Collection XML_NS_PREFIX_COLLECTION = Collections.singletonList(Constants.XML_NS_PREFIX);
30      private static final Collection XMLNS_ATTRIBUTE_COLLECTION = Collections.singletonList(Constants.XMLNS_ATTRIBUTE);
31      private Node node;
32      private Map declarations;
33      private String[] prefixes;
34  
35      /**
36       * Creates a new instance with the given nodes context.
37       */
38      public NodeNamespaceContext(Node pNode) {
39          node = pNode;
40      }
41  
42      private Map getDeclarations() {
43          if (declarations == null) {
44              declarations = new HashMap();
45              //FIXME: Do we really need to add this mapping? shows up in the serialized schema as xmlns="" 
46              //declarations.put(Constants.DEFAULT_NS_PREFIX, Constants.NULL_NS_URI);
47              new PrefixCollector(){
48                  protected void declare(String pPrefix, String pNamespaceURI) {
49                      declarations.put(pPrefix, pNamespaceURI);
50                  }
51              }.searchAllPrefixDeclarations(node);
52              Collection keys = declarations.keySet();
53              prefixes = (String[]) keys.toArray(new String[keys.size()]);
54          }
55          return declarations;
56      }
57  
58      public String getNamespaceURI(String pPrefix) {
59          if (pPrefix == null) {
60              throw new IllegalArgumentException("The prefix must not be null.");
61          }
62          if (Constants.XML_NS_PREFIX.equals(pPrefix)) {
63              return Constants.XML_NS_URI;
64          }
65          if (Constants.XMLNS_ATTRIBUTE.equals(pPrefix)) {
66              return Constants.XMLNS_ATTRIBUTE_NS_URI;
67          }
68          final String uri = (String) getDeclarations().get(pPrefix);
69          return uri == null ? Constants.NULL_NS_URI : uri;
70      }
71  
72      public String getPrefix(String pNamespaceURI) {
73          if (pNamespaceURI == null) {
74              throw new IllegalArgumentException("The namespace URI must not be null.");
75          }
76          if (Constants.XML_NS_URI.equals(pNamespaceURI)) {
77              return Constants.XML_NS_PREFIX;
78          }
79          if (Constants.XMLNS_ATTRIBUTE_NS_URI.equals(pNamespaceURI)) {
80              return Constants.XMLNS_ATTRIBUTE;
81          }
82          Map decl = getDeclarations();
83          for (Iterator iter = decl.entrySet().iterator();  iter.hasNext();  ) {
84              Map.Entry entry = (Map.Entry) iter.next();
85              if (pNamespaceURI.equals(entry.getValue())) {
86                  return (String) entry.getKey();
87              }
88          }
89          return null;
90      }
91  
92      public Iterator getPrefixes(String pNamespaceURI) {
93          if (pNamespaceURI == null) {
94              throw new IllegalArgumentException("The namespace URI must not be null.");
95          }
96          if (Constants.XML_NS_URI.equals(pNamespaceURI)) {
97              return XML_NS_PREFIX_COLLECTION.iterator();
98          }
99          if (Constants.XMLNS_ATTRIBUTE_NS_URI.equals(pNamespaceURI)) {
100             return XMLNS_ATTRIBUTE_COLLECTION.iterator();
101         }
102         final List list = new ArrayList();
103         for (Iterator iter = getDeclarations().entrySet().iterator();  iter.hasNext();  ) {
104             Map.Entry entry = (Map.Entry) iter.next();
105             if (pNamespaceURI.equals(entry.getValue())) {
106                 list.add(entry.getKey());
107             }
108         }
109         return list.iterator();
110     }
111 
112     public String[] getDeclaredPrefixes() {
113         getDeclarations(); // Make sure, that the prefixes array is valid
114         return prefixes;
115     }
116 }