1 27 package org.htmlparser.tests.utilTests; 28 29 import org.htmlparser.NodeFilter; 30 import org.htmlparser.filters.*; 31 import org.htmlparser.tags.*; 32 import org.htmlparser.tests.ParserTestCase; 33 import org.htmlparser.util.ParserUtils; 34 35 public class HTMLParserUtilsTest extends ParserTestCase { 36 37 static 38 { 39 System.setProperty ("org.htmlparser.tests.utilTests.HTMLParserUtilsTest", "HTMLParserUtilsTest"); 40 } 41 42 public HTMLParserUtilsTest(String name) { 43 super(name); 44 } 45 46 public void testRemoveTrailingSpaces() { 47 String text = "Hello World "; 48 assertStringEquals( 49 "modified text", 50 "Hello World", 51 ParserUtils.removeTrailingBlanks(text) 52 ); 53 } 54 55 public void testButCharsMethods() { 56 String [] tmpSplitButChars = ParserUtils.splitButChars("<DIV> +12.5, +3.4 </DIV>", "+.1234567890"); 57 assertStringEquals( 58 "modified text", 59 "+12.5*+3.4", 60 new String (tmpSplitButChars[0] + '*' + tmpSplitButChars[1]) 61 ); 62 assertStringEquals( 63 "modified text", 64 "+12.5", 65 ParserUtils.trimButChars("<DIV> +12.5 </DIV>", "+.1234567890") 66 ); 67 assertStringEquals( 68 "modified text", 69 "+12.5", 70 ParserUtils.trimButChars("<DIV> +1 2 . 5 </DIV>", "+.1234567890") 71 ); 72 assertStringEquals( 73 "modified text", 74 "+12.5", 75 ParserUtils.trimButCharsBeginEnd("<DIV> +12.5 </DIV>", "+.1234567890") 76 ); 77 assertStringEquals( 78 "modified text", 79 "+1 2 . 5", 80 ParserUtils.trimButCharsBeginEnd("<DIV> +1 2 . 5 </DIV>", "+.1234567890") 81 ); 82 } 83 84 public void testButDigitsMethods() { 85 String [] tmpSplitButDigits = ParserUtils.splitButDigits("<DIV> +12.5, +3.4 </DIV>", "+."); 86 assertStringEquals( 87 "modified text", 88 "+12.5*+3.4", 89 new String (tmpSplitButDigits[0] + '*' + tmpSplitButDigits[1]) 90 ); 91 assertStringEquals( 92 "modified text", 93 "+12.5", 94 ParserUtils.trimButDigits("<DIV> +12.5 </DIV>", "+.") 95 ); 96 assertStringEquals( 97 "modified text", 98 "+12.5", 99 ParserUtils.trimButDigits("<DIV> +1 2 . 5 </DIV>", "+.") 100 ); 101 assertStringEquals( 102 "modified text", 103 "+12.5", 104 ParserUtils.trimButDigitsBeginEnd("<DIV> +12.5 </DIV>", "+.") 105 ); 106 assertStringEquals( 107 "modified text", 108 "+1 2 . 5", 109 ParserUtils.trimButDigitsBeginEnd("<DIV> +1 2 . 5 </DIV>", "+.") 110 ); 111 } 112 113 public void testCharsMethods() { 114 String [] tmpSplitChars = ParserUtils.splitChars("<DIV> +12.5, +3.4 </DIV>", " <>DIV/,"); 115 assertStringEquals( 116 "modified text", 117 "+12.5*+3.4", 118 new String (tmpSplitChars[0] + '*' + tmpSplitChars[1]) 119 ); 120 assertStringEquals( 121 "modified text", 122 "+12.5", 123 ParserUtils.trimChars("<DIV> +12.5 </DIV>", "<>DIV/ ") 124 ); 125 assertStringEquals( 126 "modified text", 127 "Trimallchars", 128 ParserUtils.trimChars("<DIV> Trim all chars </DIV>", "<>DIV/ ") 129 ); 130 assertStringEquals( 131 "modified text", 132 "+12.5", 133 ParserUtils.trimCharsBeginEnd("<DIV> +12.5 </DIV>", "<>DIV/ ") 134 ); 135 assertStringEquals( 136 "modified text", 137 "Trim all spaces but not the ones inside the string", 138 ParserUtils.trimCharsBeginEnd("<DIV> Trim all spaces but not the ones inside the string </DIV>", "<>DIV/ ") 139 ); 140 } 141 142 public void testSpacesMethods() { 143 String [] tmpSplitSpaces = ParserUtils.splitSpaces("<DIV> +12.5, +3.4 </DIV>", "<>DIV/,"); 144 assertStringEquals( 145 "modified text", 146 "+12.5*+3.4", 147 new String (tmpSplitSpaces[0] + '*' + tmpSplitSpaces[1]) 148 ); 149 assertStringEquals( 150 "modified text", 151 "+12.5", 152 ParserUtils.trimSpaces("<DIV> +12.5 </DIV>", "<>DIV/") 153 ); 154 assertStringEquals( 155 "modified text", 156 "Trimallspaces", 157 ParserUtils.trimSpaces("<DIV> Trim all spaces </DIV>", "<>DIV/") 158 ); 159 assertStringEquals( 160 "modified text", 161 "+12.5", 162 ParserUtils.trimSpacesBeginEnd("<DIV> +12.5 </DIV>", "<>DIV/") 163 ); 164 assertStringEquals( 165 "modified text", 166 "Trim all spaces but not the ones inside the string", 167 ParserUtils.trimSpacesBeginEnd("<DIV> Trim all spaces but not the ones inside the string </DIV>", "<>DIV/") 168 ); 169 assertStringEquals( 170 "modified text", 171 "0", 172 ParserUtils.trimSpacesBeginEnd("0", "") 173 ); 174 assertStringEquals( 175 "modified text", 176 "verifying the last char x", 177 ParserUtils.trimSpacesBeginEnd("verifying the last char x", "") 178 ); 179 assertStringEquals( 180 "modified text", 181 "verifying the last char x", 182 ParserUtils.trimSpacesBeginEnd("verifying the last char x ", "") 183 ); 184 assertStringEquals( 185 "modified text", 186 "x verifying the first char", 187 ParserUtils.trimSpacesBeginEnd("x verifying the first char", "") 188 ); 189 assertStringEquals( 190 "modified text", 191 "x verifying the first char", 192 ParserUtils.trimSpacesBeginEnd(" x verifying the first char", "") 193 ); 194 } 195 196 public void testTagsMethods() { 197 try 198 { 199 String [] tmpSplitTags = ParserUtils.splitTags("Begin <DIV><DIV> +12.5 </DIV></DIV> ALL OK", new String [] {"DIV"}); 200 assertStringEquals( 201 "modified text", 202 "Begin * ALL OK", 203 new String (tmpSplitTags[0] + '*' + tmpSplitTags[1]) 204 ); 205 tmpSplitTags = ParserUtils.splitTags("Begin <DIV><DIV> +12.5 </DIV></DIV> ALL OK", new String [] {"DIV"}, false, false); 206 assertStringEquals( 207 "modified text", 208 "Begin *<DIV> +12.5 </DIV>* ALL OK", 209 new String (tmpSplitTags[0] + '*' + tmpSplitTags[1] + '*' + tmpSplitTags[2]) 210 ); 211 tmpSplitTags = ParserUtils.splitTags("Begin <DIV><DIV> +12.5 </DIV></DIV> ALL OK", new String [] {"DIV"}, true, false); 212 assertStringEquals( 213 "modified text", 214 "Begin * +12.5 * ALL OK", 215 new String (tmpSplitTags[0] + '*' + tmpSplitTags[1] + '*' + tmpSplitTags[2]) 216 ); 217 tmpSplitTags = ParserUtils.splitTags("Begin <DIV><DIV> +12.5 </DIV></DIV> ALL OK", new String [] {"DIV"}, false, true); 218 assertStringEquals( 219 "modified text", 220 "Begin * ALL OK", 221 new String (tmpSplitTags[0] + '*' + tmpSplitTags[1]) 222 ); 223 assertStringEquals( 224 "modified text", 225 " ALL OK", 226 ParserUtils.trimTags("<DIV><DIV> +12.5 </DIV></DIV> ALL OK", new String [] {"DIV"}) 227 ); 228 assertStringEquals( 229 "modified text", 230 "<DIV> +12.5 </DIV> ALL OK", 231 ParserUtils.trimTags("<DIV><DIV> +12.5 </DIV></DIV> ALL OK", new String [] {"DIV"}, false, false) 232 ); 233 assertStringEquals( 234 "modified text", 235 " +12.5 ALL OK", 236 ParserUtils.trimTags("<DIV><DIV> +12.5 </DIV></DIV> ALL OK", new String [] {"DIV"}, true, false) 237 ); 238 assertStringEquals( 239 "modified text", 240 " ALL OK", 241 ParserUtils.trimTags("<DIV><DIV> +12.5 </DIV></DIV> ALL OK", new String [] {"DIV"}, false, true) 242 ); 243 assertStringEquals( 245 "modified text", 246 " +12.5 ALL OK", 247 ParserUtils.trimAllTags("<DIV><DIV> +12.5 </DIV></DIV> ALL OK", false) 248 ); 249 assertStringEquals( 250 "modified text", 251 " ALL OK", 252 ParserUtils.trimAllTags("<DIV><DIV> +12.5 </DIV></DIV> ALL OK", true) 253 ); 254 assertStringEquals( 255 "modified text", 256 " +12.5 ", 257 ParserUtils.trimAllTags("<DIV><DIV> +12.5 </DIV></DIV>", false) 258 ); 259 assertStringEquals( 260 "modified text", 261 "", 262 ParserUtils.trimAllTags("<DIV><DIV> +12.5 </DIV></DIV>", true) 263 ); 264 assertStringEquals( 265 "modified text", 266 " YYY ", 267 ParserUtils.trimAllTags("<XXX> YYY <ZZZ>", false) 268 ); 269 assertStringEquals( 270 "modified text", 271 "YYY", 272 ParserUtils.trimAllTags("YYY", false) 273 ); 274 assertStringEquals( 275 "modified text", 276 "> OK <", 277 ParserUtils.trimAllTags("> OK <", true) 278 ); 279 } 280 catch (Exception e) 281 { 282 String msg = e.getMessage (); 283 if (null == msg) 284 msg = e.getClass ().getName (); 285 fail (msg); 286 } 287 } 288 289 public void testTagsFilterMethods() { 290 try 291 { 292 NodeFilter filter = new TagNameFilter ("DIV"); 293 String [] tmpSplitTags = ParserUtils.splitTags("Begin <DIV><DIV> +12.5 </DIV></DIV> ALL OK", filter); 294 assertStringEquals( 295 "modified text", 296 "Begin * ALL OK", 297 new String (tmpSplitTags[0] + '*' + tmpSplitTags[1]) 298 ); 299 tmpSplitTags = ParserUtils.splitTags("Begin <DIV><DIV> +12.5 </DIV></DIV> ALL OK", filter, false, false); 300 assertStringEquals( 301 "modified text", 302 "Begin *<DIV> +12.5 </DIV>* ALL OK", 303 new String (tmpSplitTags[0] + '*' + tmpSplitTags[1] + '*' + tmpSplitTags[2]) 304 ); 305 tmpSplitTags = ParserUtils.splitTags("Begin <DIV><DIV> +12.5 </DIV></DIV> ALL OK", filter, true, false); 306 assertStringEquals( 307 "modified text", 308 "Begin * +12.5 * ALL OK", 309 new String (tmpSplitTags[0] + '*' + tmpSplitTags[1] + '*' + tmpSplitTags[2]) 310 ); 311 tmpSplitTags = ParserUtils.splitTags("Begin <DIV><DIV> +12.5 </DIV></DIV> ALL OK", filter, false, true); 312 assertStringEquals( 313 "modified text", 314 "Begin * ALL OK", 315 new String (tmpSplitTags[0] + '*' + tmpSplitTags[1]) 316 ); 317 assertStringEquals( 318 "modified text", 319 " ALL OK", 320 ParserUtils.trimTags("<DIV><DIV> +12.5 </DIV></DIV> ALL OK", filter) 321 ); 322 assertStringEquals( 323 "modified text", 324 "<DIV> +12.5 </DIV> ALL OK", 325 ParserUtils.trimTags("<DIV><DIV> +12.5 </DIV></DIV> ALL OK", filter, false, false) 326 ); 327 assertStringEquals( 328 "modified text", 329 " +12.5 ALL OK", 330 ParserUtils.trimTags("<DIV><DIV> +12.5 </DIV></DIV> ALL OK", filter, true, false) 331 ); 332 assertStringEquals( 333 "modified text", 334 " ALL OK", 335 ParserUtils.trimTags("<DIV><DIV> +12.5 </DIV></DIV> ALL OK", filter, false, true) 336 ); 337 NodeFilter filterTableRow = new TagNameFilter("TR"); 338 NodeFilter filterTableColumn = new TagNameFilter("TD"); 339 OrFilter filterOr = new OrFilter(filterTableRow, filterTableColumn); 340 assertStringEquals( 341 "modified text", 342 " ALL OK", 343 ParserUtils.trimTags("<TR><TD> +12.5 </TD></TR> ALL OK", filterOr) 344 ); 345 assertStringEquals( 346 "modified text", 347 "<TD> +12.5 </TD> ALL OK", 348 ParserUtils.trimTags("<TR><TD> +12.5 </TD></TR> ALL OK", filterOr, false, false) 349 ); 350 assertStringEquals( 351 "modified text", 352 " +12.5 ALL OK", 353 ParserUtils.trimTags("<TR><TD> +12.5 </TD></TR> ALL OK", filterOr, true, false) 354 ); 355 assertStringEquals( 356 "modified text", 357 " ALL OK", 358 ParserUtils.trimTags("<TR><TD> +12.5 </TD></TR> ALL OK", filterOr, false, true) 359 ); 360 } 361 catch (Exception e) 362 { 363 String msg = e.getMessage (); 364 if (null == msg) 365 msg = e.getClass ().getName (); 366 fail (msg); 367 } 368 } 369 370 public void testTagsClassMethods() { 371 try 372 { 373 NodeFilter filter = new NodeClassFilter (Div.class); 374 String [] tmpSplitTags = ParserUtils.splitTags("Begin <DIV><DIV> +12.5 </DIV></DIV> ALL OK", filter); 375 assertStringEquals( 376 "modified text", 377 "Begin * ALL OK", 378 new String (tmpSplitTags[0] + '*' + tmpSplitTags[1]) 379 ); 380 tmpSplitTags = ParserUtils.splitTags("Begin <DIV><DIV> +12.5 </DIV></DIV> ALL OK", filter, false, false); 381 assertStringEquals( 382 "modified text", 383 "Begin *<DIV> +12.5 </DIV>* ALL OK", 384 new String (tmpSplitTags[0] + '*' + tmpSplitTags[1] + '*' + tmpSplitTags[2]) 385 ); 386 tmpSplitTags = ParserUtils.splitTags("Begin <DIV><DIV> +12.5 </DIV></DIV> ALL OK", filter, true, false); 387 assertStringEquals( 388 "modified text", 389 "Begin * +12.5 * ALL OK", 390 new String (tmpSplitTags[0] + '*' + tmpSplitTags[1] + '*' + tmpSplitTags[2]) 391 ); 392 tmpSplitTags = ParserUtils.splitTags("Begin <DIV><DIV> +12.5 </DIV></DIV> ALL OK", filter, false, true); 393 assertStringEquals( 394 "modified text", 395 "Begin * ALL OK", 396 new String (tmpSplitTags[0] + '*' + tmpSplitTags[1]) 397 ); 398 assertStringEquals( 399 "modified text", 400 " ALL OK", 401 ParserUtils.trimTags("<DIV><DIV> +12.5 </DIV></DIV> ALL OK", filter) 402 ); 403 assertStringEquals( 404 "modified text", 405 "<DIV> +12.5 </DIV> ALL OK", 406 ParserUtils.trimTags("<DIV><DIV> +12.5 </DIV></DIV> ALL OK", filter, false, false) 407 ); 408 assertStringEquals( 409 "modified text", 410 " +12.5 ALL OK", 411 ParserUtils.trimTags("<DIV><DIV> +12.5 </DIV></DIV> ALL OK", filter, true, false) 412 ); 413 assertStringEquals( 414 "modified text", 415 " ALL OK", 416 ParserUtils.trimTags("<DIV><DIV> +12.5 </DIV></DIV> ALL OK", filter, false, true) 417 ); 418 NodeFilter filterTableRow = new NodeClassFilter(TableRow.class); 419 NodeFilter filterTableColumn = new NodeClassFilter(TableColumn.class); 420 OrFilter filterOr = new OrFilter(filterTableRow, filterTableColumn); 421 assertStringEquals( 422 "modified text", 423 " ALL OK", 424 ParserUtils.trimTags("<TR><TD> +12.5 </TD></TR> ALL OK", filterOr) 425 ); 426 assertStringEquals( 427 "modified text", 428 "<TD> +12.5 </TD> ALL OK", 429 ParserUtils.trimTags("<TR><TD> +12.5 </TD></TR> ALL OK", filterOr, false, false) 430 ); 431 assertStringEquals( 432 "modified text", 433 " +12.5 ALL OK", 434 ParserUtils.trimTags("<TR><TD> +12.5 </TD></TR> ALL OK", filterOr, true, false) 435 ); 436 assertStringEquals( 437 "modified text", 438 " ALL OK", 439 ParserUtils.trimTags("<TR><TD> +12.5 </TD></TR> ALL OK", filterOr, false, true) 440 ); 441 } 442 catch (Exception e) 443 { 444 String msg = e.getMessage (); 445 if (null == msg) 446 msg = e.getClass ().getName (); 447 fail (msg); 448 } 449 } 450 451 public void testTagsComplexMethods() { 452 try 453 { 454 NodeFilter filterLink = new NodeClassFilter (LinkTag.class); 455 NodeFilter filterDiv = new NodeClassFilter (Div.class); 456 OrFilter filterLinkDiv = new OrFilter (filterLink, filterDiv); 457 NodeFilter filterTable = new NodeClassFilter (TableColumn.class); 458 OrFilter filter = new OrFilter (filterLinkDiv, filterTable); 459 String [] tmpSplitTags = ParserUtils.splitTags("OutsideLeft<A>AInside</A><DIV><DIV>DivInside</DIV></DIV><TD>TableColoumnInside</TD>OutsideRight", filter); 460 assertStringEquals( 461 "modified text", 462 "OutsideLeft*OutsideRight", 463 new String (tmpSplitTags[0] + '*' + tmpSplitTags[1]) 464 ); 465 tmpSplitTags = ParserUtils.splitTags("OutsideLeft<A>AInside</A><DIV><DIV>DivInside</DIV></DIV><TD>TableColoumnInside</TD>OutsideRight", filter, false, false); 466 assertStringEquals( 467 "modified text", 468 "OutsideLeft*AInside*<DIV>DivInside</DIV>*TableColoumnInside*OutsideRight", 469 new String (tmpSplitTags[0] + '*' + tmpSplitTags[1] + '*' + tmpSplitTags[2] + '*' + tmpSplitTags[3] + '*' + tmpSplitTags[4]) 470 ); 471 tmpSplitTags = ParserUtils.splitTags("OutsideLeft<A>AInside</A><DIV><DIV>DivInside</DIV></DIV><TD>TableColoumnInside</TD>OutsideRight", filter, true, false); 472 assertStringEquals( 473 "modified text", 474 "OutsideLeft*AInside*DivInside*TableColoumnInside*OutsideRight", 475 new String (tmpSplitTags[0] + '*' + tmpSplitTags[1] + '*' + tmpSplitTags[2] + '*' + tmpSplitTags[3] + '*' + tmpSplitTags[4]) 476 ); 477 tmpSplitTags = ParserUtils.splitTags("OutsideLeft<A>AInside</A><DIV><DIV>DivInside</DIV></DIV><TD>TableColoumnInside</TD>OutsideRight", filter, false, true); 478 assertStringEquals( 479 "modified text", 480 "OutsideLeft*OutsideRight", 481 new String (tmpSplitTags[0] + '*' + tmpSplitTags[1]) 482 ); 483 tmpSplitTags = ParserUtils.splitTags("OutsideLeft<A>AInside<DIV><DIV>DivInside</DIV></DIV></A><TD>TableColoumnInside</TD>OutsideRight", new String [] {"DIV", "TD", "A"}); 484 assertStringEquals( 485 "modified text", 486 "OutsideLeft*OutsideRight", 487 new String (tmpSplitTags[0] + '*' + tmpSplitTags[1]) 488 ); 489 assertStringEquals( 490 "modified text", 491 "OutsideLeftOutsideRight", 492 ParserUtils.trimTags("OutsideLeft<A>AInside<DIV><DIV>DivInside</DIV></DIV></A><TD>TableColoumnInside</TD>OutsideRight", new String [] {"DIV", "TD", "A"}) 493 ); 494 } 495 catch (Exception e) 496 { 497 String msg = e.getMessage (); 498 if (null == msg) 499 msg = e.getClass ().getName (); 500 fail (msg); 501 } 502 } 503 } 504 | Popular Tags |