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 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.xml.sax.InputSource; 36 import org.xml.sax.SAXException; 37 import org.xml.sax.XMLReader; 38 import org.xml.sax.helpers.XMLFilterImpl; 39 import static org.xml.sax.ptests.SAXTestConst.CLASS_DIR; 40 import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; 41 import static org.xml.sax.ptests.SAXTestConst.XML_DIR; 42 43 /** 44 * Entity resolver should be invoked in XML parse. This test verifies parsing 45 * process by checking the output with golden file. 46 */ 47 public class ResolverTest extends JAXPFileBaseTest { 48 /** 49 * Unit test for entityResolver setter. 50 * 51 * @throws SAXException If there is a problem processing the document. 52 * @throws IOException if the file exists but is a directory rather than 53 * a regular file, does not exist but cannot be created, or cannot 54 * be opened for any other reason. 55 * @throws ParserConfigurationException if a DocumentBuilder cannot be 56 * created which satisfies the configuration requested. 57 */ 58 public void testResolver() throws IOException, SAXException, 59 ParserConfigurationException { 60 String outputFile = CLASS_DIR + "EntityResolver.out"; 61 String goldFile = GOLDEN_DIR + "EntityResolverGF.out"; 62 String xmlFile = XML_DIR + "publish.xml"; 63 64 try(FileInputStream instream = new FileInputStream(xmlFile); 65 MyEntityResolver eResolver = new MyEntityResolver(outputFile)) { 66 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); 67 XMLReader xmlReader = saxParser.getXMLReader(); 68 xmlReader.setEntityResolver(eResolver); 69 InputSource is = new InputSource(instream); 70 xmlReader.parse(is); 71 } 72 assertTrue(compareWithGold(goldFile, outputFile)); 73 } 74 } 75 76 /** 77 * Simple entity resolver to write every entity to an output file. 78 */ 79 class MyEntityResolver extends XMLFilterImpl implements AutoCloseable { 80 /** 81 * FileWriter to write string to output file. 82 */ 83 private final BufferedWriter bWriter; 84 85 /** 86 * Initiate FileWriter when construct a MyContentHandler. 87 * @param outputFileName output file name. 88 * @throws SAXException creation of FileWriter failed. 89 */ 90 MyEntityResolver(String outputFileName) throws SAXException { 91 super(); 92 try { 93 bWriter = new BufferedWriter(new FileWriter(outputFileName)); 94 } catch (IOException ex) { 95 throw new SAXException(ex); 96 } 97 } 98 99 /** 100 * Write In resolveEntity tag along with publicid and systemId when meet 101 * resolveEntity event. 102 * @throws IOException error happen when writing file. 103 */ 104 @Override 105 public InputSource resolveEntity(String publicid, String systemid) 106 throws SAXException, IOException { 107 String str = "In resolveEntity.." + " " + publicid + " " + systemid; 108 bWriter.write( str, 0,str.length()); 109 bWriter.newLine(); 110 return super.resolveEntity(publicid, systemid); 111 } 112 113 /** 114 * Flush the content and close the file. 115 * @throws IOException error happen when writing file or closing file. 116 */ 117 @Override 118 public void close() throws IOException { 119 bWriter.flush(); 120 bWriter.close(); 121 } 122 }