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 @Listeners({jaxp.library.BasePolicy.class}) 106 public class MyHandler1 extends DefaultHandler2 implements ErrorHandler { 107 private Writer out; 108 109 StringBuffer textBuffer; 110 private String indentString = " "; // Amount to indent 111 private int indentLevel = 0; 112 113 public MyHandler1() { 114 try { 115 out = new OutputStreamWriter(System.out, "UTF8"); 116 } catch (UnsupportedEncodingException ex) { 117 ex.printStackTrace(); 118 } 119 } 120 121 public void startDocument() throws SAXException { 122 } 123 124 public void endDocument() throws SAXException { 125 } 126 127 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 128 try { 129 System.out.println("uri: " + uri); 130 System.out.println("localName: " + localName); 131 System.out.println("qName: " + qName); 132 } catch (Exception e) { 133 throw new SAXException(e); 134 } 135 136 } 137 138 public void endElement(String uri, String localName, String qName) throws SAXException { 139 } 140 141 public void characters(char ch[], int start, int length) throws SAXException { 142 } 143 144 public void comment(char[] ch, int start, int length) { 145 String text = new String(ch, start, length); 146 // System.out.println(text); 147 try { 148 nl(); 149 emit("COMMENT: " + text); 150 } catch (Exception e) { 151 e.printStackTrace(); 152 } 153 } 154 155 public void error(SAXParseException exception) { 156 exception.printStackTrace(); 157 } 158 159 public void fatalError(SAXParseException exception) { 160 exception.printStackTrace(); 161 } 162 163 public void warning(SAXParseException exception) { 164 exception.printStackTrace(); 165 } 166 167 // Wrap I/O exceptions in SAX exceptions, to 168 // suit handler signature requirements 169 private void emit(String s) throws SAXException { 170 try { 171 out.write(s); 172 out.flush(); 173 } catch (IOException e) { 174 throw new SAXException("I/O error", e); 175 } 176 } 177 178 // Start a new line 179 // and indent the next line appropriately 180 private void nl() throws SAXException { 181 String lineEnd = System.getProperty("line.separator"); 182 183 try { 184 out.write(lineEnd); 185 186 for (int i = 0; i < indentLevel; i++) 187 out.write(indentString); 188 } catch (IOException e) { 189 throw new SAXException("I/O error", e); 190 } 191 } 192 193 } 194 }