Changeset 1346

Show
Ignore:
Timestamp:
02/07/09 15:06:52 (8 months ago)
Author:
bruno
Message:

Introduce a TemplateException? class, and use that in the template code instead of a generic RuntimeException?.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/universe/kauri-template/src/main/java/org/kauriproject/template/CallMacroBlock.java

    r1110 r1346  
    7878            MacroBlock block = context.getMacroRegistry().get(getName()); 
    7979            if (block == null) { 
    80                 throw new RuntimeException("KTL: macro " + getName() + " not found, location " 
    81                         + getLocation()); 
     80                throw new TemplateException("Macro " + getName() + " not found, location " + getLocation()); 
    8281            } 
    8382 
  • trunk/universe/kauri-template/src/main/java/org/kauriproject/template/CommentBlock.java

    r600 r1346  
    8383                value = bos.toString("UTF-8"); 
    8484            } catch (IOException ex) { 
    85                 log.error(ex); 
     85                throw new TemplateException(ex); 
    8686            } 
    8787            result.comment(value.toCharArray(), 0, value.length()); 
  • trunk/universe/kauri-template/src/main/java/org/kauriproject/template/DefaultTemplateBuilder.java

    r1344 r1346  
    139139            xmlReader.parse(is); 
    140140        } catch (Exception ex) { 
    141             throw new RuntimeException("Error parsing template file " + source.getReference(), ex); 
     141            throw new TemplateException("Error parsing template file " + source.getReference(), ex); 
    142142        } finally { 
    143143            IOUtils.closeQuietly(in); 
     
    160160            parser = factory.newSAXParser(); 
    161161        } catch (SAXException saxex) { 
    162             log.error(saxex); 
     162            throw new TemplateException(saxex); 
    163163        } catch (ParserConfigurationException parsex) { 
    164             log.error(parsex); 
     164            throw new TemplateException(parsex); 
    165165        } 
    166166        return parser; 
     
    396396                        blocksPushed++; 
    397397                        if (macroRegistry.containsKey(macroblock.getName())) { 
    398                             throw new RuntimeException("A macro with name " + macroblock.getName() 
     398                            throw new TemplateException("A macro with name " + macroblock.getName() 
    399399                                    + " is already defined, location " 
    400400                                    + macroblock.getStartStep().getLocation()); 
     
    417417 
    418418                        if (parentBlock == null) { 
    419                             throw new RuntimeException( 
     419                            throw new TemplateException( 
    420420                                    "Parameter must appear as a direct child in a macro or callMacro element, location " 
    421421                                            + parameterblock.getStartStep().getLocation()); 
     
    435435                        blocksPushed++; 
    436436                        if (inheritanceRegistry.containsKey(inheritblock.getName())) { 
    437                             throw new RuntimeException("A block with name " + inheritblock.getName() 
     437                            throw new TemplateException("A block with name " + inheritblock.getName() 
    438438                                    + " is already defined, location " 
    439439                                    + inheritblock.getStartStep().getLocation()); 
     
    445445                                attributes), parent); 
    446446                        if (parent == null) { 
    447                             throw new RuntimeException( 
     447                            throw new TemplateException( 
    448448                                    "A call to a superBlock is only possible from within an overriding block, location " 
    449449                                            + superblock.getStartStep().getLocation()); 
  • trunk/universe/kauri-template/src/main/java/org/kauriproject/template/Directive.java

    r1344 r1346  
    121121    } 
    122122 
     123    /** 
     124     * Returns null if name is not recognized. 
     125     */ 
    123126    public static Directive fromString(String name) { 
    124127        if (name.equals(FOREACH.tagName)) 
     
    163166            return TEXT; 
    164167        else 
    165             return null;// throw RuntimeException ? 
     168            return null; 
    166169    } 
    167170} 
  • trunk/universe/kauri-template/src/main/java/org/kauriproject/template/ElementBlock.java

    r967 r1346  
    6767            // TODO is this the place and the way to check this? 
    6868            if (name == null) 
    69                 throw new RuntimeException("name attribute required on " + Directive.ELEMENT.getTagName() + " instruction."); 
     69                throw new TemplateException("name attribute required on " + Directive.ELEMENT.getTagName() + " instruction."); 
    7070 
    7171            nameExpression = elFacade.createExpression(name, String.class); 
  • trunk/universe/kauri-template/src/main/java/org/kauriproject/template/ForEachBlock.java

    r966 r1346  
    179179            return Integer.parseInt(value); 
    180180        } catch (NumberFormatException e) { 
    181             throw new RuntimeException("Value \"" + value + "\" is not a valid integer, in forEach attribute " + what + " at " + startStep.getLocation()); 
     181            throw new TemplateException("Value \"" + value + "\" is not a valid integer, in forEach attribute " + what + " at " + startStep.getLocation()); 
    182182        } 
    183183    } 
  • trunk/universe/kauri-template/src/main/java/org/kauriproject/template/IfBlock.java

    r921 r1346  
    7070            String expression = attributes.getValue(TEST); 
    7171            if (expression == null) 
    72                 throw new RuntimeException("if instruction is missing required test attribute. Location: " + getLocation()); 
     72                throw new TemplateException("if instruction is missing required test attribute. Location: " + getLocation()); 
    7373 
    7474            elExpression = elFacade.createExpression(expression, Boolean.class); 
  • trunk/universe/kauri-template/src/main/java/org/kauriproject/template/ImportBlock.java

    r1111 r1346  
    6565            String expression = attributes.getValue(SRC); 
    6666            if (expression == null) 
    67                 throw new RuntimeException(SRC + " attribute is required on " + templateBlock.getSaxElement().getName()); 
     67                throw new TemplateException(SRC + " attribute is required on " + templateBlock.getSaxElement().getName()); 
    6868            sourceExpression = elFacade.createExpression(expression, String.class); 
    6969            String mode = attributes.getValue(MODE); 
     
    8787                imported = templateService.buildTemplate(sourceLocation, context, context.isSilencing()); 
    8888            } catch (Exception ex) { 
    89                 throw new RuntimeException("Error parsing template included at location " + getLocation(), ex); 
     89                throw new TemplateException("Error parsing template included at location " + getLocation(), ex); 
    9090            } 
    9191 
     
    9393            // detect recursion 
    9494            if (old != null) { 
    95                 throw new RuntimeException("KTL: recursion is not allowed in import, location " 
     95                throw new TemplateException("KTL: recursion is not allowed in import, location " 
    9696                        + getLocation()); 
    9797            } 
  • trunk/universe/kauri-template/src/main/java/org/kauriproject/template/IncludeBlock.java

    r1111 r1346  
    7676            String expression = attributes.getValue(SRC); 
    7777            if (expression == null) 
    78                 throw new RuntimeException(SRC + " attribute is required on " + templateBlock.getSaxElement().getName()); 
     78                throw new TemplateException(SRC + " attribute is required on " + templateBlock.getSaxElement().getName()); 
    7979            sourceExpression = elFacade.createExpression(expression, String.class); 
    8080            String mode = attributes.getValue(MODE); 
     
    9898                included = templateService.buildTemplate(sourceLocation, context, context.isSilencing()); 
    9999            } catch (Exception ex) { 
    100                 throw new RuntimeException("Error parsing template included at location " + getLocation(), ex); 
     100                throw new TemplateException("Error parsing template included at location " + getLocation(), ex); 
    101101            } 
    102102 
     
    104104            // detect recursion 
    105105            if (old != null) { 
    106                 throw new RuntimeException("KTL: recursion is not allowed in include, location " 
    107                         + getLocation()); 
     106                throw new TemplateException("Recursion is not allowed in include, location " + getLocation()); 
    108107            } 
    109108 
  • trunk/universe/kauri-template/src/main/java/org/kauriproject/template/InheritBlock.java

    r1020 r1346  
    8080                baseTemplate = templateService.buildTemplate(sourceLocation, context); 
    8181            } catch (Exception ex) { 
    82                 throw new RuntimeException("Error parsing inherited template specified at location " + getLocation(), ex); 
     82                throw new TemplateException("Error parsing inherited template specified at location " + getLocation(), ex); 
    8383            } 
    8484 
  • trunk/universe/kauri-template/src/main/java/org/kauriproject/template/InheritanceBlock.java

    r894 r1346  
    9494                    return chain.get(chain.size() - 1).getEndStep().getCompiledNext(); 
    9595                } else { 
    96                     throw new RuntimeException("Parent block not found for " + getName() + ", location " 
     96                    throw new TemplateException("Parent block not found for " + getName() + ", location " 
    9797                            + getLocation()); 
    9898                } 
  • trunk/universe/kauri-template/src/main/java/org/kauriproject/template/InsertBlock.java

    r900 r1346  
    112112                } 
    113113            } catch (FileNotFoundException ex) { 
    114                 throw new RuntimeException("Error inserting XML from " + sourceLocation + ": " + ex); 
     114                throw new TemplateException("Error inserting XML from " + sourceLocation + ": " + ex); 
    115115            } catch (Exception ex) { 
    116                 throw new RuntimeException("Error parsing XML from " + sourceLocation + ": " + ex); 
     116                throw new TemplateException("Error parsing XML from " + sourceLocation + ": " + ex); 
    117117            } finally { 
    118118                if (source != null) { 
  • trunk/universe/kauri-template/src/main/java/org/kauriproject/template/KauriSaxHandler.java

    r600 r1346  
    109109                    xmlHandler)) : new TransformerHandlerAdapter(xmlHandler); 
    110110        } catch (TransformerConfigurationException ex) { 
    111             throw new RuntimeException("Error creating serializer.", ex); 
     111            throw new TemplateException("Error creating serializer.", ex); 
    112112        } 
    113113    } 
     
    270270            return needsIt; 
    271271        } catch (Throwable t) { 
    272             throw new RuntimeException( 
     272            throw new TemplateException( 
    273273                    "Error while testing if we need to add namespace attributes before xml serialization", t); 
    274274        } 
  • trunk/universe/kauri-template/src/main/java/org/kauriproject/template/MacroBlock.java

    r1110 r1346  
    8181        @Override 
    8282        public Step executeAndProceed(ExecutionContext context, TemplateResult result) throws SAXException { 
    83             throw new RuntimeException("This situation should never occur."); 
     83            throw new TemplateException("This situation should never occur."); 
    8484        } 
    8585 
  • trunk/universe/kauri-template/src/main/java/org/kauriproject/template/ProtectBlock.java

    r1301 r1346  
    4545            String expression = attributes.getValue(ACCESS_ATTR); 
    4646            if (expression == null) 
    47                 throw new RuntimeException("protect instruction is missing required access attribute. Location: " 
     47                throw new TemplateException("protect instruction is missing required access attribute. Location: " 
    4848                        + getLocation()); 
    4949 
     
    5555            AccessDecider decider = context.getAccessDecider(); 
    5656            if (decider == null) 
    57                 throw new RuntimeException("protect instruction is used but no AccessDecider is provided."); 
     57                throw new TemplateException("protect instruction is used but no AccessDecider is provided."); 
    5858 
    5959            String accessString = (String) accessExpr.evaluate(context.getTemplateContext()); 
  • trunk/universe/kauri-template/src/main/java/org/kauriproject/template/SuperBlock.java

    r894 r1346  
    6666        public Step executeAndProceed(ExecutionContext context, TemplateResult result) throws SAXException { 
    6767            if (!context.isInherited()) { 
    68                 throw new RuntimeException( 
     68                throw new TemplateException( 
    6969                        "No template inheritance, so call to super block not possible, location " 
    7070                                + getLocation()); 
     
    7979                return chain.get(index + 1).getStartStep().getCompiledNext(); 
    8080            } else { 
    81                 throw new RuntimeException("Parent block not found for " + inheritanceBlock.getName() 
     81                throw new TemplateException("Parent block not found for " + inheritanceBlock.getName() 
    8282                        + ", location " + getLocation()); 
    8383            } 
  • trunk/universe/kauri-template/src/main/java/org/kauriproject/template/VariableBlock.java

    r1329 r1346  
    3737 */ 
    3838public class VariableBlock extends TemplateBlock { 
    39  
    40     private static Log log = LogFactory.getLog(VariableBlock.class); 
    4139 
    4240    // VARIABLE CONSTANTS 
     
    135133                    buffer.endDocument(); 
    136134                    buffer.flush(); 
    137                     String value = ""
     135                    String value
    138136                    try { 
    139137                        bos.flush(); 
    140138                        value = bos.toString("UTF-8"); 
    141139                    } catch (IOException ex) { 
    142                         log.error(ex); 
     140                        throw new TemplateException("Error building variable value.", ex); 
    143141                    } 
    144142 
     
    164162                } 
    165163            } catch (Throwable e) { 
    166                 throw new RuntimeException("Some error occured while loading data for variable " + name + " from URI " + src, e); 
     164                throw new TemplateException("Some error occured while loading data for variable " + name + " from URI " + src, e); 
    167165            } finally { 
    168166                if (source != null) { 
  • trunk/universe/kauri-template/src/main/java/org/kauriproject/template/el/Expression.java

    r999 r1346  
    5151        } 
    5252 
     53        /** 
     54         * Returns null if value is not recongized. 
     55         */ 
    5356        public static ExpressionParser fromString(String value) { 
    5457            if (value.equals(EL.id)) 
     
    5760                return GROOVY; 
    5861            else 
    59                 return null;// throw RuntimeException ? 
     62                return null; 
    6063        } 
    6164    } 
  • trunk/universe/kauri-template/src/main/java/org/kauriproject/template/el/TemplateFunctionMapper.java

    r1020 r1346  
    1616package org.kauriproject.template.el; 
    1717 
    18 import org.apache.commons.logging.Log; 
    19 import org.apache.commons.logging.LogFactory; 
     18import org.kauriproject.template.TemplateException; 
    2019 
    2120import javax.el.FunctionMapper; 
     
    2625public class TemplateFunctionMapper extends FunctionMapper { 
    2726    private Map<String, Method> functions = new HashMap<String, Method>(); 
    28     private Log log = LogFactory.getLog(getClass()); 
    2927 
    3028    public TemplateFunctionMapper(FunctionRegistry functionRegistry) { 
     
    5250            addFunction(prefix, "max", Math.class.getMethod("max", double.class, double.class)); 
    5351            addFunction(prefix, "min", Math.class.getMethod("min", double.class, double.class)); 
    54             // add additional math functions 
    55             Method[] methods = MathFunctions.class.getDeclaredMethods(); 
    56             for (Method method : methods) { 
    57                 addFunction(prefix, method.getName(), method); 
    58             } 
    5952        } catch (NoSuchMethodException nex) { 
    60             log.error("Error adding math functions to the EL: " + nex); 
     53            throw new TemplateException("Error registering math functions with EL", nex); 
     54        } 
     55 
     56        // add additional math functions 
     57        Method[] methods = MathFunctions.class.getDeclaredMethods(); 
     58        for (Method method : methods) { 
     59            addFunction(prefix, method.getName(), method); 
    6160        } 
    6261    }