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