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.SAXParserFactory; 34 import static jaxp.library.JAXPTestUtilities.compareWithGold; 35 import static jaxp.library.JAXPTestUtilities.failCleanup; 36 import static jaxp.library.JAXPTestUtilities.failUnexpected; 37 import static org.testng.Assert.assertTrue; 38 import org.xml.sax.Attributes; 39 import org.xml.sax.InputSource; 40 import org.xml.sax.Locator; 41 import org.xml.sax.SAXException; 42 import org.xml.sax.SAXParseException; 43 import org.xml.sax.XMLReader; 44 import org.xml.sax.helpers.XMLFilterImpl; 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 * Set parent of XMLFilter to XMLReader. Parsing on XML file will invoke XMLFilter 51 * to write to output file. Test verifies output is same as the golden file. 52 */ 53 public class XMLFilterCBTest { 54 public void testXMLFilterCB() { 55 String outputFile = CLASS_DIR + "XMLFilter.out"; 56 String goldFile = GOLDEN_DIR + "XMLFilterGF.out"; 57 String xmlFile = XML_DIR + "namespace1.xml"; 58 59 try (FileInputStream fis = new FileInputStream(xmlFile)){ 60 SAXParserFactory spf = SAXParserFactory.newInstance(); 61 spf.setNamespaceAware(true); 62 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 63 64 MyXMLFilter myXmlFilter = new MyXMLFilter(outputFile); 65 myXmlFilter.setParent(xmlReader); 66 InputSource is = new InputSource(fis); 67 myXmlFilter.parse(is); 68 } catch( SAXException | IOException | ParserConfigurationException ex) { 69 failUnexpected(ex); 70 } 71 // Need close the output file before we compare it with golden file. 72 try { 73 assertTrue(compareWithGold(goldFile, outputFile)); 74 } catch (IOException ex) { 75 failUnexpected(ex); 76 } finally { 77 try { 78 Path outputPath = Paths.get(outputFile); 79 if(Files.exists(outputPath)) 80 Files.delete(outputPath); 81 } catch (IOException ex) { 82 failCleanup(ex, outputFile); 83 } 84 } 85 } 86 } 87 88 /** 89 * Writer XMLFiler which write all tags to output file when event happens. 90 */ 91 class MyXMLFilter extends XMLFilterImpl{ 92 /** 93 * FileWriter to write string to output file. 94 */ 95 private final BufferedWriter bWriter; 96 97 /** 98 * Initiate FileWriter for output file. 99 * @param outputFileName output file name. 100 * @throws SAXException if open file failed. 101 */ 102 MyXMLFilter(String outputFileName) throws SAXException { 103 try { 104 bWriter = new BufferedWriter(new FileWriter(outputFileName)); 105 } catch (IOException ex) { 106 throw new SAXException(ex); 107 } 108 } 109 110 /** 111 * Write characters tag along with content of characters when meet 261 */ 262 @Override 263 public void startPrefixMapping(String prefix, String uri) throws SAXException { 264 println("startPrefixMapping...\n" + "prefix: " 265 + prefix + " uri: " + uri); 266 } 267 268 /** 269 * Write outString to file. 270 * @param outString String to be written to File 271 * @throws SAXException if write file failed 272 */ 273 private void println(String outString) throws SAXException { 274 try { 275 bWriter.write( outString, 0, outString.length()); 276 bWriter.newLine(); 277 } catch (IOException ex) { 278 throw new SAXException(ex); 279 } 280 } 281 } | 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.SAXParserFactory; 31 import jaxp.library.JAXPFileBaseTest; 32 import static jaxp.library.JAXPTestUtilities.compareWithGold; 33 import static org.testng.Assert.assertTrue; 34 import org.xml.sax.Attributes; 35 import org.xml.sax.InputSource; 36 import org.xml.sax.Locator; 37 import org.xml.sax.SAXException; 38 import org.xml.sax.SAXParseException; 39 import org.xml.sax.XMLReader; 40 import org.xml.sax.helpers.XMLFilterImpl; 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 * Set parent of XMLFilter to XMLReader. Parsing on XML file will invoke XMLFilter 47 * to write to output file. Test verifies output is same as the golden file. 48 */ 49 public class XMLFilterCBTest extends JAXPFileBaseTest { 50 /** 51 * Test XMLFilter working with XML reader. 52 * 53 * @throws SAXException If there is a problem processing the document. 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 public void testXMLFilterCB() throws SAXException, IOException, 61 ParserConfigurationException { 62 String outputFile = CLASS_DIR + "XMLFilter.out"; 63 String goldFile = GOLDEN_DIR + "XMLFilterGF.out"; 64 String xmlFile = XML_DIR + "namespace1.xml"; 65 66 try (FileInputStream fis = new FileInputStream(xmlFile); 67 MyXMLFilter myXmlFilter = new MyXMLFilter(outputFile)){ 68 SAXParserFactory spf = SAXParserFactory.newInstance(); 69 spf.setNamespaceAware(true); 70 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 71 myXmlFilter.setParent(xmlReader); 72 myXmlFilter.parse(new InputSource(fis)); 73 } 74 // Need close the output file before we compare it with golden file. 75 assertTrue(compareWithGold(goldFile, outputFile)); 76 } 77 } 78 79 /** 80 * Writer XMLFiler which write all tags to output file when event happens. 81 */ 82 class MyXMLFilter extends XMLFilterImpl implements AutoCloseable { 83 /** 84 * FileWriter to write string to output file. 85 */ 86 private final BufferedWriter bWriter; 87 88 /** 89 * Initiate FileWriter for output file. 90 * @param outputFileName output file name. 91 * @throws SAXException if open file failed. 92 */ 93 MyXMLFilter(String outputFileName) throws SAXException { 94 try { 95 bWriter = new BufferedWriter(new FileWriter(outputFileName)); 96 } catch (IOException ex) { 97 throw new SAXException(ex); 98 } 99 } 100 101 /** 102 * Write characters tag along with content of characters when meet 252 */ 253 @Override 254 public void startPrefixMapping(String prefix, String uri) throws SAXException { 255 println("startPrefixMapping...\n" + "prefix: " 256 + prefix + " uri: " + uri); 257 } 258 259 /** 260 * Write outString to file. 261 * @param outString String to be written to File 262 * @throws SAXException if write file failed 263 */ 264 private void println(String outString) throws SAXException { 265 try { 266 bWriter.write( outString, 0, outString.length()); 267 bWriter.newLine(); 268 } catch (IOException ex) { 269 throw new SAXException(ex); 270 } 271 } 272 273 /** 274 * Close writer handler. 275 * @throws IOException if any I/O error when close writer handler. 276 */ 277 @Override 278 public void close() throws IOException { 279 if (bWriter != null) 280 bWriter.close(); 281 } 282 } |