KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > jcr > dictionary > NamespaceRegistryImpl


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.jcr.dictionary;
18
19 import java.util.Collection JavaDoc;
20
21 import javax.jcr.AccessDeniedException;
22 import javax.jcr.NamespaceException;
23 import javax.jcr.NamespaceRegistry;
24 import javax.jcr.RepositoryException;
25 import javax.jcr.UnsupportedRepositoryOperationException;
26
27 import org.alfresco.service.namespace.NamespaceService;
28
29
30 /**
31  * Alfresco implementation of a JCR Namespace registry
32  *
33  * @author David Caruana
34  */

35 public class NamespaceRegistryImpl implements NamespaceRegistry
36 {
37
38     private boolean allowRegistration;
39     private NamespaceService namespaceService;
40     
41     
42     /**
43      * Construct
44      *
45      * @param namespaceService namespace service
46      */

47     public NamespaceRegistryImpl(boolean allowRegistraton, NamespaceService namespaceService)
48     {
49         this.allowRegistration = allowRegistraton;
50         this.namespaceService = namespaceService;
51     }
52
53     /**
54      * Get the namespace prefix resolver
55      *
56      * @return the namespace prefix resolver
57      */

58     public NamespaceService getNamespaceService()
59     {
60         return this.namespaceService;
61     }
62     
63     /* (non-Javadoc)
64      * @see javax.jcr.NamespaceRegistry#registerNamespace(java.lang.String, java.lang.String)
65      */

66     public void registerNamespace(String JavaDoc prefix, String JavaDoc uri) throws NamespaceException, UnsupportedRepositoryOperationException, AccessDeniedException, RepositoryException
67     {
68         try
69         {
70             if (!allowRegistration)
71             {
72                 throw new UnsupportedRepositoryOperationException();
73             }
74             namespaceService.registerNamespace(prefix, uri);
75         }
76         catch(org.alfresco.service.namespace.NamespaceException e)
77         {
78             throw new NamespaceException(e);
79         }
80     }
81
82     /* (non-Javadoc)
83      * @see javax.jcr.NamespaceRegistry#unregisterNamespace(java.lang.String)
84      */

85     public void unregisterNamespace(String JavaDoc prefix) throws NamespaceException, UnsupportedRepositoryOperationException, AccessDeniedException, RepositoryException
86     {
87         try
88         {
89             if (!allowRegistration)
90             {
91                 throw new UnsupportedRepositoryOperationException();
92             }
93             namespaceService.unregisterNamespace(prefix);
94         }
95         catch(org.alfresco.service.namespace.NamespaceException e)
96         {
97             throw new NamespaceException(e);
98         }
99     }
100
101     /* (non-Javadoc)
102      * @see javax.jcr.NamespaceRegistry#getPrefixes()
103      */

104     public String JavaDoc[] getPrefixes() throws RepositoryException
105     {
106         Collection JavaDoc<String JavaDoc> prefixes = namespaceService.getPrefixes();
107         return prefixes.toArray(new String JavaDoc[prefixes.size()]);
108     }
109
110     /* (non-Javadoc)
111      * @see javax.jcr.NamespaceRegistry#getURIs()
112      */

113     public String JavaDoc[] getURIs() throws RepositoryException
114     {
115         Collection JavaDoc<String JavaDoc> uris = namespaceService.getURIs();
116         return uris.toArray(new String JavaDoc[uris.size()]);
117     }
118
119     /* (non-Javadoc)
120      * @see javax.jcr.NamespaceRegistry#getURI(java.lang.String)
121      */

122     public String JavaDoc getURI(String JavaDoc prefix) throws NamespaceException, RepositoryException
123     {
124         String JavaDoc uri = namespaceService.getNamespaceURI(prefix);
125         if (uri == null)
126         {
127             throw new NamespaceException("Prefix " + prefix + " is unknown.");
128         }
129         return uri;
130     }
131
132     /* (non-Javadoc)
133      * @see javax.jcr.NamespaceRegistry#getPrefix(java.lang.String)
134      */

135     public String JavaDoc getPrefix(String JavaDoc uri) throws NamespaceException, RepositoryException
136     {
137         Collection JavaDoc<String JavaDoc> prefixes = namespaceService.getPrefixes(uri);
138         if (prefixes.size() == 0)
139         {
140             throw new NamespaceException("URI " + uri + " is unknown.");
141         }
142         // Return first prefix registered for uri
143
return prefixes.iterator().next();
144     }
145
146 }
147
Popular Tags