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