1 64 65 package com.jcorporate.expresso.services.html; 66 67 import org.apache.log4j.Logger; 68 69 import java.io.OutputStream ; 70 import java.io.PrintWriter ; 71 import java.util.Hashtable ; 72 import java.util.Vector ; 73 74 75 81 public abstract class HtmlElement { 82 protected Vector contents = new Vector (5); 83 private String thisClass = (this.getClass().getName() + "."); 84 private HtmlElement myParent = null; 85 private String myObjName = "No Name"; 86 private boolean hasBeenDisplayed = false; 87 protected String cSSClass = "jc-default"; 88 protected String cSSID = null; 89 protected Hashtable attributes = null; 90 private static Logger log = Logger.getLogger(HtmlElement.class); 91 private static char[] padding = new char[256]; 92 93 { 95 for (int i = 0; i < padding.length; i++) { 96 padding[i] = '\t'; 97 } 98 } 99 100 105 public HtmlElement() 106 throws HtmlException { 107 super(); 108 } 109 110 117 public HtmlElement(HtmlElement newElement) 118 throws HtmlException { 119 super(); 120 add(newElement); 121 } 122 123 130 public HtmlElement(String elementName) 131 throws HtmlException { 132 super(); 133 134 if (elementName != null) { 135 setName(elementName); 136 } else { 137 setName("null"); 138 } 139 } 140 141 147 public synchronized void add(HtmlElement newElement) 148 throws HtmlException { 149 String myName = (thisClass + "add(HtmlElement)"); 150 151 if (newElement == null) { 152 throw new HtmlException(myName + ":Cannot add null element to '" + 153 getName() + "'"); 154 } 155 156 newElement.setParent(this); 157 contents.addElement(newElement); 158 } 159 160 161 167 protected void display(OutputStream out) 168 throws HtmlException { 169 display(new PrintWriter (out), 0); 170 } 171 172 173 181 protected abstract void display(PrintWriter out, int depth) 182 throws HtmlException; 183 184 185 190 protected void finalize() 191 throws HtmlException { 192 String myName = (thisClass + "finalize()"); 193 194 if (!hasBeenDisplayed) { 195 throw new HtmlException(myName + ":WARNING: Object '" + getName() + 196 "' was never displayed!"); 197 } 198 } 199 200 201 208 public Object getAttribute(String attribName) 209 throws HtmlException { 210 if (attributes == null) { 211 return null; 212 } 213 214 return attributes.get(attribName); 215 } 216 217 218 223 public int getContentCount() { 224 return contents.size(); 225 } 226 227 230 public String getCSSClass() { 231 return cSSClass; 232 } 233 234 241 public String getName() 242 throws HtmlException { 243 String myName = (thisClass + "getName()"); 244 245 if (myObjName == null) { 246 myObjName = ("Null Name"); 247 } 248 if (myParent == this) { 249 throw new HtmlException(myName + ":Object " + myObjName + 250 " cannot have itself as a parent"); 251 } 252 253 return ("<strong>" + myObjName + "</strong>"); 254 } 255 256 257 264 protected void padWithTabs(PrintWriter out, int count) { 265 if (count > padding.length) { 266 log.warn("Encountered > " + padding.length + 267 " nesting. Source format will no longer be padded"); 268 out.print(new String (padding, 0, count)); 269 } 270 } 271 272 279 public synchronized void setAttribute(String attribName, 280 Object attribValue) 281 throws HtmlException { 282 if (attributes == null) { 283 attributes = new Hashtable (); 284 } 285 286 attributes.put(attribName, attribValue); 287 } 288 289 290 293 public void setCSSClass(String className) { 294 cSSClass = className; 295 } 296 297 300 public void setCSSID(String id) { 301 cSSID = id; 302 } 303 304 309 protected void setDisplayed() 310 throws HtmlException { 311 String myName = (thisClass + "setDisplayed()"); 312 313 if (hasBeenDisplayed) { 314 throw new HtmlException(myName + ":Element " + getName() + 315 " has already been displayed - cannot display twice"); 316 } 317 } 318 319 320 326 public void setName(String newName) 327 throws HtmlException { 328 String myName = (thisClass + "setName(String)"); 329 330 if (newName == null) { 331 throw new HtmlException(myName + ":Cannot set name to null"); 332 } 333 334 myObjName = newName; 335 } 336 337 338 345 protected void setParent(HtmlElement newParent) 346 throws HtmlException { 347 String myName = (thisClass + "setParent(HtmlElement)"); 348 349 if (newParent == null) { 350 throw new HtmlException(myName + ":Can't set parent of " + 351 getName() + " to a null element"); 352 } 353 if (myParent != null) { 354 if (newParent == myParent) { 355 throw new HtmlException(myName + ":This element, " + 356 getName() + 357 ", has already been added to the element you are " + 358 "adding it to: " + 359 newParent.getName() + 360 " - cannot add again"); 361 } else { 362 throw new HtmlException(myName + ":This element, '" + 363 getName() + 364 "', has already been added to another element:" + 365 myParent.getName() + 366 " - cannot add it to another elements"); 367 } 368 } 369 370 371 myParent = newParent; 372 } 373 374 } 375 | Popular Tags |