/* * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package javax.xml.parsers.ptests; import static jaxp.library.JAXPTestUtilities.ERROR_MSG_HEADER; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import static jaxp.library.JAXPTestUtilities.FILE_SEP; import org.xml.sax.Attributes; import org.xml.sax.Locator; import org.xml.sax.SAXParseException; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.LocatorImpl; /** * Utility interface which includes final variables of XML, golden file * directories. */ interface TestUtils { /** * XML source file directory. */ final String XML_DIR = System.getProperty("test.src") + FILE_SEP + TestUtils.class.getPackage().getName().replaceAll("[.]", FILE_SEP) + FILE_SEP + ".." + FILE_SEP + "xmlfiles" + FILE_SEP; /** * Golden validation files directory. */ final String GOLDEN_DIR = XML_DIR + "out" + FILE_SEP; } /** * Customized DefaultHandler which writes output document when methods are * called by Transformer. Test may use output document to compare with golden * file for verification. */ class MyCHandler extends DefaultHandler implements AutoCloseable { private final BufferedWriter bWriter; private final Locator locator = new LocatorImpl(); private MyCHandler(File file) throws IOException { bWriter = new BufferedWriter(new FileWriter(file)); } public static MyCHandler newInstance(File file) throws IOException { MyCHandler handler = new MyCHandler(file); return handler; } @Override public void characters(char[] ch, int start, int length) { String s = new String(ch, start, length); String str = String.format("characters...length is:%d\n<%s>", s.length(), s); try { bWriter.write(str, 0, str.length()); bWriter.newLine(); } catch (IOException e) { throw new RuntimeException(ERROR_MSG_HEADER, e); } } @Override public void endDocument() { String str = "endDocument..."; try { bWriter.write(str, 0, str.length()); bWriter.newLine(); bWriter.flush(); bWriter.close(); } catch (IOException e) { throw new RuntimeException(ERROR_MSG_HEADER, e); } } @Override public void endElement(String namespaceURI, String localName, String qName) { String str = String.format("endElement...\nnamespaceURI: <%s> localName: <%s> qName: <%s>", namespaceURI, localName, qName); try { bWriter.write(str, 0, str.length()); bWriter.newLine(); } catch (IOException e) { throw new RuntimeException(ERROR_MSG_HEADER, e); } } @Override public void endPrefixMapping(String prefix) { String str = String.format("endPrefixMapping...\nprefix: <%s>", prefix); try { bWriter.write(str, 0, str.length()); bWriter.newLine(); } catch (IOException e) { throw new RuntimeException(ERROR_MSG_HEADER, e); } } @Override public void ignorableWhitespace(char[] ch, int start, int length) { String s = new String(ch, start, length); String str = String.format("ignorableWhitespace...\n%s ignorable white space string length: %d", s, s.length()); try { bWriter.write(str, 0, str.length()); bWriter.newLine(); } catch (IOException e) { throw new RuntimeException(ERROR_MSG_HEADER, e); } } @Override public void processingInstruction(String target, String data) { String str = String.format("processingInstruction...target:<%s> data: <%s>", target, data); try { bWriter.write(str, 0, str.length()); bWriter.newLine(); } catch (IOException e) { throw new RuntimeException(ERROR_MSG_HEADER, e); } } @Override public void skippedEntity(String name) { String str = String.format("skippedEntity...\nname: <%s>", name); try { bWriter.write(str, 0, str.length()); bWriter.newLine(); } catch (IOException e) { throw new RuntimeException(ERROR_MSG_HEADER, e); } } @Override public void startDocument() { String str = "startDocument..."; try { bWriter.write(str, 0, str.length()); bWriter.newLine(); } catch (IOException e) { throw new RuntimeException(ERROR_MSG_HEADER, e); } } @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) { String str = String.format("startElement...\nnamespaceURI: <%s> localName: <%s> qName: <%s> Number of Attributes: <%d> Line# <%d>", namespaceURI, localName, qName, atts.getLength(), locator.getLineNumber()); try { bWriter.write(str, 0, str.length()); bWriter.newLine(); } catch (IOException e) { throw new RuntimeException(ERROR_MSG_HEADER, e); } } @Override public void startPrefixMapping(String prefix, String uri) { String str = String.format("startPrefixMapping...\nprefix: <%s> uri: <%s>", prefix, uri); try { bWriter.write(str, 0, str.length()); bWriter.newLine(); } catch (IOException e) { throw new RuntimeException(ERROR_MSG_HEADER, e); } } @Override public void close() throws IOException { if (bWriter != null) bWriter.close(); } } /** * Customized DefaultHandler used for SAXParseException testing. */ class MyErrorHandler extends DefaultHandler { private volatile boolean errorOccured; private MyErrorHandler() { errorOccured = false; } /** * Factory method to create a MyErrorHandler instance. * @return a MyErrorHandler instance. */ public static MyErrorHandler newInstance() { return new MyErrorHandler(); } /** * Receive notification of a recoverable error. * @param e a recoverable parser exception error. */ @Override public void error(SAXParseException e) { errorOccured = true; } /** * * @param e a parser warning event. */ @Override public void warning(SAXParseException e) { errorOccured = true; } @Override public void fatalError(SAXParseException e) { errorOccured = true; } /** * Has any event been received. * * @return true if any event has been received. * false if no event has been received. */ public boolean isErrorOccured() { return errorOccured; } }