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