View Javadoc

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 org.webmacro.Context;
26  import org.webmacro.FastWriter;
27  import org.webmacro.PropertyException;
28  import org.webmacro.engine.BuildContext;
29  import org.webmacro.engine.BuildException;
30  import org.webmacro.engine.Variable;
31  
32  import java.io.IOException;
33  
34  /***
35   * The #param directive is provided for backward compatibility.
36   * Invoking
37   *   #param $a="stuff"
38   * is equivalent to
39   *   #attribute $a="stuff"
40   *   #set $a="stuff"
41   */
42  
43  public class ParamDirective extends Directive
44  {
45  
46      private static final int PARAM_TARGET = 1;
47      private static final int PARAM_RESULT = 2;
48  
49      private static final ArgDescriptor[]
50              myArgs = new ArgDescriptor[]{
51                  new LValueArg(PARAM_TARGET),
52                  new AssignmentArg(),
53                  new RValueArg(PARAM_RESULT)
54              };
55  
56      private static final DirectiveDescriptor
57              myDescr = new DirectiveDescriptor("param", null, myArgs, null);
58  
59      public static DirectiveDescriptor getDescriptor ()
60      {
61          return myDescr;
62      }
63  
64      public Object build (DirectiveBuilder builder,
65                           BuildContext bc)
66              throws BuildException
67      {
68          Variable target = null;
69          Object result = null;
70          try
71          {
72              target = (Variable) builder.getArg(PARAM_TARGET, bc);
73              result = builder.getArg(PARAM_RESULT, bc);
74              if (!target.isSimpleName())
75                  throw new NotSimpleVariableBuildException(myDescr.name);
76  
77              target.setValue(bc, result);
78          }
79          catch (ClassCastException e)
80          {
81              throw new NotVariableBuildException(myDescr.name, e);
82          }
83          catch (PropertyException e)
84          {
85              throw new BuildException("#param: Exception setting variable "
86                      + target.toString(), e);
87          }
88          return new SetDirective(target, result);
89      }
90  
91      public void write (FastWriter out, Context context)
92              throws PropertyException, IOException
93      {
94      }
95  
96  }
97