View Javadoc
1   /**
2    * Copyright 2005-2015 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.ArrayList;
19  import java.util.Iterator;
20  import java.util.List;
21  
22  import javax.xml.namespace.NamespaceContext;
23  
24  import org.apache.log4j.Logger;
25  import org.w3c.dom.Document;
26  
27  /**
28   * XPath NamespaceContext implementation that delegates all lookups to a DOM Document,
29   * which supplies all prefix/NS mappings defined in the doc.
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   */
32  public class DocumentNamespaceContext implements NamespaceContext {
33      private static final Logger LOG = Logger.getLogger(DocumentNamespaceContext.class);
34  
35      // the DOM Document
36      private final Document doc;
37  
38      /**
39       * Constructs a DocumentNamespaceContext.java.
40       * @param doc
41       */
42      public DocumentNamespaceContext(Document doc) {
43          this.doc = doc;
44      }
45  
46      /**
47       * @see javax.xml.namespace.NamespaceContext#getNamespaceURI(java.lang.String)
48       */
49      public String getNamespaceURI(String prefix) {
50          LOG.debug("getNamespaceURI(" + prefix + ")");
51          if (prefix == null) {
52              throw new IllegalArgumentException("The prefix cannot be null.");
53          }
54          if (prefix.length() == 0) {
55              return doc.lookupNamespaceURI(null);
56          }
57          return doc.lookupNamespaceURI(prefix);
58      }
59  
60      /**
61       * @see javax.xml.namespace.NamespaceContext#getPrefix(java.lang.String)
62       */
63      public String getPrefix(String namespaceURI) {
64          LOG.debug("getPrefix(" + namespaceURI + ")");
65          if (namespaceURI == null) {
66              throw new IllegalArgumentException("The namespace uri cannot be null.");
67          }
68          return doc.lookupPrefix(namespaceURI);
69      }
70  
71      /**
72       * @see javax.xml.namespace.NamespaceContext#getPrefixes(java.lang.String)
73       */
74      public Iterator getPrefixes(String namespaceURI) {
75          LOG.debug("getPrefixes(" + namespaceURI + ")");
76          if (namespaceURI == null) {
77              throw new IllegalArgumentException("The namespace uri cannot be null.");
78          }
79          List<String> list = new ArrayList<String>(1);
80          String s = getPrefix(namespaceURI);
81          if (s != null) {
82              list.add(s);
83          }
84          return list.iterator();
85      }
86  }