001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: /* $Id: NamespaceMap.java 473861 2006-11-12 03:51:14Z gregor $ */
020:
021: package org.apache.lenya.util;
022:
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.Map;
026: import java.util.Set;
027:
028: /**
029: * An object of this class provides an easy way to access
030: * Strings in a Map that are prefixed like "prefix.foo".
031: * The actual map wrapped by this object can contain more
032: * key-value-pairs, but you can access only the prefixed keys
033: * through the mapper.
034: */
035: public class NamespaceMap {
036: /**
037: * <code>SEPARATOR</code> The seperator character
038: */
039: public static final String SEPARATOR = ".";
040: private Map map;
041: private String prefix;
042:
043: /**
044: * Creates a new NamespaceMap object.
045: * @param _prefix The prefix.
046: */
047: public NamespaceMap(String _prefix) {
048: this (new HashMap(), _prefix);
049: }
050:
051: /**
052: * Creates a new NamespaceMap.
053: * @param _map A map containing the prefixed key-value-pairs.
054: * @param _prefix The prefix.
055: */
056: public NamespaceMap(Map _map, String _prefix) {
057: this .map = _map;
058: this .prefix = _prefix;
059: }
060:
061: /**
062: * Returns the prefix.
063: * @return A string.
064: */
065: public String getPrefix() {
066: return this .prefix;
067: }
068:
069: /**
070: * Returns the namespace prefix.
071: * @return The namespace prefix.
072: */
073: protected Map getMapObject() {
074: return this .map;
075: }
076:
077: /**
078: * Returns a map that contains only the un-prefixed key-value-pairs.
079: * @return The map.
080: */
081: public Map getMap() {
082: Map resultMap = new HashMap();
083:
084: Set keys = getMapObject().keySet();
085:
086: for (Iterator i = keys.iterator(); i.hasNext();) {
087: Object key = i.next();
088:
089: if (key instanceof String) {
090: String keyString = (String) key;
091:
092: if (keyString.startsWith(getPrefix() + SEPARATOR)) {
093: resultMap.put(getShortName(getPrefix(), keyString),
094: getMapObject().get(key));
095: }
096: }
097: }
098:
099: return resultMap;
100: }
101:
102: /**
103: * Puts a value for prefixed key into the map.
104: * @param key The key without prefix.
105: * @param value The value.
106: */
107: public void put(String key, Object value) {
108: getMapObject().put(getFullName(getPrefix(), key), value);
109: }
110:
111: /**
112: * Returns the value for a prefixed key.
113: * @param key The key without prefix.
114: * @return The value.
115: */
116: public Object get(String key) {
117: return getMap().get(key);
118: }
119:
120: /**
121: * Returns the full (prefixed) key for a short (un-prefixed) key.
122: * @param prefix The prefix.
123: * @param key The un-prefixed key.
124: * @return A string (prefix + {@link #SEPARATOR} + key).
125: */
126: public static String getFullName(String prefix, String key) {
127: return prefix + SEPARATOR + key;
128: }
129:
130: /**
131: * Returns the short (un-prefixed) key for a full (prefixed) key.
132: * @param prefix The prefix.
133: * @param key The full (prefixed) key.
134: * @return A string.
135: */
136: public static String getShortName(String prefix, String key) {
137: return key.substring(prefix.length() + SEPARATOR.length());
138: }
139:
140: /**
141: * Puts all prefixed key-value-pairs of map into this map.
142: * @param _map A map.
143: */
144: public void putAll(Map _map) {
145: for (Iterator i = _map.keySet().iterator(); i.hasNext();) {
146: String key = (String) i.next();
147: put(key, _map.get(key));
148: }
149: }
150:
151: /**
152: * Returns a map with prefixed keys.
153: * @return A map.
154: */
155: public Map getPrefixedMap() {
156: return new HashMap(getMapObject());
157: }
158:
159: }
|