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