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