1 23 package com.sun.enterprise.diagnostics.report.html; 24 25 import java.io.File ; 26 import java.io.FileWriter ; 27 import java.io.IOException ; 28 import java.io.Writer ; 29 import java.util.LinkedList ; 30 import java.util.List ; 31 import java.util.Iterator ; 32 33 34 37 public class HTMLElement implements Element { 38 39 42 private String name = null; 43 44 47 private List <HTMLComponent> children = new LinkedList <HTMLComponent>(); 48 49 52 private List <Attribute> attributes = new LinkedList <Attribute>(); 53 54 private static final char TAG_MARKER_BEGIN = '<'; 55 private static final char TAG_MARKER_END = '>'; 56 private static final String SLASH_BEGIN="/>"; 57 private static final String SLASH_END="</"; 58 59 63 public HTMLElement(String name) { 64 setName(name); 65 } 66 67 71 public void add(HTMLComponent child) { 72 if (child == null) { 73 throw new NullPointerException ("Child node is null."); 74 } 75 if (child == this) { 76 throw new IllegalArgumentException ("Attempt to add a node " + 77 "to itself. Node is " + toString() + "."); 78 } 79 if (child instanceof Attribute) { 80 attributes.add((Attribute) child); 81 } else { 82 children.add(child); 83 } 84 } 85 86 87 89 public Element addElement(String name) { 90 if (name == null) { 91 throw new NullPointerException ("Element name is null."); 92 } 93 Element newElement = new HTMLElement(name); 94 add(newElement); 95 return newElement; 96 } 97 98 100 public Text addComment(String content) { 101 HTMLComment comment = new HTMLComment(content); 102 add(comment); 103 return comment; 104 105 } 106 107 109 public Text addText(String text) { 110 if(text != null) { 111 Text textNode = new HTMLText(text); 112 add(textNode); 113 return textNode; 114 } 115 return null; 116 } 117 118 120 public void addText(Iterator <String > textValues) { 121 while(textValues.hasNext()) { 122 Text textNode = new HTMLText(textValues.next()); 123 add(textNode); 124 } 125 } 126 public Attribute addAttribute(String id, String value) { 127 Attribute att = new HTMLAttribute(id, value); 128 add(att); 129 return att; 130 } 131 133 public List <Element> getElements() { 134 List <Element> retval = new LinkedList <Element>(); 135 for (HTMLComponent node : children) { 136 if (node instanceof Element) { 137 retval.add((Element) node); 138 } 139 } return retval; 141 } 142 143 144 146 public List <Element> getElements(String name) { 147 List <Element> list = new LinkedList <Element>(); 148 for (HTMLComponent node : children) { 149 if (node instanceof Element) { 150 Element element = (Element) node; 151 if (element.getName().equalsIgnoreCase(name)) { 152 list.add((Element) node); 153 } 154 } 155 } return list; 157 } 158 159 161 public List <Text> getComments() { 162 List <Text> list = new LinkedList <Text>(); 163 for (HTMLComponent node : children) { 164 if (node instanceof HTMLComment) { 165 list.add((HTMLComment) node); 166 } 167 } return list; 169 } 170 171 173 public List <Text> getTexts() { 174 List <Text> retval = new LinkedList <Text>(); 175 for (HTMLComponent node : children) { 176 if (node instanceof Text) { 177 retval.add((Text) node); 178 } 179 } return retval; 181 } 182 183 185 public List <Attribute> getAttributes() { 186 List <Attribute> retval = new LinkedList <Attribute>(); 187 retval.addAll(attributes); 188 return retval; 189 } 190 191 192 194 public List <Attribute> getAttributes(String name) { 195 List <Attribute> retval = new LinkedList <Attribute>(); 196 for (HTMLComponent node : getAttributes()) { 197 if (node instanceof Attribute) { 198 Attribute att = (Attribute) node; 199 if (att.getName().equalsIgnoreCase(name)) { 200 retval.add((Attribute) node); 201 } 202 } 203 } return retval; 205 } 206 207 209 public List <HTMLComponent> children() { 210 List <HTMLComponent> childrenList = new LinkedList <HTMLComponent>(); 211 childrenList.addAll(children); 212 childrenList.addAll(attributes); 213 return childrenList; 214 } 215 216 217 220 public void delete(HTMLComponent child) { 221 if (child instanceof Attribute) { 222 attributes.remove(child); 223 } else { 224 children.remove(child); 225 } 226 } 227 228 229 232 public String toString() { 233 StringBuffer buf = new StringBuffer (); 234 235 String eName = Escape.getInstance().encodeEntities(name, " \t\r\n"); 236 buf.append(TAG_MARKER_BEGIN).append(eName); 237 for (Attribute att : attributes) { 238 buf.append(" ").append(att.toString()); 239 } if (children.size() == 0) { 241 buf.append(SLASH_BEGIN); 242 } else { 243 buf.append(TAG_MARKER_END); 244 for (HTMLComponent node : children) { 245 buf.append(node.toString()); 246 } buf.append(SLASH_END).append(eName).append(TAG_MARKER_END); 248 } 249 return buf.toString(); 250 } 251 252 253 255 public void write(Writer output) throws IOException { 256 output.append(toString()); 257 output.flush(); 258 } 259 260 261 263 public void write(File file) throws IOException { 264 FileWriter fileWriter = new FileWriter (file); 265 write(fileWriter); 266 fileWriter.close(); 267 } 268 269 270 272 public String getName() { 273 return name; 274 } 275 276 277 279 public void setName(String name) { 280 if (name == null) { 281 throw new NullPointerException ("Element name is null."); 282 } 283 if (name.length() == 0) { 284 throw new IllegalArgumentException ("Element name is empty."); 285 } 286 this.name = name; 287 } 288 289 290 295 public <T extends HTMLComponent> List <T> get(Class <T> type) { 296 List <T> list = new LinkedList <T>(); 297 for (HTMLComponent child : children()) { 298 if (type.isInstance(child)) { 299 list.add((T) child); 300 } 301 } 302 return list; 303 } 304 } 305 | Popular Tags |