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