[Mulgara-svn] r474 - branches/nw-interface/src/jar/itql/java/org/mulgara/itql

pag at mulgara.org pag at mulgara.org
Thu Oct 11 17:12:21 UTC 2007


Author: pag
Date: 2007-10-11 12:12:20 -0500 (Thu, 11 Oct 2007)
New Revision: 474

Added:
   branches/nw-interface/src/jar/itql/java/org/mulgara/itql/ItqlUtil.java
Modified:
   branches/nw-interface/src/jar/itql/java/org/mulgara/itql/ItqlInterpreter.java
Log:
Moved getCause method into a static context in a util class, so that ItqlInterpreter is not required to get to this method.

Modified: branches/nw-interface/src/jar/itql/java/org/mulgara/itql/ItqlInterpreter.java
===================================================================
--- branches/nw-interface/src/jar/itql/java/org/mulgara/itql/ItqlInterpreter.java	2007-10-11 17:10:16 UTC (rev 473)
+++ branches/nw-interface/src/jar/itql/java/org/mulgara/itql/ItqlInterpreter.java	2007-10-11 17:12:20 UTC (rev 474)
@@ -351,78 +351,6 @@
   // toNode()
 
   /**
-   * Returns the Exception Chain in a pretty fashion.
-   *
-   * @param e the throwable exception
-   * @param preferredDepth the preferred depth to go into the exception to
-   *   retrieve the root cause.  A depth of zero will chain all exceptions
-   *   together.
-   * @return String the Exception Chain in a pretty fashion
-   */
-  public String getCause(Throwable e, int preferredDepth) {
-
-    // Keep getting the cause of the message until we reach preferred depth or
-    // null cause.
-    Throwable preferredException = e;
-    int index = 0;
-    while ( (preferredException != null) && (index != preferredDepth)) {
-
-      // Pre-check next exception and increment index if it's not null.
-      if (preferredException.getCause() != null) {
-
-        index++;
-      }
-
-      // Get next exception
-      preferredException = preferredException.getCause();
-    }
-
-    // If the preferredException is not null.
-    if (preferredException != null) {
-
-      e = preferredException;
-    }
-
-    // get the exception's message
-    String message = e.getMessage();
-
-    // we don't want nulls
-    if (message == null) {
-
-      message = "";
-    }
-
-    // end if
-    // get the cause of the exception
-    Throwable cause = e.getCause();
-
-    // decend into it if we can
-    if (cause != null) {
-
-      // get the cause's message
-      String causeMsg = this.getCause(cause, 0);
-
-      // only add the cause's message if there was one
-      if (causeMsg != null) {
-
-        // format the class name
-        String exceptionClassName = cause.getClass().getName();
-        exceptionClassName =
-            exceptionClassName.substring(exceptionClassName.lastIndexOf('.') +
-                                         1);
-        message += (EOL + "Caused by: (" + exceptionClassName + ") " +
-                    causeMsg);
-      }
-
-      // end if
-    }
-
-    // end if
-    // return the message
-    return message;
-  }
-
-  /**
    * Returns the error of the last query. Methods overriding {@link
    * org.mulgara.itql.analysis.DepthFirstAdapter} are expected to set a
    * results message, even if that message is null.  The is for APIs, logging
@@ -520,7 +448,7 @@
         // let the user know the problem
         this.setLastError(pe);
         this.setLastAnswer(null);
-        this.setLastMessage("Syntax error " + EOL + this.getCause(pe, 2));
+        this.setLastMessage("Syntax error " + EOL + ItqlUtil.getCause(pe, 2));
         flush();
         throw new MulgaraParserException(pe);
       } catch (LexerException le) {
@@ -528,7 +456,7 @@
         // let the user know the problem
         this.setLastError(le);
         this.setLastAnswer(null);
-        this.setLastMessage("Lexer exception " + EOL + this.getCause(le, 2));
+        this.setLastMessage("Lexer exception " + EOL + ItqlUtil.getCause(le, 2));
         flush();
         throw le;
       }
@@ -1095,7 +1023,7 @@
       this.setLastError(qe);
       this.setLastAnswer(null);
       this.setLastMessage("Unable to commit transaction." + EOL +
-                          this.getCause(qe, 2));
+                          ItqlUtil.getCause(qe, 2));
       logger.warn("Unable to commit transaction", qe);
     }
     catch (RuntimeException re) {
@@ -1103,7 +1031,7 @@
       // let the user know the problem
       this.setLastError(re);
       this.setLastAnswer(null);
-      this.setLastMessage("Failed to commit transaction:" + this.getCause(re, 0));
+      this.setLastMessage("Failed to commit transaction:" + ItqlUtil.getCause(re, 0));
       logger.fatal("Failed to commit transaction", re);
     }
 
@@ -1150,7 +1078,7 @@
       this.setLastError(qe);
       this.setLastAnswer(null);
       this.setLastMessage("Unable to roll back changes." + EOL +
-                          this.getCause(qe, 2));
+                          ItqlUtil.getCause(qe, 2));
       logger.warn("Unable to roll back changes", qe);
     }
     catch (RuntimeException re) {
@@ -1158,7 +1086,7 @@
       // let the user know the problem
       this.setLastError(re);
       this.setLastAnswer(null);
-      this.setLastMessage("Unable to roll back changes:" + this.getCause(re, 0));
+      this.setLastMessage("Unable to roll back changes:" + ItqlUtil.getCause(re, 0));
       logger.fatal("Failed to roll back changes", re);
     }
   }
@@ -1219,7 +1147,7 @@
       // let the user know the problem
       this.setLastError(te);
       this.setLastAnswer(null);
-      this.setLastMessage("Couldn't answer select query." + EOL + this.getCause(te, 2) + EOL + te);
+      this.setLastMessage("Couldn't answer select query." + EOL + ItqlUtil.getCause(te, 2) + EOL + te);
       logger.warn("Couldn't answer query", te);
     }
     catch (QueryException qe) {
@@ -1227,7 +1155,7 @@
       // let the user know the problem
       this.setLastError(qe);
       this.setLastAnswer(null);
-      this.setLastMessage("Couldn't answer select query." + EOL + this.getCause(qe, 2) + EOL + qe);
+      this.setLastMessage("Couldn't answer select query." + EOL + ItqlUtil.getCause(qe, 2) + EOL + qe);
       logger.warn("Couldn't answer query", qe);
     }
     catch (URISyntaxException use) {
@@ -1236,14 +1164,14 @@
       this.setLastError(use);
       this.setLastAnswer(null);
       this.setLastMessage("Couldn't answer query: Invalid resource URI.");
-      logger.warn("Invalid resource URI." + EOL + this.getCause(use, 0));
+      logger.warn("Invalid resource URI." + EOL + ItqlUtil.getCause(use, 0));
     }
     catch (RuntimeException re) {
 
       // let the user know the problem
       this.setLastError(re);
       this.setLastAnswer(null);
-      this.setLastMessage("Couldn't answer query :" + this.getCause(re, 0));
+      this.setLastMessage("Couldn't answer query :" + ItqlUtil.getCause(re, 0));
       logger.fatal("Failed to select statements", re);
     }
 
@@ -1566,7 +1494,7 @@
       this.setLastAnswer(null);
       int depth = (e instanceof QueryException) ? 2 : 0;
       this.setLastMessage("Could not run " + ruleModel + " on " + baseModel + EOL +
-                          this.getCause(e, depth));
+                          ItqlUtil.getCause(e, depth));
       if (e instanceof QueryException) {
         logger.warn("Failed to run " + ruleModel + " on " + baseModel, e);
       } else {
@@ -1638,7 +1566,7 @@
       this.setLastError(qe);
       this.setLastAnswer(null);
       this.setLastMessage("Could not create " + modelURI + EOL +
-                          this.getCause(qe, 2));
+                          ItqlUtil.getCause(qe, 2));
       logger.warn("Failed to create " + modelURI + " as type " + modelTypeURI,
                   qe);
     }
@@ -1647,7 +1575,7 @@
       // let the user know the problem
       this.setLastError(re);
       this.setLastAnswer(null);
-      this.setLastMessage("Failed to create model:" + this.getCause(re, 0));
+      this.setLastMessage("Failed to create model:" + ItqlUtil.getCause(re, 0));
       logger.fatal("Failed to create model", re);
     }
 
@@ -1699,14 +1627,14 @@
       this.setLastError(qe);
       this.setLastAnswer(null);
       this.setLastMessage("Could not remove " + resourceURI + EOL +
-                          this.getCause(qe, 2));
+                          ItqlUtil.getCause(qe, 2));
       logger.warn("Failed to remove " + resourceURI, qe);
     }
     catch (RuntimeException re) {
       // let the user know the problem
       this.setLastError(re);
       this.setLastAnswer(null);
-      this.setLastMessage("Failed to remove model:" + this.getCause(re, 0));
+      this.setLastMessage("Failed to remove model:" + ItqlUtil.getCause(re, 0));
       logger.fatal("Failed to remove model", re);
     }
 
@@ -1830,7 +1758,7 @@
       this.setLastError(qe);
       this.setLastAnswer(null);
       this.setLastMessage("Could not load " + sourceURI + " into " +
-                          destinationURI + EOL + this.getCause(qe, 2));
+                          destinationURI + EOL + ItqlUtil.getCause(qe, 2));
       logger.warn("Failed to load " + sourceURI + " into " + destinationURI,
                   qe);
     }
@@ -1839,7 +1767,7 @@
       // let the user know the problem
       this.setLastError(re);
       this.setLastAnswer(null);
-      this.setLastMessage("Failed to load statements:" + this.getCause(re, 0));
+      this.setLastMessage("Failed to load statements:" + ItqlUtil.getCause(re, 0));
       logger.fatal("Failed to load statements", re);
     }
     finally {
@@ -1932,7 +1860,7 @@
       this.setLastMessage("Syntax error in script (line " + line + "): " +
                           pe.getMessage());
       logger.warn("Unable to execute script - " + resource + EOL +
-                  this.getCause(pe, 0));
+                  ItqlUtil.getCause(pe, 0));
     }
     catch (LexerException le) {
 
@@ -1942,7 +1870,7 @@
       this.setLastMessage("Syntax error in script (line " + line + "): " +
                           le.getMessage());
       logger.warn("Unable to execute script - " + resource + EOL +
-                  this.getCause(le, 0));
+                  ItqlUtil.getCause(le, 0));
     }
     catch (MalformedURLException mue) {
 
@@ -1950,7 +1878,7 @@
       this.setLastError(mue);
       this.setLastAnswer(null);
       this.setLastMessage("Could not execute script: Invalid script URL.");
-      logger.warn("Invalid script source URL." + EOL + this.getCause(mue, 0));
+      logger.warn("Invalid script source URL." + EOL + ItqlUtil.getCause(mue, 0));
     }
     catch (Exception e) {
 
@@ -1958,9 +1886,9 @@
       this.setLastError(e);
       this.setLastAnswer(null);
       this.setLastMessage("Could not execute script." + EOL +
-                          this.getCause(e, 0));
+                          ItqlUtil.getCause(e, 0));
       logger.error("Unable to execute script - " + resource + EOL +
-                   this.getCause(e, 0));
+                   ItqlUtil.getCause(e, 0));
     }
     // try-catch
   }
@@ -2020,7 +1948,7 @@
       this.setLastError(qe);
       this.setLastAnswer(null);
       this.setLastMessage("Could not insert statements into " + resourceURI +
-                          EOL + this.getCause(qe, 2));
+                          EOL + ItqlUtil.getCause(qe, 2));
       logger.warn("Failed to insert statements into " + resourceURI, qe);
     }
     catch (URISyntaxException use) {
@@ -2030,14 +1958,14 @@
       this.setLastAnswer(null);
       this.setLastMessage("Could not insert into resource: Invalid resource " +
                           "URI.");
-      logger.warn("Invalid resource URI." + EOL + this.getCause(use, 0));
+      logger.warn("Invalid resource URI." + EOL + ItqlUtil.getCause(use, 0));
     }
     catch (RuntimeException re) {
 
       // let the user know the problem
       this.setLastError(re);
       this.setLastAnswer(null);
-      this.setLastMessage("Failed to insert statements:" + this.getCause(re, 0));
+      this.setLastMessage("Failed to insert statements:" + ItqlUtil.getCause(re, 0));
       logger.fatal("Failed to insert statements", re);
     }
   }
@@ -2097,7 +2025,7 @@
       this.setLastError(qe);
       this.setLastAnswer(null);
       this.setLastMessage("Could not delete statements from " + resourceURI +
-                          EOL + this.getCause(qe, 2));
+                          EOL + ItqlUtil.getCause(qe, 2));
       logger.warn("Failed to delete statements from " + resourceURI, qe);
     }
     catch (URISyntaxException use) {
@@ -2107,14 +2035,14 @@
       this.setLastAnswer(null);
       this.setLastMessage("Could not delete from resource: Invalid resource " +
                           "URI.");
-      logger.warn("Invalid resource URI." + EOL + this.getCause(use, 0));
+      logger.warn("Invalid resource URI." + EOL + ItqlUtil.getCause(use, 0));
     }
     catch (RuntimeException re) {
 
       // let the user know the problem
       this.setLastError(re);
       this.setLastAnswer(null);
-      this.setLastMessage("Failed to delete statements:" + this.getCause(re, 0));
+      this.setLastMessage("Failed to delete statements:" + ItqlUtil.getCause(re, 0));
       logger.fatal("Failed to delete statements", re);
     }
 
@@ -2216,7 +2144,7 @@
         this.setLastError(qe);
         this.setLastAnswer(null);
         this.setLastMessage("Unable to set interpreter option" + EOL +
-                            this.getCause(qe, 2));
+                            ItqlUtil.getCause(qe, 2));
         logger.warn("Unable to set interpreter property", qe);
       }
 
@@ -2339,7 +2267,7 @@
       this.setLastError(qe);
       this.setLastAnswer(null);
       this.setLastMessage("Could not backup " + sourceURI + " to " +
-                          destinationURI + EOL + this.getCause(qe, 2) + ".");
+                          destinationURI + EOL + ItqlUtil.getCause(qe, 2) + ".");
       logger.warn("Failed to backup server " + sourceURI + " to " +
                   destinationURI,
                   qe);
@@ -2446,7 +2374,7 @@
       this.setLastError(qe);
       this.setLastAnswer(null);
       this.setLastMessage("Could not restore " + destinationURI + " from " +
-                          sourceURI + EOL + this.getCause(qe, 2));
+                          sourceURI + EOL + ItqlUtil.getCause(qe, 2));
       logger.warn("Failed to restore server " + destinationURI + " from " +
                   sourceURI, qe);
     }

Added: branches/nw-interface/src/jar/itql/java/org/mulgara/itql/ItqlUtil.java
===================================================================
--- branches/nw-interface/src/jar/itql/java/org/mulgara/itql/ItqlUtil.java	2007-10-11 17:10:16 UTC (rev 473)
+++ branches/nw-interface/src/jar/itql/java/org/mulgara/itql/ItqlUtil.java	2007-10-11 17:12:20 UTC (rev 474)
@@ -0,0 +1,71 @@
+/**
+ * The contents of this file are subject to the Open Software License
+ * Version 3.0 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.opensource.org/licenses/osl-3.0.txt
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+ * the License for the specific language governing rights and limitations
+ * under the License.
+ */
+package org.mulgara.itql;
+
+/**
+ * A set of utilities to provide legacy support for ItqlInterpreter operations.
+ * @created Oct 11, 2007
+ * @author Paul Gearon
+ * @copyright &copy; 2007 <a href="mailto:pgearon at users.sourceforge.net">Paul Gearon</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+class ItqlUtil {
+
+  /** System line separator character */
+  private static final String EOL = System.getProperty("line.separator");
+
+  /**
+   * Pretty prints the Exception Chain.
+   *
+   * @param e the throwable exception
+   * @param preferredDepth the preferred depth to go into the exception to
+   *   retrieve the root cause.  A depth of zero will chain all exceptions
+   *   together.
+   * @return A string containing the pretty printed Exception Chain.
+   */
+  public static String getCause(Throwable e, int preferredDepth) {
+
+    // Keep getting the cause of the message until we reach preferred depth or null cause.
+    Throwable preferredException = e;
+    int index = 0;
+    while ((preferredException != null) && (index != preferredDepth)) {
+      // Pre-check next exception and increment index if it's not null.
+      if (preferredException.getCause() != null) index++;
+      // Get next exception in the chain
+      preferredException = preferredException.getCause();
+    }
+
+    // Update e to the preferred exception if it is not null
+    if (preferredException != null) e = preferredException;
+
+    String message = e.getMessage();
+    if (message == null) message = "";
+
+    // get the cause of the exception
+    Throwable cause = e.getCause();
+
+    // descend into exception if possible
+    if (cause != null) {
+      // pretty print the cause
+      String causeMsg = getCause(cause, 0);
+      // only add the cause's message if there was one
+      if (causeMsg != null) {
+        // Extract the class name from the full path
+        String exceptionClassName = cause.getClass().getName();
+        exceptionClassName = exceptionClassName.substring(exceptionClassName.lastIndexOf('.') + 1);
+        message += (EOL + "Caused by: (" + exceptionClassName + ") " + causeMsg);
+      }
+    }
+    return message;
+  }
+
+}




More information about the Mulgara-svn mailing list