< prev index next >

test/javax/xml/jaxp/libs/javax/xml/parsers/ptests/TestUtils.java

Print this page

        

@@ -21,40 +21,47 @@
  * questions.
  */
 package javax.xml.parsers.ptests;
 
 import static jaxp.library.JAXPTestUtilities.ERROR_MSG_HEADER;
-import static jaxp.library.JAXPTestUtilities.FILE_SEP;
 
 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.StandardCopyOption;
+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
+ * Utility interface which includes final variables of XML, golden file
  * directories.
  */
 interface TestUtils {
-    final String XML_DIR = System.getProperty("test.src", ".") + FILE_SEP + "javax/xml/parsers/xmlfiles";
-    final String GOLDEN_DIR = XML_DIR + FILE_SEP + "out";
+    /**
+     * 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 {
+class MyCHandler extends DefaultHandler implements AutoCloseable {
 
     private final BufferedWriter bWriter;
     private final Locator locator = new LocatorImpl();
 
     private MyCHandler(File file) throws IOException {

@@ -64,10 +71,11 @@
     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());

@@ -75,10 +83,11 @@
         } 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();

@@ -88,30 +97,33 @@
         } 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());

@@ -119,40 +131,44 @@
         } 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());

@@ -160,41 +176,74 @@
         } 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 {
-    boolean errorOccured = false;
+    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;
+    }
 }
< prev index next >