1 /* 2 * Copyright (c) 2014, 2016, 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 transform.util; 25 26 import java.io.FileInputStream; 27 import java.io.FileOutputStream; 28 import java.io.InputStream; 29 30 import javax.xml.stream.XMLEventReader; 31 import javax.xml.stream.XMLInputFactory; 32 import javax.xml.stream.XMLOutputFactory; 33 import javax.xml.stream.XMLStreamReader; 34 import javax.xml.stream.XMLStreamWriter; 35 import javax.xml.transform.Result; 36 import javax.xml.transform.Source; 37 import javax.xml.transform.stax.StAXResult; 38 import javax.xml.transform.stax.StAXSource; 39 40 import org.testng.Assert; 41 42 import transform.TransformerUtilFactory; 43 import transform.VersionEventWriter; 44 45 public class StAXUtil extends TransformerUtil { 46 47 private static StAXUtil instance = null; 48 49 /** Creates a new instance of StAXUtil */ 50 private StAXUtil() { 51 } 52 53 public static synchronized StAXUtil getInstance() throws Exception { 54 if (instance == null) 55 instance = new StAXUtil(); 56 return instance; 57 } 58 59 public Source prepareSource(InputStream is) throws Exception { 60 XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(is); 61 return new StAXSource(reader); 62 } 63 64 public Result prepareResult() throws Exception { 65 VersionEventWriter writer = new VersionEventWriter(); 66 return new StAXResult(writer); 67 } 68 69 public void checkResult(Result staxResult, String version) throws Exception { 70 VersionEventWriter writer = (VersionEventWriter) ((StAXResult) staxResult).getXMLEventWriter(); 71 Assert.assertTrue(writer.getVersion().equals(version), "Expected XML Version is 1.1, but actual version " + writer.getVersion()); 72 } 73 74 public void checkResult(Result staxResult, String version, String encoding) throws Exception { 75 VersionEventWriter writer = (VersionEventWriter) ((StAXResult) staxResult).getXMLEventWriter(); 76 Assert.assertTrue(writer.getVersion().equals(version), "Expected XML Version is 1.1, but actual version " + writer.getVersion()); 77 Assert.assertTrue(writer.getEncoding().equals(encoding), "Expected encoding is " + encoding + ", but actual encoding " + writer.getEncoding()); 78 } 79 80 public Source prepareStreamSource(InputStream is) throws Exception { 81 XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(is); 82 return new StAXSource(reader); 83 } 84 85 public Result prepareStreamResult() throws Exception { 86 XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(new FileOutputStream(TEMP_FILE)); 87 return new StAXResult(writer); 88 } 89 90 public void checkStreamResult(Result staxResult, String version) throws Exception { 91 ((StAXResult) staxResult).getXMLStreamWriter().close(); 92 ((StreamUtil) TransformerUtilFactory.getUtil(TransformerUtilFactory.STREAM)).checkStream(new FileInputStream(TEMP_FILE), version); 93 } 94 }