KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > util > NamespaceMap


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: NamespaceMap.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.util;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26
27
28 /**
29  * An object of this class provides an easy way to access
30  * Strings in a Map that are prefixed like "prefix.foo".
31  * The actual map wrapped by this object can contain more
32  * key-value-pairs, but you can access only the prefixed keys
33  * through the mapper.
34  */

35 public class NamespaceMap {
36     public static final String JavaDoc SEPARATOR = ".";
37     private Map JavaDoc map;
38     private String JavaDoc prefix;
39
40     /**
41      * Creates a new NamespaceMap object.
42      * @param prefix The prefix.
43      */

44     public NamespaceMap(String JavaDoc prefix) {
45         this(new HashMap JavaDoc(), prefix);
46     }
47
48     /**
49      * Creates a new NamespaceMap.
50      * @param map A map containing the prefixed key-value-pairs.
51      * @param prefix The prefix.
52      */

53     public NamespaceMap(Map JavaDoc map, String JavaDoc prefix) {
54         this.map = map;
55         this.prefix = prefix;
56     }
57
58     /**
59      * Returns the prefix.
60      * @return A string.
61      */

62     public String JavaDoc getPrefix() {
63         return prefix;
64     }
65
66     /**
67      * Returns the namespace prefix.
68      * @return The namespace prefix.
69      */

70     protected Map JavaDoc getMapObject() {
71         return map;
72     }
73
74     /**
75      * Returns a map that contains only the un-prefixed key-value-pairs.
76      * @return The map.
77      */

78     public Map JavaDoc getMap() {
79         Map JavaDoc resultMap = new HashMap JavaDoc();
80
81         Set JavaDoc keys = getMapObject().keySet();
82
83         for (Iterator JavaDoc i = keys.iterator(); i.hasNext();) {
84             Object JavaDoc key = i.next();
85
86             if (key instanceof String JavaDoc) {
87                 String JavaDoc keyString = (String JavaDoc) key;
88
89                 if (keyString.startsWith(getPrefix() + SEPARATOR)) {
90                     resultMap.put(getShortName(getPrefix(), keyString), getMapObject().get(key));
91                 }
92             }
93         }
94
95         return resultMap;
96     }
97
98     /**
99      * Puts a value for prefixed key into the map.
100      * @param key The key without prefix.
101      * @param value The value.
102      */

103     public void put(String JavaDoc key, Object JavaDoc value) {
104         getMapObject().put(getFullName(getPrefix(), key), value);
105     }
106
107     /**
108      * Returns the value for a prefixed key.
109      * @param key The key without prefix.
110      * @return The value.
111      */

112     public Object JavaDoc get(String JavaDoc key) {
113         return getMap().get(key);
114     }
115
116     /**
117      * Returns the full (prefixed) key for a short (un-prefixed) key.
118      * @param prefix The prefix.
119      * @param key The un-prefixed key.
120      * @return A string (prefix + {@link #SEPARATOR} + key).
121      */

122     public static String JavaDoc getFullName(String JavaDoc prefix, String JavaDoc key) {
123         return prefix + SEPARATOR + key;
124     }
125
126     /**
127      * Returns the short (un-prefixed) key for a full (prefixed) key.
128      * @param prefix The prefix.
129      * @param key The full (prefixed) key.
130      * @return A string.
131      */

132     public static String JavaDoc getShortName(String JavaDoc prefix, String JavaDoc key) {
133         return key.substring(prefix.length() + SEPARATOR.length());
134     }
135     
136     /**
137      * Puts all prefixed key-value-pairs of map into this map.
138      * @param map A map.
139      */

140     public void putAll(Map JavaDoc map) {
141         for (Iterator JavaDoc i = map.keySet().iterator(); i.hasNext(); ) {
142             String JavaDoc key = (String JavaDoc) i.next();
143             put(key, map.get(key));
144         }
145     }
146     
147     /**
148      * Returns a map with prefixed keys.
149      * @return A map.
150      */

151     public Map JavaDoc getPrefixedMap() {
152         return new HashMap JavaDoc(getMapObject());
153     }
154     
155 }
156
Popular Tags