1 5 6 package org.exoplatform.services.jcr.impl.core; 7 8 import java.util.Collection ; 9 import java.util.HashMap ; 10 import java.util.Iterator ; 11 import java.util.Map ; 12 import java.util.Set ; 13 14 import javax.jcr.NamespaceException; 15 import javax.jcr.NamespaceRegistry; 16 import javax.jcr.RepositoryException; 17 18 import org.apache.commons.lang.ArrayUtils; 19 20 26 27 public class NamespaceRegistryImpl implements NamespaceRegistry { 28 29 private HashMap namespaces; 30 31 private static final String [] protectedNamespaces = {"jcr", "nt", "mix","pt", "sv", "exo"}; 32 33 public NamespaceRegistryImpl() { 34 namespaces = new HashMap (); 35 namespaces.put("jcr", "http://www.jcp.org/jcr/1.0"); 36 namespaces.put("nt", "http://www.jcp.org/jcr/nt/1.0"); 37 namespaces.put("mix", "http://www.jcp.org/jcr/mix/1.0"); 38 namespaces.put("pt", "http://www.jcp.org/jcr/pt/1.0"); 39 namespaces.put("sv", "http://www.jcp.org/jcr/sv/1.0"); 40 namespaces.put("exo", "http://www.exoplatform.com/jcr/exo/1.0"); 41 } 42 43 47 public String getURI(String prefix) { 48 return (String ) namespaces.get(prefix); 49 } 50 51 56 public void setMapping(String prefix, String uri) throws NamespaceException, RepositoryException { 57 if(ArrayUtils.contains(protectedNamespaces, prefix)){ 58 if(uri == null) 59 throw new NamespaceException("Can not remove built-in namespace"); 60 else 61 throw new NamespaceException("Can not change built-in namespace"); 62 } 63 if(uri == null) 64 namespaces.remove(prefix); 65 else { 66 Collection values = namespaces.values(); 67 if(values.contains(uri)){ 68 String key2Remove = null; 69 Set keys = namespaces.keySet(); 70 for (Iterator iterator = keys.iterator(); iterator.hasNext();) { 71 String key = (String ) iterator.next(); 72 String value = (String ) namespaces.get(key); 73 if(value.equals(uri)){ 74 key2Remove = key; 75 break; 76 } 77 } 78 namespaces.remove(key2Remove); 79 } 80 namespaces.put(prefix, uri); 81 } 82 } 83 84 88 public String [] getPrefixes() { 89 return (String []) namespaces.keySet().toArray(new String [namespaces.keySet().size()]); 90 } 91 92 public Map getURIMap() { 93 return namespaces; 94 } 95 96 public String [] getURIs() { 97 return (String []) namespaces.values().toArray(new String [namespaces.size()]); 98 } 99 100 101 107 public String getPrefix(String uri) throws NamespaceException, RepositoryException { 108 String [] prefixes = getPrefixes(); 109 for (int i = 0; i < prefixes.length; i++) { 110 if (getURI(prefixes[i]).equals(uri)) 111 return (String ) namespaces.get(getPrefixes()[i]); 112 } 113 throw new NamespaceException("There is no uri <" + uri + "> in the repository!"); 114 } 115 116 } 117 | Popular Tags |