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