jaxp/test/javax/xml/jaxp/functional/javax/xml/parsers/ptests/TestUtils.java
changeset 28729 062f02fa1d17
parent 28728 0b2795366c28
parent 28718 6bb984acf342
child 28730 106944a21769
equal deleted inserted replaced
28728:0b2795366c28 28729:062f02fa1d17
     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 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 }