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

Classes in this File | Line Coverage | Branch Coverage | Complexity
SetDirective |
46% | 14/30
100% | 2/2
| 2.0;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 setting of a variable.
38 |  |
      */
39 |  |
     public class SetDirective extends Directive
40 |  |
     {
41 |  |
42 |  |
         private static final int SET_TARGET = 1;
43 |  |
         private static final int SET_RESULT = 2;
44 |  |
45 |  |
         private Variable target;
46 |  |
         private Object result;
47 |  |
48 |  |
         private static final ArgDescriptor[]
49 | 2 |
                 myArgs = new ArgDescriptor[]{
50 |  |
                     new LValueArg(SET_TARGET),
51 |  |
                     new AssignmentArg(),
52 |  |
                     new RValueArg(SET_RESULT)
53 |  |
                 };
54 |  |
55 |  |
         private static final DirectiveDescriptor
56 | 2 |
                 myDescr = new DirectiveDescriptor("set", null, myArgs, null);
57 |  |
58 |  |
         public static DirectiveDescriptor getDescriptor ()
59 |  |
         {
60 | 6 |
             return myDescr;
61 |  |
         }
62 |  |
63 |  |
         public SetDirective ()
64 | 360 |
         {
65 | 360 |
         }
66 |  |
67 |  |
         public SetDirective (Variable target, Object result)
68 | 0 |
         {
69 | 0 |
             this.target = target;
70 | 0 |
             this.result = result;
71 | 0 |
         }
72 |  |
73 |  |
         public Object build (DirectiveBuilder builder,
74 |  |
                              BuildContext bc)
75 |  |
                 throws BuildException
76 |  |
         {
77 |  |
             try
78 |  |
             {
79 | 360 |
                 target = (Variable) builder.getArg(SET_TARGET, bc);
80 |  |
             }
81 | 0 |
             catch (ClassCastException e)
82 |  |
             {
83 | 0 |
                 throw new NotVariableBuildException(myDescr.name, e);
84 | 360 |
             }
85 | 360 |
             result = builder.getArg(SET_RESULT, bc);
86 | 360 |
             return this;
87 |  |
         }
88 |  |
89 |  |
         public void write (FastWriter out, Context context)
90 |  |
                 throws PropertyException, IOException
91 |  |
         {
92 |  |
93 |  |
             try
94 |  |
             {
95 | 8582 |
                 if (result instanceof Macro)
96 | 7404 |
                     target.setValue(context, ((Macro) result).evaluate(context));
97 |  |
                 else
98 | 1178 |
                     target.setValue(context, result);
99 |  |
             }
100 | 0 |
             catch (PropertyException e)
101 |  |
             {
102 | 0 |
                 throw e;
103 |  |
             }
104 | 0 |
             catch (Exception e)
105 |  |
             {
106 | 0 |
                 String errorText = "#set: Unable to set value: " + target;
107 | 0 |
                 writeWarning(errorText, context, out);
108 | 8582 |
             }
109 | 8582 |
         }
110 |  |
111 |  |
         public void accept (TemplateVisitor v)
112 |  |
         {
113 | 0 |
             v.beginDirective(myDescr.name);
114 | 0 |
             v.visitDirectiveArg("SetTarget", target);
115 | 0 |
             v.visitDirectiveArg("SetValue", result);
116 | 0 |
             v.endDirective();
117 | 0 |
         }
118 |  |
119 |  |
     }