KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > jcr > impl > core > NamespaceRegistryImpl


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5
6 package org.exoplatform.services.jcr.impl.core;
7
8 import java.util.Collection JavaDoc;
9 import java.util.HashMap JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.Map JavaDoc;
12 import java.util.Set JavaDoc;
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 /**
21  * Created by The eXo Platform SARL .
22  *
23  * @author <a HREF="mailto:geaz@users.sourceforge.net">Gennady Azarenkov</a>
24  * @version $Id: NamespaceRegistryImpl.java,v 1.7 2004/08/15 12:01:51 benjmestrallet Exp $
25  */

26
27 public class NamespaceRegistryImpl implements NamespaceRegistry {
28
29   private HashMap JavaDoc namespaces;
30
31   private static final String JavaDoc[] protectedNamespaces = {"jcr", "nt", "mix","pt", "sv", "exo"};
32
33   public NamespaceRegistryImpl() {
34     namespaces = new HashMap JavaDoc();
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   /**
44    * 6.7
45    * Get the URI to which the given prefix is mapped.
46    */

47   public String JavaDoc getURI(String JavaDoc prefix) {
48     return (String JavaDoc) namespaces.get(prefix);
49   }
50
51   /**
52    * 6.7
53    * Set a mapping from prefix to URI. To remove an existing
54    * mapping, set its prefix to null.
55    */

56   public void setMapping(String JavaDoc prefix, String JavaDoc 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 JavaDoc values = namespaces.values();
67       if(values.contains(uri)){
68         String JavaDoc key2Remove = null;
69         Set JavaDoc keys = namespaces.keySet();
70         for (Iterator JavaDoc iterator = keys.iterator(); iterator.hasNext();) {
71           String JavaDoc key = (String JavaDoc) iterator.next();
72           String JavaDoc value = (String JavaDoc) 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   /**
85    * 6.7
86    * Returns an array holding all currently registered prefixes.
87    */

88   public String JavaDoc[] getPrefixes() {
89     return (String JavaDoc[]) namespaces.keySet().toArray(new String JavaDoc[namespaces.keySet().size()]);
90   }
91
92   public Map JavaDoc getURIMap() {
93     return namespaces;
94   }
95
96   public String JavaDoc[] getURIs() {
97     return (String JavaDoc[]) namespaces.values().toArray(new String JavaDoc[namespaces.size()]);
98   }
99
100
101   /**
102    * Returns the prefix to which the given uri is mapped
103    *
104    * @param uri a string
105    * @return a string
106    */

107   public String JavaDoc getPrefix(String JavaDoc uri) throws NamespaceException, RepositoryException {
108     String JavaDoc[] prefixes = getPrefixes();
109     for (int i = 0; i < prefixes.length; i++) {
110       if (getURI(prefixes[i]).equals(uri))
111         return (String JavaDoc) namespaces.get(getPrefixes()[i]);
112     }
113     throw new NamespaceException("There is no uri <" + uri + "> in the repository!");
114   }
115
116 }
117
Popular Tags