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(); | 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(); |