Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
TempletDirective |
|
| 2.6;2.6 |
1 | /* | |
2 | * TempletDirective.java | |
3 | * | |
4 | * Created on May 12, 2003, 2:25 PM | |
5 | * | |
6 | * Copyright (C) 1998-2003 Semiotek Inc. All Rights Reserved. | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted under the terms of either of the following | |
10 | * Open Source licenses: | |
11 | * | |
12 | * The GNU General Public License, version 2, or any later version, as | |
13 | * published by the Free Software Foundation | |
14 | * (http://www.fsf.org/copyleft/gpl.html); | |
15 | * | |
16 | * or | |
17 | * | |
18 | * The Semiotek Public License (http://webmacro.org/LICENSE.) | |
19 | * | |
20 | * This software is provided "as is", with NO WARRANTY, not even the | |
21 | * implied warranties of fitness to purpose, or merchantability. You | |
22 | * assume all risks and liabilities associated with its use. | |
23 | * | |
24 | * See www.webmacro.org for more information on the WebMacro project. | |
25 | */ | |
26 | ||
27 | package org.webmacro.directive; | |
28 | ||
29 | import java.io.IOException; | |
30 | ||
31 | import org.webmacro.PropertyException; | |
32 | import org.webmacro.TemplateVisitor; | |
33 | import org.webmacro.engine.BuildContext; | |
34 | import org.webmacro.engine.BuildException; | |
35 | import org.webmacro.engine.Variable; | |
36 | ||
37 | /** | |
38 | * This directive allows a block within a template to be reused | |
39 | * as a "templet" that can be invoked using the #eval directive. | |
40 | * @author Keats Kirsch | |
41 | * @since June 2003 | |
42 | * @see org.webmacro.directive.EvalDirective | |
43 | */ | |
44 | ||
45 | public class TempletDirective extends org.webmacro.directive.Directive | |
46 | { | |
47 | ||
48 | private static final int TEMPLET_TARGET = 1; | |
49 | private static final int TEMPLET_RESULT = 2; | |
50 | ||
51 | private Variable _target; | |
52 | private org.webmacro.engine.Block _result; | |
53 | //static private org.webmacro.servlet.TemplateTool _templateTool; | |
54 | ||
55 | private static final ArgDescriptor[] | |
56 | 2 | myArgs = new ArgDescriptor[] |
57 | { | |
58 | new LValueArg(TEMPLET_TARGET), | |
59 | new BlockArg(TEMPLET_RESULT) | |
60 | }; | |
61 | ||
62 | private static final DirectiveDescriptor | |
63 | 2 | myDescr = new DirectiveDescriptor("templet", null, myArgs, null); |
64 | ||
65 | /** | |
66 | * Returns the descriptor for this directive. | |
67 | * @return the directive descriptor | |
68 | */ | |
69 | public static DirectiveDescriptor getDescriptor() | |
70 | { | |
71 | 6 | return myDescr; |
72 | } | |
73 | ||
74 | /** Creates a new instance of TempletDirective. */ | |
75 | public TempletDirective() | |
76 | 0 | { |
77 | 0 | } |
78 | ||
79 | /** Build the directive. Parses the block and saves it. | |
80 | * @param builder The Builder | |
81 | * @param bc The BuildContext | |
82 | * @throws BuildException when directive cannot build its arguments, | |
83 | * e.g., when the first argument is not a valid lval. | |
84 | * @return the built directive | |
85 | */ | |
86 | public Object build(DirectiveBuilder builder, BuildContext bc) | |
87 | throws BuildException | |
88 | { | |
89 | try | |
90 | { | |
91 | 0 | _target = (Variable) builder.getArg(TEMPLET_TARGET, bc); |
92 | } | |
93 | 0 | catch (ClassCastException e) |
94 | { | |
95 | 0 | throw new NotVariableBuildException(myDescr.name, e); |
96 | 0 | } |
97 | 0 | _result = (org.webmacro.engine.Block)builder.getArg(TEMPLET_RESULT, bc); |
98 | // store the variable name in the block for debugging | |
99 | 0 | _result.setTemplateName(_target.getVariableName()); |
100 | 0 | return this; |
101 | } | |
102 | ||
103 | /** Do nothing. This directive is completely evaluated | |
104 | * at build time. | |
105 | * @param out the FastWriter | |
106 | * @param context the Context | |
107 | * @throws PropertyException N/A | |
108 | * @throws IOException N/A | |
109 | */ | |
110 | public void write(org.webmacro.FastWriter out, org.webmacro.Context context) | |
111 | throws org.webmacro.PropertyException, IOException | |
112 | { | |
113 | try | |
114 | { | |
115 | 0 | _target.setValue(context, _result); |
116 | } | |
117 | 0 | catch (PropertyException e) |
118 | { | |
119 | 0 | throw e; |
120 | } | |
121 | 0 | catch (Exception e) |
122 | { | |
123 | 0 | String errorText = "#templet: Unable to set " + _target; |
124 | 0 | if (!(e instanceof PropertyException)) |
125 | { | |
126 | 0 | throw new PropertyException(errorText, e); |
127 | } | |
128 | 0 | else throw (PropertyException)e; |
129 | //writeWarning(errorText, context, out); | |
130 | 0 | } |
131 | 0 | } |
132 | ||
133 | /** Used by template visitors. | |
134 | * @param v a template vistor | |
135 | */ | |
136 | public void accept(TemplateVisitor v) | |
137 | { | |
138 | 0 | v.beginDirective(myDescr.name); |
139 | 0 | v.visitDirectiveArg("TempletTarget", _target); |
140 | 0 | v.visitDirectiveArg("TempletValue", _result); |
141 | 0 | v.endDirective(); |
142 | 0 | } |
143 | ||
144 | } |