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.ByteArrayOutputStream;
  27 import java.io.File;
  28 import java.io.FileInputStream;
  29 import java.io.IOException;
  30 import java.io.InputStream;
  31 import java.io.PrintStream;
  32 
  33 import javax.xml.parsers.DocumentBuilder;
  34 import javax.xml.parsers.DocumentBuilderFactory;
  35 import javax.xml.parsers.ParserConfigurationException;
  36 import javax.xml.stream.XMLOutputFactory;
  37 import javax.xml.stream.XMLStreamWriter;
  38 import javax.xml.transform.Result;
  39 import javax.xml.transform.Transformer;
  40 import javax.xml.transform.TransformerFactory;
  41 import javax.xml.transform.dom.DOMSource;
  42 import javax.xml.transform.stream.StreamResult;
  43 
  44 import org.testng.annotations.Listeners;
  45 import org.testng.annotations.Test;
  46 import org.w3c.dom.Element;
  47 import org.w3c.dom.Node;
  48 import org.xml.sax.SAXException;
  49 
  50 /*
  51  * @summary Test XMLStreamWriter writes a soap message.
  52  */
  53 @Listeners({jaxp.library.FilePolicy.class})
  54 public class DomUtilTest {
  55 
  56     private XMLOutputFactory staxOut;
  57     private static final String INPUT_FILE1 = "message_12.xml";
  58 
  59     public void setup() {
  60         this.staxOut = XMLOutputFactory.newInstance();
  61         staxOut.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
  62     }
  63 
  64     @Test
  65     public void testSOAPEnvelope1() throws Exception {
  66         setup();
  67 
  68         File f = new File(this.getClass().getResource(INPUT_FILE1).getFile());
  69         System.out.println("***********" + f.getName() + "***********");
  70         DOMSource src = makeDomSource(f);
  71         Node node = src.getNode();
  72         XMLStreamWriter writer = staxOut.createXMLStreamWriter(new PrintStream(System.out));
  73         DOMUtil.serializeNode((Element) node.getFirstChild(), writer);
  74         writer.close();
  75         assert (true);
  76         System.out.println("*****************************************");
  77 
  78     }
  79 
  80     public static DOMSource makeDomSource(File f) throws Exception {
  81         InputStream is = new FileInputStream(f);
  82         DOMSource domSource = new DOMSource(createDOMNode(is));
  83         return domSource;
  84     }
  85 
  86     public static void printNode(Node node) {
  87         DOMSource source = new DOMSource(node);
  88         String msgString = null;
  89         try {
  90             Transformer xFormer = TransformerFactory.newInstance().newTransformer();
  91             xFormer.setOutputProperty("omit-xml-declaration", "yes");
  92             ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  93             Result result = new StreamResult(outStream);
  94             xFormer.transform(source, result);
  95             outStream.writeTo(System.out);
  96         } catch (Exception ex) {
  97             ex.printStackTrace();
  98         }
  99     }
 100 
 101     public static Node createDOMNode(InputStream inputStream) {
 102         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 103         dbf.setNamespaceAware(true);
 104         dbf.setValidating(false);
 105         try {
 106             DocumentBuilder builder = dbf.newDocumentBuilder();
 107             try {
 108                 return builder.parse(inputStream);
 109             } catch (SAXException e) {
 110                 e.printStackTrace();
 111             } catch (IOException e) {
 112                 e.printStackTrace();
 113             }
 114         } catch (ParserConfigurationException pce) {
 115             IllegalArgumentException iae = new IllegalArgumentException(pce.getMessage());
 116             iae.initCause(pce);
 117             throw iae;
 118         }
 119         return null;
 120     }
 121 
 122 }