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 javax.xml.stream.XMLEventWriterTest; 25 26 import java.io.File; 27 import java.io.FileInputStream; 28 import java.io.InputStream; 29 30 import javax.xml.namespace.QName; 31 import javax.xml.stream.XMLEventReader; 32 import javax.xml.stream.XMLEventWriter; 33 import javax.xml.stream.XMLInputFactory; 34 import javax.xml.stream.XMLOutputFactory; 35 import javax.xml.stream.events.XMLEvent; 36 import javax.xml.transform.stream.StreamSource; 37 38 import org.testng.Assert; 39 import org.testng.annotations.Test; 40 41 /* 42 * @summary Test XMLEventWriter. 43 */ 44 public class XMLEventWriterTest { 45 46 /** 47 * Test XMLStreamWriter parsing a file with an external entity reference. 48 */ 49 @Test 50 public void testXMLStreamWriter() { 51 52 try { 53 XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); 54 XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(System.out); 55 XMLInputFactory inputFactory = XMLInputFactory.newInstance(); 56 String file = getClass().getResource("XMLEventWriterTest.xml").getPath(); 57 XMLEventReader eventReader = inputFactory.createXMLEventReader(new StreamSource(new File(file))); 58 59 // adds the event to the consumer. 60 eventWriter.add(eventReader); 61 eventWriter.flush(); 62 eventWriter.close(); 63 64 // expected success 65 } catch (Exception exception) { 66 exception.printStackTrace(); 67 Assert.fail(exception.toString()); 68 } 69 } 70 71 /** 72 * Inspired by CR 6245284 Sun Stax /sjsxp.jar does not behave properly 73 * during merge of xml files. 74 */ 75 @Test 76 public void testMerge() { 77 78 try { 79 // Create the XML input factory 80 XMLInputFactory factory = XMLInputFactory.newInstance(); 81 82 // Create XML event reader 1 83 InputStream inputStream1 = new FileInputStream(new File(XMLEventWriterTest.class.getResource("merge-1.xml").toURI())); 84 XMLEventReader r1 = factory.createXMLEventReader(inputStream1); 85 86 // Create XML event reader 2 87 InputStream inputStream2 = new FileInputStream(new File(XMLEventWriterTest.class.getResource("merge-2.xml").toURI())); 88 XMLEventReader r2 = factory.createXMLEventReader(inputStream2); 89 90 // Create the output factory 91 XMLOutputFactory xmlof = XMLOutputFactory.newInstance(); 92 93 // Create XML event writer 94 XMLEventWriter xmlw = xmlof.createXMLEventWriter(System.out); 95 96 // Read to first <product> element in document 1 97 // and output to result document 98 QName bName = new QName("b"); 99 100 while (r1.hasNext()) { 101 // Read event to be written to result document 102 XMLEvent event = r1.nextEvent(); 103 104 if (event.getEventType() == XMLEvent.END_ELEMENT) { 105 106 // Start element - stop at <product> element 107 QName name = event.asEndElement().getName(); 108 if (name.equals(bName)) { 109 110 QName zName = new QName("z"); 111 112 boolean isZr = false; 113 114 while (r2.hasNext()) { 115 // Read event to be written to result document 116 XMLEvent event2 = r2.nextEvent(); 117 // Output event 118 if (event2.getEventType() == XMLEvent.START_ELEMENT && event2.asStartElement().getName().equals(zName)) { 119 isZr = true; 120 } 121 122 if (xmlw != null && isZr) { 123 xmlw.add(event2); 124 } 125 126 // stop adding events after </z> 127 // i.e. do not write END_DOCUMENT :) 128 if (isZr && event2.getEventType() == XMLEvent.END_ELEMENT && event2.asEndElement().getName().equals(zName)) { 129 isZr = false; 130 } 131 } 132 xmlw.flush(); 133 } 134 } 135 136 // Output event 137 if (xmlw != null) { 138 xmlw.add(event); 139 } 140 } 141 142 // Read to first <product> element in document 1 143 // without writing to result document 144 xmlw.close(); 145 146 // expected success 147 } catch (Exception ex) { 148 ex.printStackTrace(); 149 Assert.fail(ex.toString()); 150 } 151 } 152 }