1 6 9 package org.objectweb.jac.aspects.gui.web.html; 10 11 import java.io.IOException ; 12 import java.io.OutputStream ; 13 import java.io.OutputStreamWriter ; 14 import java.io.StringWriter ; 15 import java.io.Writer ; 16 import java.util.Enumeration ; 17 import java.util.Hashtable ; 18 import org.mortbay.util.Code; 19 20 21 32 public abstract class Element 33 { 34 35 public static final String 36 noAttributes="", 37 ALIGN="align", 38 LEFT="left", 39 RIGHT="right", 40 CENTER="center", 41 VALIGN="valign", 42 TOP="top", 43 BOTTOM="bottom", 44 MIDDLE="middle", 45 WIDTH="width", 46 HEIGHT="height", 47 SIZE="size", 48 COLOR="color", 49 BGCOLOR="bgcolor", 50 STYLE="style", 51 CLASS="class", 52 ID="id"; 53 54 55 56 57 58 private int width=-1; 59 private int height=-1; 60 private int size=-1; 61 62 63 65 private String attributes=null; 66 protected Hashtable attributeMap=null; 67 68 69 71 public Element(){} 72 73 74 77 public Element(String attributes) 78 { 79 attribute(attributes); 80 } 81 82 83 89 public abstract void write(Writer out) 90 throws IOException ; 91 92 93 99 public void write(OutputStream out) 100 throws IOException 101 { 102 Writer writer = new OutputStreamWriter (out); 103 write(writer); 104 writer.flush(); 105 } 106 107 108 114 public void write(OutputStream out, String encoding) 115 throws IOException 116 { 117 Writer writer = new OutputStreamWriter (out,encoding); 118 write(writer); 119 writer.flush(); 120 } 121 122 123 public String attributes() 124 { 125 if (attributes==null && attributeMap==null) 126 return noAttributes; 127 128 StringBuffer buf = new StringBuffer (128); 129 synchronized(buf) 130 { 131 if (attributeMap!=null) 132 { 133 Enumeration e = attributeMap.keys(); 134 while (e.hasMoreElements()) 135 { 136 buf.append(' '); 137 String a = (String )e.nextElement(); 138 buf.append(a); 139 buf.append('='); 140 buf.append("\""); 141 buf.append(attributeMap.get(a).toString()); 142 buf.append("\""); 143 } 144 } 145 146 if(attributes!=null && attributes.length()>0) 147 { 148 if (!attributes.startsWith(" ")) 149 buf.append(' '); 150 buf.append(attributes); 151 } 152 } 153 154 return buf.toString(); 155 } 156 157 158 166 public Element attributes(String attributes) 167 { 168 if (Code.debug() && attributes!=null && attributes.indexOf('=')>=0) 169 Code.warning("Set attribute with old method: "+attributes+ 170 " on " + getClass().getName()); 171 172 if (attributes==null) 173 { 174 this.attributes=null; 175 return this; 176 } 177 178 if (attributes==noAttributes) 179 return this; 180 181 if (this.attributes==null) 182 this.attributes=attributes; 183 else 184 this.attributes += ' '+attributes; 185 return this; 186 } 187 188 189 193 public Element setAttributesFrom(Element e) 194 { 195 attributes=e.attributes; 196 attributeMap=(Hashtable )e.attributeMap.clone(); 197 return this; 198 } 199 200 201 202 210 public Element attribute(String attributes) 211 { 212 if (Code.debug() && attributes!=null && attributes.indexOf('=')>=0) 213 Code.warning("Set attribute with old method: "+attributes+ 214 " on " + getClass().getName()); 215 216 if (attributes==null || 217 this.attributes==null || 218 this.attributes==noAttributes || 219 this.attributes.length()==0) 220 this.attributes=attributes; 221 else 222 this.attributes += ' '+attributes; 223 return this; 224 } 225 226 227 232 public Element attribute(String attribute, Object value) 233 { 234 if (attributeMap==null) 235 attributeMap=new Hashtable (10); 236 237 if (value!=null) 238 { 239 if (value instanceof String && ((String )value).indexOf('"')!=-1) 240 value=quoteAttributeValue((String )value); 241 242 attributeMap.put(attribute,value.toString()); 243 } 244 return this; 245 } 246 247 static String quoteAttributeValue(String value) { 248 String s=(String )value; 249 int q=0; 250 while((q=s.indexOf('"',q))>=0) 251 { 252 s=s.substring(0,q)+"""+s.substring(++q); 253 q+=6; 254 } 255 return s; 256 } 257 258 public Element appendAttribute(String attribute, Object value) 259 { 260 if (attributeMap==null) 261 attributeMap=new Hashtable (10); 262 263 if (value!=null) 264 { 265 if (value instanceof String && ((String )value).indexOf('"')!=-1) 266 value=quoteAttributeValue((String )value); 267 String currentValue = (String )attributeMap.get(attribute); 268 if (currentValue!=null) 269 value = currentValue+value; 270 attributeMap.put(attribute,value); 271 } 272 return this; 273 } 274 275 280 public Element attribute(String attribute, long value) 281 { 282 if (attributeMap==null) 283 attributeMap=new Hashtable (10); 284 285 attributeMap.put(attribute,Long.toString(value)); 286 return this; 287 } 288 289 290 294 public String toString() 295 { 296 try{ 297 StringWriter out = new StringWriter (); 298 write(out); 299 out.flush(); 300 return out.toString(); 301 } 302 catch(IOException e){ 303 Code.ignore(e); 304 } 305 return null; 306 } 307 308 309 313 public Element left() 314 { 315 return attribute(ALIGN,LEFT); 316 } 317 318 319 323 public Element right() 324 { 325 return attribute(ALIGN,RIGHT); 326 } 327 328 329 333 public Element center() 334 { 335 return attribute(ALIGN,CENTER); 336 } 337 338 339 343 public Element top() 344 { 345 return attribute(VALIGN,TOP); 346 } 347 348 349 353 public Element bottom() 354 { 355 return attribute(VALIGN,BOTTOM); 356 } 357 358 359 363 public Element middle() 364 { 365 return attribute(VALIGN,MIDDLE); 366 } 367 368 369 373 public Element width(int w) 374 { 375 width=w; 376 return attribute(WIDTH,w); 377 } 378 379 380 384 public Element width(String w) 385 { 386 width=-1; 387 return attribute(WIDTH,w); 388 } 389 390 391 public int width() 392 { 393 return width; 394 } 395 396 397 401 public Element height(int h) 402 { 403 height=h; 404 return attribute(HEIGHT,h); 405 } 406 407 408 412 public Element height(String h) 413 { 414 height=-1; 415 return attribute(HEIGHT,h); 416 } 417 418 419 public int height() 420 { 421 return height; 422 } 423 424 425 429 public Element size(int s) 430 { 431 size=s; 432 return attribute(SIZE,s); 433 } 434 435 436 440 public Element size(String s) 441 { 442 size=-1; 443 return attribute(SIZE,s); 444 } 445 446 447 public int size() 448 { 449 return size; 450 } 451 452 453 457 public Element color(String color) 458 { 459 return attribute(COLOR,color); 460 } 461 462 463 467 public Element bgColor(String color) 468 { 469 return attribute(BGCOLOR,color); 470 } 471 472 473 475 public Element cssClass(String c) 476 { 477 return attribute(CLASS,c); 478 } 479 480 481 483 public Element addCssClass(String c) 484 { 485 return appendAttribute(CLASS," "+c); 486 } 487 488 489 492 public Element cssID(String id) 493 { 494 return attribute(ID,id); 495 } 496 497 498 501 public Element style(String style) 502 { 503 return attribute(STYLE,style); 504 } 505 } 506 507 508 509 510 | Popular Tags |