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 package org.xml.sax.ptests; 24 25 import java.io.BufferedWriter; 26 import java.io.File; 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.parsers.ParserConfigurationException; 33 import javax.xml.parsers.SAXParser; 34 import javax.xml.parsers.SAXParserFactory; 35 import static jaxp.library.JAXPTestUtilities.compareWithGold; 36 import static jaxp.library.JAXPTestUtilities.failCleanup; 37 import static jaxp.library.JAXPTestUtilities.failUnexpected; 38 import static org.testng.Assert.assertTrue; 39 import org.testng.annotations.Test; 40 import org.xml.sax.Attributes; 41 import org.xml.sax.Locator; 42 import org.xml.sax.SAXException; 43 import org.xml.sax.SAXParseException; 44 import org.xml.sax.helpers.DefaultHandler; 45 import static org.xml.sax.ptests.SAXTestConst.CLASS_DIR; 46 import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; 47 import static org.xml.sax.ptests.SAXTestConst.XML_DIR; 48 49 /** 50 * XMLReader parse XML with default handler that transverses XML and 51 * print all visited node. Test verifies output is same as the golden file. 52 */ 53 public class DefaultHandlerTest { 54 /** 55 * Test default handler that transverses XML and print all visited node. 56 */ 57 @Test 58 public void testDefaultHandler() { 59 String outputFile = CLASS_DIR + "DefaultHandler.out"; 60 String goldFile = GOLDEN_DIR + "DefaultHandlerGF.out"; 61 String xmlFile = XML_DIR + "namespace1.xml"; 62 63 try { 64 SAXParserFactory spf = SAXParserFactory.newInstance(); 65 spf.setNamespaceAware(true); 66 SAXParser saxparser = spf.newSAXParser(); 67 68 MyDefaultHandler handler = new MyDefaultHandler(outputFile); 69 File file = new File(xmlFile); 70 String Absolutepath = file.getAbsolutePath(); 71 String newAbsolutePath = Absolutepath; 72 if (File.separatorChar == '\\') 73 newAbsolutePath = Absolutepath.replace('\\', '/'); 74 String uri = "file:///" + newAbsolutePath; 75 saxparser.parse(uri, handler); 76 } catch (IOException | ParserConfigurationException | SAXException ex) { 77 failUnexpected(ex); 78 } 79 // Need close the output file before we compare it with golden file. 80 try { 81 assertTrue(compareWithGold(goldFile, outputFile)); 82 } catch (IOException ex) { 83 failUnexpected(ex); 84 } finally { 85 try { 86 Path outputPath = Paths.get(outputFile); 87 if(Files.exists(outputPath)) 88 Files.delete(outputPath); 89 } catch (IOException ex) { 90 failCleanup(ex, outputFile); 91 } 92 } 93 } 94 } 95 96 class MyDefaultHandler extends DefaultHandler { 97 /** 98 * Prefix to every exception. 99 */ 100 private final static String WRITE_ERROR = "bWrite error"; 101 102 /** 103 * FileWriter to write string to output file. 104 */ 105 private final BufferedWriter bWriter; 106 107 /** 108 * Initiate FileWriter when construct a MyContentHandler. 109 * @param outputFileName output file name. 110 * @throws SAXException creation of FileWriter failed. 111 */ 112 MyDefaultHandler(String outputFileName) throws SAXException { | 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 package org.xml.sax.ptests; 24 25 import java.io.BufferedWriter; 26 import java.io.File; 27 import java.io.FileWriter; 28 import java.io.IOException; 29 import javax.xml.parsers.ParserConfigurationException; 30 import javax.xml.parsers.SAXParser; 31 import javax.xml.parsers.SAXParserFactory; 32 import jaxp.library.JAXPFileBaseTest; 33 import static jaxp.library.JAXPTestUtilities.compareWithGold; 34 import static org.testng.Assert.assertTrue; 35 import org.testng.annotations.Test; 36 import org.xml.sax.Attributes; 37 import org.xml.sax.Locator; 38 import org.xml.sax.SAXException; 39 import org.xml.sax.SAXParseException; 40 import org.xml.sax.helpers.DefaultHandler; 41 import static org.xml.sax.ptests.SAXTestConst.CLASS_DIR; 42 import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; 43 import static org.xml.sax.ptests.SAXTestConst.XML_DIR; 44 45 /** 46 * XMLReader parse XML with default handler that transverses XML and 47 * print all visited node. Test verifies output is same as the golden file. 48 */ 49 public class DefaultHandlerTest extends JAXPFileBaseTest { 50 /** 51 * Test default handler that transverses XML and print all visited node. 52 * 53 * @throws SAXException If any parse errors occur. 54 * @throws IOException if the file exists but is a directory rather than 55 * a regular file, does not exist but cannot be created, or cannot 56 * be opened for any other reason. 57 * @throws ParserConfigurationException if a DocumentBuilder cannot be 58 * created which satisfies the configuration requested. 59 */ 60 @Test 61 public void testDefaultHandler() throws ParserConfigurationException, 62 SAXException, IOException { 63 String outputFile = CLASS_DIR + "DefaultHandler.out"; 64 String goldFile = GOLDEN_DIR + "DefaultHandlerGF.out"; 65 String xmlFile = XML_DIR + "namespace1.xml"; 66 67 SAXParserFactory spf = SAXParserFactory.newInstance(); 68 spf.setNamespaceAware(true); 69 SAXParser saxparser = spf.newSAXParser(); 70 71 MyDefaultHandler handler = new MyDefaultHandler(outputFile); 72 File file = new File(xmlFile); 73 String Absolutepath = file.getAbsolutePath(); 74 String newAbsolutePath = Absolutepath; 75 if (File.separatorChar == '\\') 76 newAbsolutePath = Absolutepath.replace('\\', '/'); 77 saxparser.parse("file:///" + newAbsolutePath, handler); 78 79 assertTrue(compareWithGold(goldFile, outputFile)); 80 81 } 82 } 83 84 class MyDefaultHandler extends DefaultHandler { 85 /** 86 * Prefix to every exception. 87 */ 88 private final static String WRITE_ERROR = "bWrite error"; 89 90 /** 91 * FileWriter to write string to output file. 92 */ 93 private final BufferedWriter bWriter; 94 95 /** 96 * Initiate FileWriter when construct a MyContentHandler. 97 * @param outputFileName output file name. 98 * @throws SAXException creation of FileWriter failed. 99 */ 100 MyDefaultHandler(String outputFileName) throws SAXException { |