Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.xml » stream » utils » [javadoc | source]

    1   /*   Copyright 2004 The Apache Software Foundation
    2    *
    3    *   Licensed under the Apache License, Version 2.0 (the "License");
    4    *   you may not use this file except in compliance with the License.
    5    *   You may obtain a copy of the License at
    6    *
    7    *       http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    *   Unless required by applicable law or agreed to in writing, software
   10    *   distributed under the License is distributed on an "AS IS" BASIS,
   11    *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12    *   See the License for the specific language governing permissions and
   13    *  limitations under the License.
   14    */
   15   
   16   package org.apache.xmlbeans.xml.stream.utils;
   17   
   18   /**
   19    * The interface implemented by NestedException, NestedError, and
   20    * NestedRuntimeException largely so Util can provide a standard
   21    * implementation of toString() and printStackTrace()
   22    *
   23    * @deprecated use JDK 1.4 style nested throwables where possible.
   24    *
   25    */
   26   
   27   import java.io.PrintWriter;
   28   import java.io.PrintStream;
   29   import java.lang.reflect.InvocationTargetException;
   30   
   31   public interface NestedThrowable {
   32   
   33     /** Get the nested Throwable. */
   34     Throwable getNested();
   35   
   36     /** Call super.toString(). [Kludge but necessary.] */
   37     String superToString();
   38   
   39     /** Call super.printStackTrace(). [Kludge but necessary.] */
   40     void superPrintStackTrace(PrintStream ps);
   41   
   42     /** Call super.printStackTrace(). [Kludge but necessary.] */
   43     void superPrintStackTrace(PrintWriter po);
   44   
   45     static class Util {
   46   
   47       private static String EOL = System.getProperty("line.separator");
   48   
   49       /**
   50        * Prints the exception message and its nested exception message.
   51        *
   52        * @return                 String representation of the exception
   53        */
   54       public static String toString(NestedThrowable nt) {
   55         Throwable nested = nt.getNested();
   56         if (nested == null) {
   57           return nt.superToString();
   58         } else {
   59           return nt.superToString() + " - with nested exception:" + 
   60             EOL + "[" + nestedToString(nested) + "]";
   61         }
   62       }
   63   
   64       private static String nestedToString(Throwable nested) {
   65         if (nested instanceof InvocationTargetException) {
   66           InvocationTargetException ite = (InvocationTargetException) nested;
   67           return nested.toString() + " - with target exception:" + 
   68             EOL + "[" + ite.getTargetException().toString() +
   69             "]";
   70         }
   71         return nested.toString();
   72       }
   73   
   74       /**
   75        * Prints the stack trace associated with this exception and
   76        * its nested exception.
   77        *
   78        * @param s                 PrintStream
   79        */
   80       public static void printStackTrace(NestedThrowable nt, PrintStream s) { 
   81         Throwable nested = nt.getNested();
   82         if (nested != null) {
   83           nested.printStackTrace(s);
   84           s.println("--------------- nested within: ------------------");
   85         }
   86         nt.superPrintStackTrace(s);
   87       }
   88   
   89       /**
   90        * Prints the stack trace associated with this exception and
   91        * its nested exception.
   92        *
   93        * @param w                 PrintWriter
   94        */
   95       public static void printStackTrace(NestedThrowable nt, PrintWriter w) { 
   96         Throwable nested = nt.getNested();
   97         if (nested != null) {
   98           nested.printStackTrace(w);
   99           w.println("--------------- nested within: ------------------");
  100         }
  101         nt.superPrintStackTrace(w);
  102       }
  103     }
  104   
  105   }

Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.xml » stream » utils » [javadoc | source]