1 5 package xdoclet.modules.util; 6 7 import java.util.*; 8 9 import xdoclet.*; 10 import xdoclet.util.Translator; 11 12 21 public class CollectionTagsHandler extends XDocletTagSupport 22 { 23 private Map collections = new HashMap(); 25 26 37 public String get(Properties attributes) throws XDocletException 38 { 39 String name = attributes.getProperty("name"); 40 String key = attributes.getProperty("key"); 41 42 if (name == null || name.length() == 0) { 44 throw new XDocletException(Translator.getString(XDocletMessages.class, 45 XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String []{"name"})); 46 } 47 48 if (key == null || key.length() == 0) { 50 throw new XDocletException(Translator.getString(XDocletMessages.class, 51 XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String []{"key"})); 52 } 53 54 if (!collections.containsKey(name)) { 56 throw new XDocletException(Translator.getString(CollectionMessages.class, 57 CollectionMessages.COLLECTION_NOT_DEFINED, new String []{name})); 58 } 59 60 if (!(collections.get(name) instanceof Map)) { 62 throw new XDocletException(Translator.getString(CollectionMessages.class, 63 CollectionMessages.COLLECTION_IS_NOT_MAP, new String []{name})); 64 } 65 66 return (String ) ((Map) collections.get(name)).get(key); 67 } 68 69 70 81 public void create(Properties attributes) throws XDocletException 82 { 83 String name = attributes.getProperty("name"); 84 String type = attributes.getProperty("type"); 85 86 if (name == null || name.length() == 0) { 88 throw new XDocletException(Translator.getString(XDocletMessages.class, 89 XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String []{"name"})); 90 } 91 92 if (collections.containsKey(name)) { 94 throw new XDocletException(Translator.getString(CollectionMessages.class, 95 CollectionMessages.COLLECTION_ALREADY_EXISTS, new String []{name})); 96 } 97 98 if ("map".equals(type)) 100 collections.put(name, new HashMap()); 101 else 102 collections.put(name, new HashSet()); 103 } 104 105 120 public void put(Properties attributes) throws XDocletException 121 { 122 String name = attributes.getProperty("name"); 123 String key = attributes.getProperty("key"); 124 String value = attributes.getProperty("value"); 125 126 if (name == null || name.length() == 0) { 128 throw new XDocletException(Translator.getString(XDocletMessages.class, 129 XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String []{"name"})); 130 } 131 132 if (value == null || value.length() == 0) { 134 throw new XDocletException(Translator.getString(XDocletMessages.class, 135 XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String []{"value"})); 136 } 137 138 if (!collections.containsKey(name)) { 140 throw new XDocletException(Translator.getString(CollectionMessages.class, 141 CollectionMessages.COLLECTION_NOT_DEFINED, new String []{name})); 142 } 143 144 if (collections.get(name) instanceof Map) { 146 if (key == null || key.length() == 0) { 147 throw new XDocletException(Translator.getString(XDocletMessages.class, 148 XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String []{"key"})); 149 } 150 151 ((Map) collections.get(name)).put(key, value); 152 } 153 else { 154 if (key != null) { 156 throw new XDocletException(Translator.getString(CollectionMessages.class, 157 CollectionMessages.COLLECTION_IS_NOT_MAP, new String []{name})); 158 } 159 160 ((Set) collections.get(name)).add(value); 161 } 162 } 163 164 165 179 public void remove(Properties attributes) throws XDocletException 180 { 181 String name = attributes.getProperty("name"); 182 String key = attributes.getProperty("key"); 183 String value = attributes.getProperty("value"); 184 185 if (name == null || name.length() == 0) { 187 throw new XDocletException(Translator.getString(XDocletMessages.class, 188 XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String []{"name"})); 189 } 190 191 if (!collections.containsKey(name)) { 193 throw new XDocletException(Translator.getString(CollectionMessages.class, 194 CollectionMessages.COLLECTION_NOT_DEFINED, new String []{name})); 195 } 196 197 if (collections.get(name) instanceof Map) { 199 if (key == null || key.length() == 0) { 201 throw new XDocletException(Translator.getString(XDocletMessages.class, 202 XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String []{"key"})); 203 } 204 205 ((Map) collections.get(name)).remove(key); 206 } 207 else { 208 if (value == null || value.length() == 0) { 210 throw new XDocletException(Translator.getString(XDocletMessages.class, 211 XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String []{"value"})); 212 } 213 214 ((Set) collections.get(name)).remove(key); 215 } 216 } 217 218 234 public void ifContains(String template, Properties attributes) throws XDocletException 235 { 236 if (contains(attributes)) 237 generate(template); 238 } 239 240 256 public void ifDoesntContain(String template, Properties attributes) throws XDocletException 257 { 258 if (!contains(attributes)) 259 generate(template); 260 } 261 262 270 public void destroy(Properties attributes) throws XDocletException 271 { 272 String name = attributes.getProperty("name"); 273 274 if (name == null || name.length() == 0) { 276 throw new XDocletException(Translator.getString(XDocletMessages.class, 277 XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String []{"name"})); 278 } 279 280 if (!collections.containsKey(name)) { 282 throw new XDocletException(Translator.getString(CollectionMessages.class, 283 CollectionMessages.COLLECTION_NOT_DEFINED, new String []{name})); 284 } 285 286 collections.remove(name); 287 } 288 289 290 299 private boolean contains(Properties attributes) throws XDocletException 300 { 301 String name = attributes.getProperty("name"); 302 String key = attributes.getProperty("key"); 303 String value = attributes.getProperty("value"); 304 305 if (name == null || name.length() == 0) { 307 throw new XDocletException(Translator.getString(XDocletMessages.class, 308 XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String []{"name"})); 309 } 310 311 if (!collections.containsKey(name)) { 313 throw new XDocletException(Translator.getString(CollectionMessages.class, 314 CollectionMessages.COLLECTION_NOT_DEFINED, new String []{name})); 315 } 316 317 if (collections.get(name) instanceof Map) { 319 if (key == null || key.length() == 0) { 321 throw new XDocletException(Translator.getString(XDocletMessages.class, 322 XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String []{"key"})); 323 } 324 325 if (value == null) 327 return ((Map) collections.get(name)).containsKey(key); 328 else 329 return value.equals(((Map) collections.get(name)).get(key)); 330 } 331 else { 332 if (value == null || value.length() == 0) { 334 throw new XDocletException(Translator.getString(XDocletMessages.class, 335 XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String []{"value"})); 336 } 337 338 return ((Set) collections.get(name)).contains(value); 339 } 340 } 341 } 342 | Popular Tags |