StAXUtil.java revision 997:540334ae53fe
1/*
2 * Copyright (c) 2014, 2015, 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 transform.util;
25
26import java.io.FileInputStream;
27import java.io.FileOutputStream;
28import java.io.InputStream;
29
30import javax.xml.stream.XMLEventReader;
31import javax.xml.stream.XMLInputFactory;
32import javax.xml.stream.XMLOutputFactory;
33import javax.xml.stream.XMLStreamReader;
34import javax.xml.stream.XMLStreamWriter;
35import javax.xml.transform.Result;
36import javax.xml.transform.Source;
37import javax.xml.transform.stax.StAXResult;
38import javax.xml.transform.stax.StAXSource;
39
40import org.testng.Assert;
41
42import transform.TransformerUtilFactory;
43import transform.VersionEventWriter;
44
45public 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}
95