001 /** 002 * Copyright 2005-2013 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.ken.util; 017 018 import java.util.ArrayList; 019 import java.util.Iterator; 020 import java.util.List; 021 022 import javax.xml.namespace.NamespaceContext; 023 024 /** 025 * XPath NamespaceContext implementation that delegates in sequence to a list of NamespaceContexts, 026 * returning the first match. 027 * @author Kuali Rice Team (rice.collab@kuali.org) 028 */ 029 public class CompoundNamespaceContext implements NamespaceContext { 030 private final List<NamespaceContext> contexts; 031 032 /** 033 * Constructs a CompoundNamespaceContext.java. 034 * @param first 035 * @param second 036 */ 037 public CompoundNamespaceContext(NamespaceContext first, NamespaceContext second) { 038 this.contexts = new ArrayList<NamespaceContext>(2); 039 this.contexts.add(first); 040 this.contexts.add(second); 041 } 042 043 /** 044 * Constructs a CompoundNamespaceContext.java. 045 * @param contexts 046 */ 047 public CompoundNamespaceContext(List<NamespaceContext> contexts) { 048 this.contexts = contexts; 049 } 050 051 /** 052 * @see javax.xml.namespace.NamespaceContext#getNamespaceURI(java.lang.String) 053 */ 054 public String getNamespaceURI(String prefix) { 055 if (prefix == null) { 056 throw new IllegalArgumentException("The prefix cannot be null."); 057 } 058 for (NamespaceContext nc: contexts) { 059 String uri = nc.getNamespaceURI(prefix); 060 if (uri != null) { 061 return uri; 062 } 063 } 064 return null; 065 } 066 067 /** 068 * @see javax.xml.namespace.NamespaceContext#getPrefix(java.lang.String) 069 */ 070 public String getPrefix(String namespaceURI) { 071 if (namespaceURI == null) { 072 throw new IllegalArgumentException("The namespace uri cannot be null."); 073 } 074 for (NamespaceContext nc: contexts) { 075 String prefix = nc.getPrefix(namespaceURI); 076 if (prefix != null) { 077 return prefix; 078 } 079 } 080 return null; 081 } 082 083 /** 084 * @see javax.xml.namespace.NamespaceContext#getPrefixes(java.lang.String) 085 */ 086 public Iterator getPrefixes(String namespaceURI) { 087 if (namespaceURI == null) { 088 throw new IllegalArgumentException("The namespace uri cannot be null."); 089 } 090 for (NamespaceContext nc: contexts) { 091 Iterator prefixes = nc.getPrefixes(namespaceURI); 092 if (prefixes != null) { 093 return prefixes; 094 } 095 } 096 return null; 097 } 098 }