1 58 package org.apache.ecs; 59 60 import java.util.Enumeration; 61 import java.io.OutputStream; 62 import java.io.PrintWriter; 63 import java.io.IOException; 64 import java.io.ByteArrayOutputStream; 65 import java.io.BufferedOutputStream; 66 67 74 75 public abstract class ElementAttributes extends GenericElement implements Attributes 76 { 77 80 public ElementAttributes() 81 { 82 } 83 84 88 private Filter attribute_filter = getFilter(); 90 94 private boolean filter_attribute_state = ECSDefaults.getDefaultFilterAttributeState(); 95 96 100 private char attribute_equality_sign = ECSDefaults.getDefaultAttributeEqualitySign(); 101 102 106 private char attribute_quote_char = ECSDefaults.getDefaultAttributeQuoteChar(); 107 108 112 private boolean attribute_quote = ECSDefaults.getDefaultAttributeQuote(); 113 114 118 public Element setAttributeQuoteChar(char quote_char) 119 { 120 attribute_quote_char = quote_char; 121 return(this); 122 } 123 124 127 public char getAttributeQuoteChar() 128 { 129 return(attribute_quote_char); 130 } 131 132 136 public Element setAttributeEqualitySign(char equality_sign) 137 { 138 attribute_equality_sign = equality_sign; 139 return(this); 140 } 141 142 145 public char getAttributeEqualitySign() 146 { 147 return(attribute_equality_sign); 148 } 149 150 153 public boolean getAttributeQuote() 154 { 155 return(attribute_quote); 156 } 157 158 161 public Element setAttributeQuote(boolean attribute_quote) 162 { 163 this.attribute_quote = attribute_quote; 164 return(this); 165 } 166 167 170 public Element setID(String id) 171 { 172 addAttribute("id",id); 173 return(this); 174 } 175 176 179 public Element setClass(String element_class) 180 { 181 addAttribute("class",element_class); 182 return(this); 183 } 184 185 189 public Element setLang(String lang) 190 { 191 addAttribute("lang",lang); 192 return(this); 193 } 194 195 199 public Element setStyle(String style) 200 { 201 addAttribute("style",style); 202 return(this); 203 } 204 205 209 public Element setDir(String dir) 210 { 211 addAttribute("dir",dir); 212 return(this); 213 } 214 218 public Element setTitle(String title) 219 { 220 addAttribute("title",title); 221 return(this); 222 } 223 224 227 public boolean getAttributeFilterState() 228 { 229 return(filter_attribute_state); 230 } 231 232 236 public Element setAttributeFilterState(boolean filter_attribute_state) 237 { 238 this.filter_attribute_state = filter_attribute_state; 239 return(this); 240 } 241 242 248 public Element setAttributeFilter(Filter attribute_filter) 249 { 250 filter_attribute_state = true; this.attribute_filter = attribute_filter; 252 return(this); 253 } 254 255 261 public Filter getAttributeFilter() 262 { 263 return(this.attribute_filter); 264 } 265 266 267 268 public Element addAttribute(String attribute_name, Object attribute_value) 269 { 270 getElementHashEntry().put(attribute_name, attribute_value); 271 return(this); 272 } 273 274 275 public Element addAttribute(String attribute_name, int attribute_value) 276 { 277 getElementHashEntry().put(attribute_name, new Integer(attribute_value)); 278 return(this); 279 } 280 281 282 public Element addAttribute(String attribute_name, String attribute_value) 283 { 284 getElementHashEntry().put(attribute_name, attribute_value); 285 return(this); 286 } 287 288 289 public Element addAttribute(String attribute_name, Integer attribute_value) 290 { 291 getElementHashEntry().put(attribute_name, attribute_value); 292 return(this); 293 } 294 295 296 public Element removeAttribute(String attribute_name) 297 { 298 try 299 { 300 getElementHashEntry().remove(attribute_name); 301 } 302 catch ( Exception e ) 303 { 304 } 305 return(this); 306 } 307 308 309 public boolean hasAttribute(String attribute) 310 { 311 return(getElementHashEntry().containsKey(attribute)); 312 } 313 314 315 public Enumeration attributes() 316 { 317 return getElementHashEntry().keys(); 318 } 319 320 324 public String getAttribute(String attribute) 325 { 326 return (String)getElementHashEntry().get(attribute); 327 } 328 329 333 public String createStartTag() 334 { 335 StringBuffer out = new StringBuffer(); 336 337 out.append(getStartTagChar()); 338 339 if(getBeginStartModifierDefined()) 340 { 341 out.append(getBeginStartModifier()); 342 } 343 out.append(getElementType()); 344 345 Enumeration enum = getElementHashEntry().keys(); 346 String value = null; 348 while (enum.hasMoreElements()) 349 { 350 String attr = (String) enum.nextElement(); 351 if(getAttributeFilterState()) 352 { 353 value = getAttributeFilter().process(getElementHashEntry().get(attr).toString()); 354 } 355 else 356 { 357 Object valueObj = getElementHashEntry().get(attr); 358 if (valueObj == null) { 359 return null; 360 } 361 value = valueObj.toString(); 362 } 363 out.append(' '); 364 out.append(alterCase(attr)); 365 int iStartPos = out.length(); 366 if ( !value.equalsIgnoreCase(NO_ATTRIBUTE_VALUE) ) 367 { 368 boolean quoteThisAttribute = attribute_quote; 371 int singleQuoteFound = 0; 372 int doubleQuoteFound = 0; 373 if ( value.length() == 0 ) 374 { quoteThisAttribute = true; 376 } 377 for ( int ii = 0; ii < value.length(); ii++ ) 378 { 379 char c = value.charAt( ii ); 380 if ( 'a' <= c && c <= 'z' ) 381 { 382 continue; 383 } 384 if ( 'A' <= c && c <= 'Z' ) 385 { 386 continue; 387 } 388 if ( '0' <= c && c <= '9' ) 389 { 390 continue; 391 } 392 if ( c == ':' || 393 c == '-' || 394 c == '_' || 395 c == '.' ) 396 { 397 continue; 398 } 399 if ( c == '\'' ) 400 { 401 singleQuoteFound++; 402 } 403 if ( c == '"' ) 404 { 405 doubleQuoteFound++; 406 } 407 quoteThisAttribute = true; 408 } 409 out.append( getAttributeEqualitySign() ); 410 if ( !quoteThisAttribute ) 411 { out.append( value ); 413 } 414 else 415 { if ( singleQuoteFound == 0 ) 417 { out.append( '\'' ); 419 out.append( value ); 420 out.append( '\'' ); 421 } 422 else 423 if ( doubleQuoteFound == 0 ) 424 { out.append( '"' ); 426 out.append( value ); 427 out.append( '"' ); 428 } 429 else if ( singleQuoteFound <= doubleQuoteFound ) 430 { out.append( '\'' ); 432 int startPos = out.length(); 433 out.append( value ); 434 for ( int ii = startPos; ii < out.length(); ii++ ) 435 { if ( out.charAt( ii ) == '\'' ) 437 { 438 out.setCharAt( ii, '&'); 439 out.insert( ii+1, "#39;" ); 440 ii++; 441 } 442 } 443 out.append( '\'' ); 444 } 445 else 446 { out.append( '"' ); 448 int startPos = out.length(); 449 out.append( value ); 450 for ( int ii = startPos; ii < out.length(); ii++ ) 451 { if ( out.charAt( ii ) == '"' ) 453 { 454 out.setCharAt( ii, '&'); 455 out.insert( ii+1, "#34;" ); 456 ii++; 457 } 458 } 459 out.append( '"' ); 460 } 461 } 462 } 463 } 464 if(getBeginEndModifierDefined()) 465 { 466 out.append(getBeginEndModifier()); 467 } 468 out.append(getEndTagChar()); 469 470 return(out.toString()); 471 } 472 } 473
| Popular Tags
|