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 
  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 }