| 1 21 package nu.xom.tests; 22 23 import java.io.ByteArrayOutputStream ; 24 import java.io.File ; 25 import java.io.FileNotFoundException ; 26 import java.io.FileOutputStream ; 27 import java.io.IOException ; 28 import java.io.OutputStream ; 29 import java.io.PrintStream ; 30 import java.net.UnknownHostException ; 31 import java.util.MissingResourceException ; 32 33 import nu.xom.Attribute; 34 import nu.xom.Builder; 35 import nu.xom.Comment; 36 import nu.xom.DocType; 37 import nu.xom.Document; 38 import nu.xom.Element; 39 import nu.xom.Elements; 40 import nu.xom.IllegalAddException; 41 import nu.xom.MalformedURIException; 42 import nu.xom.Node; 43 import nu.xom.NodeFactory; 44 import nu.xom.Nodes; 45 import nu.xom.ParentNode; 46 import nu.xom.ParsingException; 47 import nu.xom.ProcessingInstruction; 48 import nu.xom.Serializer; 49 import nu.xom.Text; 50 import nu.xom.XMLException; 51 import nu.xom.xslt.XSLException; 52 import nu.xom.xslt.XSLTransform; 53 54 69 public class XSLTransformTest extends XOMTestCase { 70 71 72 public XSLTransformTest(String name) { 73 super(name); 74 } 75 76 77 private String notAStyleSheet = 80 "<?xml-stylesheet HREF=\"file.css\" type=\"text/css\"?>" 81 + "<!-- test -->" 82 + "<test xmlns:xlink='http://www.w3.org/TR/1999/xlink'>Hello dear" 83 + "\r\n<em id=\"p1\" xmlns:none=\"http://www.example.com\">" 84 + "very important</em>" 85 + "<span xlink:type='simple'>here's the link</span>\r\n" 86 + "<svg:svg xmlns:svg='http://www.w3.org/TR/2000/svg'>" 87 + "<svg:text>text in a namespace</svg:text></svg:svg>\r\n" 88 + "<svg xmlns='http://www.w3.org/TR/2000/svg'>" 89 + "<text>text in a namespace</text></svg>" 90 + "</test>\r\n" 91 + "<!--epilog-->"; 92 93 94 private PrintStream systemErr = System.err; 98 99 private File inputDir; 100 101 protected void setUp() { 102 103 System.setErr(new PrintStream (new ByteArrayOutputStream ())); 104 105 inputDir = new File ("data"); 106 inputDir = new File (inputDir, "xslt"); 107 inputDir = new File (inputDir, "input"); 108 109 } 110 111 112 protected void tearDown() { 113 System.setErr(systemErr); 114 } 115 116 public void testIdentityTransform() 119 throws ParsingException, IOException , XSLException { 120 121 122 File stylesheet = new File (inputDir, "identity.xsl"); 123 Builder builder = new Builder(); 124 Document stylesheetDoc = builder.build(stylesheet); 125 XSLTransform xform = new XSLTransform(stylesheetDoc); 126 Element root = new Element("root", "http://www.example.org"); 127 root.appendChild(new Text("some data")); 128 root.appendChild(new Element("something")); 129 root.addAttribute(new Attribute("test", "test")); 130 root.addAttribute(new Attribute("pre:red", "http://www.red.com/", "value")); 131 Document input = new Document(root); 132 Nodes output = xform.transform(input); 133 assertEquals(root, output.get(0)); 134 135 } 136 137 138 public void testPrefixMappingIssues() 139 throws XSLException, ParsingException, IOException { 140 141 String doc = "<test>" 142 + "<span xmlns:a='http://www.example.com'/>" 143 + "<span xmlns:b='http://www.example.net'/>" 144 + "</test>"; 145 File stylesheet = new File (inputDir, "identity.xsl"); 146 Builder builder = new Builder(); 147 Document stylesheetDoc = builder.build(stylesheet); 148 XSLTransform xform = new XSLTransform(stylesheetDoc); 149 Document input = builder.build(doc, "http://example.org/"); 150 Nodes result = xform.transform(input); 151 assertEquals(input.getRootElement(), result.get(0)); 152 153 } 154 155 156 public void testDocumentConstructor() 157 throws ParsingException, IOException { 158 159 try { 160 Builder builder = new Builder(); 161 Document doc = builder.build(notAStyleSheet, 162 "http://www.example.com"); 163 new XSLTransform(doc); 164 fail("Compiled non-stylesheet"); 165 } 166 catch (XSLException success) { 167 assertNotNull(success.getMessage()); 168 } 169 170 } 171 172 173 public void testLiteralResultElementUsedAsStylesheet() 174 throws ParsingException, IOException , XSLException { 175 176 String literalResultElementAsStylesheet = 177 "<html xsl:version='1.0'\n" 178 + " xmlns:xsl='http://www.w3.org/1999/XSL/Transform'\n" 179 + " xmlns='http://www.w3.org/TR/xhtml1/strict'>\n" 180 + " <head>\n" 181 + " <title>Expense Report Summary</title>\n" 182 + " </head>\n" 183 + " <body>\n" 184 + " <p>Total Amount: <xsl:value-of select='expense-report/total'/></p>\n" 185 + " </body>\n" 186 + "</html>\n"; 187 188 Builder builder = new Builder(); 189 Document stylesheet = builder.build(literalResultElementAsStylesheet, 190 "http://www.example.com"); 191 XSLTransform transform = new XSLTransform(stylesheet); 192 Document doc = builder.build(notAStyleSheet, 193 "http://www.example.com"); 194 Nodes result = transform.transform(doc); 195 Element root = (Element) (result.get(0)); 196 assertEquals("html", root.getQualifiedName()); 197 assertEquals(2, root.getChildCount()); 198 199 } 200 201 202 210 211 public void testCreateDocumentFragment() 212 throws ParsingException, IOException , XSLException { 213 214 Element element1 = new Element("element1"); 215 element1.appendChild("some data and "); 216 element1.appendChild(new Element("content")); 217 element1.appendChild(" for a test"); 218 219 Element element2 = new Element("element2"); 220 element2.appendChild( 221 "Remember, the XSLT processor is going to strip out the literal white space" 222 ); 223 File doc = new File (inputDir, "8-14.xml"); 224 File stylesheet = new File (inputDir, "fragment.xsl"); 225 Builder builder = new Builder(); 226 Document stylesheetDoc = builder.build(stylesheet); 227 XSLTransform xform = new XSLTransform(stylesheetDoc); 228 Document input = builder.build(doc); 229 Nodes output = xform.transform(input); 230 assertEquals(6, output.size()); 231 assertEquals(element1, output.get(0)); 232 assertEquals(element2, output.get(1)); 233 assertEquals(new Element("element4"), output.get(3)); 234 assertEquals(new Comment("test"), output.get(4)); 235 assertEquals(new ProcessingInstruction("test", 236 "PIs are not treated as literals in XSLT?"), output.get(5)); 237 238 } 239 240 241 public void testTransform() 242 throws ParsingException, IOException , XSLException { 243 244 File doc = new File (inputDir, "8-1.xml"); 245 File stylesheet = new File (inputDir, "8-8.xsl"); 246 Builder builder = new Builder(); 247 Document stylesheetDoc = builder.build(stylesheet); 248 XSLTransform xform = new XSLTransform(stylesheetDoc); 249 Nodes output = xform.transform(builder.build(doc)); 250 assertEquals(1, output.size()); 251 Document result = new Document((Element) (output.get(0))); 252 253 Document expected = builder.build("data/xslt/output/8-8.xml"); 254 assertEquals(expected, result); 255 256 } 257 258 259 public void testTransformWithCFilter() 260 throws ParsingException, IOException , XSLException { 261 262 File doc = new File (inputDir, "8-1.xml"); 263 File stylesheet = new File (inputDir, "8-8.xsl"); 264 Builder builder = new Builder(); 265 Document stylesheetDoc = builder.build(stylesheet); 266 XSLTransform xform = new XSLTransform( 267 stylesheetDoc, new NodeFactoryTest.CFactory()); 268 269 Nodes output = xform.transform(builder.build(doc)); 270 assertEquals(1, output.size()); 271 Document result = new Document((Element) (output.get(0))); 272 273 Document expected = builder.build("data/xslt/output/8-8c.xml"); 274 assertEquals(expected, result); 275 276 } 277 278 279 public void testCreateDocumentFragmentWithCommentFilter() 280 throws ParsingException, IOException , XSLException { 281 282 Element element1 = new Element("element1"); 283 element1.appendChild("some data and "); 284 element1.appendChild(new Element("content")); 285 element1.appendChild(" for a test"); 286 287 Element element2 = new Element("element2"); 288 element2.appendChild( 289 "Remember, the XSLT processor is going to strip out the literal white space" 290 ); 291 File doc = new File (inputDir, "8-14.xml"); 292 File stylesheet = new File (inputDir, "fragment.xsl"); 293 Builder builder = new Builder(); 294 Document stylesheetDoc = builder.build(stylesheet); 295 XSLTransform xform = new XSLTransform( 296 stylesheetDoc, new NodeFactoryTest.CommentFilter()); 297 298 Document input = builder.build(doc); 299 Nodes output = xform.transform(input); 300 assertEquals(5, output.size()); 301 assertEquals(element1, output.get(0)); 302 assertEquals(element2, output.get(1)); 303 assertEquals(new Element("element4"), output.get(3)); 304 assertEquals(new ProcessingInstruction("test", 305 "PIs are not treated as literals in XSLT?"), output.get(4)); 306 307 } 308 309 310 public void testCreateDocumentFragmentWithProcessingInstructionFilter() 311 throws ParsingException, IOException , XSLException { 312 313 Element element1 = new Element("element1"); 314 element1.appendChild("some data and "); 315 element1.appendChild(new Element("content")); 316 element1.appendChild(" for a test"); 317 318 Element element2 = new Element("element2"); 319 element2.appendChild( 320 "Remember, the XSLT processor is going to strip out the literal white space" 321 ); 322 File doc = new File (inputDir, "8-14.xml"); 323 File stylesheet = new File (inputDir, "fragment.xsl"); 324 Builder builder = new Builder(); 325 Document stylesheetDoc = builder.build(stylesheet); 326 XSLTransform xform = new XSLTransform(stylesheetDoc, 327 new NodeFactoryTest.ProcessingInstructionFilter()); 328 329 Document input = builder.build(doc); 330 Nodes output = xform.transform(input); 331 assertEquals(5, output.size()); 332 assertEquals(element1, output.get(0)); 333 assertEquals(element2, output.get(1)); 334 assertEquals(new Element("element4"), output.get(3)); 335 assertEquals(new Comment("test"), output.get(4)); 336 337 } 338 339 340 public void testCreateDocumentFragmentWithUncommentFilter() 341 throws ParsingException, IOException , XSLException { 342 343 Element element1 = new Element("element1"); 344 element1.appendChild("some data and "); 345 element1.appendChild(new Element("content")); 346 element1.appendChild(" for a test"); 347 348 Element element2 = new Element("element2"); 349 element2.appendChild( 350 "Remember, the XSLT processor is going to strip out the literal white space" 351 ); 352 File doc = new File (inputDir, "8-14.xml"); 353 File stylesheet = new File (inputDir, "fragment.xsl"); 354 Builder builder = new Builder(); 355 Document stylesheetDoc = builder.build(stylesheet); 356 XSLTransform xform = new XSLTransform(stylesheetDoc, 357 new NodeFactoryTest.UncommentFilter()); 358 359 Document input = builder.build(doc); 360 Nodes output = xform.transform(input); 361 assertEquals(6, output.size()); 362 assertEquals(element1, output.get(0)); 363 assertEquals(element2, output.get(1)); 364 assertEquals(new Element("element4"), output.get(3)); 365 assertEquals(new Text("test"), output.get(4)); 366 assertEquals(new ProcessingInstruction("test", 367 "PIs are not treated as literals in XSLT?"), output.get(5)); 368 369 } 370 371 372 public void testTransform2() 373 throws ParsingException, IOException , XSLException { 374 375 File doc = new File (inputDir, "8-1.xml"); 376 File stylesheet = new File (inputDir, "8-12.xsl"); 377 Builder builder = new Builder(); 378 Document stylesheetDoc = builder.build(stylesheet); 379 XSLTransform xform = new XSLTransform(stylesheetDoc); 380 Nodes output = xform.transform(builder.build(doc)); 381 assertEquals(1, output.size()); 382 Document result = new Document((Element) (output.get(0))); 383 384 Document expected = builder.build("data/xslt/output/8-12.xml"); 385 assertEquals(expected, result); 386 387 } 388 389 390 391 private static void dumpResult(Document result, String filename) 393 throws IOException { 394 395 File debug = new File ("data"); 396 debug = new File (debug, "xslt"); 397 debug = new File (debug, "debug/" + filename); 398 OutputStream out = new FileOutputStream (debug); 399 Serializer serializer = new Serializer(out); 400 serializer.write(result); 401 serializer.flush(); 402 out.close(); 403 404 } 405 406 407 public void testTransformFromDocument() 408 throws ParsingException, IOException , XSLException { 409 410 File doc = new File (inputDir, "8-1.xml"); 411 Builder builder = new Builder(); 412 Document stylesheet = builder.build("data/xslt/input/8-12.xsl"); 413 XSLTransform xform = new XSLTransform(stylesheet); 414 Nodes output = xform.transform(builder.build(doc)); 415 assertEquals(1, output.size()); 416 Document result = new Document((Element) (output.get(0))); 417 418 Document expected = builder.build("data/xslt/output/8-12.xml"); 419 assertEquals(expected, result); 420 421 } 422 423 424 public void testTransformFromSystemID() 425 throws ParsingException, IOException , XSLException { 426 427 File doc = new File (inputDir, "8-1.xml"); 428 Builder builder = new Builder(); 429 String stylesheet = "data/xslt/input/8-12.xsl"; 430 Document stylesheetDoc = builder.build(stylesheet); 431 XSLTransform xform = new XSLTransform(stylesheetDoc); 432 Nodes output = xform.transform(builder.build(doc)); 433 assertEquals(1, output.size()); 434 Document result = new Document((Element) (output.get(0))); 435 436 Document expected = builder.build("data/xslt/output/8-12.xml"); 437 assertEquals(expected, result); 438 439 } 440 441 442 public void testTransformWithNamespaces() 443 throws ParsingException, IOException , XSLException { 444 445 File doc = new File (inputDir, "8-14.xml"); 446 File stylesheet = new File (inputDir, "8-15.xsl"); 447 Builder builder = new Builder(); 448 Document stylesheetDoc = builder.build(stylesheet); 449 XSLTransform xform = new XSLTransform(stylesheetDoc); 450 Document input = builder.build(doc); 451 Nodes output = xform.transform(input); 452 assertEquals(1, output.size()); 453 Document result = new Document((Element) (output.get(0))); 454 455 Document expected = builder.build("data/xslt/output/8-15.xml"); 456 assertEquals(expected, result); 457 458 } 459 460 461 public void testSingleTextNode() 462 throws ParsingException, IOException , XSLException { 463 464 File doc = new File (inputDir, "8-14.xml"); 465 File stylesheet = new File (inputDir, "singlestring.xsl"); 466 Builder builder = new Builder(); 467 Document stylesheetDoc = builder.build(stylesheet); 468 XSLTransform xform = new XSLTransform(stylesheetDoc); 469 Document input = builder.build(doc); 470 Nodes output = xform.transform(input); 471 assertEquals(1, output.size()); 472 Text data = (Text) (output.get(0)); 473 assertEquals("Data", data.getValue()); 474 475 } 476 477 478 public void testToString() 479 throws XSLException, ParsingException, IOException { 480 481 File stylesheet = new File (inputDir, "singlestring.xsl"); 482 Builder builder = new Builder(); 483 Document stylesheetDoc = builder.build(stylesheet); 484 XSLTransform xform = new XSLTransform(stylesheetDoc); 485 assertTrue(xform.toString().startsWith("[nu.xom.xslt.XSLTransform: ")); 486 487 } 488 489 490 public void testTextMethod() 494 throws ParsingException, IOException , XSLException { 495 496 File doc = new File (inputDir, "8-14.xml"); 497 File stylesheet = new File (inputDir, "textmethod.xsl"); 498 Builder builder = new Builder(); 499 Document stylesheetDoc = builder.build(stylesheet); 500 XSLTransform xform = new XSLTransform(stylesheetDoc); 501 Document input = builder.build(doc); 502 Nodes output = xform.transform(input); 503 assertEquals(6, output.size()); 504 assertEquals("12345", output.get(0).getValue()); 505 assertEquals("67890", output.get(1).getValue()); 506 assertEquals("", output.get(2).getValue()); 507 assertEquals("0987654321", output.get(3).getValue()); 508 assertTrue(output.get(4) instanceof Comment); 509 assertTrue(output.get(5) instanceof ProcessingInstruction); 510 511 } 512 513 514 public void testCommentWithParent() 515 throws XSLException, ParsingException, IOException { 516 517 Builder builder = new Builder(); 518 File stylesheet = new File (inputDir, "commentwithparent.xsl"); 519 Document stylesheetDoc = builder.build(stylesheet); 520 XSLTransform xform = new XSLTransform(stylesheetDoc); 521 Document input = new Document(new Element("root")); 522 Nodes output = xform.transform(input); 523 assertEquals(1, output.size()); 524 assertEquals("", output.get(0).getValue()); 525 Element root = (Element) output.get(0); 526 assertEquals(1, root.getChildCount()); 527 Comment child = (Comment) root.getChild(0); 528 assertEquals("test", child.getValue()); 529 530 } 531 532 533 public void testProcessingInstructionWithParent() 534 throws XSLException, ParsingException, IOException { 535 536 Builder builder = new Builder(); 537 File stylesheet = new File (inputDir, "piwithparent.xsl"); 538 Document stylesheetDoc = builder.build(stylesheet); 539 XSLTransform xform = new XSLTransform(stylesheetDoc); 540 Document input = new Document(new Element("root")); 541 Nodes output = xform.transform(input); 542 assertEquals(1, output.size()); 543 assertEquals("", output.get(0).getValue()); 544 Element root = (Element) output.get(0); 545 assertEquals(1, root.getChildCount()); 546 ProcessingInstruction child = (ProcessingInstruction) root.getChild(0); 547 assertEquals("target", child.getTarget()); 548 assertEquals("test", child.getValue()); 549 550 } 551 552 553 public void testTransformNodes() 554 throws XSLException, ParsingException, IOException { 555 556 File stylesheet = new File (inputDir, "piwithparent.xsl"); 557 Builder builder = new Builder(); 558 Nodes input = new Nodes(new Element("root")); 559 Document stylesheetDoc = builder.build(stylesheet); 560 XSLTransform xform = new XSLTransform(stylesheetDoc); 561 Nodes output = xform.transform(input); 562 assertEquals(1, output.size()); 563 assertEquals("", output.get(0).getValue()); 564 Element root = (Element) output.get(0); 565 assertEquals(1, root.getChildCount()); 566 ProcessingInstruction child = (ProcessingInstruction) root.getChild(0); 567 assertEquals("target", child.getTarget()); 568 assertEquals("test", child.getValue()); 569 570 } 571 572 573 public void testTriple() 574 throws IOException , ParsingException, XSLException { 575 576 File stylesheet = new File (inputDir, "identity.xsl"); 577 Builder builder = new Builder(); 578 Document stylesheetDoc = builder.build(stylesheet); 579 XSLTransform xform = new XSLTransform(stylesheetDoc, 580 new NodeFactoryTest.TripleElementFilter()); 581 582 String data = "<a><b><c/></b></a>"; 583 Document doc = builder.build(data, "http://www.example.org/"); 584 585 Nodes result = xform.transform(doc); 586 587 assertEquals(3, result.size()); 588 assertEquals(result.get(0), result.get(1)); 589 assertEquals(result.get(1), result.get(2)); 590 Element a = (Element) result.get(2); 591 assertEquals("a", a.getLocalName()); 592 assertEquals(3, a.getChildCount()); 593 assertEquals(0, a.getAttributeCount()); 594 Element b = (Element) a.getChild(1); 595 assertEquals(3, b.getChildCount()); 596 assertEquals("b", b.getLocalName()); 597 598 } 599 600 601 public void testPassingNullSetsDefaultFactory() 602 throws IOException , ParsingException, XSLException { 603 604 File stylesheet = new File (inputDir, "identity.xsl"); 605 Builder builder = new Builder(); 606 Document stylesheetDoc = builder.build(stylesheet); 607 XSLTransform xform = new XSLTransform(stylesheetDoc, null); 608 609 String data = "<a><b><c/></b></a>"; 610 Document doc = builder.build(data, "http://www.example.org/"); 611 612 Nodes result = xform.transform(doc); 613 614 assertEquals(1, result.size()); 615 Element a = (Element) result.get(0); 616 assertEquals("a", a.getLocalName()); 617 assertEquals(1, a.getChildCount()); 618 assertEquals(0, a.getAttributeCount()); 619 Element b = (Element) a.getChild(0); 620 assertEquals(1, b.getChildCount()); 621 assertEquals("b", b.getLocalName()); 622 623 } 624 625 626 public void testTransformEmptyNodesList() 627 throws IOException , ParsingException, XSLException { 628 629 File stylesheet = new File (inputDir, "identity.xsl"); 630 Builder builder = new Builder(); 631 Document stylesheetDoc = builder.build(stylesheet); 632 XSLTransform xform = new XSLTransform(stylesheetDoc); 633 634 Nodes result = xform.transform(new Nodes()); 635 636 assertEquals(0, result.size()); 637 638 } 639 640 641 public void testMinimizingFactory() 642 throws XSLException, ParsingException, IOException { 643 644 File stylesheet = new File (inputDir, "identity.xsl"); 645 Builder builder = new Builder(); 646 Document stylesheetDoc = builder.build(stylesheet); 647 XSLTransform xform = new XSLTransform(stylesheetDoc, 648 new NodeFactoryTest.MinimizingFactory()); 649 650 Document input = builder.build("<!-- test--><test>" + 651 "<em>data</em>\r\n<span>test</span></test>" + 652 "<?target data?>", "http://example.org/"); 653 Nodes output = xform.transform(input); 654 assertEquals(0, output.size()); 655 656 } 657 658 659 public void testIllegalTransform() 660 throws XSLException, ParsingException, IOException { 661 662 File stylesheet = new File (inputDir, "illegaltransform.xsl"); 663 Builder builder = new Builder(); 664 Document stylesheetDoc = builder.build(stylesheet); 665 XSLTransform xform = new XSLTransform(stylesheetDoc); 666 Element root = new Element("root", "http://www.example.org"); 667 Document input = new Document(root); 668 try { 669 xform.transform(input); 670 fail("Allowed illegal transform"); 671 } 672 catch (XSLException ex) { 673 assertNotNull(ex.getMessage()); 674 } 675 676 } 677 678 679 public void testRemapPrefixToSameURI() 680 |