KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > dictionary > NamespaceDAOImpl


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.repo.dictionary;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.alfresco.service.namespace.NamespaceException;
26
27 /**
28  * Simple in-memory namespace DAO
29  *
30  * TODO: Remove the many to one mapping of prefixes to URIs
31  */

32 public class NamespaceDAOImpl implements NamespaceDAO
33 {
34
35     private List JavaDoc<String JavaDoc> uris = new ArrayList JavaDoc<String JavaDoc>();
36     private HashMap JavaDoc<String JavaDoc, String JavaDoc> prefixes = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
37
38     
39     public Collection JavaDoc<String JavaDoc> getURIs()
40     {
41         return Collections.unmodifiableCollection(uris);
42     }
43
44
45     /* (non-Javadoc)
46      * @see org.alfresco.repo.ref.NamespacePrefixResolver#getPrefixes()
47      */

48     public Collection JavaDoc<String JavaDoc> getPrefixes()
49     {
50         return Collections.unmodifiableCollection(prefixes.keySet());
51     }
52
53     
54     /* (non-Javadoc)
55      * @see org.alfresco.repo.dictionary.impl.NamespaceDAO#addURI(java.lang.String)
56      */

57     public void addURI(String JavaDoc uri)
58     {
59         if (uris.contains(uri))
60         {
61             throw new NamespaceException("URI " + uri + " has already been defined");
62         }
63         uris.add(uri);
64     }
65
66     
67     /* (non-Javadoc)
68      * @see org.alfresco.repo.dictionary.impl.NamespaceDAO#addPrefix(java.lang.String, java.lang.String)
69      */

70     public void addPrefix(String JavaDoc prefix, String JavaDoc uri)
71     {
72         if (!uris.contains(uri))
73         {
74             throw new NamespaceException("Namespace URI " + uri + " does not exist");
75         }
76         prefixes.put(prefix, uri);
77     }
78
79
80     /* (non-Javadoc)
81      * @see org.alfresco.repo.dictionary.impl.NamespaceDAO#removeURI(java.lang.String)
82      */

83     public void removeURI(String JavaDoc uri)
84     {
85         uris.remove(uri);
86     }
87
88     
89     /* (non-Javadoc)
90      * @see org.alfresco.repo.dictionary.impl.NamespaceDAO#removePrefix(java.lang.String)
91      */

92     public void removePrefix(String JavaDoc prefix)
93     {
94         prefixes.remove(prefix);
95     }
96
97
98     /* (non-Javadoc)
99      * @see org.alfresco.repo.ref.NamespacePrefixResolver#getNamespaceURI(java.lang.String)
100      */

101     public String JavaDoc getNamespaceURI(String JavaDoc prefix)
102     {
103         return prefixes.get(prefix);
104     }
105
106     
107     /* (non-Javadoc)
108      * @see org.alfresco.repo.ref.NamespacePrefixResolver#getPrefixes(java.lang.String)
109      */

110     public Collection JavaDoc<String JavaDoc> getPrefixes(String JavaDoc URI)
111     {
112         Collection JavaDoc<String JavaDoc> uriPrefixes = new ArrayList JavaDoc<String JavaDoc>();
113         for (String JavaDoc key : prefixes.keySet())
114         {
115             String JavaDoc uri = prefixes.get(key);
116             if ((uri != null) && (uri.equals(URI)))
117             {
118                 uriPrefixes.add(key);
119             }
120         }
121         return uriPrefixes;
122     }
123
124 }
125
Popular Tags