1 /*
   2  * Copyright (c) 2003, 2016, 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 static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
  26 import static jaxp.library.JAXPTestUtilities.USER_DIR;
  27 import static jaxp.library.JAXPTestUtilities.compareWithGold;
  28 import static org.testng.Assert.assertTrue;
  29 import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR;
  30 import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
  31 
  32 import java.io.BufferedWriter;
  33 import java.io.File;
  34 import java.io.FileInputStream;
  35 import java.io.FileWriter;
  36 import java.io.IOException;
  37 import java.net.URI;
  38 import java.net.URISyntaxException;
  39 import java.nio.file.Files;
  40 import java.nio.file.Paths;
  41 
  42 import javax.xml.parsers.SAXParser;
  43 import javax.xml.parsers.SAXParserFactory;
  44 
  45 import org.testng.annotations.Listeners;
  46 import org.testng.annotations.Test;
  47 import org.xml.sax.InputSource;
  48 import org.xml.sax.SAXException;
  49 import org.xml.sax.XMLReader;
  50 import org.xml.sax.helpers.XMLFilterImpl;
  51 
  52 /**
  53  * Entity resolver should be invoked in XML parse. This test verifies parsing
  54  * process by checking the output with golden file.
  55  */
  56 @Test
  57 @Listeners({jaxp.library.FilePolicy.class})
  58 public class ResolverTest {
  59     /**
  60      * Unit test for entityResolver setter.
  61      *
  62      * @throws Exception If any errors occur.
  63      */
  64     public void testResolver() throws Exception {
  65         String outputFile = USER_DIR + "EntityResolver.out";
  66         String goldFile = GOLDEN_DIR + "EntityResolverGF.out";
  67         String xmlFile = XML_DIR + "publish.xml";
  68 
  69         Files.copy(Paths.get(XML_DIR + "publishers.dtd"),
  70                 Paths.get(USER_DIR + File.separator + "publishers.dtd"), REPLACE_EXISTING);
  71         Files.copy(Paths.get(XML_DIR + "familytree.dtd"),
  72                 Paths.get(USER_DIR + File.separator + "familytree.dtd"), REPLACE_EXISTING);
  73         
  74         try(FileInputStream instream = new FileInputStream(xmlFile);
  75                 MyEntityResolver eResolver = new MyEntityResolver(outputFile)) {
  76             SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
  77             XMLReader xmlReader = saxParser.getXMLReader();
  78             xmlReader.setEntityResolver(eResolver);
  79             InputSource is = new InputSource(instream);
  80             xmlReader.parse(is);
  81         }
  82         assertTrue(compareWithGold(goldFile, outputFile));
  83     }
  84 }
  85 
  86 /**
  87  * Simple entity resolver to write every entity to an output file.
  88  */
  89 class MyEntityResolver extends XMLFilterImpl implements AutoCloseable {
  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     MyEntityResolver(String outputFileName) throws SAXException {
 101         super();
 102         try {
 103             bWriter = new BufferedWriter(new FileWriter(outputFileName));
 104         } catch (IOException ex) {
 105             throw new SAXException(ex);
 106         }
 107     }
 108 
 109     /**
 110      * Write In resolveEntity tag along with publicid and systemId when meet
 111      * resolveEntity event.
 112      * @throws IOException error happen when writing file.
 113      */
 114     @Override
 115     public InputSource resolveEntity(String publicid, String systemid)
 116             throws SAXException, IOException {
 117         String str = "In resolveEntity.." + " " + publicid + " " + getFileName(systemid);
 118         bWriter.write( str, 0,str.length());
 119         bWriter.newLine();
 120         return super.resolveEntity(publicid, systemid);
 121     }
 122 
 123     /**
 124      * Flush the content and close the file.
 125      * @throws IOException error happen when writing file or closing file.
 126      */
 127     @Override
 128     public void close() throws IOException {
 129         bWriter.flush();
 130         bWriter.close();
 131     }
 132     
 133     private String getFileName(String systemid) {
 134         try {
 135             return Paths.get(new URI(systemid)).getFileName().toString();
 136         } catch (URISyntaxException e) {
 137             throw new RuntimeException(e);
 138         }
 139     }
 140 }