001    /**
002     * Copyright 2005-2012 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.HashMap;
019    import java.util.HashSet;
020    import java.util.Iterator;
021    import java.util.Map;
022    
023    import javax.xml.namespace.NamespaceContext;
024    
025    import org.apache.log4j.Logger;
026    
027    /**
028     * XPath NamespaceContext that is configured with a predefined prefix->NS map.
029     * @author Kuali Rice Team (rice.collab@kuali.org)
030     */
031    public class ConfiguredNamespaceContext implements NamespaceContext {
032        private static final Logger LOG = Logger.getLogger(ConfiguredNamespaceContext.class);
033    
034        private Map<String, String> prefixToNamespace = new HashMap<String, String>();
035        private Map<String, HashSet<String>> namespaceToPrefix = new HashMap<String, HashSet<String>>();
036    
037        /**
038         * Constructs a ConfiguredNamespaceContext.java.
039         * @param prefixToNamespace
040         */
041        public ConfiguredNamespaceContext(Map<String, String> prefixToNamespace) {
042            this.prefixToNamespace = prefixToNamespace;
043            // create a reverse namespace to prefix(es) map
044            for (Map.Entry<String, String> entry: prefixToNamespace.entrySet()) {
045                String namespace = entry.getValue();
046                String prefix = entry.getKey();
047                HashSet<String> prefixes = namespaceToPrefix.get(namespace);
048                if (prefixes == null) {
049                    prefixes = new HashSet<String>(4);
050                    namespaceToPrefix.put(namespace, prefixes);
051                }
052                prefixes.add(prefix);
053            }
054        }
055    
056        /**
057         * @see javax.xml.namespace.NamespaceContext#getNamespaceURI(java.lang.String)
058         */
059        public String getNamespaceURI(String prefix) {
060            //LOG.trace("getNamespaceURI(" + prefix + ")");
061            if (prefix == null) {
062                throw new IllegalArgumentException("The prefix cannot be null.");
063            }
064    
065            return prefixToNamespace.get(prefix);
066        }
067    
068        /**
069         * @see javax.xml.namespace.NamespaceContext#getPrefix(java.lang.String)
070         */
071        public String getPrefix(String namespaceURI) {
072            //LOG.trace("getPrefix(" + namespaceURI + ")");
073            if (namespaceURI == null) {
074                throw new IllegalArgumentException("The namespace uri cannot be null.");
075            }
076            Iterator<String> prefixes = getPrefixes(namespaceURI);
077            if (prefixes != null) {
078                return prefixes.next();
079            } else {
080                return null;
081            }
082        }
083    
084        /**
085         * @see javax.xml.namespace.NamespaceContext#getPrefixes(java.lang.String)
086         */
087        public Iterator<String> getPrefixes(String namespaceURI) {
088            //LOG.trace("getPrefixes(" + namespaceURI + ")");
089            if (namespaceURI == null) {
090                throw new IllegalArgumentException("The namespace uri cannot be null.");
091            }
092    
093            HashSet<String> prefixes = namespaceToPrefix.get(namespaceURI);
094            if (prefixes != null && prefixes.size() > 0) {
095                return prefixes.iterator();
096            } else {
097                return null;
098            }
099        }
100    }