Coverage Report - org.kuali.rice.ken.util.ConfiguredNamespaceContext
 
Classes in this File Line Coverage Branch Coverage Complexity
ConfiguredNamespaceContext
0%
0/30
0%
0/16
4.25
 
 1  
 /**
 2  
  * Copyright 2005-2011 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  0
     private static final Logger LOG = Logger.getLogger(ConfiguredNamespaceContext.class);
 33  
 
 34  0
     private Map<String, String> prefixToNamespace = new HashMap<String, String>();
 35  0
     private Map<String, HashSet<String>> namespaceToPrefix = new HashMap<String, HashSet<String>>();
 36  
 
 37  
     /**
 38  
      * Constructs a ConfiguredNamespaceContext.java.
 39  
      * @param prefixToNamespace
 40  
      */
 41  0
     public ConfiguredNamespaceContext(Map<String, String> prefixToNamespace) {
 42  0
         this.prefixToNamespace = prefixToNamespace;
 43  
         // create a reverse namespace to prefix(es) map
 44  0
         for (Map.Entry<String, String> entry: prefixToNamespace.entrySet()) {
 45  0
             String namespace = entry.getValue();
 46  0
             String prefix = entry.getKey();
 47  0
             HashSet<String> prefixes = namespaceToPrefix.get(namespace);
 48  0
             if (prefixes == null) {
 49  0
                 prefixes = new HashSet<String>(4);
 50  0
                 namespaceToPrefix.put(namespace, prefixes);
 51  
             }
 52  0
             prefixes.add(prefix);
 53  0
         }
 54  0
     }
 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  0
         if (prefix == null) {
 62  0
             throw new IllegalArgumentException("The prefix cannot be null.");
 63  
         }
 64  
 
 65  0
         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  0
         if (namespaceURI == null) {
 74  0
             throw new IllegalArgumentException("The namespace uri cannot be null.");
 75  
         }
 76  0
         Iterator<String> prefixes = getPrefixes(namespaceURI);
 77  0
         if (prefixes != null) {
 78  0
             return prefixes.next();
 79  
         } else {
 80  0
             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  0
         if (namespaceURI == null) {
 90  0
             throw new IllegalArgumentException("The namespace uri cannot be null.");
 91  
         }
 92  
 
 93  0
         HashSet<String> prefixes = namespaceToPrefix.get(namespaceURI);
 94  0
         if (prefixes != null && prefixes.size() > 0) {
 95  0
             return prefixes.iterator();
 96  
         } else {
 97  0
             return null;
 98  
         }
 99  
     }
 100  
 }