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 throws IOException , ParsingException, XSLException { 681 682 File stylesheet = new File (inputDir, "identity.xsl"); 683 Builder builder = new Builder(); 684 Document stylesheetDoc = builder.build(stylesheet); 685 XSLTransform xform = new XSLTransform(stylesheetDoc); 686 687 String data = "<a xmlns:pre='http://www.example.org/'>" + 688 "<b xmlns:pre='http://www.example.org/'>in B</b></a>"; 689 Document doc = builder.build(data, "http://www.example.org/"); 690 691 Nodes result = xform.transform(doc); 692 693 assertEquals(doc.getRootElement(), result.get(0)); 694 695 } 696 697 698 public void testElementsToAttributes() 699 throws IOException , ParsingException, XSLException { 700 701 File stylesheet = new File (inputDir, "identity.xsl"); 702 Builder builder = new Builder(); 703 Document stylesheetDoc = builder.build(stylesheet); 704 XSLTransform xform = new XSLTransform(stylesheetDoc, 705 new AttributeFactory()); 706 707 String data = "<a><b>in B<c>in C</c></b></a>"; 708 Document doc = builder.build(data, "http://www.example.org/"); 709 710 Nodes result = xform.transform(doc); 711 712 assertEquals(1, result.size()); 713 Element a = (Element) result.get(0); 714 assertEquals("a", a.getLocalName()); 715 assertEquals(0, a.getChildCount()); 716 assertEquals(1, a.getAttributeCount()); 717 assertEquals("in B", a.getAttribute("b").getValue()); 718 719 } 720 721 722 private static class AttributeFactory extends NodeFactory { 723 724 public Nodes finishMakingElement(Element element) { 725 ParentNode parent = element.getParent(); 726 if (parent == null || parent instanceof Document) { 727 return new Nodes(element); 728 } 729 return new Nodes(new Attribute(element.getQualifiedName(), 730 element.getNamespaceURI(), element.getValue())); 731 } 732 733 } 734 735 736 public void testAttributesToElements() 737 throws IOException , ParsingException, XSLException { 738 739 File stylesheet = new File (inputDir, "identity.xsl"); 740 Builder builder = new Builder(); 741 Document stylesheetDoc = builder.build(stylesheet); 742 XSLTransform xform = new XSLTransform(stylesheetDoc, 743 new AttributesToElements()); 744 745 String data = "<a name='value'><b x='y' a='b'/></a>"; 746 Document doc = builder.build(data, "http://www.example.org/"); 747 748 Nodes result = xform.transform(doc); 749 750 assertEquals(1, result.size()); 751 Element a = (Element) result.get(0); 752 assertEquals("a", a.getLocalName()); 753 assertEquals(2, a.getChildCount()); 754 assertEquals(0, a.getAttributeCount()); 755 Element name = (Element) a.getChild(0); 756 assertEquals("name", name.getLocalName()); 757 assertEquals("value", name.getValue()); 758 Element b = (Element) a.getChild(1); 759 assertEquals("b", b.getLocalName()); 760 assertEquals(2, b.getChildCount()); 761 assertEquals("y", b.getFirstChildElement("x").getValue()); 762 assertEquals("b", b.getFirstChildElement("a").getValue()); 763 764 } 765 766 767 private static class AttributesToElements extends NodeFactory { 768 769 public Nodes makeAttribute(String name, String URI, 770 String value, Attribute.Type type) { 771 Element element = new Element(name, URI); 772 element.appendChild(value); 773 return new Nodes(element); 774 } 775 776 } 777 778 779 public void testCommentsAreTransformed() 780 throws IOException , ParsingException, XSLException { 781 782 File stylesheet = new File (inputDir, "identity.xsl"); 783 Builder builder = new Builder(); 784 Document stylesheetDoc = builder.build(stylesheet); 785 XSLTransform xform = new XSLTransform(stylesheetDoc); 786 787 String data = "<a><!--test--></a>"; 788 Document doc = builder.build(data, "http://www.example.org/"); 789 790 Nodes result = xform.transform(doc); 791 792 assertEquals(1, result.size()); 793 Element a = (Element) result.get(0); 794 assertEquals("a", a.getLocalName()); 795 assertEquals(1, a.getChildCount()); 796 assertEquals(0, a.getAttributeCount()); 797 Node child = a.getChild(0); 798 assertTrue(child instanceof Comment); 799 assertTrue(child.getValue().equals("test")); 800 801 } 802 803 804 public void testCommentToAttribute() 805 throws IOException , ParsingException, XSLException { 806 807 File stylesheet = new File (inputDir, "identity.xsl"); 808 Builder builder = new Builder(); 809 Document stylesheetDoc = builder.build(stylesheet); 810 XSLTransform xform = new XSLTransform(stylesheetDoc, 811 new NodeFactory() { 812 public Nodes makeComment(String text) { 813 return new Nodes(new Attribute("comment", text)); 814 } 815 }); 816 817 String data = "<a><!--test--></a>"; 818 Document doc = builder.build(data, "http://www.example.org/"); 819 820 Nodes result = xform.transform(doc); 821 822 assertEquals(1, result.size()); 823 Element a = (Element) result.get(0); 824 assertEquals("a", a.getLocalName()); 825 assertEquals(0, a.getChildCount()); 826 assertEquals(1, a.getAttributeCount()); 827 Attribute comment = a.getAttribute(0); 828 assertEquals("comment", comment.getLocalName()); 829 assertEquals("test", comment.getValue()); 830 831 } 832 833 834 public void testAdditionalDefaultNamespace() 835 throws IOException , ParsingException, XSLException { 836 837 File stylesheet = new File (inputDir, "identity.xsl"); 838 Builder builder = new Builder(); 839 Document stylesheetDoc = builder.build(stylesheet); 840 XSLTransform xform = new XSLTransform(stylesheetDoc); 841 842 String data = "<pre:a xmlns:pre='http://www.example.org' " + 843 "xmlns='http://www.example.net'>data</pre:a>"; 844 Document doc = builder.build(data, "http://www.example.org/"); 845 846 Nodes result = xform.transform(doc); 847 848 assertEquals(1, result.size()); 849 Element a = (Element) result.get(0); 850 assertEquals("a", a.getLocalName()); 851 assertEquals("pre:a", a.getQualifiedName()); 852 assertEquals("data", a.getValue()); 853 assertEquals("http://www.example.org", a.getNamespaceURI("pre")); 854 assertEquals("http://www.example.net", a.getNamespaceURI("")); 855 assertEquals(2, a.getNamespaceDeclarationCount()); 856 857 } 858 859 860 861 private static boolean indentYes(Document styleDoc) { 862 863 Element output = styleDoc 864 .getRootElement() 865 .getFirstChildElement("output", 866 "http://www.w3.org/1999/XSL/Transform"); 867 if (output == null) return false; 868 869 String indent = output.getAttributeValue("indent"); 870 if ("yes".equals(indent)) { 871 return true; 872 } 873 else return false; 874 875 } 876 877 878 private static class StrippingFactory extends NodeFactory { 879 880 public Nodes makeText(String s) { 881 882 String stripped = stripSpace(s); 883 if (stripped.length() == 0) return new Nodes(); 884 Text result = new Text(stripped); 885 return new Nodes(result); 886 } 887 888 public Nodes makeAttribute(String name, String URI, 889 String value, Attribute.Type type) { 890 return new Nodes(new Attribute(name, URI, stripSpace(value), type)); 891 } 892 893 private String stripSpace(String s) { 894 895 StringBuffer sb = new StringBuffer (); 896 for (int i = 0; i < s.length(); i++) { 897 if (!Character.isWhitespace(s.charAt(i))) { 898 sb.append(s.charAt(i)); 899 } 900 } 901 902 return sb.toString(); 903 904 } 905 906 } 907 908 909 public void testOASISXalanConformanceSuite() 910 throws IOException , ParsingException, XSLException { 911 912 Builder builder = new Builder(); 913 NodeFactory stripper = new StrippingFactory(); 914 Builder strippingBuilder = new Builder(stripper); 915 916 File base = new File ("data"); 917 base = new File (base, "oasis-xslt-testsuite"); 918 base = new File (base, "TESTS"); 919 base = new File ("Xalan_Conformance_Tests"); 920 File catalog = new File (base, "catalog.xml"); 921 922 if (catalog.exists()) { 925 Document doc = builder.build(catalog); 926 Element testsuite = doc.getRootElement(); 927 Elements submitters = testsuite.getChildElements("test-catalog"); 928 for (int i = 0; i < submitters.size(); i++) { 929 Element submitter = submitters.get(i); 930 Elements testcases = submitter.getChildElements("test-case"); 931 for (int j = 0; j < testcases.size(); j++) { 932 Element testcase = testcases.get(j); 933 String id = testcase.getAttributeValue("id"); 934 if (id.startsWith("output_")) { 935 continue; 939 } 940 File root = new File (base, testcase.getFirstChildElement("file-path").getValue()); 941 File input = null; 942 File style = null; 943 File output = null; 944 Element scenario = testcase.getFirstChildElement("scenario"); 945 Elements inputs = scenario.getChildElements("input-file"); 946 for (int k = 0; k < inputs.size(); k++) { 947 Element file = inputs.get(k); 948 String role = file.getAttributeValue("role"); 949 if ("principal-data".equals(role)) { 950 input = new File (root, file.getValue()); 951 } 952 else if ("principal-stylesheet".equals(role)) { 953 style = new File (root, file.getValue()); 954 } 955 } 956 Elements outputs = scenario.getChildElements("output-file"); 957 for (int k = 0; k < outputs.size(); k++) { 958 Element file = outputs.get(k); 959 String role = file.getAttributeValue("role"); 960 if ("principal".equals(role)) { 961 File parent = new File (root.getParent()); 963 parent = new File (parent, "REF_OUT"); 964 parent = new File (parent, root.getName()); 965 String outputFileName = file.getValue(); 966 output = new File (parent, outputFileName); 967 } 968 } 969 970 try { 971 Document inputDoc = builder.build(input); 972 Document styleDoc = builder.build(style); 973 XSLTransform xform; 976 if (indentYes(styleDoc)) { 977 xform = new XSLTransform(styleDoc, stripper); 978 } 979 else xform = new XSLTransform(styleDoc); 980 Nodes result = xform.transform(inputDoc); 981 if (output == null) { 982 fail("Transformed " + id); 984 } 985 else { 986 try { 987 Document expectedResult; 988 if (indentYes(styleDoc)) { 989 expectedResult = strippingBuilder.build(output); 990 } 991 else { 992 expectedResult = builder.build(output); 993 } 994 Document actualResult = XSLTransform.toDocument(result); 995 996 if (id.equals("attribset_attribset40")) { 997 continue; 1002 } 1003 else if (id.equals("axes_axes129")) { 1004 } 1007 else if (id.equals("copy_copy56") 1008 || id.equals("copy_copy58") 1009 || id.equals("copy_copy60") 1010 || id.equals("copy_copy59")) { 1011 } 1015 else if (id.equals("expression_expression02")) { 1016 } 1018 else if (id.equals("idkey_idkey31")) { 1019 } 1022 else if (id.equals("idkey_idkey61") 1023 || id.equals("idkey_idkey62")) { 1024 } 1028 else if (id.equals("impincl_impincl11")) { 1029 } 1032 else if (id.equals("math_math110") 1033 || id.equals("math_math111")) { 1034 } 1038 else if (id.equals("position_position104")) { 1039 } 1042 else if (id.equals("position_position106")) { 1043 } 1046 else if (id.equals("position_position107") 1047 || id.equals("position_position109")) { 1048 } 1052 else { 1053 assertEquals("Problem with " + id, 1054 expectedResult, actualResult); 1055 } 1056 } 1057 catch (ParsingException ex) { 1058 continue; 1063 } 1064 catch (IllegalAddException ex) { 1065 } 1073 } 1074 1075 } 1076 catch (MalformedURIException ex) { 1077 } 1080 catch (XSLException ex) { 1081 if (output != null) { 1084 Throwable cause = ex.getCause(); 1087 if (cause instanceof MalformedURIException) { 1088 continue; 1089 } 1090 1091 if ("axes_axes62".equals(id)) { 1092 continue; 1095 } 1096 else if ("impincl_impincl27".equals(id)) { 1097 continue; 1099 } 1100 else if ("select_select85".equals(id)) { 1101 continue; 1105 } 1106 else if ("numberformat_numberformat45".equals(id) 1107 || "numberformat_numberformat46".equals(id)) { 1108 continue; 1113 } 1114 1115 System.err.println(id); 1116 System.err.println(ex.getMessage()); 1117 throw ex; 1118 } 1119 } 1120 1121 } 1122 } 1123 1124 } 1125 1126 } 1127 1128 1129 public void testOASISMicrosoftConformanceSuite() 1130 throws IOException , ParsingException, XSLException { 1131 1132 Builder builder = new Builder(); 1133 NodeFactory stripper = new StrippingFactory(); 1134 Builder strippingBuilder = new Builder(stripper); 1135 File base = new File ("data"); 1136 base = new File (base, "oasis-xslt-testsuite"); 1137 base = new File ("TESTS"); 1138 File catalog = new File (base, "catalog.xml"); 1139 1140 if (catalog.exists()) { 1143 Document doc = builder.build(catalog); 1144 Element testsuite = doc.getRootElement(); 1145 Elements submitters = testsuite.getChildElements("test-catalog"); 1146 Element submitter = submitters.get(1); 1147 Elements testcases = submitter.getChildElements("test-case"); 1148 for (int j = 0; j < testcases.size(); j++) { 1149 Element testcase = testcases.get(j); 1150 String id = testcase.getAttributeValue("id"); 1151 File root = new File (base, "MSFT_Conformance_Tests"); 1152 root = new File (root, testcase.getFirstChildElement("file-path").getValue()); 1153 File input = null; 1154 File style = null; 1155 File output = null; 1156 Element scenario = testcase.getFirstChildElement("scenario"); 1157 Elements inputs = scenario.getChildElements("input-file"); 1158 for (int k = 0; k < inputs.size(); k++) { 1159 Element file = inputs.get(k); 1160 String role = file.getAttributeValue("role"); 1161 if ("principal-data".equals(role)) { 1162 input = new File (root, file.getValue()); 1163 } 1164 else if ("principal-stylesheet".equals(role)) { 1165 style = new File (root, file.getValue()); 1166 } 1167 } Elements outputs = scenario.getChildElements("output-file"); 1169 for (int k = 0; k < outputs.size(); k++) { 1170 Element file = outputs.get(k); 1171 String role = file.getAttributeValue("role"); 1172 if ("principal".equals(role)) { 1173 File parent = new File (root.getParent()); 1175 parent = new File (parent, "REF_OUT"); 1176 parent = new File (parent, root.getName()); 1177 String outputFileName = file.getValue(); 1178 output = new File (parent, outputFileName); 1179 } 1180 } 1182 try { 1183 Document styleDoc = builder.build(style); 1184 boolean strip = indentYes(styleDoc); 1185 if ("BVTs_bvt002".equals(id) || "BVTs_bvt077".equals(id)) { 1186 continue; 1190 } 1191 else if ("XSLTFunctions_Bug76984".equals(id)) { 1192 continue; 1196 } 1197 else if ("BVTs_bvt020".equals(id) || "BVTs_bvt022".equals(id) 1198 || "BVTs_bvt024".equals(id) || "BVTs_bvt058".equals(id)) { 1199 continue; 1202 } 1203 else if ("BVTs_bvt038".equals(id) 1204 || "Namespace-alias__91785".equals(id) 1205 || "Namespace-alias__91786".equals(id)) { 1206 continue; 1209 } 1210 else if ("Namespace_XPath_CopyNamespaceNodeToOutput".equals(id)) { 1211 continue; 1214 } 1215 else if ("Namespace-alias_Namespace-Alias_WithinRTF".equals(id)) { 1216 continue; 1219 } 1220 else if ("Completeness__84361".equals(id) 1221 || "Namespace-alias__91781".equals(id) 1222 || "Namespace-alias__91782".equals(id) 1223 || "Namespace-alias_Namespace-Alias_Test1".equals(id) 1224 || "Namespace-alias_Namespace-Alias_Test2".equals(id) 1225 ) { 1226 continue; 1228 } 1229 else if ("Output__84008".equals(id)) { 1230 continue; 1232 } 1233 else if ("XSLTFunctions_ElementAvailFunctionFalseTest".equals(id)) { 1234 continue; 1237 } 1238 else if ("XSLTFunctions_GenereateIdAppliedToNamespaceNodesOnDifferentElements".equals(id)) { 1239 continue; 1242 } 1243 else if ("XSLTFunctions__specialCharInPattern".equals(id)) { 1244 continue; 1246 } 1247 else if ("XSLTFunctions_DocumentFunctionWithAbsoluteArgument".equals(id)) { 1248 continue; 1250 } 1251 else if ("BVTs_bvt052".equals(id) || "Keys_PerfRepro2".equals(id)) { 1252 continue; 1254 } 1255 else if ("BVTs_bvt044".equals(id)) { 1256 continue; 1259 } 1260 else if ("BVTs_bvt039".equals(id)) { 1261 continue; 1263 } 1264 else if ("BVTs_bvt033".equals(id) || "BVTs_bvt034".equals(id)) { 1265 continue; 1267 } 1268 else if ("Text__78274".equals(id) || "Text__78276".equals(id)) { 1269 continue; 1271 } 1272 else if ("XSLTFunctions__minimumValue".equals(id) 1273 || "XSLTFunctions__minimalValue".equals(id)) { 1274 continue; 1276 } 1277 else if ("Errors_err073".equals(id)) { 1278 continue; 1280 } 1281 else if ("Sorting_SortExprWithCurrentInsideForEach1".equals(id)) { 1282 continue; 1285 } 1286 else if ("BVTs_bvt041".equals(id) || "BVTs_bvt063".equals(id) 1287 || "BVTs_bvt070".equals(id)) { 1288 continue; 1293 } 1294 Document inputDoc = builder.build(input); 1295 XSLTransform xform; 1296 if (strip) xform = new XSLTransform(styleDoc, stripper); 1297 else xform = new XSLTransform(styleDoc); 1298 Nodes result = xform.transform(inputDoc); 1299 if (output == null) { 1300 if ("Attributes__89463".equals(id) 1301 || "Attributes__89465".equals(id)) { 1302 assertEquals(0, result.size()); 1305 } 1306 else if ("Attributes__89464".equals(id)) { 1307 assertEquals(0, ((Element) result.get(0)).getAttributeCount()); 1310 } 1311 else if ("Namespace-alias__91772".equals(id) 1312 || "Namespace-alias__91774".equals(id) 1313 || "Namespace-alias__91780".equals(id) 1314 || "Namespace-alias__91790".equals(id) 1315 || "Namespace-alias__91791".equals(id) 1316 || "Sorting__84006".equals(id) 1317 || "Sorting__91754".equals(id) 1318 ) { 1319 continue; 1322 } 1323 else if (id.startsWith("Errors_")) { 1324 } 1327 else if (id.startsWith("FormatNumber")) { 1328 } 1331 else if ("BVTs_bvt074".equals(id)) { 1332 assertEquals(0, result.get(0).getChildCount()); 1335 } 1336 else if ("XSLTFunctions__currency".equals(id) 1337 || "XSLTFunctions__mixingInvalids".equals(id)) { 1338 continue; 1341 } 1342 else if ("Attributes_Attribute_UseXmlnsNsAsNamespaceForAttribute".equals(id) 1343 || "Attributes_Attribute_UseXmlnsAsNamespaceForAttributeImplicitly".equals(id) 1344 || "Elements_Element_UseXslElementWithNameSpaceAttrEqualToXmlnsUri".equalsIgnoreCase(id) 1345 || "Elements_Element_UseXslElementWithNameSpaceEqualToXmlnsUri".equalsIgnoreCase(id) 1346 ) { 1347 } 1349 else if ("AttributeSets_RefToUndefinedAttributeSet".equals(id)) { 1350 } 1354 else if ("Namespace__77665".equals(id) 1355 || "Namespace__77675".equals(id)) { 1356 } 1361 else if ("Variables__84633".equals(id) 1362 || "Variables__84634".equals(id) 1363 || "Variables__84697".equals(id) 1364 || "Variables__84710".equals(id) 1365 ) { 1366 } 1373 else if ("Output__78176".equals(id)) { 1374 } 1378 else if (id.startsWith("XSLTFunctions__100")) { 1379 } 1385 else if ("Namespace__78027".equals(id)) { 1386 } 1390 else if ("Output_Output_UseStandAloneAttributeWithMultipleRoots".equals(id)) { 1391 } 1394 else { fail("Transformed " + style + "\n id: " 1396 + testcase.getAttributeValue("id")); 1397 } 1398 } 1399 else { 1400 try { 1401 if ("Attributes_xsl_attribute_dup_attr_with_namespace_conflict".equals(id) 1402 || "BVTs_bvt057".equals(id)) { 1403 continue; 1406 } 1407 else if ("Comment_DisableOutputEscaping_XslTextInXslComment".equals(id)) { 1408 continue; 1410 } 1411 else if ("Output__77927".equals(id) 1412 || "Output__77928".equals(id) 1413 || "Output__84304".equals(id) 1414 || "Output__84305".equals(id) 1415 || "Output__84312".equals(id) 1416 || "Output__84619".equals(id) 1417 || "Output__84620".equals(id) 1418 || "Output_EntityRefInAttribHtml".equals(id) 1419 ) { 1420 continue; 1423 } 1424 else if ("Output_Modified84433".equals(id)) { 1425 continue; 1428 } 1429 else if ("Sorting_Sort_SortTextWithNonTextCharacters".equals(id)) { 1430 continue; 1433 } 1434 else if ("Text_DoeWithCdataInText".equals(id)) { 1435 continue; 1437 } 1438 else if ("Whitespaces__91443".equals(id) 1439 || "Whitespaces__91444".equals(id)) { 1440 continue; 1443 } 1444 else if ("AVTs__77591".equals(id)) { 1445 } 1448 else if ("Keys_MultipltKeysInclude".equals(id) ) { 1449 } 1452 else if ("Keys_PerfRepro3".equals(id) ) { 1453 } 1456 else if ("Number__84683".equals(id)) { 1457 } 1459 else if ("Number__84687".equals(id)) { 1460 } 1462 else if ("Number__84692".equals(id)) { 1463 } 1465 else if ("Number__84694".equals(id)) { 1466 } 1469 else if ("Number__84699".equals(id)) { 1470 } 1472 else if ("Number__84700".equals(id)) { 1473 } 1476 else if ("Number__84716".equals(id)) { 1477 } 1480 else if ("Number__84717".equals(id)) { 1481 } 1484 else if ("Number__84722".equals(id) 1485 || "Number__84723".equals(id) 1486 || "Number__84724".equals(id) 1487 || "Number__84725".equals(id) 1488 ) { 1489 } 1491 else if ("Number_NaNOrInvalidValue".equals(id)) { 1492 } 1495 else if ("Number_ValueAsNodesetTest1".equals(id) 1496 || "Number_ValueAsEmptyNodeset".equals(id)) { 1497 } 1500 else if (id.equals("XSLTFunctions_BooleanFunction")) { 1501 } 1503 else if (id.equals("XSLTFunctions_TestIdFuncInComplexStruct")) { 1504 } 1507 else { 1508 Document expectedResult; 1509 if (strip) expectedResult = strippingBuilder.build(output); 1510 else expectedResult = builder.build(output); 1511 Document actualResult = XSLTransform.toDocument(result); 1512 assertEquals("Mismatch with " + id, 1513 expectedResult, actualResult); 1514 } 1515 } catch (ParsingException ex) { 1517 continue; 1522 } 1523 catch (IllegalAddException ex) { 1524 } 1532 } 1534 } catch (MalformedURIException ex) { 1536 } 1539 catch (FileNotFoundException ex) { 1540 } 1542 catch (UnknownHostException ex) { 1543 } 1546 catch (ParsingException ex) { 1547 String operation = scenario.getAttributeValue("operation"); 1548 if (!"execution-error".equals(operation)) { 1549 if ("Namespace_XPath_PredefinedPrefix_XML".equals(id)) { 1550 } 1552 else if ("Sorting__78191".equals(id) 1553 || "Text__78245".equals(id) 1554 || "Text__78273".equals(id) 1555 || "Text__78281".equals(id) 1556 ) { 1557 } 1559 else { 1560 System.err.println(id + ": " + ex.getMessage()); 1561 throw ex; 1562 } 1563 } 1564 } 1565 catch (XSLException ex) { 1566 if (output != null) { 1569 Throwable cause = ex.getCause(); 1570 if ("Attributes__81487".equals(id) 1571 || "Attributes__81551".equals(id)) { 1572 continue; 1575 } 1576 else if (cause instanceof MissingResourceException ) { 1577 } 1580 else if ("Include_Include_IncludedStylesheetShouldHaveDifferentBaseUri".equals(id)) { 1581 } 1583 else if ("Elements__89070".equals(id)) { 1584 } 1586 else if ("Namespace-alias_Namespace-Alias_NSAliasForDefaultWithExcludeResPref".equals(id)) { 1587 } 1589 else if ("Variables_VariableWithinVariable".equals(id)) { 1590 } 1592 else if ("BVTs_bvt054".equals(id)) { 1593 continue; 1596 } 1597 else if ("BVTs_bvt094".equals(id)) { 1598 continue; 1601 } 1602 else if ("Output__78177".equals(id) 1603 || "Output__84009".equals(id)) { 1604 continue; 1607 } 1608 else if ("Comment_Comment_CDATAWithSingleHyphen".equals(id) 1609 || "Comment_Comment_DoubleHypenEntitywithDelCharacter".equals(id) 1610 || "Comment_Comment_LineOfAllHyphens".equals(id) 1611 || "Comment_Comment_SingleHyphenOnly".equals(id) 1612 || "Comment_Comment_DoubleHyphenONLY".equals(id)) { 1613 continue; 1615 } 1616 else if ("ProcessingInstruction_ValueOfandTextWithDoeInProcInstr".equals(id)) { 1617 continue; 1619 } 1620 else if ("Elements__89716".equals(id) 1621 || "Elements__89717".equals(id) 1622 || "Elements__89718".equals(id) 1623 || "Output__84309".equals(id) 1624 || "Namespace__77670".equals(id)) 1625 { 1626 continue; 1628 } 1629 else if ("Output__84306".equals(id)) { 1630 continue; 1633 } 1634 else if ("Output__84014".equals(id)) { 1635 continue; 1637 } 1638 else if (cause != null 1639 && cause instanceof MalformedURIException) { 1640 continue; 1643 } 1644 else { 1645 System.err.println(id + ": " + ex.getMessage()); 1646 System.err.println("in " + style); 1647 if (cause != null) { 1648 System.err.println("cause: " + cause.getMessage()); 1649 } 1650 throw ex; 1651 } 1652 } 1653 } catch (XMLException ex) { 1655 if ("Text_modified78309".equals(id)) { 1656 } 1658 else { 1659 System.err.println(id); 1660 throw ex; 1661 } 1662 } 1663 1664 } 1666 } 1668 } 1669 1670 1671 public void testToDocumentWithEmptyNodes() { 1672 1673 try { 1674 XSLTransform.toDocument(new Nodes()); 1675 fail("Converted empty nodes to document"); 1676 } 1677 catch (XMLException success) { 1678 assertNotNull(success.getMessage()); 1679 } 1680 1681 } 1682 1683 1684 public void testToDocumentWithNoRoot() { 1685 1686 Nodes input = new Nodes(); 1687 input.append(new Comment("data")); 1688 try { 1689 XSLTransform.toDocument(new Nodes()); 1690 fail("Converted comment to document"); 1691 } 1692 catch (XMLException success) { 1693 assertNotNull(success.getMessage()); 1694 } 1695 1696 } 1697 1698 1699 public void testToDocumentWithText() { 1700 1701 Nodes input = new Nodes(); 1702 Element root = new Element("root"); 1703 Comment comment = new Comment("data"); 1704 ProcessingInstruction pi = new ProcessingInstruction("target", "data"); 1705 input.append(comment); 1706 input.append(root); 1707 input.append(pi); 1708 input.append(new Text("text")); 1709 try { 1710 XSLTransform.toDocument(new Nodes()); 1711 fail("Converted text to document"); 1712 } 1713 catch (XMLException success) { 1714 assertNotNull(success.getMessage()); 1715 } 1716 1717 } 1718 1719 1720 public void testToDocumentWithAttribute() { 1721 1722 Nodes input = new Nodes(); 1723 Element root = new Element("root"); 1724 Comment comment = new Comment("data"); 1725 ProcessingInstruction pi = new ProcessingInstruction("target", "data"); 1726 input.append(comment); 1727 input.append(root); 1728 input.append(pi); 1729 input.append(new Attribute("name", "text")); 1730 try { 1731 XSLTransform.toDocument(new Nodes()); 1732 fail("Converted text to document"); 1733 } 1734 catch (XMLException success) { 1735 assertNotNull(success.getMessage()); 1736 } 1737 1738 } 1739 1740 1741 public void testToDocumentWithDocType() { 1742 1743 Nodes input = new Nodes(); 1744 Element root = new Element("root"); 1745 DocType doctype = new DocType("root"); 1746 Comment comment = new Comment("data"); 1747 ProcessingInstruction pi = new ProcessingInstruction("target", "data"); 1748 input.append(comment); 1749 input.append(doctype); 1750 input.append(root); 1751 input.append(pi); 1752 Document output = XSLTransform.toDocument(input); 1753 assertEquals(root, output.getRootElement()); 1754 assertEquals(comment, output.getChild(0)); 1755 assertEquals(doctype, output.getChild(1)); 1756 assertEquals(pi, output.getChild(3)); 1757 assertEquals(input.size(), output.getChildCount()); 1758 1759 } 1760 1761 1762 public void testToDocumentWithDocTypeInEpilog() { 1763 1764 Nodes input = new Nodes(); 1765 Element root = new Element("root"); 1766 DocType doctype = new DocType("root"); 1767 Comment comment = new Comment("data"); 1768 ProcessingInstruction pi = new ProcessingInstruction("target", "data"); 1769 input.append(comment); 1770 input.append(root); 1771 input.append(doctype); 1772 input.append(pi); 1773 try { 1774 XSLTransform.toDocument(input); 1775 fail("Allowed doctype in epilog"); 1776 } 1777 catch (XMLException success) { 1778 assertNotNull(success.getMessage()); 1779 } 1780 1781 } 1782 1783 1784 public void testToDocumentWithDoubleRoot() { 1785 1786 Nodes input = new Nodes(); 1787 Element root = new Element("root"); 1788 Comment comment = new Comment("data"); 1789 input.append(comment); 1790 input.append(root); 1791 input.append(new Element("root2")); 1792 try { 1793 XSLTransform.toDocument(input); 1794 fail("Allowed two root elements"); 1795 } 1796 catch (XMLException success) { 1797 assertNotNull(success.getMessage()); 1798 } 1799 1800 } 1801 1802 1803 public void testToDocumentWithSingleRoot() { 1804 1805 Nodes input = new Nodes(); 1806 Element root = new Element("root"); 1807 input.append(root); 1808 Document output = XSLTransform.toDocument(input); 1809 assertEquals(root, output.getRootElement()); 1810 assertEquals(input.size(), output.getChildCount()); 1811 1812 } 1813 1814 1815 public void testToDocumentWithPrologAndEpilog() { 1816 1817 Nodes input = new Nodes(); 1818 Element root = new Element("root"); 1819 Comment comment = new Comment("data"); 1820 ProcessingInstruction pi = new ProcessingInstruction("target", "data"); 1821 input.append(comment); 1822 input.append(root); 1823 input.append(pi); 1824 Document output = XSLTransform.toDocument(input); 1825 assertEquals(root, output.getRootElement()); 1826 assertEquals(comment, output.getChild(0)); 1827 assertEquals(pi, output.getChild(2)); 1828 assertEquals(input.size(), output.getChildCount()); 1829 1830 } 1831 1832 1833} 1834 | Popular Tags |