58411
|
1 |
/*
|
|
2 |
* Copyright (c) 2019, 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;
|
|
25 |
|
|
26 |
import javax.xml.stream.XMLInputFactory;
|
|
27 |
import javax.xml.stream.XMLStreamConstants;
|
|
28 |
import javax.xml.stream.XMLStreamReader;
|
|
29 |
import javax.xml.transform.Transformer;
|
|
30 |
import javax.xml.transform.TransformerFactory;
|
|
31 |
import javax.xml.transform.dom.DOMResult;
|
|
32 |
import javax.xml.transform.stax.StAXSource;
|
|
33 |
import org.testng.annotations.DataProvider;
|
|
34 |
import org.testng.annotations.Test;
|
|
35 |
import org.w3c.dom.Node;
|
|
36 |
|
|
37 |
/*
|
|
38 |
* @test
|
|
39 |
* @bug 8016914
|
|
40 |
* @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
|
|
41 |
* @run testng transform.StAX2DOMTest
|
|
42 |
* @summary Verifies transforming a StAXSource to a DOMResult.
|
|
43 |
*/
|
|
44 |
public class StAX2DOMTest {
|
|
45 |
/**
|
|
46 |
* Data files for test.
|
|
47 |
* Column(s): xml file
|
|
48 |
*
|
|
49 |
* @return data for test
|
|
50 |
*/
|
|
51 |
@DataProvider(name = "datafiles")
|
|
52 |
public Object[][] getData() {
|
|
53 |
return new Object[][] {
|
|
54 |
{ "StAX2DOMTest.xml"}, //without declaration
|
|
55 |
{ "StAX2DOMTest1.xml"}, //with declaration
|
|
56 |
};
|
|
57 |
}
|
|
58 |
|
|
59 |
/**
|
|
60 |
* Verifies that transforming a StAX source to a DOM result passes with
|
|
61 |
* or without the XML declaration.
|
|
62 |
*
|
|
63 |
* @param file the XML file
|
|
64 |
* @throws Exception if the test fails
|
|
65 |
*/
|
|
66 |
@Test(dataProvider = "datafiles")
|
|
67 |
public void test(String file) throws Exception {
|
|
68 |
final XMLInputFactory xif = XMLInputFactory.newInstance();
|
|
69 |
final XMLStreamReader xsr = xif.createXMLStreamReader(
|
|
70 |
this.getClass().getResourceAsStream(file));
|
|
71 |
xsr.nextTag(); // Advance to statements element
|
|
72 |
|
|
73 |
final TransformerFactory tf = TransformerFactory.newInstance();
|
|
74 |
final Transformer t = tf.newTransformer();
|
|
75 |
while(xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
|
|
76 |
final DOMResult result = new DOMResult();
|
|
77 |
t.transform(new StAXSource(xsr), result);
|
|
78 |
final Node domNode = result.getNode();
|
|
79 |
System.out.println(domNode);
|
|
80 |
}
|
|
81 |
}
|
|
82 |
}
|