1 /* 2 * Copyright (c) 2003, 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 24 package javax.xml.transform.ptests; 25 26 import java.io.BufferedWriter; 27 import java.io.FileWriter; 28 import java.io.IOException; 29 import javax.xml.transform.TransformerFactory; 30 import javax.xml.transform.dom.DOMResult; 31 import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR; 32 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; 33 import javax.xml.transform.sax.SAXSource; 34 import javax.xml.transform.sax.SAXTransformerFactory; 35 import javax.xml.transform.sax.TransformerHandler; 36 import jaxp.library.JAXPFileBaseTest; 37 import static jaxp.library.JAXPTestUtilities.USER_DIR; 38 import static jaxp.library.JAXPTestUtilities.compareWithGold; 39 import static org.testng.Assert.assertTrue; 40 import org.testng.annotations.Test; 41 import org.w3c.dom.Attr; 42 import org.w3c.dom.NamedNodeMap; 43 import org.w3c.dom.Node; 44 import org.w3c.dom.NodeList; 45 import org.xml.sax.InputSource; 46 import org.xml.sax.XMLReader; 47 import org.xml.sax.helpers.XMLReaderFactory; 48 49 /** 50 * DOM parse on test file to be compared with golden output file. No Exception 51 * is expected. 52 */ 53 public class DOMResultTest extends JAXPFileBaseTest { 54 /** 55 * Unit test for simple DOM parsing. 56 * @throws Exception If any errors occur. 57 */ 58 @Test 59 public void testcase01() throws Exception { 60 String resultFile = USER_DIR + "domresult01.out"; 61 String goldFile = GOLDEN_DIR + "domresult01GF.out"; 62 String xsltFile = XML_DIR + "cities.xsl"; 63 String xmlFile = XML_DIR + "cities.xml"; 64 65 XMLReader reader = XMLReaderFactory.createXMLReader(); 66 SAXTransformerFactory saxTFactory 67 = (SAXTransformerFactory) TransformerFactory.newInstance(); 68 SAXSource saxSource = new SAXSource(new InputSource(xsltFile)); 69 TransformerHandler handler 70 = saxTFactory.newTransformerHandler(saxSource); 71 72 DOMResult result = new DOMResult(); 73 74 handler.setResult(result); 75 reader.setContentHandler(handler); 76 reader.parse(xmlFile); 77 78 Node node = result.getNode(); 79 try (BufferedWriter writer = new BufferedWriter(new FileWriter(resultFile))) { 80 writeNodes(node, writer); 81 } 82 assertTrue(compareWithGold(goldFile, resultFile)); 83 } 84 85 /** 86 * Prints all node names, attributes to file 87 * @param node a node that need to be recursively access. 88 * @param bWriter file writer. 89 * @throws IOException if writing file failed. 90 */ 91 private void writeNodes(Node node, BufferedWriter bWriter) throws IOException { 92 String str = "Node: " + node.getNodeName(); 93 bWriter.write( str, 0,str.length()); 94 bWriter.newLine(); 95 96 NamedNodeMap nnm = node.getAttributes(); 97 if (nnm != null && nnm.getLength() > 0) 98 for (int i=0; i<nnm.getLength(); i++) { 99 str = "AttributeName:" + ((Attr) nnm.item(i)).getName() + 100 ", AttributeValue:" +((Attr) nnm.item(i)).getValue(); 101 bWriter.write( str, 0,str.length()); 102 bWriter.newLine(); 103 } 104 105 NodeList kids = node.getChildNodes(); 106 if (kids != null) 107 for (int i=0; i<kids.getLength(); i++) 108 writeNodes(kids.item(i), bWriter); 109 } 110 }