KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
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 /**
31  * JCR Namespace Resolver
32  *
33  * @author David Caruana
34  */

35 public class JCRNamespacePrefixResolver implements NamespaceService
36 {
37     // delegate
38
private NamespacePrefixResolver delegate;
39
40     // prefix -> uri
41
private Map JavaDoc<String JavaDoc, String JavaDoc> prefixes = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
42     
43     // uri -> prefix
44
private Map JavaDoc<String JavaDoc, String JavaDoc> uris = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
45     
46
47     /**
48      * Construct
49      *
50      * @param delegate namespace delegate
51      */

52     public JCRNamespacePrefixResolver(NamespacePrefixResolver delegate)
53     {
54         this.delegate = delegate;
55     }
56
57     /* (non-Javadoc)
58      * @see org.alfresco.service.namespace.NamespacePrefixResolver#getPrefixes(java.lang.String)
59      */

60     public Collection JavaDoc<String JavaDoc> getPrefixes(String JavaDoc namespaceURI) throws NamespaceException
61     {
62         String JavaDoc prefix = uris.get(namespaceURI);
63         if (prefix == null)
64         {
65             return delegate.getPrefixes(namespaceURI);
66         }
67         List JavaDoc<String JavaDoc> prefixes = new ArrayList JavaDoc<String JavaDoc>();
68         prefixes.add(prefix);
69         return prefixes;
70     }
71
72     /* (non-Javadoc)
73      * @see org.alfresco.service.namespace.NamespacePrefixResolver#getPrefixes()
74      */

75     public Collection JavaDoc<String JavaDoc> getPrefixes()
76     {
77         List JavaDoc<String JavaDoc> prefixes = new ArrayList JavaDoc<String JavaDoc>();
78         Collection JavaDoc<String JavaDoc> uris = getURIs();
79         for (String JavaDoc uri : uris)
80         {
81             Collection JavaDoc<String JavaDoc> uriPrefixes = getPrefixes(uri);
82             prefixes.addAll(uriPrefixes);
83         }
84         return prefixes;
85     }
86
87     /* (non-Javadoc)
88      * @see org.alfresco.service.namespace.NamespaceService#registerNamespace(java.lang.String, java.lang.String)
89      */

90     public void registerNamespace(String JavaDoc prefix, String JavaDoc uri)
91     {
92         //
93
// Check re-mapping according to JCR specification
94
//
95

96         // Cannot map any prefix that starts with xml
97
if (prefix.toLowerCase().startsWith(JCRNamespace.XML_PREFIX))
98         {
99             throw new NamespaceException("Cannot map prefix " + prefix + " as it is reserved");
100         }
101         
102         // Cannot remap a prefix that is already assigned to a uri
103
String JavaDoc 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         // Cannot map a prefix to a non-existent uri
110
Collection JavaDoc<String JavaDoc> 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     /* (non-Javadoc)
121      * @see org.alfresco.service.namespace.NamespaceService#unregisterNamespace(java.lang.String)
122      */

123     public void unregisterNamespace(String JavaDoc prefix)
124     {
125         String JavaDoc uri = prefixes.get(prefix);
126         if (uri != null)
127         {
128             uris.remove(uri);
129         }
130         prefixes.remove(prefix);
131     }
132
133     public String JavaDoc getNamespaceURI(String JavaDoc prefix) throws NamespaceException
134     {
135         String JavaDoc uri = prefixes.get(prefix);
136         if (uri == null)
137         {
138             return delegate.getNamespaceURI(prefix);
139         }
140         return uri;
141     }
142
143     /* (non-Javadoc)
144      * @see org.alfresco.service.namespace.NamespacePrefixResolver#getURIs()
145      */

146     public Collection JavaDoc<String JavaDoc> getURIs()
147     {
148         return delegate.getURIs();
149     }
150
151 }
152
Popular Tags