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.ArrayList;
19  import java.util.Iterator;
20  import java.util.List;
21  
22  import javax.xml.namespace.NamespaceContext;
23  
24  /**
25   * XPath NamespaceContext implementation that delegates in sequence to a list of NamespaceContexts,
26   * returning the first match. 
27   * @author Kuali Rice Team (rice.collab@kuali.org)
28   */
29  public class CompoundNamespaceContext implements NamespaceContext {
30      private final List<NamespaceContext> contexts;
31  
32      /**
33       * Constructs a CompoundNamespaceContext.java.
34       * @param first
35       * @param second
36       */
37      public CompoundNamespaceContext(NamespaceContext first, NamespaceContext second) {
38          this.contexts = new ArrayList<NamespaceContext>(2);
39          this.contexts.add(first);
40          this.contexts.add(second);
41      }
42      
43      /**
44       * Constructs a CompoundNamespaceContext.java.
45       * @param contexts
46       */
47      public CompoundNamespaceContext(List<NamespaceContext> contexts) {
48          this.contexts = contexts;
49      }
50  
51      /**
52       * @see javax.xml.namespace.NamespaceContext#getNamespaceURI(java.lang.String)
53       */
54      public String getNamespaceURI(String prefix) {
55          if (prefix == null) {
56              throw new IllegalArgumentException("The prefix cannot be null.");
57          }
58          for (NamespaceContext nc: contexts) {
59              String uri = nc.getNamespaceURI(prefix);
60              if (uri != null) {
61                  return uri;
62              }
63          }
64          return null;
65      }
66  
67      /**
68       * @see javax.xml.namespace.NamespaceContext#getPrefix(java.lang.String)
69       */
70      public String getPrefix(String namespaceURI) {
71          if (namespaceURI == null) {
72              throw new IllegalArgumentException("The namespace uri cannot be null.");
73          }
74          for (NamespaceContext nc: contexts) {
75              String prefix = nc.getPrefix(namespaceURI);
76              if (prefix != null) {
77                  return prefix;
78              }
79          }
80          return null;
81      }
82  
83      /**
84       * @see javax.xml.namespace.NamespaceContext#getPrefixes(java.lang.String)
85       */
86      public Iterator getPrefixes(String namespaceURI) {
87          if (namespaceURI == null) {
88              throw new IllegalArgumentException("The namespace uri cannot be null.");
89          }
90          for (NamespaceContext nc: contexts) {
91              Iterator prefixes = nc.getPrefixes(namespaceURI);
92              if (prefixes != null) {
93                  return prefixes;
94              }
95          }
96          return null;
97      }
98  }