1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package stream.XMLStreamWriterTest; 25 26 import java.io.FileInputStream; 27 import java.io.FileOutputStream; 28 import java.io.IOException; 29 import java.io.InputStreamReader; 30 import java.io.LineNumberReader; 31 import java.io.Reader; 32 import java.net.URL; 33 import java.util.Iterator; 34 35 import javax.xml.namespace.NamespaceContext; 36 import javax.xml.stream.XMLInputFactory; 37 import javax.xml.stream.XMLOutputFactory; 38 import javax.xml.stream.XMLStreamWriter; 39 40 import org.testng.Assert; 41 import org.testng.annotations.AfterMethod; 42 import org.testng.annotations.BeforeMethod; 43 import org.testng.annotations.Listeners; 44 import org.testng.annotations.Test; 45 46 /* 47 * @test 48 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 49 * @run testng/othervm -DrunSecMngr=true stream.XMLStreamWriterTest.WriterTest 50 * @run testng/othervm stream.XMLStreamWriterTest.WriterTest 51 * @summary Test XMLStreamWriter functionality. 52 */ 53 @Listeners({jaxp.library.FilePolicy.class}) 54 public class WriterTest { 55 56 final String ENCODING = "UTF-8"; 57 XMLOutputFactory outputFactory = null; 58 XMLInputFactory inputFactory = null; 59 XMLStreamWriter xtw = null; 60 String[] files = new String[] { "testOne.xml", "testTwo.xml", "testThree.xml", "testFour.xml", "testFive.xml", "testSix.xml", "testSeven.xml", 61 "testEight.xml", "testNine.xml", "testTen.xml", "testEleven.xml", "testTwelve.xml", "testDefaultNS.xml", null, "testFixAttr.xml" }; 62 63 String output = ""; 64 65 @BeforeMethod 66 public void setUp() { 67 try { 68 outputFactory = XMLOutputFactory.newInstance(); 69 inputFactory = XMLInputFactory.newInstance(); 70 } catch (Exception ex) { 71 Assert.fail("Could not create XMLInputFactory"); 72 } 73 } 74 75 @AfterMethod 76 public void tearDown() { 77 outputFactory = null; 78 inputFactory = null; 79 } 80 81 @Test 82 public void testOne() { 83 84 System.out.println("Test StreamWriter with out any namespace functionality"); 85 86 try { 87 String outputFile = files[0] + ".out"; 88 System.out.println("Writing output to " + outputFile); 89 90 xtw = outputFactory.createXMLStreamWriter(new FileOutputStream(outputFile), ENCODING); 91 xtw.writeStartDocument("utf-8", "1.0"); 92 xtw.writeStartElement("elmeOne"); 93 xtw.writeStartElement("elemTwo"); 94 xtw.writeStartElement("elemThree"); 95 xtw.writeStartElement("elemFour"); 96 xtw.writeStartElement("elemFive"); 97 xtw.writeEndDocument(); 98 xtw.flush(); 99 xtw.close(); 100 101 Assert.assertTrue(checkResults(files[0] + ".out", files[0] + ".org")); 102 103 } catch (Exception ex) { 104 Assert.fail("testOne Failed " + ex); 105 ex.printStackTrace(); 106 } 107 108 } 109 110 @Test 111 public void testTwo() { 112 113 System.out.println("Test StreamWriter's Namespace Context"); 114 115 try { 116 String outputFile = files[1] + ".out"; 117 System.out.println("Writing output to " + outputFile); 118 119 xtw = outputFactory.createXMLStreamWriter(System.out); 120 xtw.writeStartDocument(); 121 xtw.writeStartElement("elemTwo"); 122 xtw.setPrefix("html", "http://www.w3.org/TR/REC-html40"); 123 xtw.writeNamespace("html", "http://www.w3.org/TR/REC-html40"); 124 xtw.writeEndDocument(); 125 NamespaceContext nc = xtw.getNamespaceContext(); 126 // Got a Namespace Context.class 127 128 XMLStreamWriter xtw1 = outputFactory.createXMLStreamWriter(new FileOutputStream(outputFile), ENCODING); 129 130 xtw1.writeComment("all elements here are explicitly in the HTML namespace"); 131 xtw1.setNamespaceContext(nc); 132 xtw1.writeStartDocument("utf-8", "1.0"); 133 xtw1.setPrefix("htmlOne", "http://www.w3.org/TR/REC-html40"); 134 NamespaceContext nc1 = xtw1.getNamespaceContext(); 135 xtw1.close(); 136 Iterator it = nc1.getPrefixes("http://www.w3.org/TR/REC-html40"); 137 138 // FileWriter fw = new FileWriter(outputFile); 139 while (it.hasNext()) { 140 System.out.println("Prefixes :" + it.next()); 141 // fw.write((String)it.next()); 142 // fw.write(";"); 143 } 144 // fw.close(); 145 // assertTrue(checkResults(testTwo+".out", testTwo+".org")); 146 System.out.println("Done"); 147 } catch (Exception ex) { 148 Assert.fail("testTwo Failed " + ex); 149 ex.printStackTrace(); 150 } 151 152 } 153 154 @Test 155 public void testThree() { 156 157 System.out.println("Test StreamWriter for proper element sequence."); 158 159 try { 160 String outputFile = files[2] + ".out"; 161 System.out.println("Writing output to " + outputFile); 162 163 xtw = outputFactory.createXMLStreamWriter(new FileOutputStream(outputFile), ENCODING); 164 xtw.writeStartDocument("utf-8", "1.0"); 165 xtw.writeStartElement("elmeOne"); 166 xtw.writeStartElement("elemTwo"); 167 xtw.writeEmptyElement("emptyElem"); 168 xtw.writeStartElement("elemThree"); 169 xtw.writeStartElement("elemFour"); 170 xtw.writeStartElement("elemFive"); 171 xtw.writeEndDocument(); 172 xtw.flush(); 173 xtw.close(); 174 175 Assert.assertTrue(checkResults(files[2] + ".out", files[2] + ".org")); 176 177 } catch (Exception ex) { 178 Assert.fail("testThree Failed " + ex); 179 ex.printStackTrace(); 180 } 181 182 } 183 184 @Test 185 public void testFour() { 186 187 System.out.println("Test StreamWriter with elements,attribute and element content."); 188 189 try { 190 191 String outputFile = files[3] + ".out"; 192 System.out.println("Writing output to " + outputFile); 193 194 xtw = outputFactory.createXMLStreamWriter(new FileOutputStream(outputFile), ENCODING); 195 xtw.writeStartDocument("utf-8", "1.0"); 196 xtw.writeStartElement("elmeOne"); 197 xtw.writeStartElement("elemTwo"); 198 xtw.writeEmptyElement("emptyElem"); 199 xtw.writeAttribute("testAttr", "testValue"); 200 xtw.writeStartElement("elemThree"); 201 xtw.writeStartElement("elemFour"); 202 xtw.writeCharacters("TestCharacterData"); 203 xtw.writeStartElement("elemFive"); 204 xtw.writeEndDocument(); 205 xtw.flush(); 206 xtw.close(); 207 208 Assert.assertTrue(checkResults(files[3] + ".out", files[3] + ".org")); 209 210 } catch (Exception ex) { 211 Assert.fail("testFour Failed " + ex); 212 ex.printStackTrace(); 213 } 214 215 } 216 217 @Test 218 public void testFive() { 219 220 System.out.println("Test StreamWriter's Namespace Context."); 221 222 try { 223 224 String outputFile = files[4] + ".out"; 225 System.out.println("Writing output to " + outputFile); 226 227 xtw = outputFactory.createXMLStreamWriter(System.out); 228 xtw.writeStartDocument(); 229 xtw.writeStartElement("elemTwo"); 230 xtw.setPrefix("html", "http://www.w3.org/TR/REC-html40"); 231 xtw.writeNamespace("html", "http://www.w3.org/TR/REC-html40"); 232 // xtw.writeEndDocument(); 233 NamespaceContext nc = xtw.getNamespaceContext(); 234 // Got a Namespace Context.class 235 236 xtw = outputFactory.createXMLStreamWriter(new FileOutputStream(outputFile), ENCODING); 237 238 xtw.writeComment("all elements here are explicitly in the HTML namespace"); 239 xtw.setNamespaceContext(nc); 240 xtw.writeStartDocument("utf-8", "1.0"); 241 // xtw.setPrefix("html", "http://www.w3.org/TR/REC-html40"); 242 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "html"); 243 // xtw.writeNamespace("html", "http://www.w3.org/TR/REC-html40"); 244 245 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "head"); 246 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "title"); 247 248 xtw.writeCharacters("Frobnostication"); 249 xtw.writeEndElement(); 250 xtw.writeEndElement(); 251 252 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "body"); 253 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "p"); 254 xtw.writeCharacters("Moved to"); 255 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "a"); 256 xtw.writeAttribute("href", "http://frob.com"); 257 258 xtw.writeCharacters("here"); 259 xtw.writeEndElement(); 260 xtw.writeEndElement(); 261 xtw.writeEndElement(); 262 263 xtw.writeEndElement(); 264 265 xtw.writeEndDocument(); 266 xtw.flush(); 267 xtw.close(); 268 Assert.assertTrue(checkResults(files[4] + ".out", files[4] + ".org")); 269 System.out.println("Done"); 270 } catch (Exception ex) { 271 Assert.fail("testFive Failed " + ex); 272 ex.printStackTrace(); 273 } 274 275 } 276 277 @Test 278 public void testSix() { 279 280 System.out.println("Test StreamWriter, uses the Namespace Context set by the user to resolve namespaces."); 281 282 try { 283 284 String outputFile = files[5] + ".out"; 285 System.out.println("Writing output to " + outputFile); 286 287 xtw = outputFactory.createXMLStreamWriter(System.out); 288 xtw.writeStartDocument(); 289 xtw.writeStartElement("elemTwo"); 290 xtw.setPrefix("html", "http://www.w3.org/TR/REC-html40"); 291 xtw.writeNamespace("html", "http://www.w3.org/TR/REC-html40"); 292 xtw.writeEndDocument(); 293 NamespaceContext nc = xtw.getNamespaceContext(); 294 // Got a Namespace Context information. 295 296 xtw = outputFactory.createXMLStreamWriter(new FileOutputStream(outputFile), ENCODING); 297 298 xtw.writeComment("all elements here are explicitly in the HTML namespace"); 299 xtw.setNamespaceContext(nc); 300 xtw.writeStartDocument("utf-8", "1.0"); 301 xtw.setPrefix("htmlNewPrefix", "http://www.w3.org/TR/REC-html40"); 302 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "html"); 303 // xtw.writeNamespace("html", "http://www.w3.org/TR/REC-html40"); 304 305 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "head"); 306 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "title"); 307 308 xtw.writeCharacters("Frobnostication"); 309 xtw.writeEndElement(); 310 xtw.writeEndElement(); 311 312 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "body"); 313 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "p"); 314 xtw.writeCharacters("Moved to"); 315 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "a"); 316 xtw.writeAttribute("href", "http://frob.com"); 317 318 xtw.writeCharacters("here"); 319 xtw.writeEndElement(); 320 xtw.writeEndElement(); 321 xtw.writeEndElement(); 322 323 xtw.writeEndElement(); 324 325 xtw.writeEndDocument(); 326 xtw.flush(); 327 xtw.close(); 328 Assert.assertTrue(checkResults(files[5] + ".out", files[5] + ".org")); 329 System.out.println("Done"); 330 } catch (Exception ex) { 331 Assert.fail("testSix Failed " + ex); 332 ex.printStackTrace(); 333 } 334 335 } 336 337 @Test 338 public void testSeven() { 339 340 System.out.println("Test StreamWriter supplied with correct namespace information"); 341 342 try { 343 344 String outputFile = files[6] + ".out"; 345 System.out.println("Writing output to " + outputFile); 346 347 xtw = outputFactory.createXMLStreamWriter(new FileOutputStream(outputFile), ENCODING); 348 xtw.writeComment("all elements here are explicitly in the HTML namespace"); 349 xtw.writeStartDocument("utf-8", "1.0"); 350 xtw.setPrefix("html", "http://www.w3.org/TR/REC-html40"); 351 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "html"); 352 xtw.writeNamespace("html", "http://www.w3.org/TR/REC-html40"); 353 354 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "head"); 355 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "title"); 356 357 xtw.writeCharacters("Frobnostication"); 358 xtw.writeEndElement(); 359 xtw.writeEndElement(); 360 361 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "body"); 362 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "p"); 363 xtw.writeCharacters("Moved to"); 364 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "a"); 365 xtw.writeAttribute("href", "http://frob.com"); 366 367 xtw.writeCharacters("here"); 368 xtw.writeEndElement(); 369 xtw.writeEndElement(); 370 xtw.writeEndElement(); 371 372 xtw.writeEndElement(); 373 374 xtw.writeEndDocument(); 375 xtw.flush(); 376 xtw.close(); 377 Assert.assertTrue(checkResults(files[6] + ".out", files[6] + ".org")); 378 System.out.println("Done"); 379 } catch (Exception ex) { 380 Assert.fail("testSeven Failed " + ex); 381 ex.printStackTrace(); 382 } 383 384 } 385 386 @Test 387 public void testEight() { 388 389 System.out.println("Test StreamWriter supplied with correct namespace information and" + "isRepairingNamespace is set to true."); 390 391 try { 392 393 String outputFile = files[7] + ".out"; 394 System.out.println("Writing output to " + outputFile); 395 outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, new Boolean(true)); 396 xtw = outputFactory.createXMLStreamWriter(new FileOutputStream(outputFile), ENCODING); 397 xtw.writeComment("all elements here are explicitly in the HTML namespace"); 398 xtw.writeStartDocument("utf-8", "1.0"); 399 xtw.setPrefix("html", "http://www.w3.org/TR/REC-html40"); 400 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "html"); 401 xtw.writeNamespace("html", "http://www.w3.org/TR/REC-html40"); 402 403 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "head"); 404 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "title"); 405 406 xtw.writeCharacters("Frobnostication"); 407 xtw.writeEndElement(); 408 xtw.writeEndElement(); 409 410 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "body"); 411 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "p"); 412 xtw.writeCharacters("Moved to"); 413 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "a"); 414 xtw.writeAttribute("href", "http://frob.com"); 415 416 xtw.writeCharacters("here"); 417 xtw.writeEndElement(); 418 xtw.writeEndElement(); 419 xtw.writeEndElement(); 420 421 xtw.writeEndElement(); 422 423 xtw.writeEndDocument(); 424 xtw.flush(); 425 xtw.close(); 426 // check against testSeven.xml.org 427 Assert.assertTrue(checkResults(files[7] + ".out", files[7] + ".org")); 428 System.out.println("Done"); 429 } catch (Exception ex) { 430 ex.printStackTrace(); 431 Assert.fail("testEight Failed " + ex); 432 433 } 434 435 } 436 437 @Test 438 public void testNine() { 439 440 System.out.println("Test StreamWriter supplied with correct namespace information and" + "isRepairingNamespace is set to true." 441 + "pass namespace information using" + "writenamespace function"); 442 443 try { 444 445 String outputFile = files[8] + ".out"; 446 System.out.println("Writing output to " + outputFile); 447 outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, new Boolean(true)); 448 xtw = outputFactory.createXMLStreamWriter(new FileOutputStream(outputFile), ENCODING); 449 xtw.writeComment("all elements here are explicitly in the HTML namespace"); 450 xtw.writeStartDocument("utf-8", "1.0"); 451 // xtw.setPrefix("html", "http://www.w3.org/TR/REC-html40"); 452 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "html"); 453 xtw.writeNamespace("html", "http://www.w3.org/TR/REC-html40"); 454 455 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "head"); 456 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "title"); 457 458 xtw.writeCharacters("Frobnostication"); 459 xtw.writeEndElement(); 460 xtw.writeEndElement(); 461 462 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "body"); 463 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "p"); 464 xtw.writeCharacters("Moved to"); 465 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "a"); 466 xtw.writeAttribute("href", "http://frob.com"); 467 468 xtw.writeCharacters("here"); 469 xtw.writeEndElement(); 470 xtw.writeEndElement(); 471 xtw.writeEndElement(); 472 473 xtw.writeEndElement(); 474 475 xtw.writeEndDocument(); 476 xtw.flush(); 477 xtw.close(); 478 // check against testSeven.xml.org 479 Assert.assertTrue(checkResults(files[8] + ".out", files[7] + ".org")); 480 System.out.println("Done"); 481 } catch (Exception ex) { 482 Assert.fail("testNine Failed " + ex); 483 ex.printStackTrace(); 484 } 485 486 } 487 488 @Test 489 public void testTen() { 490 491 System.out.println("Test StreamWriter supplied with no namespace information and" + "isRepairingNamespace is set to true."); 492 try { 493 494 String outputFile = files[9] + ".out"; 495 System.out.println("Writing output to " + outputFile); 496 outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, new Boolean(true)); 497 xtw = outputFactory.createXMLStreamWriter(new FileOutputStream(outputFile), ENCODING); 498 xtw.writeComment("all elements here are explicitly in the HTML namespace"); 499 xtw.writeStartDocument("utf-8", "1.0"); 500 // xtw.setPrefix("html", "http://www.w3.org/TR/REC-html40"); 501 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "html"); 502 // xtw.writeNamespace("html", "http://www.w3.org/TR/REC-html40"); 503 504 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "head"); 505 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "title"); 506 507 xtw.writeCharacters("Frobnostication"); 508 xtw.writeEndElement(); 509 xtw.writeEndElement(); 510 511 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "body"); 512 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "p"); 513 xtw.writeCharacters("Moved to"); 514 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "a"); 515 xtw.writeAttribute("href", "http://frob.com"); 516 517 xtw.writeCharacters("here"); 518 xtw.writeEndElement(); 519 xtw.writeEndElement(); 520 xtw.writeEndElement(); 521 522 xtw.writeEndElement(); 523 524 xtw.writeEndDocument(); 525 xtw.flush(); 526 xtw.close(); 527 // check against testSeven.xml.org 528 // prefix is generated while it was defined in the 'org' file, the 529 // following comparison method needs a rewrite. 530 // assertTrue(checkResults(files[9]+".out",files[7]+".org")); 531 System.out.println("Done"); 532 } catch (Exception ex) { 533 Assert.fail("testTen Failed " + ex); 534 ex.printStackTrace(); 535 } 536 537 } 538 539 @Test 540 public void testEleven() { 541 542 System.out.println("Test StreamWriter supplied with namespace information passed through startElement and" + "isRepairingNamespace is set to true."); 543 try { 544 545 String outputFile = files[10] + ".out"; 546 System.out.println("Writing output to " + outputFile); 547 outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, new Boolean(true)); 548 xtw = outputFactory.createXMLStreamWriter(new FileOutputStream(outputFile), ENCODING); 549 xtw.writeComment("all elements here are explicitly in the HTML namespace"); 550 xtw.writeStartDocument("utf-8", "1.0"); 551 // xtw.setPrefix("html", "http://www.w3.org/TR/REC-html40"); 552 xtw.writeStartElement("html", "html", "http://www.w3.org/TR/REC-html40"); 553 // xtw.writeNamespace("html", "http://www.w3.org/TR/REC-html40"); 554 555 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "head"); 556 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "title"); 557 558 xtw.writeCharacters("Frobnostication"); 559 xtw.writeEndElement(); 560 xtw.writeEndElement(); 561 562 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "body"); 563 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "p"); 564 xtw.writeCharacters("Moved to"); 565 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "a"); 566 xtw.writeAttribute("href", "http://frob.com"); 567 568 xtw.writeCharacters("here"); 569 xtw.writeEndElement(); 570 xtw.writeEndElement(); 571 xtw.writeEndElement(); 572 573 xtw.writeEndElement(); 574 575 xtw.writeEndDocument(); 576 xtw.flush(); 577 xtw.close(); 578 // check against testSeven.xml.org 579 Assert.assertTrue(checkResults(files[10] + ".out", files[7] + ".org")); 580 System.out.println("Done"); 581 } catch (Exception ex) { 582 Assert.fail("testEleven Failed " + ex); 583 ex.printStackTrace(); 584 } 585 586 } 587 588 @Test 589 public void testTwelve() { 590 591 System.out.println("Test StreamWriter supplied with namespace information set at few places"); 592 593 try { 594 595 String outputFile = files[11] + ".out"; 596 System.out.println("Writing output to " + outputFile); 597 outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, new Boolean(true)); 598 xtw = outputFactory.createXMLStreamWriter(new FileOutputStream(outputFile), ENCODING); 599 xtw.writeComment("all elements here are explicitly in the HTML namespace"); 600 xtw.writeStartDocument("utf-8", "1.0"); 601 602 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "html"); 603 // xtw.writeNamespace("html", "http://www.w3.org/TR/REC-html40"); 604 605 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "head"); 606 xtw.setPrefix("html", "http://www.w3.org/TR/REC-html40"); 607 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "title"); 608 609 xtw.writeCharacters("Frobnostication"); 610 xtw.writeEndElement(); 611 xtw.writeEndElement(); 612 613 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "body"); 614 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "p"); 615 xtw.writeCharacters("Moved to"); 616 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "a"); 617 xtw.writeAttribute("href", "http://frob.com"); 618 619 xtw.writeCharacters("here"); 620 xtw.writeEndElement(); 621 xtw.writeEndElement(); 622 xtw.writeEndElement(); 623 624 xtw.writeEndElement(); 625 626 xtw.writeEndDocument(); 627 xtw.flush(); 628 xtw.close(); 629 // check against testSeven.xml.org 630 // assertTrue(checkResults(files[10]+".out",files[7]+".org")); 631 System.out.println("Done"); 632 } catch (Exception ex) { 633 Assert.fail("testtwelve Failed " + ex); 634 ex.printStackTrace(); 635 } 636 637 } 638 639 @Test 640 public void testDefaultNamespace() { 641 642 System.out.println("Test StreamWriter supplied with namespace information set at few places"); 643 644 try { 645 646 String outputFile = files[12] + ".out"; 647 System.out.println("Writing output to " + outputFile); 648 outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, new Boolean(true)); 649 xtw = outputFactory.createXMLStreamWriter(new FileOutputStream(outputFile), ENCODING); 650 xtw.writeComment("all elements here are explicitly in the HTML namespace"); 651 xtw.writeStartDocument("utf-8", "1.0"); 652 653 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "html"); 654 xtw.writeDefaultNamespace("http://www.w3.org/TR/REC-html40"); 655 656 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "head"); 657 // xtw.setPrefix("html", "http://www.w3.org/TR/REC-html40"); 658 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "title"); 659 660 xtw.writeCharacters("Frobnostication"); 661 xtw.writeEndElement(); 662 xtw.writeEndElement(); 663 664 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "body"); 665 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "p"); 666 xtw.writeCharacters("Moved to"); 667 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "a"); 668 xtw.writeAttribute("href", "http://frob.com"); 669 670 xtw.writeCharacters("here"); 671 xtw.writeEndElement(); 672 xtw.writeEndElement(); 673 xtw.writeEndElement(); 674 675 xtw.writeEndElement(); 676 677 xtw.writeEndDocument(); 678 xtw.flush(); 679 xtw.close(); 680 // check against testSeven.xml.org 681 // assertTrue(checkResults(files[10]+".out",files[7]+".org")); 682 System.out.println("Done"); 683 } catch (Exception ex) { 684 ex.printStackTrace(); 685 Assert.fail("testDefaultNamespace Failed " + ex); 686 687 } 688 689 } 690 691 @Test 692 public void testRepairNamespace() { 693 694 System.out.println("Test StreamWriter supplied with namespace information set at few places"); 695 696 try { 697 698 String outputFile = files[14] + ".out"; 699 System.out.println("Writing output to " + outputFile); 700 outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, new Boolean(true)); 701 xtw = outputFactory.createXMLStreamWriter(new FileOutputStream(outputFile), ENCODING); 702 xtw.writeComment("all elements here are explicitly in the HTML namespace"); 703 xtw.writeStartDocument("utf-8", "1.0"); 704 xtw.writeStartElement("html", "html", "http://www.w3.org/TR/REC-html40"); 705 // xtw.writeStartElement("http://www.w3.org/TR/REC-html40","html"); 706 // xtw.writeDefaultNamespace("http://www.w3.org/TR/REC-html40"); 707 xtw.writeAttribute("html", "testPrefix", "attr1", "http://frob.com"); 708 xtw.writeAttribute("html", "testPrefix", "attr2", "http://frob2.com"); 709 xtw.writeAttribute("html", "http://www.w3.org/TR/REC-html40", "attr4", "http://frob4.com"); 710 711 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "head"); 712 xtw.setPrefix("html", "http://www.w3.org/TR/REC-html40"); 713 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "title"); 714 715 xtw.writeCharacters("Frobnostication"); 716 xtw.writeEndElement(); 717 xtw.writeEndElement(); 718 719 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "body"); 720 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "p"); 721 xtw.writeCharacters("Moved to"); 722 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "a"); 723 xtw.writeAttribute("href", "http://frob.com"); 724 725 xtw.writeCharacters("here"); 726 xtw.writeEndElement(); 727 xtw.writeEndElement(); 728 xtw.writeEndElement(); 729 730 xtw.writeEndElement(); 731 732 xtw.writeEndDocument(); 733 xtw.flush(); 734 xtw.close(); 735 // check against testSeven.xml.org 736 // assertTrue(checkResults(files[10]+".out",files[7]+".org")); 737 System.out.println("Done"); 738 } catch (Exception ex) { 739 ex.printStackTrace(); 740 Assert.fail("testDefaultNamespace Failed " + ex); 741 742 } 743 744 } 745 746 protected boolean checkResults(String checkFile, String orgFile) { 747 try { 748 URL fileName = WriterTest.class.getResource(orgFile); 749 // URL outputFileName = WriterTest.class.getResource(checkFile); 750 return compareOutput(new InputStreamReader(fileName.openStream()), new InputStreamReader(new FileInputStream(checkFile))); 751 752 } catch (Exception ex) { 753 ex.printStackTrace(); 754 Assert.fail(ex.getMessage()); 755 } 756 return false; 757 } 758 759 protected boolean compareOutput(Reader expected, Reader actual) throws IOException { 760 LineNumberReader expectedOutput = null; 761 LineNumberReader actualOutput = null; 762 try { 763 expectedOutput = new LineNumberReader(expected); 764 actualOutput = new LineNumberReader(actual); 765 766 while (expectedOutput.ready() && actualOutput.ready()) { 767 String expectedLine = expectedOutput.readLine(); 768 String actualLine = actualOutput.readLine(); 769 if (!expectedLine.equals(actualLine)) { 770 System.out.println("Entityreference expansion failed, line no: " + expectedOutput.getLineNumber()); 771 System.out.println("Expected: " + expectedLine); 772 System.out.println("Actual : " + actualLine); 773 return false; 774 } 775 } 776 return true; 777 } catch (IOException ex) { 778 System.err.println("Error occured while comparing results."); 779 throw ex; 780 } finally { 781 expectedOutput.close(); 782 actualOutput.close(); 783 784 } 785 } 786 }