1 57 58 59 63 64 package com.sun.org.apache.xml.internal.serialize; 65 66 import com.sun.org.apache.xerces.internal.dom.DOMMessageFormatter; 67 68 import java.io.InputStream ; 69 import java.io.InputStreamReader ; 70 import java.io.BufferedReader ; 71 import java.util.Hashtable ; 72 import java.util.Locale ; 73 74 75 89 public final class HTMLdtd 90 { 91 92 95 public static final String HTMLPublicId = "-//W3C//DTD HTML 4.01//EN"; 96 97 100 public static final String HTMLSystemId = 101 "http://www.w3.org/TR/html4/strict.dtd"; 102 103 106 public static final String XHTMLPublicId = 107 "-//W3C//DTD XHTML 1.0 Strict//EN"; 108 109 112 public static final String XHTMLSystemId = 113 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"; 114 115 119 private static Hashtable _byChar; 120 121 122 126 private static Hashtable _byName; 127 128 129 private static Hashtable _boolAttrs; 130 131 132 135 private static Hashtable _elemDefs; 136 137 138 142 private static final String ENTITIES_RESOURCE = "HTMLEntities.res"; 143 144 145 148 private static final int ONLY_OPENING = 0x0001; 149 150 153 private static final int ELEM_CONTENT = 0x0002; 154 155 156 159 private static final int PRESERVE = 0x0004; 160 161 162 165 private static final int OPT_CLOSING = 0x0008; 166 167 168 171 private static final int EMPTY = 0x0010 | ONLY_OPENING; 172 173 174 177 private static final int ALLOWED_HEAD = 0x0020; 178 179 180 183 private static final int CLOSE_P = 0x0040; 184 185 186 189 private static final int CLOSE_DD_DT = 0x0080; 190 191 192 195 private static final int CLOSE_SELF = 0x0100; 196 197 198 201 private static final int CLOSE_TABLE = 0x0200; 202 203 204 207 private static final int CLOSE_TH_TD = 0x04000; 208 209 210 217 public static boolean isEmptyTag( String tagName ) 218 { 219 return isElement( tagName, EMPTY ); 220 } 221 222 223 231 public static boolean isElementContent( String tagName ) 232 { 233 return isElement( tagName, ELEM_CONTENT ); 234 } 235 236 237 245 public static boolean isPreserveSpace( String tagName ) 246 { 247 return isElement( tagName, PRESERVE ); 248 } 249 250 251 259 public static boolean isOptionalClosing( String tagName ) 260 { 261 return isElement( tagName, OPT_CLOSING ); 262 } 263 264 265 272 public static boolean isOnlyOpening( String tagName ) 273 { 274 return isElement( tagName, ONLY_OPENING ); 275 } 276 277 278 288 public static boolean isClosing( String tagName, String openTag ) 289 { 290 if ( openTag.equalsIgnoreCase( "HEAD" ) ) 292 return ! isElement( tagName, ALLOWED_HEAD ); 293 if ( openTag.equalsIgnoreCase( "P" ) ) 295 return isElement( tagName, CLOSE_P ); 296 if ( openTag.equalsIgnoreCase( "DT" ) || openTag.equalsIgnoreCase( "DD" ) ) 298 return isElement( tagName, CLOSE_DD_DT ); 299 if ( openTag.equalsIgnoreCase( "LI" ) || openTag.equalsIgnoreCase( "OPTION" ) ) 301 return isElement( tagName, CLOSE_SELF ); 302 if ( openTag.equalsIgnoreCase( "THEAD" ) || openTag.equalsIgnoreCase( "TFOOT" ) || 304 openTag.equalsIgnoreCase( "TBODY" ) || openTag.equalsIgnoreCase( "TR" ) || 305 openTag.equalsIgnoreCase( "COLGROUP" ) ) 306 return isElement( tagName, CLOSE_TABLE ); 307 if ( openTag.equalsIgnoreCase( "TH" ) || openTag.equalsIgnoreCase( "TD" ) ) 309 return isElement( tagName, CLOSE_TH_TD ); 310 return false; 311 } 312 313 314 322 public static boolean isURI( String tagName, String attrName ) 323 { 324 return ( attrName.equalsIgnoreCase( "href" ) || attrName.equalsIgnoreCase( "src" ) ); 326 } 327 328 329 337 public static boolean isBoolean( String tagName, String attrName ) 338 { 339 String [] attrNames; 340 341 attrNames = (String []) _boolAttrs.get( tagName.toUpperCase(Locale.ENGLISH) ); 342 if ( attrNames == null ) 343 return false; 344 for ( int i = 0 ; i < attrNames.length ; ++i ) 345 if ( attrNames[ i ].equalsIgnoreCase( attrName ) ) 346 return true; 347 return false; 348 } 349 350 351 359 public static int charFromName( String name ) 360 { 361 Object value; 362 363 initialize(); 364 value = _byName.get( name ); 365 if ( value != null && value instanceof Integer ) 366 return ( (Integer ) value ).intValue(); 367 else 368 return -1; 369 } 370 371 372 380 public static String fromChar(int value ) 381 { 382 if (value > 0xffff) 383 return null; 384 385 String name; 386 387 initialize(); 388 name = (String ) _byChar.get( new Integer ( value ) ); 389 return name; 390 } 391 392 393 399 private static void initialize() 400 { 401 InputStream is = null; 402 BufferedReader reader = null; 403 int index; 404 String name; 405 String value; 406 int code; 407 String line; 408 409 if ( _byName != null ) 411 return; 412 try { 413 _byName = new Hashtable (); 414 _byChar = new Hashtable (); 415 is = HTMLdtd.class.getResourceAsStream( ENTITIES_RESOURCE ); 416 if ( is == null ) { 417 throw new RuntimeException ( 418 DOMMessageFormatter.formatMessage( 419 DOMMessageFormatter.SERIALIZER_DOMAIN, 420 "ResourceNotFound", new Object [] {ENTITIES_RESOURCE})); 421 } 422 reader = new BufferedReader ( new InputStreamReader ( is, "ASCII" ) ); 423 line = reader.readLine(); 424 while ( line != null ) { 425 if ( line.length() == 0 || line.charAt( 0 ) == '#' ) { 426 line = reader.readLine(); 427 continue; 428 } 429 index = line.indexOf( ' ' ); 430 if ( index > 1 ) { 431 name = line.substring( 0, index ); 432 ++index; 433 if ( index < line.length() ) { 434 value = line.substring( index ); 435 index = value.indexOf( ' ' ); 436 if ( index > 0 ) 437 value = value.substring( 0, index ); 438 code = Integer.parseInt( value ); 439 defineEntity( name, (char) code ); 440 } 441 } 442 line = reader.readLine(); 443 } 444 is.close(); 445 } catch ( Exception except ) { 446 throw new RuntimeException ( 447 DOMMessageFormatter.formatMessage( 448 DOMMessageFormatter.SERIALIZER_DOMAIN, 449 "ResourceNotLoaded", new Object [] {ENTITIES_RESOURCE, except.toString()})); 450 } finally { 451 if ( is != null ) { 452 try { 453 is.close(); 454 } catch ( Exception except ) { } 455 } 456 } 457 } 458 459 460 472 private static void defineEntity( String name, char value ) 473 { 474 if ( _byName.get( name ) == null ) { 475 _byName.put( name, new Integer ( value ) ); 476 _byChar.put( new Integer ( value ), name ); 477 } 478 } 479 480 481 private static void defineElement( String name, int flags ) 482 { 483 _elemDefs.put( name, new Integer ( flags ) ); 484 } 485 486 487 private static void defineBoolean( String tagName, String attrName ) 488 { 489 defineBoolean( tagName, new String [] { attrName } ); 490 } 491 492 493 private static void defineBoolean( String tagName, String [] attrNames ) 494 { 495 _boolAttrs.put( tagName, attrNames ); 496 } 497 498 499 private static boolean isElement( String name, int flag ) 500 { 501 Integer flags; 502 503 flags = (Integer ) _elemDefs.get( name.toUpperCase(Locale.ENGLISH) ); 504 if ( flags == null ) 505 return false; 506 else 507 return ( ( flags.intValue() & flag ) == flag ); 508 } 509 510 511 static 512 { 513 _elemDefs = new Hashtable (); 514 defineElement( "ADDRESS", CLOSE_P ); 515 defineElement( "AREA", EMPTY ); 516 defineElement( "BASE", EMPTY | ALLOWED_HEAD ); 517 defineElement( "BASEFONT", EMPTY ); 518 defineElement( "BLOCKQUOTE", CLOSE_P ); 519 defineElement( "BODY", OPT_CLOSING ); 520 defineElement( "BR", EMPTY ); 521 defineElement( "COL", EMPTY ); 522 defineElement( "COLGROUP", ELEM_CONTENT | OPT_CLOSING | CLOSE_TABLE ); 523 defineElement( "DD", OPT_CLOSING | ONLY_OPENING | CLOSE_DD_DT ); 524 defineElement( "DIV", CLOSE_P ); 525 defineElement( "DL", ELEM_CONTENT | CLOSE_P ); 526 defineElement( "DT", OPT_CLOSING | ONLY_OPENING | CLOSE_DD_DT ); 527 defineElement( "FIELDSET", CLOSE_P ); 528 defineElement( "FORM", CLOSE_P ); 529 defineElement( "FRAME", EMPTY | OPT_CLOSING ); 530 defineElement( "H1", CLOSE_P ); 531 defineElement( "H2", CLOSE_P ); 532 defineElement( "H3", CLOSE_P ); 533 defineElement( "H4", CLOSE_P ); 534 defineElement( "H5", CLOSE_P ); 535 defineElement( "H6", CLOSE_P ); 536 defineElement( "HEAD", ELEM_CONTENT | OPT_CLOSING ); 537 defineElement( "HR", EMPTY | CLOSE_P ); 538 defineElement( "HTML", ELEM_CONTENT | OPT_CLOSING ); 539 defineElement( "IMG", EMPTY ); 540 defineElement( "INPUT", EMPTY ); 541 defineElement( "ISINDEX", EMPTY | ALLOWED_HEAD ); 542 defineElement( "LI", OPT_CLOSING | ONLY_OPENING | CLOSE_SELF ); 543 defineElement( "LINK", EMPTY | ALLOWED_HEAD ); 544 defineElement( "MAP", ALLOWED_HEAD ); 545 defineElement( "META", EMPTY | ALLOWED_HEAD ); 546 defineElement( "OL", ELEM_CONTENT | CLOSE_P ); 547 defineElement( "OPTGROUP", ELEM_CONTENT ); 548 defineElement( "OPTION", OPT_CLOSING | ONLY_OPENING | CLOSE_SELF ); 549 defineElement( "P", OPT_CLOSING | CLOSE_P | CLOSE_SELF ); 550 defineElement( "PARAM", EMPTY ); 551 defineElement( "PRE", PRESERVE | CLOSE_P ); 552 defineElement( "SCRIPT", ALLOWED_HEAD | PRESERVE ); 553 defineElement( "NOSCRIPT", ALLOWED_HEAD | PRESERVE ); 554 defineElement( "SELECT", ELEM_CONTENT ); 555 defineElement( "STYLE", ALLOWED_HEAD | PRESERVE ); 556 defineElement( "TABLE", ELEM_CONTENT | CLOSE_P ); 557 defineElement( "TBODY", ELEM_CONTENT | OPT_CLOSING | CLOSE_TABLE ); 558 defineElement( "TD", OPT_CLOSING | CLOSE_TH_TD ); 559 defineElement( "TEXTAREA", PRESERVE ); 560 defineElement( "TFOOT", ELEM_CONTENT | OPT_CLOSING | CLOSE_TABLE ); 561 defineElement( "TH", OPT_CLOSING | CLOSE_TH_TD ); 562 defineElement( "THEAD", ELEM_CONTENT | OPT_CLOSING | CLOSE_TABLE ); 563 defineElement( "TITLE", ALLOWED_HEAD ); 564 defineElement( "TR", ELEM_CONTENT | OPT_CLOSING | CLOSE_TABLE ); 565 defineElement( "UL", ELEM_CONTENT | CLOSE_P ); 566 567 _boolAttrs = new Hashtable (); 568 defineBoolean( "AREA", "href" ); 569 defineBoolean( "BUTTON", "disabled" ); 570 defineBoolean( "DIR", "compact" ); 571 defineBoolean( "DL", "compact" ); 572 defineBoolean( "FRAME", "noresize" ); 573 defineBoolean( "HR", "noshade" ); 574 defineBoolean( "IMAGE", "ismap" ); 575 defineBoolean( "INPUT", new String [] { "defaultchecked", "checked", "readonly", "disabled" } ); 576 defineBoolean( "LINK", "link" ); 577 defineBoolean( "MENU", "compact" ); 578 defineBoolean( "OBJECT", "declare" ); 579 defineBoolean( "OL", "compact" ); 580 defineBoolean( "OPTGROUP", "disabled" ); 581 defineBoolean( "OPTION", new String [] { "default-selected", "selected", "disabled" } ); 582 defineBoolean( "SCRIPT", "defer" ); 583 defineBoolean( "SELECT", new String [] { "multiple", "disabled" } ); 584 defineBoolean( "STYLE", "disabled" ); 585 defineBoolean( "TD", "nowrap" ); 586 defineBoolean( "TH", "nowrap" ); 587 defineBoolean( "TEXTAREA", new String [] { "disabled", "readonly" } ); 588 defineBoolean( "UL", "compact" ); 589 590 initialize(); 591 } 592 593 594 595 } 596 597 | Popular Tags |