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 package org.xml.sax.ptests; 24 25 import java.io.BufferedWriter; 26 import java.io.FileInputStream; 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.xml.sax.InputSource; 40 import org.xml.sax.SAXException; 41 import org.xml.sax.XMLReader; 42 import org.xml.sax.helpers.XMLFilterImpl; 43 import static org.xml.sax.ptests.SAXTestConst.CLASS_DIR; 44 import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; 45 import static org.xml.sax.ptests.SAXTestConst.XML_DIR; 46 47 /** 48 * Entity resolver should be invoked in XML parse. This test verifies parsing 49 * process by checking the output with golden file. 50 */ 51 public class ResolverTest { 52 /** 53 * Unit test for entityResolver setter. 54 */ 55 public void testResolver() { 56 String outputFile = CLASS_DIR + "EntityResolver.out"; 57 String goldFile = GOLDEN_DIR + "EntityResolverGF.out"; 58 String xmlFile = XML_DIR + "publish.xml"; 59 60 try(FileInputStream instream = new FileInputStream(xmlFile); 61 MyEntityResolver eResolver = new MyEntityResolver(outputFile)) { 62 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); 63 XMLReader xmlReader = saxParser.getXMLReader(); 64 xmlReader.setEntityResolver(eResolver); 65 InputSource is = new InputSource(instream); 66 xmlReader.parse(is); 67 } catch(IOException | SAXException | ParserConfigurationException ex ) { 68 failUnexpected(ex); 69 } 70 // Need close the output file before we compare it with golden file. 71 try { 72 assertTrue(compareWithGold(goldFile, outputFile)); 73 } catch (IOException ex) { 74 failUnexpected(ex); 75 } finally { 76 try { 77 Path outputPath = Paths.get(outputFile); 78 if(Files.exists(outputPath)) 79 Files.delete(outputPath); 80 } catch (IOException ex) { 81 failCleanup(ex, outputFile); 82 } 83 } 84 } 85 } 86 87 /** 88 * Simple entity resolver to write every entity to an output file. 89 */ 90 class MyEntityResolver extends XMLFilterImpl implements AutoCloseable { 91 /** 92 * FileWriter to write string to output file. 93 */ 94 private final BufferedWriter bWriter; 95 96 /** 97 * Initiate FileWriter when construct a MyContentHandler. 98 * @param outputFileName output file name. 99 * @throws SAXException creation of FileWriter failed. 100 */ 101 MyEntityResolver(String outputFileName) throws SAXException { 102 super(); 103 try { 104 bWriter = new BufferedWriter(new FileWriter(outputFileName)); 105 } catch (IOException ex) { 106 throw new SAXException(ex); 107 } 108 } 109 110 /** 111 * Write In resolveEntity tag along with publicid and systemId when meet 112 * resolveEntity event. 113 * @throws IOException error happen when writing file. 114 */ 115 @Override 116 public InputSource resolveEntity(String publicid, String systemid) 117 throws SAXException, IOException { 118 String str = "In resolveEntity.." + " " + publicid + " " + systemid; 119 bWriter.write( str, 0,str.length()); 120 bWriter.newLine(); 121 return super.resolveEntity(publicid, systemid); 122 } 123 124 /** 125 * Flush the content and close the file. 126 * @throws IOException error happen when writing file or closing file. 127 */ 128 @Override 129 public void close() throws IOException { 130 bWriter.flush(); 131 bWriter.close(); 132 } 133 }