| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 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 | |
|
| 29 | |
|
| 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 | |
|
| 39 | |
|
| 40 | |
|
| 41 | 0 | public ConfiguredNamespaceContext(Map<String, String> prefixToNamespace) { |
| 42 | 0 | this.prefixToNamespace = prefixToNamespace; |
| 43 | |
|
| 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 | |
|
| 58 | |
|
| 59 | |
public String getNamespaceURI(String prefix) { |
| 60 | |
|
| 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 | |
|
| 70 | |
|
| 71 | |
public String getPrefix(String namespaceURI) { |
| 72 | |
|
| 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 | |
|
| 86 | |
|
| 87 | |
public Iterator<String> getPrefixes(String namespaceURI) { |
| 88 | |
|
| 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 | |
} |