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