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