1 17 package org.ajaxtags.xml; 18 19 import java.lang.reflect.InvocationTargetException ; 20 import java.util.Collection ; 21 import java.util.HashMap ; 22 import java.util.Map ; 23 24 import org.ajaxtags.helpers.TreeItem; 25 import org.apache.commons.beanutils.BeanUtils; 26 import org.apache.commons.lang.StringUtils; 27 28 36 public final class AjaxTreeXmlBuilder extends BaseXmlBuilder<TreeItem> { 37 38 private Map <String , String > ensureMap(Map <String , String > attributes) { 39 if (attributes == null) { 40 return new HashMap <String , String >(); 41 } 42 return attributes; 43 } 44 45 52 public AjaxTreeXmlBuilder addItem(String name, String value) { 53 return addItem(name, value, null); 54 } 55 56 64 public AjaxTreeXmlBuilder addItemAsCData(String name, String value) { 65 return addItemAsCData(name, value, null); 66 } 67 68 77 public AjaxTreeXmlBuilder addItemAsCData(String name, String value, 78 Map <String , String > attributes) { 79 return addItem(name, value, true, attributes); 80 } 81 82 91 public AjaxTreeXmlBuilder addItem(String name, String value, 92 Map <String , String > attributes) { 93 return addItem(name, value, false, attributes); 94 } 95 96 106 public AjaxTreeXmlBuilder addItem(String name, String value, 107 boolean asCData, Map <String , String > attributes) { 108 TreeItem treeitem = new TreeItem(name, value, asCData, 109 ensureMap(attributes)); 110 getListe().add(treeitem); 111 return this; 112 } 113 114 124 public AjaxTreeXmlBuilder addItem(String name, String value, String url, 125 boolean asCData) { 126 return addItem(name, value, false, url, asCData); 127 } 128 129 138 public AjaxTreeXmlBuilder addItem(String name, String value, 139 boolean collapsed, String url) { 140 return addItem(name, value, collapsed, url, false); 141 } 142 143 154 public AjaxTreeXmlBuilder addItem(String name, String value, 155 boolean collapsed, String url, boolean asCData) { 156 Map <String , String > data = ensureMap(null); 157 data.put(TreeItem.URL, url); 158 data.put(TreeItem.COLLAPSED, String.valueOf(collapsed)); 159 return addItem(name, value, asCData, data); 160 } 161 162 172 public AjaxTreeXmlBuilder addItemAsCData(String name, String value, 173 boolean collapsed, String url) { 174 Map <String , String > data = ensureMap(null); 175 data.put(TreeItem.URL, url); 176 data.put(TreeItem.COLLAPSED, String.valueOf(collapsed)); 177 return addItem(name, value, true, data); 178 } 179 180 194 public AjaxTreeXmlBuilder addItems(Collection <?> collection, 195 String nameProperty, String valueProperty, 196 String collapsedProperty, String urlProperty) 197 throws IllegalAccessException , InvocationTargetException , 198 NoSuchMethodException { 199 return addItems(collection, nameProperty, valueProperty, 200 collapsedProperty, urlProperty, false); 201 } 202 203 218 public AjaxTreeXmlBuilder addItems(Collection <?> collection, 219 String nameProperty, String valueProperty, 220 String collapsedProperty, String urlProperty, boolean asCData) 221 throws IllegalAccessException , InvocationTargetException , 222 NoSuchMethodException { 223 return addItems(collection, nameProperty, valueProperty, 224 collapsedProperty, urlProperty, null, asCData); 225 } 226 227 243 public AjaxTreeXmlBuilder addItems(Collection <?> collection, 244 String nameProperty, String valueProperty, 245 String collapsedProperty, String urlProperty, String leafProperty, 246 boolean asCData) throws IllegalAccessException , 247 InvocationTargetException , NoSuchMethodException { 248 249 for (Object element : collection) { 250 String name = BeanUtils.getProperty(element, nameProperty); 251 String value = BeanUtils.getProperty(element, valueProperty); 252 Map <String , String > data = ensureMap(null); 253 if (StringUtils.isNotEmpty(urlProperty)) { 254 data.put(TreeItem.URL, BeanUtils.getProperty(element, 255 urlProperty)); 256 } 257 if (StringUtils.isNotEmpty(collapsedProperty)) { 258 data.put(TreeItem.COLLAPSED, String.valueOf(BeanUtils 259 .getProperty(element, collapsedProperty))); 260 } 261 if (StringUtils.isNotEmpty(leafProperty)) { 262 data.put(TreeItem.LEAF, BeanUtils.getProperty(element, 263 leafProperty)); 264 } 265 addItem(name, value, data); 266 } 267 return this; 268 } 269 270 284 public AjaxTreeXmlBuilder addItemsAsCData(Collection <?> collection, 285 String nameProperty, String valueProperty, 286 String collapsedProperty, String urlProperty) 287 throws IllegalAccessException , InvocationTargetException , 288 NoSuchMethodException { 289 return addItemsAsCData(collection, nameProperty, valueProperty, 290 collapsedProperty, urlProperty, null); 291 } 292 293 308 public AjaxTreeXmlBuilder addItemsAsCData(Collection <?> collection, 309 String nameProperty, String valueProperty, 310 String collapsedProperty, String urlProperty, String leafProperty) 311 throws IllegalAccessException , InvocationTargetException , 312 NoSuchMethodException { 313 return addItems(collection, nameProperty, valueProperty, 314 collapsedProperty, urlProperty, leafProperty, true); 315 } 316 317 323 @Override 324 protected String getXMLString() { 325 StringBuffer xml = new StringBuffer (); 326 327 xml.append("<ajax-response>"); 328 xml.append("<response>"); 329 for (TreeItem item : getItems()) { 330 xml.append("<item>"); 331 xml.append("<name>"); 332 if (item.isAsCData()) { 333 xml.append("<![CDATA["); 334 } 335 xml.append(item.getName()); 336 if (item.isAsCData()) { 337 xml.append("]]>"); 338 } 339 xml.append("</name>"); 340 xml.append("<value>"); 341 if (item.isAsCData()) { 342 xml.append("<![CDATA["); 343 } 344 xml.append(item.getValue()); 345 if (item.isAsCData()) { 346 xml.append("]]>"); 347 } 348 xml.append("</value>"); 349 350 for (String attr : item.getAttributeKeySet()) { 351 352 xml.append("<").append(attr).append(">"); 353 xml.append(item.getAttributeValue(attr)); 354 xml.append("</").append(attr).append(">"); 355 } 356 357 xml.append("</item>"); 358 } 359 xml.append("</response>"); 360 xml.append("</ajax-response>"); 361 362 return xml.toString(); 363 364 } 365 366 } 367 | Popular Tags |