1 /* 2 * Copyright (c) 2014, 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 javax.xml.parsers.ptests; 24 25 import static jaxp.library.JAXPTestUtilities.ERROR_MSG_HEADER; 26 import static jaxp.library.JAXPTestUtilities.FILE_SEP; 27 28 import java.io.BufferedWriter; 29 import java.io.File; 30 import java.io.FileWriter; 31 import java.io.IOException; 32 import java.nio.file.Files; 33 import java.nio.file.StandardCopyOption; 34 35 import org.xml.sax.Attributes; 36 import org.xml.sax.Locator; 37 import org.xml.sax.SAXParseException; 38 import org.xml.sax.helpers.DefaultHandler; 39 import org.xml.sax.helpers.LocatorImpl; 40 41 /** 42 * Utility interface which includes final variable of xml, golden file 43 * directories and copyFiles function. 44 */ 45 interface TestUtils { 46 final String XML_DIR = System.getProperty("test.src", ".") + FILE_SEP + "javax/xml/parsers/xmlfiles"; 47 final String GOLDEN_DIR = XML_DIR + FILE_SEP + "out"; 48 49 /** 50 * This method is called by tests to prepare xml files for testing. 51 */ 52 static void copyFiles(String srcDirName, String dstDirName, String... fileNames) throws IOException { 53 for (String fileName : fileNames) { 54 File src = new File(srcDirName, fileName); 55 File dstDir = new File(dstDirName); 56 if (!dstDir.exists()) { 57 dstDir.mkdirs(); 58 } 59 File dst = new File(dstDirName, fileName); 60 Files.copy(src.toPath(), dst.toPath(), StandardCopyOption.REPLACE_EXISTING); 61 } 62 } 63 } 64 65 /** 66 * Customized DefaultHandler which writes output document when methods are 67 * called by Transformer. Test may use output document to compare with golden 68 * file for verification. 69 */ 70 class MyCHandler extends DefaultHandler { 71 72 private final BufferedWriter bWriter; 73 private final Locator locator = new LocatorImpl(); 74 75 private MyCHandler(File file) throws IOException { 76 bWriter = new BufferedWriter(new FileWriter(file)); 77 } 78 79 public static MyCHandler newInstance(File file) throws IOException { 80 MyCHandler handler = new MyCHandler(file); 81 return handler; 82 } 83 84 public void characters(char[] ch, int start, int length) { 85 String s = new String(ch, start, length); 86 String str = String.format("characters...length is:%d\n<%s>", s.length(), s); 87 try { 88 bWriter.write(str, 0, str.length()); 89 bWriter.newLine(); 90 } catch (IOException e) { 91 throw new RuntimeException(ERROR_MSG_HEADER, e); 92 } 93 } 94 95 public void endDocument() { 96 String str = "endDocument..."; 97 try { 98 bWriter.write(str, 0, str.length()); 99 bWriter.newLine(); 100 bWriter.flush(); 101 bWriter.close(); 102 103 } catch (IOException e) { 104 throw new RuntimeException(ERROR_MSG_HEADER, e); 105 } 106 } 107 108 public void endElement(String namespaceURI, String localName, String qName) { 109 String str = String.format("endElement...\nnamespaceURI: <%s> localName: <%s> qName: <%s>", namespaceURI, localName, qName); 110 try { 111 bWriter.write(str, 0, str.length()); 112 bWriter.newLine(); 113 } catch (IOException e) { 114 throw new RuntimeException(ERROR_MSG_HEADER, e); 115 } 116 } 117 118 public void endPrefixMapping(String prefix) { 119 String str = String.format("endPrefixMapping...\nprefix: <%s>", prefix); 120 try { 121 bWriter.write(str, 0, str.length()); 122 bWriter.newLine(); 123 } catch (IOException e) { 124 throw new RuntimeException(ERROR_MSG_HEADER, e); 125 } 126 } 127 128 public void ignorableWhitespace(char[] ch, int start, int length) { 129 String s = new String(ch, start, length); 130 String str = String.format("ignorableWhitespace...\n%s ignorable white space string length: %d", s, s.length()); 131 try { 132 bWriter.write(str, 0, str.length()); 133 bWriter.newLine(); 134 } catch (IOException e) { 135 throw new RuntimeException(ERROR_MSG_HEADER, e); 136 } 137 } 138 139 public void processingInstruction(String target, String data) { 140 String str = String.format("processingInstruction...target:<%s> data: <%s>", target, data); 141 try { 142 bWriter.write(str, 0, str.length()); 143 bWriter.newLine(); 144 } catch (IOException e) { 145 throw new RuntimeException(ERROR_MSG_HEADER, e); 146 } 147 } 148 149 public void skippedEntity(String name) { 150 String str = String.format("skippedEntity...\nname: <%s>", name); 151 try { 152 bWriter.write(str, 0, str.length()); 153 bWriter.newLine(); 154 } catch (IOException e) { 155 throw new RuntimeException(ERROR_MSG_HEADER, e); 156 } 157 } 158 159 public void startDocument() { 160 String str = "startDocument..."; 161 try { 162 bWriter.write(str, 0, str.length()); 163 bWriter.newLine(); 164 } catch (IOException e) { 165 throw new RuntimeException(ERROR_MSG_HEADER, e); 166 } 167 } 168 169 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) { 170 String str = String.format("startElement...\nnamespaceURI: <%s> localName: <%s> qName: <%s> Number of Attributes: <%d> Line# <%d>", namespaceURI, 171 localName, qName, atts.getLength(), locator.getLineNumber()); 172 try { 173 bWriter.write(str, 0, str.length()); 174 bWriter.newLine(); 175 } catch (IOException e) { 176 throw new RuntimeException(ERROR_MSG_HEADER, e); 177 } 178 } 179 180 public void startPrefixMapping(String prefix, String uri) { 181 String str = String.format("startPrefixMapping...\nprefix: <%s> uri: <%s>", prefix, uri); 182 try { 183 bWriter.write(str, 0, str.length()); 184 bWriter.newLine(); 185 } catch (IOException e) { 186 throw new RuntimeException(ERROR_MSG_HEADER, e); 187 } 188 } 189 } 190 191 /** 192 * Customized DefaultHandler used for SAXParseException testing. 193 */ 194 class MyErrorHandler extends DefaultHandler { 195 boolean errorOccured = false; 196 197 private MyErrorHandler() { 198 } 199 200 public static MyErrorHandler newInstance() { 201 return new MyErrorHandler(); 202 } 203 204 public void error(SAXParseException e) { 205 errorOccured = true; 206 } 207 208 public void warning(SAXParseException e) { 209 errorOccured = true; 210 } 211 212 public void fatalError(SAXParseException e) { 213 errorOccured = true; 214 } 215 }