##### Coverage Report - org.webmacro.directive.SetblockDirective

Classes in this File | Line Coverage | Branch Coverage | Complexity
SetblockDirective |
9% | 3/32
0% | 0/8
| 2.6;2.6

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 |  |
      * Allows setting a variable to a block of template script.
38 |  |
      */
39 |  |
     public class SetblockDirective extends Directive
40 |  |
     {
41 |  |
42 |  |
         private static final int SETBLOCK_AS = 1;
43 |  |
         private static final int SETBLOCK_MACRO = 2;
44 |  |
         private static final int SETBLOCK_TARGET = 3;
45 |  |
         private static final int SETBLOCK_RESULT = 4;
46 |  |
47 |  |
         private Variable target;
48 |  |
         private Object result;
49 | 0 |
         private boolean asMacro = false;
50 |  |
51 |  |
         private static final ArgDescriptor[]
52 | 2 |
                 myArgs = new ArgDescriptor[]{
53 |  |
                     new OptionalGroup(2),
54 |  |
                     new KeywordArg(SETBLOCK_AS, "as"),
55 |  |
                     new KeywordArg(SETBLOCK_MACRO, "macro"),
56 |  |
                     new LValueArg(SETBLOCK_TARGET),
57 |  |
                     new BlockArg(SETBLOCK_RESULT)
58 |  |
                 };
59 |  |
60 |  |
         private static final DirectiveDescriptor
61 | 2 |
                 myDescr = new DirectiveDescriptor("setblock", null, myArgs, null);
62 |  |
63 |  |
         public static DirectiveDescriptor getDescriptor ()
64 |  |
         {
65 | 6 |
             return myDescr;
66 |  |
         }
67 |  |
68 |  |
         public SetblockDirective ()
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(SETBLOCK_TARGET, bc);
79 |  |
             }
80 | 0 |
             catch (ClassCastException e)
81 |  |
             {
82 | 0 |
                 throw new NotVariableBuildException(myDescr.name, e);
83 | 0 |
             }
84 | 0 |
             Object macroKeyword = builder.getArg(SETBLOCK_MACRO, bc);
85 | 0 |
             asMacro = (macroKeyword != null);
86 | 0 |
             result = builder.getArg(SETBLOCK_RESULT, bc);
87 | 0 |
             return this;
88 |  |
         }
89 |  |
90 |  |
         public void write (FastWriter out, Context context)
91 |  |
                 throws PropertyException, IOException
92 |  |
         {
93 |  |
94 |  |
             try
95 |  |
             {
96 | 0 |
                 if (result instanceof Macro && !asMacro)
97 | 0 |
                     target.setValue(context, ((Macro) result).evaluate(context));
98 |  |
                 else
99 | 0 |
                     target.setValue(context, result);
100 |  |
             }
101 | 0 |
             catch (PropertyException e)
102 |  |
             {
103 | 0 |
                 throw e;
104 |  |
             }
105 | 0 |
             catch (Exception e)
106 |  |
             {
107 | 0 |
                 String errorText = "#setblock: Unable to set " + target;
108 | 0 |
                 writeWarning(errorText, context, out);
109 | 0 |
             }
110 | 0 |
         }
111 |  |
112 |  |
         public void accept (TemplateVisitor v)
113 |  |
         {
114 | 0 |
             v.beginDirective(myDescr.name);
115 | 0 |
             if (asMacro)
116 |  |
             {
117 | 0 |
                 v.visitDirectiveArg("SetblockKeyword", "as");
118 | 0 |
                 v.visitDirectiveArg("SetblockKeyword", "macro");
119 |  |
             }
120 | 0 |
             v.visitDirectiveArg("SetblockTarget", target);
121 | 0 |
             v.visitDirectiveArg("SetblockValue", result);
122 | 0 |
             v.endDirective();
123 | 0 |
         }
124 |  |
125 |  |
     }