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.transform.util; 25 26 import java.io.File; 27 import java.io.FileInputStream; 28 import java.io.FileOutputStream; 29 import java.io.InputStream; 30 31 import javax.xml.parsers.DocumentBuilder; 32 import javax.xml.parsers.SAXParser; 33 import javax.xml.parsers.SAXParserFactory; 34 import javax.xml.transform.Result; 35 import javax.xml.transform.Source; 36 import javax.xml.transform.VersionDefaultHandler; 37 import javax.xml.transform.stream.StreamResult; 38 import javax.xml.transform.stream.StreamSource; 39 40 import org.testng.Assert; 41 import org.w3c.dom.Document; 42 43 public class StreamUtil extends TransformerUtil { 44 45 DocumentBuilder docBuilder = null; 46 47 private static StreamUtil instance = null; 48 49 /** Creates a new instance of StreamUtil */ 50 private StreamUtil() throws Exception { 51 if (docBuilder == null) 52 docBuilder = getDomParser(); 53 } 54 55 public static synchronized StreamUtil getInstance() throws Exception { 56 if (instance == null) 57 instance = new StreamUtil(); 58 return instance; 59 } 60 61 public Source prepareSource(InputStream is) throws Exception { 62 return new StreamSource(is); 63 } 64 65 public Result prepareResult() throws Exception { 66 FileOutputStream fos = new FileOutputStream(TEMP_FILE); 67 return new StreamResult(fos); 68 } 69 70 public void checkResult(Result result, String inputVersion) throws Exception { 71 ((StreamResult) result).getOutputStream().close(); 72 FileInputStream fis = new FileInputStream(TEMP_FILE); 73 checkStream(fis, inputVersion); 74 } 75 76 public void checkStream(FileInputStream fis, String inputVersion) throws Exception { 77 docBuilder.reset(); 78 Document output = docBuilder.parse(fis); 79 String version = output.getXmlVersion(); 80 Assert.assertTrue(inputVersion.equals(version), "Expected XML Version is 1.1, but actual version " + version); 81 } 82 83 public void checkResult(Result result, String version, String encoding) throws Exception { 84 // use sax parser, as encoding info cannot be set on DOM document 85 SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); 86 VersionDefaultHandler dh = new VersionDefaultHandler(); 87 parser.parse(new File(TEMP_FILE), dh); 88 Assert.assertTrue(dh.getVersion().equals(version), "Expected version is " + version + " actual version " + dh.getVersion()); 89 Assert.assertTrue(dh.getEncoding().equals(encoding), "Expected version is " + encoding + " actual version " + dh.getEncoding()); 90 } 91 }