View Javadoc

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