|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
Script.java | 0% | 0% | 0% | 0% |
|
1 |
// Copyright 2004, 2005 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
14 |
|
|
15 |
package org.apache.tapestry.html;
|
|
16 |
|
|
17 |
import java.util.HashMap;
|
|
18 |
import java.util.Iterator;
|
|
19 |
import java.util.Map;
|
|
20 |
|
|
21 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
22 |
import org.apache.hivemind.Resource;
|
|
23 |
import org.apache.tapestry.AbstractComponent;
|
|
24 |
import org.apache.tapestry.IBinding;
|
|
25 |
import org.apache.tapestry.IEngine;
|
|
26 |
import org.apache.tapestry.IMarkupWriter;
|
|
27 |
import org.apache.tapestry.IRequestCycle;
|
|
28 |
import org.apache.tapestry.IScript;
|
|
29 |
import org.apache.tapestry.Tapestry;
|
|
30 |
import org.apache.tapestry.engine.IScriptSource;
|
|
31 |
|
|
32 |
/**
|
|
33 |
* Works with the {@link Body} component to add a script (and perhaps some initialization)
|
|
34 |
* to the HTML response.
|
|
35 |
*
|
|
36 |
* [<a href="../../../../../ComponentReference/Script.html">Component Reference</a>]
|
|
37 |
*
|
|
38 |
* @author Howard Lewis Ship
|
|
39 |
*
|
|
40 |
**/
|
|
41 |
|
|
42 |
public abstract class Script extends AbstractComponent |
|
43 |
{ |
|
44 |
private Map _baseSymbols;
|
|
45 |
|
|
46 |
/**
|
|
47 |
* A Map of input and output symbols visible to the body of the Script.
|
|
48 |
*
|
|
49 |
* @since 2.2
|
|
50 |
*
|
|
51 |
**/
|
|
52 |
|
|
53 |
private Map _symbols;
|
|
54 |
|
|
55 |
/**
|
|
56 |
* Constructs the symbols {@link Map}. This starts with the
|
|
57 |
* contents of the symbols parameter (if specified) to which is added
|
|
58 |
* any informal parameters. If both a symbols parameter and informal
|
|
59 |
* parameters are bound, then a copy of the symbols parameter's value is made
|
|
60 |
* (that is, the {@link Map} provided by the symbols parameter is read, but not modified).
|
|
61 |
*
|
|
62 |
**/
|
|
63 |
|
|
64 | 0 |
private Map getInputSymbols()
|
65 |
{ |
|
66 | 0 |
Map result = new HashMap();
|
67 |
|
|
68 | 0 |
if (_baseSymbols != null) |
69 | 0 |
result.putAll(_baseSymbols); |
70 |
|
|
71 |
// Now, iterate through all the binding names (which includes both
|
|
72 |
// formal and informal parmeters). Skip the formal ones and
|
|
73 |
// access the informal ones.
|
|
74 |
|
|
75 | 0 |
Iterator i = getBindingNames().iterator(); |
76 | 0 |
while (i.hasNext())
|
77 |
{ |
|
78 | 0 |
String bindingName = (String) i.next(); |
79 |
|
|
80 |
// Skip formal parameters
|
|
81 |
|
|
82 | 0 |
if (getSpecification().getParameter(bindingName) != null) |
83 | 0 |
continue;
|
84 |
|
|
85 | 0 |
IBinding binding = getBinding(bindingName); |
86 |
|
|
87 | 0 |
Object value = binding.getObject(); |
88 |
|
|
89 | 0 |
result.put(bindingName, value); |
90 |
} |
|
91 |
|
|
92 | 0 |
return result;
|
93 |
} |
|
94 |
|
|
95 |
/**
|
|
96 |
* Gets the {@link IScript} for the correct script.
|
|
97 |
*
|
|
98 |
*
|
|
99 |
**/
|
|
100 |
|
|
101 | 0 |
private IScript getParsedScript(IRequestCycle cycle)
|
102 |
{ |
|
103 | 0 |
String scriptPath = getScriptPath(); |
104 |
|
|
105 | 0 |
if (scriptPath == null) |
106 | 0 |
throw Tapestry.createRequiredParameterException(this, "scriptPath"); |
107 |
|
|
108 | 0 |
IEngine engine = cycle.getEngine(); |
109 | 0 |
IScriptSource source = engine.getScriptSource(); |
110 |
|
|
111 |
// If the script path is relative, it should be relative to the Script component's
|
|
112 |
// container (i.e., relative to a page in the application).
|
|
113 |
|
|
114 | 0 |
Resource rootLocation = |
115 |
getContainer().getSpecification().getSpecificationLocation(); |
|
116 | 0 |
Resource scriptLocation = rootLocation.getRelativeResource(scriptPath); |
117 |
|
|
118 | 0 |
try
|
119 |
{ |
|
120 | 0 |
return source.getScript(scriptLocation);
|
121 |
} |
|
122 |
catch (RuntimeException ex)
|
|
123 |
{ |
|
124 | 0 |
throw new ApplicationRuntimeException(ex.getMessage(), this, null, ex); |
125 |
} |
|
126 |
|
|
127 |
} |
|
128 |
|
|
129 | 0 |
protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) |
130 |
{ |
|
131 | 0 |
if (!cycle.isRewinding())
|
132 |
{ |
|
133 | 0 |
Body body = Body.get(cycle); |
134 |
|
|
135 | 0 |
if (body == null) |
136 | 0 |
throw new ApplicationRuntimeException( |
137 |
Tapestry.getMessage("Script.must-be-contained-by-body"),
|
|
138 |
this,
|
|
139 |
null,
|
|
140 |
null);
|
|
141 |
|
|
142 | 0 |
_symbols = getInputSymbols(); |
143 |
|
|
144 | 0 |
getParsedScript(cycle).execute(cycle, body, _symbols); |
145 |
} |
|
146 |
|
|
147 |
// Render the body of the Script;
|
|
148 | 0 |
renderBody(writer, cycle); |
149 |
} |
|
150 |
|
|
151 |
public abstract String getScriptPath();
|
|
152 |
|
|
153 | 0 |
public Map getBaseSymbols()
|
154 |
{ |
|
155 | 0 |
return _baseSymbols;
|
156 |
} |
|
157 |
|
|
158 | 0 |
public void setBaseSymbols(Map baseSymbols) |
159 |
{ |
|
160 | 0 |
_baseSymbols = baseSymbols; |
161 |
} |
|
162 |
|
|
163 |
/**
|
|
164 |
* Returns the complete set of symbols (input and output)
|
|
165 |
* from the script execution. This is visible to the body
|
|
166 |
* of the Script, but is cleared after the Script
|
|
167 |
* finishes rendering.
|
|
168 |
*
|
|
169 |
* @since 2.2
|
|
170 |
**/
|
|
171 |
|
|
172 | 0 |
public Map getSymbols()
|
173 |
{ |
|
174 | 0 |
return _symbols;
|
175 |
} |
|
176 |
|
|
177 | 0 |
protected void cleanupAfterRender(IRequestCycle cycle) |
178 |
{ |
|
179 | 0 |
_symbols = null;
|
180 |
|
|
181 | 0 |
super.cleanupAfterRender(cycle);
|
182 |
} |
|
183 |
|
|
184 |
} |
|