KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > service > namespace > DynamicNamespacePrefixResolver


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.service.namespace;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Set JavaDoc;
24
25 /**
26  * A delegating namespace prefix resolver which allows local over rides from the
27  * delegate. Allows standard/default prefixes to be available but over ridden as
28  * required.
29  *
30  * @author andyh
31  *
32  */

33 public class DynamicNamespacePrefixResolver implements NamespaceService
34 {
35
36     /**
37      * The delegate
38      */

39     private NamespacePrefixResolver delegate;
40
41     /**
42      * The map uris keyed by prefix
43      */

44     private HashMap JavaDoc<String JavaDoc, String JavaDoc> map = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
45
46     public DynamicNamespacePrefixResolver(NamespacePrefixResolver delegate)
47     {
48         super();
49         this.delegate = delegate;
50     }
51     
52     public DynamicNamespacePrefixResolver()
53     {
54         this(null);
55     }
56
57     /**
58      * Add prefix to name space mapping override
59      *
60      * @param prefix
61      * @param uri
62      */

63     public void registerNamespace(String JavaDoc prefix, String JavaDoc uri)
64     {
65         map.put(prefix, uri);
66     }
67
68     /**
69      * Remove a prefix to namespace mapping
70      *
71      * @param prefix
72      */

73     public void unregisterNamespace(String JavaDoc prefix)
74     {
75         map.remove(prefix);
76     }
77
78     // NameSpacePrefix Resolver
79

80     public String JavaDoc getNamespaceURI(String JavaDoc prefix) throws NamespaceException
81     {
82         String JavaDoc uri = map.get(prefix);
83         if ((uri == null) && (delegate != null))
84         {
85             uri = delegate.getNamespaceURI(prefix);
86         }
87         return uri;
88     }
89
90     public Collection JavaDoc<String JavaDoc> getPrefixes(String JavaDoc namespaceURI) throws NamespaceException
91     {
92         Collection JavaDoc<String JavaDoc> prefixes = new ArrayList JavaDoc<String JavaDoc>();
93         for (String JavaDoc key : map.keySet())
94         {
95             String JavaDoc uri = map.get(key);
96             if ((uri != null) && (uri.equals(namespaceURI)))
97             {
98                 prefixes.add(key);
99             }
100         }
101         // Only add if not over ridden here (if identical already added)
102
if (delegate != null)
103         {
104             for (String JavaDoc prefix : delegate.getPrefixes(namespaceURI))
105             {
106                 if (!map.containsKey(prefix))
107                 {
108                     prefixes.add(prefix);
109                 }
110             }
111         }
112         return prefixes;
113     }
114
115     public Collection JavaDoc<String JavaDoc> getPrefixes()
116     {
117        Set JavaDoc<String JavaDoc> prefixes = new HashSet JavaDoc<String JavaDoc>();
118        if(delegate != null)
119        {
120           prefixes.addAll(delegate.getPrefixes());
121        }
122        prefixes.addAll(map.keySet());
123        return prefixes;
124     }
125     
126     public Collection JavaDoc<String JavaDoc> getURIs()
127     {
128        Set JavaDoc<String JavaDoc> uris = new HashSet JavaDoc<String JavaDoc>();
129        if(delegate != null)
130        {
131           uris.addAll(delegate.getURIs());
132        }
133        uris.addAll(map.keySet());
134        return uris;
135     }
136     
137 }
138
Popular Tags