1 17 package org.alfresco.service.namespace; 18 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.HashMap ; 22 import java.util.HashSet ; 23 import java.util.Set ; 24 25 33 public class DynamicNamespacePrefixResolver implements NamespaceService 34 { 35 36 39 private NamespacePrefixResolver delegate; 40 41 44 private HashMap <String , String > map = new HashMap <String , String >(); 45 46 public DynamicNamespacePrefixResolver(NamespacePrefixResolver delegate) 47 { 48 super(); 49 this.delegate = delegate; 50 } 51 52 public DynamicNamespacePrefixResolver() 53 { 54 this(null); 55 } 56 57 63 public void registerNamespace(String prefix, String uri) 64 { 65 map.put(prefix, uri); 66 } 67 68 73 public void unregisterNamespace(String prefix) 74 { 75 map.remove(prefix); 76 } 77 78 80 public String getNamespaceURI(String prefix) throws NamespaceException 81 { 82 String uri = map.get(prefix); 83 if ((uri == null) && (delegate != null)) 84 { 85 uri = delegate.getNamespaceURI(prefix); 86 } 87 return uri; 88 } 89 90 public Collection <String > getPrefixes(String namespaceURI) throws NamespaceException 91 { 92 Collection <String > prefixes = new ArrayList <String >(); 93 for (String key : map.keySet()) 94 { 95 String uri = map.get(key); 96 if ((uri != null) && (uri.equals(namespaceURI))) 97 { 98 prefixes.add(key); 99 } 100 } 101 if (delegate != null) 103 { 104 for (String prefix : delegate.getPrefixes(namespaceURI)) 105 { 106 if (!map.containsKey(prefix)) 107 { 108 prefixes.add(prefix); 109 } 110 } 111 } 112 return prefixes; 113 } 114 115 public Collection <String > getPrefixes() 116 { 117 Set <String > prefixes = new HashSet <String >(); 118 if(delegate != null) 119 { 120 prefixes.addAll(delegate.getPrefixes()); 121 } 122 prefixes.addAll(map.keySet()); 123 return prefixes; 124 } 125 126 public Collection <String > getURIs() 127 { 128 Set <String > uris = new HashSet <String >(); 129 if(delegate != null) 130 { 131 uris.addAll(delegate.getURIs()); 132 } 133 uris.addAll(map.keySet()); 134 return uris; 135 } 136 137 } 138 | Popular Tags |