Author: henning
Date: Thu May  3 08:00:59 2007
New Revision: 534901

URL: http://svn.apache.org/viewvc?view=rev&rev=534901
Log:
Refactored some of the error handling in the anakia transformer.

Modified:
    velocity/anakia/trunk/src/java/org/apache/anakia/Anakia.java

Modified: velocity/anakia/trunk/src/java/org/apache/anakia/Anakia.java
URL: 
http://svn.apache.org/viewvc/velocity/anakia/trunk/src/java/org/apache/anakia/Anakia.java?view=diff&rev=534901&r1=534900&r2=534901
==============================================================================
--- velocity/anakia/trunk/src/java/org/apache/anakia/Anakia.java (original)
+++ velocity/anakia/trunk/src/java/org/apache/anakia/Anakia.java Thu May  3 
08:00:59 2007
@@ -51,6 +51,11 @@
  */
 public class Anakia
 {
+
+    private static final String [] ILLEGAL_CONTEXTNAMES = new String [] {
+        "relativePath", "treeWalk", "xpath", "escape", "date", "project"
+    };
+
     private File baseDir = null;
 
     private File destDir = null;
@@ -360,32 +365,27 @@
                 Throwable rootCause = e.getCause();
                 if (rootCause instanceof SAXParseException)
                 {
-                    System.out.println("");
-                    System.out.println("Error: " + rootCause.getMessage());
-                    System.out.println(
-                        "       Line: " +
-                            ((SAXParseException)rootCause).getLineNumber() +
-                        " Column: " +
-                            ((SAXParseException)rootCause).getColumnNumber());
-                    System.out.println("");
+                    log.error("Error:  " + rootCause.getMessage());
+                    log.error("Line:   " + ((SAXParseException) 
rootCause).getLineNumber());
+                    log.error("Column: " + 
((SAXParseException)rootCause).getColumnNumber());
                 }
                 else
                 {
-                    rootCause.printStackTrace();
+                    throw new AnakiaException(e.getCause());
                 }
             }
             else
             {
-                e.printStackTrace();
+                throw new AnakiaException(e);
             }
         }
-        catch (Throwable e)
+        catch (Throwable t)
         {
             if (outFile != null)
             {
                 outFile.delete();
             }
-            e.printStackTrace();
+            throw new AnakiaException(t);
         }
         finally
         {
@@ -397,7 +397,6 @@
                 }
                 catch (IOException e)
                 {
-                    // Do nothing
                 }
 
                 try
@@ -406,7 +405,6 @@
                 }
                 catch (IOException e)
                 {
-                    // Do nothing
                 }
             }
         }
@@ -417,10 +415,13 @@
      * that we are currently in. This is good for getting
      * the relative path for images and anchor's.
      */
-    private String getRelativePath(String file)
+    private String getRelativePath(final String file)
     {
-        if (file == null || file.length()==0)
+        if (StringUtils.isEmpty(file))
+        {
             return "";
+        }
+
         StringTokenizer st = new StringTokenizer(file, "/\\");
         // needs to be -1 cause ST returns 1 even if there are no matches. huh?
         int slashCount = st.countTokens() - 1;
@@ -430,7 +431,7 @@
             sb.append ("../");
         }
 
-        if (sb.toString().length() > 0)
+        if (sb.length() > 0)
         {
             return StringUtils.chomp(sb.toString(), "/");
         }
@@ -441,9 +442,10 @@
     /**
      * create directories as needed
      */
-    private void ensureDirectoryFor(File targetFile) throws AnakiaException
+    private void ensureDirectoryFor(final File targetFile) throws 
AnakiaException
     {
         File directory = new File(targetFile.getParent());
+
         if (!directory.exists())
         {
             if (!directory.mkdirs())
@@ -458,12 +460,12 @@
     /**
      * Check to see if user context is modified.
      */
-    private boolean userContextsModifed(long lastModified)
+    private boolean userContextsModifed(final long lastModified)
     {
         for (Iterator iter = contexts.iterator(); iter.hasNext();)
         {
             Anakia.Context ctx = (Anakia.Context) iter.next();
-            if(ctx.getLastModified() > lastModified)
+            if(ctx != null && ctx.getLastModified() > lastModified)
             {
                 return true;
             }
@@ -476,9 +478,11 @@
      */
     public class Context
     {
-        private String name;
+        private String name = null;
         private Document contextDoc = null;
-        private String file;
+        private String file = null;
+
+
 
         /**
          * Public constructor.
@@ -504,17 +508,14 @@
          * name, specifically any of "relativePath", "treeWalk", "xpath",
          * "escape", "date", or "project"
          */
-        public void setName(String name)
+        public void setName(final String name)
         {
-            if (name.equals("relativePath") ||
-                    name.equals("treeWalk") ||
-                    name.equals("xpath") ||
-                    name.equals("escape") ||
-                    name.equals("date") ||
-                    name.equals("project"))
+            for (int i = 0; i < ILLEGAL_CONTEXTNAMES.length; i++)
             {
-
+                if (StringUtils.isEmpty(name) || 
ILLEGAL_CONTEXTNAMES[i].equals(name))
+                {
                     throw new AnakiaException("Context name '" + name + "' is 
reserved by Anakia");
+                }
             }
 
             this.name = name;
@@ -524,7 +525,7 @@
          * Build the context based on a file path.
          * @param file
          */
-        public void setFile(String file)
+        public void setFile(final String file)
         {
             this.file = file;
         }


Reply via email to