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 variables of xml, golden file 43 * directories. 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 /** 51 * Customized DefaultHandler which writes output document when methods are 52 * called by Transformer. Test may use output document to compare with golden 53 * file for verification. 54 */ 55 class MyCHandler extends DefaultHandler { 56 57 private final BufferedWriter bWriter; 58 private final Locator locator = new LocatorImpl(); 59 60 private MyCHandler(File file) throws IOException { 61 bWriter = new BufferedWriter(new FileWriter(file)); 62 } 63 64 public static MyCHandler newInstance(File file) throws IOException { 65 MyCHandler handler = new MyCHandler(file); 66 return handler; 67 } 68 69 public void characters(char[] ch, int start, int length) { 70 String s = new String(ch, start, length); 71 String str = String.format("characters...length is:%d\n<%s>", s.length(), s); 72 try { 73 bWriter.write(str, 0, str.length()); 74 bWriter.newLine(); 75 } catch (IOException e) { 76 throw new RuntimeException(ERROR_MSG_HEADER, e); 77 } 78 } 79 80 public void endDocument() { 81 String str = "endDocument..."; 82 try { 83 bWriter.write(str, 0, str.length()); 84 bWriter.newLine(); 85 bWriter.flush(); 86 bWriter.close(); 87 88 } catch (IOException e) { 89 throw new RuntimeException(ERROR_MSG_HEADER, e); 90 } 91 } 92 93 public void endElement(String namespaceURI, String localName, String qName) { 94 String str = String.format("endElement...\nnamespaceURI: <%s> localName: <%s> qName: <%s>", namespaceURI, localName, qName); 95 try { 96 bWriter.write(str, 0, str.length()); 97 bWriter.newLine(); 98 } catch (IOException e) { 99 throw new RuntimeException(ERROR_MSG_HEADER, e); 100 } 101 } 102 103 public void endPrefixMapping(String prefix) { 104 String str = String.format("endPrefixMapping...\nprefix: <%s>", prefix); 105 try { 106 bWriter.write(str, 0, str.length()); 107 bWriter.newLine(); 108 } catch (IOException e) { 109 throw new RuntimeException(ERROR_MSG_HEADER, e); 110 } 111 } 112 113 public void ignorableWhitespace(char[] ch, int start, int length) { 114 String s = new String(ch, start, length); 115 String str = String.format("ignorableWhitespace...\n%s ignorable white space string length: %d", s, s.length()); 116 try { 117 bWriter.write(str, 0, str.length()); 118 bWriter.newLine(); 119 } catch (IOException e) { 120 throw new RuntimeException(ERROR_MSG_HEADER, e); 121 } 122 } 123 124 public void processingInstruction(String target, String data) { 125 String str = String.format("processingInstruction...target:<%s> data: <%s>", target, data); 126 try { 127 bWriter.write(str, 0, str.length()); 128 bWriter.newLine(); 129 } catch (IOException e) { 130 throw new RuntimeException(ERROR_MSG_HEADER, e); 131 } 132 } 133 134 public void skippedEntity(String name) { 135 String str = String.format("skippedEntity...\nname: <%s>", name); 136 try { 137 bWriter.write(str, 0, str.length()); 138 bWriter.newLine(); 139 } catch (IOException e) { 140 throw new RuntimeException(ERROR_MSG_HEADER, e); 141 } 142 } 143 144 public void startDocument() { 145 String str = "startDocument..."; 146 try { 147 bWriter.write(str, 0, str.length()); 148 bWriter.newLine(); 149 } catch (IOException e) { 150 throw new RuntimeException(ERROR_MSG_HEADER, e); 151 } 152 } 153 154 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) { 155 String str = String.format("startElement...\nnamespaceURI: <%s> localName: <%s> qName: <%s> Number of Attributes: <%d> Line# <%d>", namespaceURI, 156 localName, qName, atts.getLength(), locator.getLineNumber()); 157 try { 158 bWriter.write(str, 0, str.length()); 159 bWriter.newLine(); 160 } catch (IOException e) { 161 throw new RuntimeException(ERROR_MSG_HEADER, e); 162 } 163 } 164 165 public void startPrefixMapping(String prefix, String uri) { 166 String str = String.format("startPrefixMapping...\nprefix: <%s> uri: <%s>", prefix, uri); 167 try { 168 bWriter.write(str, 0, str.length()); 169 bWriter.newLine(); 170 } catch (IOException e) { 171 throw new RuntimeException(ERROR_MSG_HEADER, e); 172 } 173 } 174 } 175 176 /** 177 * Customized DefaultHandler used for SAXParseException testing. 178 */ 179 class MyErrorHandler extends DefaultHandler { 180 boolean errorOccured = false; 181 182 private MyErrorHandler() { 183 } 184 185 public static MyErrorHandler newInstance() { 186 return new MyErrorHandler(); 187 } 188 189 public void error(SAXParseException e) { 190 errorOccured = true; 191 } 192 193 public void warning(SAXParseException e) { 194 errorOccured = true; 195 } 196 197 public void fatalError(SAXParseException e) { 198 errorOccured = true; 199 } 200 } | 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 27 import java.io.BufferedWriter; 28 import java.io.File; 29 import java.io.FileWriter; 30 import java.io.IOException; 31 import static jaxp.library.JAXPTestUtilities.FILE_SEP; 32 33 import org.xml.sax.Attributes; 34 import org.xml.sax.Locator; 35 import org.xml.sax.SAXParseException; 36 import org.xml.sax.helpers.DefaultHandler; 37 import org.xml.sax.helpers.LocatorImpl; 38 39 /** 40 * Utility interface which includes final variables of XML, golden file 41 * directories. 42 */ 43 interface TestUtils { 44 /** 45 * XML source file directory. 46 */ 47 final String XML_DIR = System.getProperty("test.src") + FILE_SEP + 48 TestUtils.class.getPackage().getName().replaceAll("[.]", FILE_SEP) 49 + FILE_SEP + ".." + FILE_SEP + "xmlfiles" + FILE_SEP; 50 51 /** 52 * Golden validation files directory. 53 */ 54 final String GOLDEN_DIR = XML_DIR + "out" + FILE_SEP; 55 } 56 57 /** 58 * Customized DefaultHandler which writes output document when methods are 59 * called by Transformer. Test may use output document to compare with golden 60 * file for verification. 61 */ 62 class MyCHandler extends DefaultHandler implements AutoCloseable { 63 64 private final BufferedWriter bWriter; 65 private final Locator locator = new LocatorImpl(); 66 67 private MyCHandler(File file) throws IOException { 68 bWriter = new BufferedWriter(new FileWriter(file)); 69 } 70 71 public static MyCHandler newInstance(File file) throws IOException { 72 MyCHandler handler = new MyCHandler(file); 73 return handler; 74 } 75 76 @Override 77 public void characters(char[] ch, int start, int length) { 78 String s = new String(ch, start, length); 79 String str = String.format("characters...length is:%d\n<%s>", s.length(), s); 80 try { 81 bWriter.write(str, 0, str.length()); 82 bWriter.newLine(); 83 } catch (IOException e) { 84 throw new RuntimeException(ERROR_MSG_HEADER, e); 85 } 86 } 87 88 @Override 89 public void endDocument() { 90 String str = "endDocument..."; 91 try { 92 bWriter.write(str, 0, str.length()); 93 bWriter.newLine(); 94 bWriter.flush(); 95 bWriter.close(); 96 97 } catch (IOException e) { 98 throw new RuntimeException(ERROR_MSG_HEADER, e); 99 } 100 } 101 102 @Override 103 public void endElement(String namespaceURI, String localName, String qName) { 104 String str = String.format("endElement...\nnamespaceURI: <%s> localName: <%s> qName: <%s>", namespaceURI, localName, qName); 105 try { 106 bWriter.write(str, 0, str.length()); 107 bWriter.newLine(); 108 } catch (IOException e) { 109 throw new RuntimeException(ERROR_MSG_HEADER, e); 110 } 111 } 112 113 @Override 114 public void endPrefixMapping(String prefix) { 115 String str = String.format("endPrefixMapping...\nprefix: <%s>", prefix); 116 try { 117 bWriter.write(str, 0, str.length()); 118 bWriter.newLine(); 119 } catch (IOException e) { 120 throw new RuntimeException(ERROR_MSG_HEADER, e); 121 } 122 } 123 124 @Override 125 public void ignorableWhitespace(char[] ch, int start, int length) { 126 String s = new String(ch, start, length); 127 String str = String.format("ignorableWhitespace...\n%s ignorable white space string length: %d", s, s.length()); 128 try { 129 bWriter.write(str, 0, str.length()); 130 bWriter.newLine(); 131 } catch (IOException e) { 132 throw new RuntimeException(ERROR_MSG_HEADER, e); 133 } 134 } 135 136 @Override 137 public void processingInstruction(String target, String data) { 138 String str = String.format("processingInstruction...target:<%s> data: <%s>", target, data); 139 try { 140 bWriter.write(str, 0, str.length()); 141 bWriter.newLine(); 142 } catch (IOException e) { 143 throw new RuntimeException(ERROR_MSG_HEADER, e); 144 } 145 } 146 147 @Override 148 public void skippedEntity(String name) { 149 String str = String.format("skippedEntity...\nname: <%s>", name); 150 try { 151 bWriter.write(str, 0, str.length()); 152 bWriter.newLine(); 153 } catch (IOException e) { 154 throw new RuntimeException(ERROR_MSG_HEADER, e); 155 } 156 } 157 158 @Override 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 @Override 170 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) { 171 String str = String.format("startElement...\nnamespaceURI: <%s> localName: <%s> qName: <%s> Number of Attributes: <%d> Line# <%d>", namespaceURI, 172 localName, qName, atts.getLength(), locator.getLineNumber()); 173 try { 174 bWriter.write(str, 0, str.length()); 175 bWriter.newLine(); 176 } catch (IOException e) { 177 throw new RuntimeException(ERROR_MSG_HEADER, e); 178 } 179 } 180 181 @Override 182 public void startPrefixMapping(String prefix, String uri) { 183 String str = String.format("startPrefixMapping...\nprefix: <%s> uri: <%s>", prefix, uri); 184 try { 185 bWriter.write(str, 0, str.length()); 186 bWriter.newLine(); 187 } catch (IOException e) { 188 throw new RuntimeException(ERROR_MSG_HEADER, e); 189 } 190 } 191 192 @Override 193 public void close() throws IOException { 194 if (bWriter != null) 195 bWriter.close(); 196 } 197 } 198 199 /** 200 * Customized DefaultHandler used for SAXParseException testing. 201 */ 202 class MyErrorHandler extends DefaultHandler { 203 private volatile boolean errorOccured; 204 205 private MyErrorHandler() { 206 errorOccured = false; 207 } 208 209 /** 210 * Factory method to create a MyErrorHandler instance. 211 * @return a MyErrorHandler instance. 212 */ 213 public static MyErrorHandler newInstance() { 214 return new MyErrorHandler(); 215 } 216 217 /** 218 * Receive notification of a recoverable error. 219 * @param e a recoverable parser exception error. 220 */ 221 @Override 222 public void error(SAXParseException e) { 223 errorOccured = true; 224 } 225 226 /** 227 * 228 * @param e a parser warning event. 229 */ 230 @Override 231 public void warning(SAXParseException e) { 232 errorOccured = true; 233 } 234 235 @Override 236 public void fatalError(SAXParseException e) { 237 errorOccured = true; 238 } 239 240 /** 241 * Has any event been received. 242 * 243 * @return true if any event has been received. 244 * false if no event has been received. 245 */ 246 public boolean isErrorOccured() { 247 return errorOccured; 248 } 249 } |