View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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  package org.kuali.rice.ken.util;
17  
18  import java.util.HashMap;
19  import java.util.HashSet;
20  import java.util.Iterator;
21  import java.util.Map;
22  
23  import javax.xml.namespace.NamespaceContext;
24  
25  import org.apache.log4j.Logger;
26  
27  /**
28   * XPath NamespaceContext that is configured with a predefined prefix->NS map.
29   * @author Kuali Rice Team (rice.collab@kuali.org)
30   */
31  public class ConfiguredNamespaceContext implements NamespaceContext {
32      private static final Logger LOG = Logger.getLogger(ConfiguredNamespaceContext.class);
33  
34      private Map<String, String> prefixToNamespace = new HashMap<String, String>();
35      private Map<String, HashSet<String>> namespaceToPrefix = new HashMap<String, HashSet<String>>();
36  
37      /**
38       * Constructs a ConfiguredNamespaceContext.java.
39       * @param prefixToNamespace
40       */
41      public ConfiguredNamespaceContext(Map<String, String> prefixToNamespace) {
42          this.prefixToNamespace = prefixToNamespace;
43          // create a reverse namespace to prefix(es) map
44          for (Map.Entry<String, String> entry: prefixToNamespace.entrySet()) {
45              String namespace = entry.getValue();
46              String prefix = entry.getKey();
47              HashSet<String> prefixes = namespaceToPrefix.get(namespace);
48              if (prefixes == null) {
49                  prefixes = new HashSet<String>(4);
50                  namespaceToPrefix.put(namespace, prefixes);
51              }
52              prefixes.add(prefix);
53          }
54      }
55  
56      /**
57       * @see javax.xml.namespace.NamespaceContext#getNamespaceURI(java.lang.String)
58       */
59      public String getNamespaceURI(String prefix) {
60          //LOG.trace("getNamespaceURI(" + prefix + ")");
61          if (prefix == null) {
62              throw new IllegalArgumentException("The prefix cannot be null.");
63          }
64  
65          return prefixToNamespace.get(prefix);
66      }
67  
68      /**
69       * @see javax.xml.namespace.NamespaceContext#getPrefix(java.lang.String)
70       */
71      public String getPrefix(String namespaceURI) {
72          //LOG.trace("getPrefix(" + namespaceURI + ")");
73          if (namespaceURI == null) {
74              throw new IllegalArgumentException("The namespace uri cannot be null.");
75          }
76          Iterator<String> prefixes = getPrefixes(namespaceURI);
77          if (prefixes != null) {
78              return prefixes.next();
79          } else {
80              return null;
81          }
82      }
83  
84      /**
85       * @see javax.xml.namespace.NamespaceContext#getPrefixes(java.lang.String)
86       */
87      public Iterator<String> getPrefixes(String namespaceURI) {
88          //LOG.trace("getPrefixes(" + namespaceURI + ")");
89          if (namespaceURI == null) {
90              throw new IllegalArgumentException("The namespace uri cannot be null.");
91          }
92  
93          HashSet<String> prefixes = namespaceToPrefix.get(namespaceURI);
94          if (prefixes != null && prefixes.size() > 0) {
95              return prefixes.iterator();
96          } else {
97              return null;
98          }
99      }
100 }