1 16 package org.mortbay.html; 17 import java.io.IOException ; 18 import java.io.OutputStream ; 19 import java.io.OutputStreamWriter ; 20 import java.io.StringWriter ; 21 import java.io.Writer ; 22 import java.util.Enumeration ; 23 import java.util.Hashtable ; 24 25 import org.apache.commons.logging.Log; 26 import org.mortbay.log.LogFactory; 27 import org.mortbay.util.LogSupport; 28 29 30 41 public abstract class Element 42 { 43 private static Log log = LogFactory.getLog(Element.class); 44 45 46 public static final String 47 noAttributes="", 48 ALIGN="align", 49 LEFT="left", 50 RIGHT="right", 51 CENTER="center", 52 VALIGN="valign", 53 TOP="top", 54 BOTTOM="bottom", 55 MIDDLE="middle", 56 WIDTH="width", 57 HEIGHT="height", 58 SIZE="size", 59 COLOR="color", 60 BGCOLOR="bgcolor", 61 STYLE="style", 62 CLASS="class", 63 ID="id"; 64 65 66 67 68 69 private int width=-1; 70 private int height=-1; 71 private int size=-1; 72 73 74 76 private String attributes=null; 77 protected Hashtable attributeMap=null; 78 79 80 82 public Element(){} 83 84 85 88 public Element(String attributes) 89 { 90 attribute(attributes); 91 } 92 93 94 100 public abstract void write(Writer out) 101 throws IOException ; 102 103 104 110 public void write(OutputStream out) 111 throws IOException 112 { 113 Writer writer = new OutputStreamWriter (out); 114 write(writer); 115 writer.flush(); 116 } 117 118 119 125 public void write(OutputStream out, String encoding) 126 throws IOException 127 { 128 Writer writer = new OutputStreamWriter (out,encoding); 129 write(writer); 130 writer.flush(); 131 } 132 133 134 public String attributes() 135 { 136 if (attributes==null && attributeMap==null) 137 return noAttributes; 138 139 StringBuffer buf = new StringBuffer (128); 140 synchronized(buf) 141 { 142 if (attributeMap!=null) 143 { 144 Enumeration e = attributeMap.keys(); 145 while (e.hasMoreElements()) 146 { 147 buf.append(' '); 148 String a = (String )e.nextElement(); 149 buf.append(a); 150 buf.append('='); 151 buf.append(attributeMap.get(a).toString()); 152 } 153 } 154 155 if(attributes!=null && attributes.length()>0) 156 { 157 if (!attributes.startsWith(" ")) 158 buf.append(' '); 159 buf.append(attributes); 160 } 161 } 162 163 return buf.toString(); 164 } 165 166 167 175 public Element attributes(String attributes) 176 { 177 if (log.isDebugEnabled() && attributes!=null && attributes.indexOf('=')>=0) 178 log.debug("Set attribute with old method: "+attributes+ 179 " on " + getClass().getName()); 180 181 if (attributes==null) 182 { 183 this.attributes=null; 184 return this; 185 } 186 187 if (attributes==noAttributes) 188 return this; 189 190 if (this.attributes==null) 191 this.attributes=attributes; 192 else 193 this.attributes += ' '+attributes; 194 return this; 195 } 196 197 198 202 public Element setAttributesFrom(Element e) 203 { 204 attributes=e.attributes; 205 attributeMap=(Hashtable )e.attributeMap.clone(); 206 return this; 207 } 208 209 210 211 219 public Element attribute(String attributes) 220 { 221 if (log.isDebugEnabled() && attributes!=null && attributes.indexOf('=')>=0) 222 log.warn("Set attribute with old method: "+attributes+ 223 " on " + getClass().getName()); 224 225 if (attributes==null || 226 this.attributes==null || 227 this.attributes==noAttributes || 228 this.attributes.length()==0) 229 this.attributes=attributes; 230 else 231 this.attributes += ' '+attributes; 232 return this; 233 } 234 235 236 241 public Element attribute(String attribute, Object value) 242 { 243 if (attributeMap==null) 244 attributeMap=new Hashtable (10); 245 246 if (value!=null) 247 { 248 if (value instanceof String && ((String )value).indexOf('"')!=-1) 249 { 250 String s=(String )value; 251 int q=0; 252 while((q=s.indexOf('"',q))>=0) 253 { 254 s=s.substring(0,q)+"""+s.substring(++q); 255 q+=6; 256 } 257 value=s; 258 } 259 260 attributeMap.put(attribute,"\""+value+'"'); 261 } 262 return this; 263 } 264 265 266 271 public Element attribute(String attribute, long value) 272 { 273 if (attributeMap==null) 274 attributeMap=new Hashtable (10); 275 276 attributeMap.put(attribute,Long.toString(value)); 277 return this; 278 } 279 280 281 285 public String toString() 286 { 287 try{ 288 StringWriter out = new StringWriter (); 289 write(out); 290 out.flush(); 291 return out.toString(); 292 } 293 catch(IOException e){ 294 LogSupport.ignore(log,e); 295 } 296 return null; 297 } 298 299 300 304 public Element left() 305 { 306 return attribute(ALIGN,LEFT); 307 } 308 309 310 314 public Element right() 315 { 316 return attribute(ALIGN,RIGHT); 317 } 318 319 320 324 public Element center() 325 { 326 return attribute(ALIGN,CENTER); 327 } 328 329 330 334 public Element top() 335 { 336 return attribute(VALIGN,TOP); 337 } 338 339 340 344 public Element bottom() 345 { 346 return attribute(VALIGN,BOTTOM); 347 } 348 349 350 354 public Element middle() 355 { 356 return attribute(VALIGN,MIDDLE); 357 } 358 359 360 364 public Element width(int w) 365 { 366 width=w; 367 return attribute(WIDTH,w); 368 } 369 370 371 375 public Element width(String w) 376 { 377 width=-1; 378 return attribute(WIDTH,w); 379 } 380 381 382 public int width() 383 { 384 return width; 385 } 386 387 388 392 public Element height(int h) 393 { 394 height=h; 395 return attribute(HEIGHT,h); 396 } 397 398 399 403 public Element height(String h) 404 { 405 height=-1; 406 return attribute(HEIGHT,h); 407 } 408 409 410 public int height() 411 { 412 return height; 413 } 414 415 416 420 public Element size(int s) 421 { 422 size=s; 423 return attribute(SIZE,s); 424 } 425 426 427 431 public Element size(String s) 432 { 433 size=-1; 434 return attribute(SIZE,s); 435 } 436 437 438 public int size() 439 { 440 return size; 441 } 442 443 444 448 public Element color(String color) 449 { 450 return attribute(COLOR,color); 451 } 452 453 454 458 public Element bgColor(String color) 459 { 460 return attribute(BGCOLOR,color); 461 } 462 463 464 466 public Element cssClass(String c) 467 { 468 return attribute(CLASS,c); 469 } 470 471 472 475 public Element cssID(String id) 476 { 477 return attribute(ID,id); 478 } 479 480 481 484 public Element style(String style) 485 { 486 return attribute(STYLE,style); 487 } 488 } 489 490 491 492 493 | Popular Tags |