1 17 18 19 20 package org.apache.lenya.util; 21 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import java.util.Set ; 26 27 28 35 public class NamespaceMap { 36 public static final String SEPARATOR = "."; 37 private Map map; 38 private String prefix; 39 40 44 public NamespaceMap(String prefix) { 45 this(new HashMap (), prefix); 46 } 47 48 53 public NamespaceMap(Map map, String prefix) { 54 this.map = map; 55 this.prefix = prefix; 56 } 57 58 62 public String getPrefix() { 63 return prefix; 64 } 65 66 70 protected Map getMapObject() { 71 return map; 72 } 73 74 78 public Map getMap() { 79 Map resultMap = new HashMap (); 80 81 Set keys = getMapObject().keySet(); 82 83 for (Iterator i = keys.iterator(); i.hasNext();) { 84 Object key = i.next(); 85 86 if (key instanceof String ) { 87 String keyString = (String ) 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 103 public void put(String key, Object value) { 104 getMapObject().put(getFullName(getPrefix(), key), value); 105 } 106 107 112 public Object get(String key) { 113 return getMap().get(key); 114 } 115 116 122 public static String getFullName(String prefix, String key) { 123 return prefix + SEPARATOR + key; 124 } 125 126 132 public static String getShortName(String prefix, String key) { 133 return key.substring(prefix.length() + SEPARATOR.length()); 134 } 135 136 140 public void putAll(Map map) { 141 for (Iterator i = map.keySet().iterator(); i.hasNext(); ) { 142 String key = (String ) i.next(); 143 put(key, map.get(key)); 144 } 145 } 146 147 151 public Map getPrefixedMap() { 152 return new HashMap (getMapObject()); 153 } 154 155 } 156 | Popular Tags |