1 23 package org.infoglue.cms.workflow.taglib; 24 25 import java.text.MessageFormat ; 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 32 36 class Element 37 { 38 41 private static final String ATTRIBUTE = "{0}=\"{1}\""; 42 43 46 private static final String CHILDREN_AND_ATTRIBUTES = "<{0} {1}>{2}</{0}>"; 47 48 51 private static final String CHILDREN_AND_NO_ATTRIBUTES = "<{0}>{1}</{0}>"; 52 53 56 private static final String NO_CHILDREN_NO_ATTRIBUTES = "<{0}></{0}>"; 57 58 61 private static final String NO_CHILDREN_AND_ATTRIBUTES = "<{0} {1}></{0}>"; 62 63 66 private final String name; 67 68 71 private Element parent; 72 73 76 private final Map attributes = new HashMap (); 77 78 81 private final List children = new ArrayList (); 82 83 88 public Element(final String name) 89 { 90 this(null, name); 91 } 92 93 99 public Element(final Element parent, final String name) 100 { 101 super(); 102 this.parent = parent; 103 this.name = name; 104 } 105 106 114 public Element addAttribute(final String name, final String value) 115 { 116 return addAttribute(name, value, true); 117 } 118 119 129 public Element addAttribute(final String name, String value, final boolean condition) 130 { 131 if(condition && value != null) 132 { 133 Object o = (Object )attributes.get(name); 134 if(o != null && o instanceof String ) 135 value = o + " " + value; 136 137 attributes.put(name, value); 138 } 139 return this; 140 } 141 142 149 public Element addChild(final String name) 150 { 151 final Element child = new Element(this, name); 152 children.add(child); 153 return child; 154 } 155 156 163 public Element addChildFirst(final String name) 164 { 165 final Element child = new Element(this, name); 166 children.add(0, child); 167 return child; 168 } 169 170 177 public Element addChild(final Element child) 178 { 179 if(child != null) 180 { 181 child.parent = this; 182 children.add(child); 183 } 184 return this; 185 } 186 187 193 public Element addText(final String text) 194 { 195 if(text != null && text.length() > 0) 196 { 197 children.add(text); 198 } 199 return this; 200 } 201 202 207 public boolean hasAttribute(final String name) 208 { 209 return name != null && attributes.containsKey(name); 210 } 211 212 217 public boolean hasAttributes() 218 { 219 return !attributes.isEmpty(); 220 } 221 222 227 public boolean hasChildren() 228 { 229 return !children.isEmpty(); 230 } 231 232 237 public Element pop() 238 { 239 return parent; 240 } 241 242 247 public String toString() 248 { 249 if(hasAttributes() && hasChildren()) 250 { 251 return MessageFormat.format(CHILDREN_AND_ATTRIBUTES, new Object [] { name, attributesString(), childrenString()}); 252 } 253 if(hasAttributes() && !hasChildren()) 254 { 255 return MessageFormat.format(NO_CHILDREN_AND_ATTRIBUTES, new Object [] { name, attributesString() }); 256 } 257 if(!hasAttributes() && hasChildren()) 258 { 259 return MessageFormat.format(CHILDREN_AND_NO_ATTRIBUTES, new Object [] { name, childrenString() }); 260 } 261 return MessageFormat.format(NO_CHILDREN_NO_ATTRIBUTES, new Object [] { name }); 262 } 263 264 269 private String attributesString() 270 { 271 final StringBuffer sb = new StringBuffer (); 272 for(final Iterator i = attributes.keySet().iterator(); i.hasNext(); ) 273 { 274 final Object key = i.next(); 275 final Object value = attributes.get(key); 276 sb.append((sb.length() > 0 ? " " : "") + MessageFormat.format(ATTRIBUTE, new Object [] { key, value })); 277 } 278 return sb.toString(); 279 } 280 281 286 private String childrenString() 287 { 288 final StringBuffer sb = new StringBuffer (); 289 for(final Iterator i = children.iterator(); i.hasNext(); ) 290 { 291 sb.append(i.next()); 292 } 293 return sb.toString(); 294 } 295 } | Popular Tags |