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.parsers.ptests;
  25 
  26 import static jaxp.library.JAXPTestUtilities.FILE_SEP;
  27 import static jaxp.library.JAXPTestUtilities.TEMP_DIR;
  28 import static jaxp.library.JAXPTestUtilities.compareWithGold;
  29 import static jaxp.library.JAXPTestUtilities.failUnexpected;
  30 import static org.testng.Assert.assertTrue;
  31 
  32 import java.io.File;
  33 import java.io.IOException;
  34 
  35 import javax.xml.parsers.DocumentBuilderFactory;
  36 import javax.xml.parsers.ParserConfigurationException;
  37 import javax.xml.transform.Transformer;
  38 import javax.xml.transform.TransformerException;
  39 import javax.xml.transform.TransformerFactory;
  40 import javax.xml.transform.TransformerFactoryConfigurationError;
  41 import javax.xml.transform.dom.DOMSource;
  42 import javax.xml.transform.sax.SAXResult;
  43 
  44 import org.testng.annotations.Test;
  45 import org.w3c.dom.Document;
  46 import org.xml.sax.SAXException;
  47 
  48 /**
  49  * This tests DocumentBuilderFactory for namespace processing and no-namespace
  50  * processing.
  51  */
  52 public class DBFNamespaceTest {
  53 
  54     /**
  55      * Test to parse and transform a document without supporting namespace.
  56      */
  57     @Test
  58     public void testNamespaceTest01() {
  59         try {
  60             String outputfile = TEMP_DIR + FILE_SEP + "dbfnstest01.out";
  61             String goldfile = TestUtils.GOLDEN_DIR + FILE_SEP + "dbfnstest01GF.out";
  62             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  63             Document doc = dbf.newDocumentBuilder().parse(new File(TestUtils.XML_DIR, "namespace1.xml"));
  64             dummyTransform(doc, outputfile);
  65             assertTrue(compareWithGold(goldfile, outputfile));
  66         } catch (SAXException | IOException | ParserConfigurationException | TransformerFactoryConfigurationError | TransformerException e) {
  67             failUnexpected(e);
  68         }
  69     }
  70 
  71     /**
  72      * Test to parse and transform a document with supporting namespace.
  73      */
  74     @Test
  75     public void testNamespaceTest02() {
  76         try {
  77             String outputfile = TEMP_DIR + FILE_SEP + "dbfnstest02.out";
  78             String goldfile = TestUtils.GOLDEN_DIR + FILE_SEP + "dbfnstest02GF.out";
  79             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  80             dbf.setNamespaceAware(true);
  81             Document doc = dbf.newDocumentBuilder().parse(new File(TestUtils.XML_DIR, "namespace1.xml"));
  82             dummyTransform(doc, outputfile);
  83             assertTrue(compareWithGold(goldfile, outputfile));
  84         } catch (SAXException | IOException | ParserConfigurationException | TransformerFactoryConfigurationError | TransformerException e) {
  85             failUnexpected(e);
  86         }
  87     }
  88 
  89     /**
  90      * This method transforms a Node without any xsl file and uses SAXResult to
  91      * invoke the callbacks through a ContentHandler. If namespace processing is
  92      * not chosen, namespaceURI in callbacks should be an empty string otherwise
  93      * it should be namespaceURI.
  94      *
  95      * @throws TransformerFactoryConfigurationError
  96      * @throws TransformerException
  97      * @throws IOException
  98      */
  99     private void dummyTransform(Document document, String fileName) throws TransformerFactoryConfigurationError, TransformerException, IOException {
 100         DOMSource domSource = new DOMSource(document);
 101         Transformer transformer = TransformerFactory.newInstance().newTransformer();
 102         File file = new File(fileName);
 103         System.out.println("The fileName is " + file.getAbsolutePath());
 104         transformer.transform(domSource, new SAXResult(MyCHandler.newInstance(file)));
 105     }
 106 
 107 }