1 /*
   2  * Copyright (c) 2014, 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 
  24 package sax;
  25 
  26 import java.io.File;
  27 import java.io.FileInputStream;
  28 import java.io.FileNotFoundException;
  29 import java.io.FileOutputStream;
  30 import java.io.IOException;
  31 import java.io.InputStream;
  32 import java.io.OutputStream;
  33 import java.io.OutputStreamWriter;
  34 import java.io.UnsupportedEncodingException;
  35 import java.io.Writer;
  36 
  37 import javax.xml.parsers.ParserConfigurationException;
  38 import javax.xml.parsers.SAXParser;
  39 import javax.xml.parsers.SAXParserFactory;
  40 
  41 import jaxp.library.JAXPTestUtilities;
  42 
  43 import org.testng.Assert;
  44 import org.testng.annotations.Listeners;
  45 import org.testng.annotations.Test;
  46 import org.xml.sax.Attributes;
  47 import org.xml.sax.ErrorHandler;
  48 import org.xml.sax.SAXException;
  49 import org.xml.sax.SAXParseException;
  50 import org.xml.sax.XMLReader;
  51 import org.xml.sax.ext.DefaultHandler2;
  52 
  53 /*
  54  * @bug 7057778
  55  * @summary Test the file can be deleted after SAXParser.parse(File, DefaultHandler).
  56  */
  57 @Listeners({jaxp.library.FilePolicy.class})
  58 public class Bug7057778Test {
  59 
  60     static final String xml = "Bug7057778.xml";
  61     static final String xml1 = "Bug7057778_1.xml";
  62 
  63     @Test
  64     public void testParse() {
  65         File src = new File(getClass().getResource(xml).getFile());
  66         File dst = new File(xml1);
  67         try {
  68             copyFile(src, dst);
  69             SAXParserFactory spf = SAXParserFactory.newInstance();
  70             SAXParser parser = spf.newSAXParser();
  71             XMLReader xmlReader = parser.getXMLReader();
  72             xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", new MyHandler1());
  73             parser.parse(dst, new MyHandler1());
  74         } catch (SAXException ex) {
  75             ex.printStackTrace();
  76         } catch (IOException ex) {
  77             // shouldn't happen
  78         } catch (ParserConfigurationException ex) {
  79             // shouldn't happen
  80         }
  81         if (dst != null) {
  82             if (dst.delete()) {
  83                 System.out.println("Delete: OK");
  84             } else {
  85                 System.out.println("Delete: NG");
  86                 Assert.fail("Error: denied to delete the file");
  87             }
  88         }
  89 
  90     }
  91 
  92     private void copyFile(File src, File dst) throws FileNotFoundException, IOException {
  93         InputStream in = new FileInputStream(src);
  94         OutputStream out = new FileOutputStream(dst);
  95         // Transfer bytes
  96         byte[] buf = new byte[1024];
  97         int len;
  98         while ((len = in.read(buf)) > 0) {
  99             out.write(buf, 0, len);
 100         }
 101         in.close();
 102         out.close();
 103     }
 104 
 105     public class MyHandler1 extends DefaultHandler2 implements ErrorHandler {
 106         private Writer out;
 107 
 108         StringBuffer textBuffer;
 109         private String indentString = "    "; // Amount to indent
 110         private int indentLevel = 0;
 111 
 112         public MyHandler1() {
 113             try {
 114                 out = new OutputStreamWriter(System.out, "UTF8");
 115             } catch (UnsupportedEncodingException ex) {
 116                 ex.printStackTrace();
 117             }
 118         }
 119 
 120         public void startDocument() throws SAXException {
 121         }
 122 
 123         public void endDocument() throws SAXException {
 124         }
 125 
 126         public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
 127             try {
 128                 System.out.println("uri: " + uri);
 129                 System.out.println("localName: " + localName);
 130                 System.out.println("qName: " + qName);
 131             } catch (Exception e) {
 132                 throw new SAXException(e);
 133             }
 134 
 135         }
 136 
 137         public void endElement(String uri, String localName, String qName) throws SAXException {
 138         }
 139 
 140         public void characters(char ch[], int start, int length) throws SAXException {
 141         }
 142 
 143         public void comment(char[] ch, int start, int length) {
 144             String text = new String(ch, start, length);
 145             // System.out.println(text);
 146             try {
 147                 nl();
 148                 emit("COMMENT: " + text);
 149             } catch (Exception e) {
 150                 e.printStackTrace();
 151             }
 152         }
 153 
 154         public void error(SAXParseException exception) {
 155             exception.printStackTrace();
 156         }
 157 
 158         public void fatalError(SAXParseException exception) {
 159             exception.printStackTrace();
 160         }
 161 
 162         public void warning(SAXParseException exception) {
 163             exception.printStackTrace();
 164         }
 165 
 166         // Wrap I/O exceptions in SAX exceptions, to
 167         // suit handler signature requirements
 168         private void emit(String s) throws SAXException {
 169             try {
 170                 out.write(s);
 171                 out.flush();
 172             } catch (IOException e) {
 173                 throw new SAXException("I/O error", e);
 174             }
 175         }
 176 
 177         // Start a new line
 178         // and indent the next line appropriately
 179         private void nl() throws SAXException {
 180             String lineEnd = System.getProperty("line.separator");
 181 
 182             try {
 183                 out.write(lineEnd);
 184 
 185                 for (int i = 0; i < indentLevel; i++)
 186                     out.write(indentString);
 187             } catch (IOException e) {
 188                 throw new SAXException("I/O error", e);
 189             }
 190         }
 191 
 192     }
 193 }