1 5 6 package org.w3c.tidy; 7 8 33 34 38 39 import java.util.Enumeration ; 40 import java.util.Properties ; 41 import java.util.StringTokenizer ; 42 import java.io.FileInputStream ; 43 import java.io.IOException ; 44 45 public class Configuration implements java.io.Serializable { 46 47 48 public static final int RAW = 0; 49 public static final int ASCII = 1; 50 public static final int LATIN1 = 2; 51 public static final int UTF8 = 3; 52 public static final int ISO2022 = 4; 53 public static final int MACROMAN = 5; 54 55 56 public static final int DOCTYPE_OMIT = 0; 57 public static final int DOCTYPE_AUTO = 1; 58 public static final int DOCTYPE_STRICT= 2; 59 public static final int DOCTYPE_LOOSE = 3; 60 public static final int DOCTYPE_USER = 4; 61 62 protected int spaces = 2; 63 protected int wraplen = 68; 64 protected int CharEncoding = ASCII; 65 protected int tabsize = 4; 66 67 protected int docTypeMode = DOCTYPE_AUTO; 68 protected String altText = null; 69 protected String slidestyle = null; 70 protected String docTypeStr = null; 71 protected String errfile = null; 72 protected boolean writeback = false; 73 74 protected boolean OnlyErrors = false; 75 protected boolean ShowWarnings = true; 76 protected boolean Quiet = false; 77 protected boolean IndentContent = false; 78 protected boolean SmartIndent = false; 79 protected boolean HideEndTags = false; 80 protected boolean XmlTags = false; 81 protected boolean XmlOut = false; 82 protected boolean xHTML = false; 83 protected boolean XmlPi = false; 84 protected boolean RawOut = false; 85 protected boolean UpperCaseTags = false; 86 protected boolean UpperCaseAttrs = false; 87 protected boolean MakeClean = false; 88 protected boolean LogicalEmphasis = false; 89 protected boolean DropFontTags = false; 90 protected boolean DropEmptyParas = true; 91 protected boolean FixComments = true; 92 protected boolean BreakBeforeBR = false; 93 protected boolean BurstSlides = false; 94 protected boolean NumEntities = false; 95 protected boolean QuoteMarks = false; 96 protected boolean QuoteNbsp = true; 97 protected boolean QuoteAmpersand = true; 98 protected boolean WrapAttVals = false; 99 protected boolean WrapScriptlets = false; 100 protected boolean WrapSection = true; 101 protected boolean WrapAsp = true; 102 protected boolean WrapJste = true; 103 protected boolean WrapPhp = true; 104 protected boolean FixBackslash = true; 105 protected boolean IndentAttributes = false; 106 protected boolean XmlPIs = false; 107 protected boolean XmlSpace = false; 108 protected boolean EncloseBodyText = false; 109 protected boolean EncloseBlockText = false; 110 protected boolean KeepFileTimes = true; 111 protected boolean Word2000 = false; 112 protected boolean TidyMark = true; 113 protected boolean Emacs = false; 114 protected boolean LiteralAttribs = false; 115 116 protected TagTable tt; 117 118 private transient Properties _properties = new Properties (); 119 120 public Configuration() 121 { 122 } 123 124 public void addProps( Properties p ) 125 { 126 Enumeration propenum = p.propertyNames(); 127 while (propenum.hasMoreElements()) 128 { 129 String key = (String ) propenum.nextElement(); 130 String value = p.getProperty(key); 131 _properties.put(key, value); 132 } 133 parseProps(); 134 } 135 136 public void parseFile( String filename ) 137 { 138 try 139 { 140 _properties.load( new FileInputStream ( filename ) ); 141 } 142 catch (IOException e) 143 { 144 System.err.println(filename + e.toString()); 145 return; 146 } 147 parseProps(); 148 } 149 150 private void parseProps() 151 { 152 String value; 153 154 value = _properties.getProperty("indent-spaces"); 155 if (value != null) 156 spaces = parseInt(value, "indent-spaces"); 157 158 value = _properties.getProperty("wrap"); 159 if (value != null) 160 wraplen = parseInt(value, "wrap"); 161 162 value = _properties.getProperty("wrap-attributes"); 163 if (value != null) 164 WrapAttVals = parseBool(value, "wrap-attributes"); 165 166 value = _properties.getProperty("wrap-script-literals"); 167 if (value != null) 168 WrapScriptlets = parseBool(value, "wrap-script-literals"); 169 170 value = _properties.getProperty("wrap-sections"); 171 if (value != null) 172 WrapSection = parseBool(value, "wrap-sections"); 173 174 value = _properties.getProperty("wrap-asp"); 175 if (value != null) 176 WrapAsp = parseBool(value, "wrap-asp"); 177 178 value = _properties.getProperty("wrap-jste"); 179 if (value != null) 180 WrapJste = parseBool(value, "wrap-jste"); 181 182 value = _properties.getProperty("wrap-php"); 183 if (value != null) 184 WrapPhp = parseBool(value, "wrap-php"); 185 186 value = _properties.getProperty("literal-attributes"); 187 if (value != null) 188 LiteralAttribs = parseBool(value, "literal-attributes"); 189 190 value = _properties.getProperty("tab-size"); 191 if (value != null) 192 tabsize = parseInt(value, "tab-size"); 193 194 value = _properties.getProperty("markup"); 195 if (value != null) 196 OnlyErrors = parseInvBool(value, "markup"); 197 198 value = _properties.getProperty("quiet"); 199 if (value != null) 200 Quiet = parseBool(value, "quiet"); 201 202 value = _properties.getProperty("tidy-mark"); 203 if (value != null) 204 TidyMark = parseBool(value, "tidy-mark"); 205 206 value = _properties.getProperty("indent"); 207 if (value != null) 208 IndentContent = parseIndent(value, "indent"); 209 210 value = _properties.getProperty("indent-attributes"); 211 if (value != null) 212 IndentAttributes = parseBool(value, "ident-attributes"); 213 214 value = _properties.getProperty("hide-endtags"); 215 if (value != null) 216 HideEndTags = parseBool(value, "hide-endtags"); 217 218 value = _properties.getProperty("input-xml"); 219 if (value != null) 220 XmlTags = parseBool(value, "input-xml"); 221 222 value = _properties.getProperty("output-xml"); 223 if (value != null) 224 XmlOut = parseBool(value, "output-xml"); 225 226 value = _properties.getProperty("output-xhtml"); 227 if (value != null) 228 xHTML = parseBool(value, "output-xhtml"); 229 230 value = _properties.getProperty("add-xml-pi"); 231 if (value != null) 232 XmlPi = parseBool(value, "add-xml-pi"); 233 234 value = _properties.getProperty("add-xml-decl"); 235 if (value != null) 236 XmlPi = parseBool(value, "add-xml-decl"); 237 238 value = _properties.getProperty("assume-xml-procins"); 239 if (value != null) 240 XmlPIs = parseBool(value, "assume-xml-procins"); 241 242 value = _properties.getProperty("raw"); 243 if (value != null) 244 RawOut = parseBool(value, "raw"); 245 246 value = _properties.getProperty("uppercase-tags"); 247 if (value != null) 248 UpperCaseTags = parseBool(value, "uppercase-tags"); 249 250 value = _properties.getProperty("uppercase-attributes"); 251 if (value != null) 252 UpperCaseAttrs = parseBool(value, "uppercase-attributes"); 253 254 value = _properties.getProperty("clean"); 255 if (value != null) 256 MakeClean = parseBool(value, "clean"); 257 258 value = _properties.getProperty("logical-emphasis"); 259 if (value != null) 260 LogicalEmphasis = parseBool(value, "logical-emphasis"); 261 262 value = _properties.getProperty("word-2000"); 263 if (value != null) 264 Word2000 = parseBool(value, "word-2000"); 265 266 value = _properties.getProperty("drop-empty-paras"); 267 if (value != null) 268 DropEmptyParas = parseBool(value, "drop-empty-paras"); 269 270 value = _properties.getProperty("drop-font-tags"); 271 if (value != null) 272 DropFontTags = parseBool(value, "drop-font-tags"); 273 274 value = _properties.getProperty("enclose-text"); 275 if (value != null) 276 EncloseBodyText = parseBool(value, "enclose-text"); 277 278 value = _properties.getProperty("enclose-block-text"); 279 if (value != null) 280 EncloseBlockText = parseBool(value, "enclose-block-text"); 281 282 value = _properties.getProperty("alt-text"); 283 if (value != null) 284 altText = value; 285 286 value = _properties.getProperty("add-xml-space"); 287 if (value != null) 288 XmlSpace = parseBool(value, "add-xml-space"); 289 290 value = _properties.getProperty("fix-bad-comments"); 291 if (value != null) 292 FixComments = parseBool(value, "fix-bad-comments"); 293 294 value = _properties.getProperty("split"); 295 if (value != null) 296 BurstSlides = parseBool(value, "split"); 297 298 value = _properties.getProperty("break-before-br"); 299 if (value != null) 300 BreakBeforeBR = parseBool(value, "break-before-br"); 301 302 value = _properties.getProperty("numeric-entities"); 303 if (value != null) 304 NumEntities = parseBool(value, "numeric-entities"); 305 306 value = _properties.getProperty("quote-marks"); 307 if (value != null) 308 QuoteMarks = parseBool(value, "quote-marks"); 309 310 value = _properties.getProperty("quote-nbsp"); 311 if (value != null) 312 QuoteNbsp = parseBool(value, "quote-nbsp"); 313 314 value = _properties.getProperty("quote-ampersand"); 315 if (value != null) 316 QuoteAmpersand = parseBool(value, "quote-ampersand"); 317 318 value = _properties.getProperty("write-back"); 319 if (value != null) 320 writeback = parseBool(value, "write-back"); 321 322 value = _properties.getProperty("keep-time"); 323 if (value != null) 324 KeepFileTimes = parseBool(value, "keep-time"); 325 326 value = _properties.getProperty("show-warnings"); 327 if (value != null) 328 ShowWarnings = parseBool(value, "show-warnings"); 329 330 value = _properties.getProperty("error-file"); 331 if (value != null) 332 errfile = parseName(value, "error-file"); 333 334 value = _properties.getProperty("slide-style"); 335 if (value != null) 336 slidestyle = parseName(value, "slide-style"); 337 338 value = _properties.getProperty("new-inline-tags"); 339 if (value != null) 340 parseInlineTagNames(value, "new-inline-tags"); 341 342 value = _properties.getProperty("new-blocklevel-tags"); 343 if (value != null) 344 parseBlockTagNames(value, "new-blocklevel-tags"); 345 346 value = _properties.getProperty("new-empty-tags"); 347 if (value != null) 348 parseEmptyTagNames(value, "new-empty-tags"); 349 350 value = _properties.getProperty("new-pre-tags"); 351 if (value != null) 352 parsePreTagNames(value, "new-pre-tags"); 353 354 value = _properties.getProperty("char-encoding"); 355 if (value != null) 356 CharEncoding = parseCharEncoding(value, "char-encoding"); 357 358 value = _properties.getProperty("doctype"); 359 if (value != null) 360 docTypeStr = parseDocType(value, "doctype"); 361 362 value = _properties.getProperty("fix-backslash"); 363 if (value != null) 364 FixBackslash = parseBool(value, "fix-backslash"); 365 366 value = _properties.getProperty("gnu-emacs"); 367 if (value != null) 368 Emacs = parseBool(value, "gnu-emacs"); 369 } 370 371 372 public void adjust() 373 { 374 if (EncloseBlockText) 375 EncloseBodyText = true; 376 377 378 379 if (SmartIndent) 380 IndentContent = true; 381 382 383 if (wraplen == 0) 384 wraplen = 0x7FFFFFFF; 385 386 387 if (Word2000) 388 { 389 tt.defineInlineTag("o:p"); 390 } 391 392 393 if (xHTML) 394 { 395 XmlOut = true; 396 UpperCaseTags = false; 397 UpperCaseAttrs = false; 398 } 399 400 401 if (XmlTags) 402 { 403 XmlOut = true; 404 XmlPIs = true; 405 } 406 407 408 if (XmlOut) 409 { 410 QuoteAmpersand = true; 411 HideEndTags = false; 412 } 413 } 414 415 private static int parseInt( String s, String option ) 416 { 417 int i = 0; 418 try { 419 i = Integer.parseInt( s ); 420 } 421 catch ( NumberFormatException e ) { 422 Report.badArgument(option); 423 i = -1; 424 } 425 return i; 426 } 427 428 private static boolean parseBool( String s, String option ) 429 { 430 boolean b = false; 431 if ( s != null && s.length() > 0 ) { 432 char c = s.charAt(0); 433 if ((c == 't') || (c == 'T') || (c == 'Y') || (c == 'y') || (c == '1')) 434 b = true; 435 else if ((c == 'f') || (c == 'F') || (c == 'N') || (c == 'n') || (c == '0')) 436 b = false; 437 else 438 Report.badArgument(option); 439 } 440 return b; 441 } 442 443 private static boolean parseInvBool( String s, String option ) 444 { 445 boolean b = false; 446 if ( s != null && s.length() > 0 ) { 447 char c = s.charAt(0); 448 if ((c == 't') || (c == 'T') || (c == 'Y') || (c == 'y')) 449 b = true; 450 else if ((c == 'f') || (c == 'F') || (c == 'N') || (c == 'n')) 451 b = false; 452 else 453 Report.badArgument(option); 454 } 455 return !b; 456 } 457 458 private static String parseName( String s, String option ) 459 { 460 StringTokenizer t = new StringTokenizer ( s ); 461 String rs = null; 462 if ( t.countTokens() >= 1 ) 463 rs = t.nextToken(); 464 else 465 Report.badArgument(option); 466 return rs; 467 } 468 469 private static int parseCharEncoding( String s, String option ) 470 { 471 int result = ASCII; 472 473 if (Lexer.wstrcasecmp(s, "ascii") == 0) 474 result = ASCII; 475 else if (Lexer.wstrcasecmp(s, "latin1") == 0) 476 result = LATIN1; 477 else if (Lexer.wstrcasecmp(s, "raw") == 0) 478 result = RAW; 479 else if (Lexer.wstrcasecmp(s, "utf8") == 0) 480 result = UTF8; 481 else if (Lexer.wstrcasecmp(s, "iso2022") == 0) 482 result = ISO2022; 483 else if (Lexer.wstrcasecmp(s, "mac") == 0) 484 result = MACROMAN; 485 else 486 Report.badArgument(option); 487 488 return result; 489 } 490 491 492 private boolean parseIndent( String s, String option ) 493 { 494 boolean b = IndentContent; 495 496 if (Lexer.wstrcasecmp(s, "yes") == 0) 497 { 498 b = true; 499 SmartIndent = false; 500 } 501 else if (Lexer.wstrcasecmp(s, "true") == 0) 502 { 503 b = true; 504 SmartIndent = false; 505 } 506 else if (Lexer.wstrcasecmp(s, "no") == 0) 507 { 508 b = false; 509 SmartIndent = false; 510 } 511 else if (Lexer.wstrcasecmp(s, "false") == 0) 512 { 513 b = false; 514 SmartIndent = false; 515 } 516 else if (Lexer.wstrcasecmp(s, "auto") == 0) 517 { 518 b = true; 519 SmartIndent = true; 520 } 521 else 522 Report.badArgument(option); 523 return b; 524 } 525 526 private void parseInlineTagNames( String s, String option ) 527 { 528 StringTokenizer t = new StringTokenizer ( s, " \t\n\r," ); 529 while ( t.hasMoreTokens() ) { 530 tt.defineInlineTag( t.nextToken() ); 531 } 532 } 533 534 private void parseBlockTagNames( String s, String option ) 535 { 536 StringTokenizer t = new StringTokenizer ( s, " \t\n\r," ); 537 while ( t.hasMoreTokens() ) { 538 tt.defineBlockTag( t.nextToken() ); 539 } 540 } 541 542 private void parseEmptyTagNames( String s, String option ) 543 { 544 StringTokenizer t = new StringTokenizer ( s, " \t\n\r," ); 545 while ( t.hasMoreTokens() ) { 546 tt.defineEmptyTag( t.nextToken() ); 547 } 548 } 549 550 private void parsePreTagNames( String s, String option ) 551 { 552 StringTokenizer t = new StringTokenizer ( s, " \t\n\r," ); 553 while ( t.hasMoreTokens() ) { 554 tt.definePreTag( t.nextToken() ); 555 } 556 } 557 558 565 protected String parseDocType( String s, String option ) 566 { 567 s = s.trim(); 568 569 570 571 if (s.startsWith("\"")) 572 { 573 docTypeMode = DOCTYPE_USER; 574 return s; 575 } 576 577 578 String word = ""; 579 StringTokenizer t = new StringTokenizer ( s, " \t\n\r," ); 580 if (t.hasMoreTokens()) 581 word = t.nextToken(); 582 583 if (Lexer.wstrcasecmp(word, "omit") == 0) 584 docTypeMode = DOCTYPE_OMIT; 585 else if (Lexer.wstrcasecmp(word, "strict") == 0) 586 docTypeMode = DOCTYPE_STRICT; 587 else if (Lexer.wstrcasecmp(word, "loose") == 0 || 588 Lexer.wstrcasecmp(word, "transitional") == 0) 589 docTypeMode = DOCTYPE_LOOSE; 590 else if (Lexer.wstrcasecmp(word, "auto") == 0) 591 docTypeMode = DOCTYPE_AUTO; 592 else 593 { 594 docTypeMode = DOCTYPE_AUTO; 595 Report.badArgument(option); 596 } 597 return null; 598 } 599 600 } 601 | Popular Tags |