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