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 @Listeners({jaxp.library.FilePolicy.class})
  57 public class DOMResultTest {
  58     /**
  59      * Unit test for simple DOM parsing.
  60      * @throws Exception If any errors occur.
  61      */
  62     @Test
  63     public void testcase01() throws Exception {
  64         String resultFile = USER_DIR  + "domresult01.out";
  65         String goldFile = GOLDEN_DIR  + "domresult01GF.out";
  66         String xsltFile = XML_DIR + "cities.xsl";
  67         String xmlFile = XML_DIR + "cities.xml";
  68 
  69         XMLReader reader = XMLReaderFactory.createXMLReader();
  70         SAXTransformerFactory saxTFactory
  71                 = (SAXTransformerFactory) TransformerFactory.newInstance();
  72         SAXSource saxSource = new SAXSource(new InputSource(xsltFile));
  73         TransformerHandler handler
  74                 = saxTFactory.newTransformerHandler(saxSource);
  75 
  76         DOMResult result = new DOMResult();
  77 
  78         handler.setResult(result);
  79         reader.setContentHandler(handler);
  80         reader.parse(xmlFile);
  81 
  82         Node node = result.getNode();
  83         try (BufferedWriter writer = new BufferedWriter(new FileWriter(resultFile))) {
  84             writeNodes(node, writer);
  85         }
  86         assertTrue(compareWithGold(goldFile, resultFile));
  87     }
  88 
  89     /**
  90      * Prints all node names, attributes to file
  91      * @param node a node that need to be recursively access.
  92      * @param bWriter file writer.
  93      * @throws IOException if writing file failed.
  94      */
  95     private void writeNodes(Node node, BufferedWriter bWriter) throws IOException {
  96         String str = "Node: " + node.getNodeName();
  97         bWriter.write( str, 0,str.length());
  98         bWriter.newLine();
  99 
 100         NamedNodeMap nnm = node.getAttributes();
 101         if (nnm != null && nnm.getLength() > 0)
 102             for (int i=0; i<nnm.getLength(); i++) {
 103                 str = "AttributeName:" + ((Attr) nnm.item(i)).getName() +
 104                       ", AttributeValue:" +((Attr) nnm.item(i)).getValue();
 105                 bWriter.write( str, 0,str.length());
 106                 bWriter.newLine();
 107             }
 108 
 109         NodeList kids = node.getChildNodes();
 110         if (kids != null)
 111             for (int i=0; i<kids.getLength(); i++)
 112                 writeNodes(kids.item(i), bWriter);
 113     }
 114 }