1 17 package org.alfresco.jcr.dictionary; 18 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.HashMap ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import org.alfresco.service.namespace.NamespaceException; 26 import org.alfresco.service.namespace.NamespacePrefixResolver; 27 import org.alfresco.service.namespace.NamespaceService; 28 29 30 35 public class JCRNamespacePrefixResolver implements NamespaceService 36 { 37 private NamespacePrefixResolver delegate; 39 40 private Map <String , String > prefixes = new HashMap <String , String >(); 42 43 private Map <String , String > uris = new HashMap <String , String >(); 45 46 47 52 public JCRNamespacePrefixResolver(NamespacePrefixResolver delegate) 53 { 54 this.delegate = delegate; 55 } 56 57 60 public Collection <String > getPrefixes(String namespaceURI) throws NamespaceException 61 { 62 String prefix = uris.get(namespaceURI); 63 if (prefix == null) 64 { 65 return delegate.getPrefixes(namespaceURI); 66 } 67 List <String > prefixes = new ArrayList <String >(); 68 prefixes.add(prefix); 69 return prefixes; 70 } 71 72 75 public Collection <String > getPrefixes() 76 { 77 List <String > prefixes = new ArrayList <String >(); 78 Collection <String > uris = getURIs(); 79 for (String uri : uris) 80 { 81 Collection <String > uriPrefixes = getPrefixes(uri); 82 prefixes.addAll(uriPrefixes); 83 } 84 return prefixes; 85 } 86 87 90 public void registerNamespace(String prefix, String uri) 91 { 92 96 if (prefix.toLowerCase().startsWith(JCRNamespace.XML_PREFIX)) 98 { 99 throw new NamespaceException("Cannot map prefix " + prefix + " as it is reserved"); 100 } 101 102 String existingUri = delegate.getNamespaceURI(prefix); 104 if (existingUri != null) 105 { 106 throw new NamespaceException("Cannot map prefix " + prefix + " as it is already assigned to uri " + existingUri); 107 } 108 109 Collection <String > existingURIs = delegate.getURIs(); 111 if (existingURIs.contains(uri) == false) 112 { 113 throw new NamespaceException("Cannot map prefix " + prefix + " to uri " + uri + " which does not exist"); 114 } 115 116 prefixes.put(prefix, uri); 117 uris.put(uri, prefix); 118 } 119 120 123 public void unregisterNamespace(String prefix) 124 { 125 String uri = prefixes.get(prefix); 126 if (uri != null) 127 { 128 uris.remove(uri); 129 } 130 prefixes.remove(prefix); 131 } 132 133 public String getNamespaceURI(String prefix) throws NamespaceException 134 { 135 String uri = prefixes.get(prefix); 136 if (uri == null) 137 { 138 return delegate.getNamespaceURI(prefix); 139 } 140 return uri; 141 } 142 143 146 public Collection <String > getURIs() 147 { 148 return delegate.getURIs(); 149 } 150 151 } 152 | Popular Tags |