Coverage Report - org.webmacro.directive.DefaultDirective
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultDirective
10%
3/30
0%
0/8
3.2
 
 1  
 /*
 2  
  * Copyright (C) 1998-2000 Semiotek Inc.  All Rights Reserved.
 3  
  *
 4  
  * Redistribution and use in source and binary forms, with or without
 5  
  * modification, are permitted under the terms of either of the following
 6  
  * Open Source licenses:
 7  
  *
 8  
  * The GNU General Public License, version 2, or any later version, as
 9  
  * published by the Free Software Foundation
 10  
  * (http://www.fsf.org/copyleft/gpl.html);
 11  
  *
 12  
  *  or
 13  
  *
 14  
  * The Semiotek Public License (http://webmacro.org/LICENSE.)
 15  
  *
 16  
  * This software is provided "as is", with NO WARRANTY, not even the
 17  
  * implied warranties of fitness to purpose, or merchantability. You
 18  
  * assume all risks and liabilities associated with its use.
 19  
  *
 20  
  * See www.webmacro.org for more information on the WebMacro project.
 21  
  */
 22  
 
 23  
 package org.webmacro.directive;
 24  
 
 25  
 import java.io.IOException;
 26  
 
 27  
 import org.webmacro.Context;
 28  
 import org.webmacro.FastWriter;
 29  
 import org.webmacro.Macro;
 30  
 import org.webmacro.PropertyException;
 31  
 import org.webmacro.TemplateVisitor;
 32  
 import org.webmacro.engine.BuildContext;
 33  
 import org.webmacro.engine.BuildException;
 34  
 import org.webmacro.engine.Variable;
 35  
 
 36  
 /**
 37  
  * Implements the directive that allows
 38  
  * a template to have a default value
 39  
  * for a reference in the context.
 40  
  */
 41  
 public class DefaultDirective extends Directive
 42  
 {
 43  
 
 44  
     private static final int DEFAULT_TARGET = 1;
 45  
     //private static final int DEFAULT_TO = 2;
 46  
     private static final int DEFAULT_RESULT = 3;
 47  
 
 48  
     private Variable target;
 49  
     private Object result;
 50  
 
 51  
     private static final ArgDescriptor[]
 52  2
             myArgs = new ArgDescriptor[]{
 53  
                 new LValueArg(DEFAULT_TARGET),
 54  
                 new OptionalGroup(2),
 55  
                 //new KeywordArg(DEFAULT_TO, "to"),
 56  
                 new AssignmentArg(),
 57  
                 new RValueArg(DEFAULT_RESULT)
 58  
             };
 59  
 
 60  
     private static final DirectiveDescriptor
 61  2
             myDescr = new DirectiveDescriptor("default", null, myArgs, null);
 62  
 
 63  
     public static DirectiveDescriptor getDescriptor ()
 64  
     {
 65  6
         return myDescr;
 66  
     }
 67  
 
 68  
     public DefaultDirective ()
 69  0
     {
 70  0
     }
 71  
 
 72  
     public Object build (DirectiveBuilder builder,
 73  
                          BuildContext bc)
 74  
             throws BuildException
 75  
     {
 76  
         try
 77  
         {
 78  0
             target = (Variable) builder.getArg(DEFAULT_TARGET, bc);
 79  
         }
 80  0
         catch (ClassCastException e)
 81  
         {
 82  0
             throw new NotVariableBuildException(myDescr.name, e);
 83  0
         }
 84  0
         if (!target.isSimpleName())
 85  
         {
 86  0
             throw new NotSimpleVariableBuildException(myDescr.name);
 87  
         }
 88  
         //Object oeq = builder.getArg(DEFAULT_AS, bc);
 89  0
         result = builder.getArg(DEFAULT_RESULT, bc);
 90  0
         if (result == null) result = "";
 91  0
         return this;
 92  
     }
 93  
 
 94  
     public void write (FastWriter out, Context context)
 95  
             throws PropertyException, IOException
 96  
     {
 97  
 
 98  
         try
 99  
         {
 100  
             // check if target in context already
 101  0
             if (context.containsKey(target.getName())) return;  // already defined, do nothing
 102  0
             if (result instanceof Macro)
 103  0
                 target.setValue(context, ((Macro) result).evaluate(context));
 104  
             else
 105  0
                 target.setValue(context, result);
 106  
         }
 107  0
         catch (PropertyException e)
 108  
         {
 109  0
             throw e;
 110  
         }
 111  0
         catch (Exception e)
 112  
         {
 113  0
             String errorText = "#default: Unable to set default value for " + target;
 114  0
             writeWarning(errorText, context, out);
 115  0
         }
 116  0
     }
 117  
 
 118  
     public void accept (TemplateVisitor v)
 119  
     {
 120  0
         v.beginDirective(myDescr.name);
 121  0
         v.visitDirectiveArg("DefaultTarget", target);
 122  0
         v.visitDirectiveArg("DefaultValue", result);
 123  0
         v.endDirective();
 124  0
     }
 125  
 
 126  
 }