%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jelly.tags.bsf.ScriptTag |
|
|
1 | /* |
|
2 | * Copyright 2002,2004 The Apache Software Foundation. |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
15 | */ |
|
16 | package org.apache.commons.jelly.tags.bsf; |
|
17 | ||
18 | import java.util.Iterator; |
|
19 | ||
20 | import org.apache.commons.jelly.JellyTagException; |
|
21 | import org.apache.commons.jelly.LocationAware; |
|
22 | import org.apache.commons.jelly.MissingAttributeException; |
|
23 | import org.apache.commons.jelly.TagSupport; |
|
24 | import org.apache.commons.jelly.XMLOutput; |
|
25 | import org.apache.commons.logging.Log; |
|
26 | import org.apache.commons.logging.LogFactory; |
|
27 | ||
28 | import org.apache.bsf.BSFEngine; |
|
29 | import org.apache.bsf.BSFManager; |
|
30 | import org.apache.bsf.BSFException; |
|
31 | ||
32 | /** |
|
33 | * A tag which evaluates its body using the current scripting language |
|
34 | * |
|
35 | * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> |
|
36 | * @version $Revision: 1.5 $ |
|
37 | */ |
|
38 | 0 | public class ScriptTag extends TagSupport implements LocationAware { |
39 | ||
40 | /** The Log to which logging calls will be made. */ |
|
41 | 0 | private static final Log log = LogFactory.getLog(ScriptTag.class.getName() + ".evaluating"); |
42 | ||
43 | private BSFEngine engine; |
|
44 | private BSFManager manager; |
|
45 | private String elementName; |
|
46 | private String fileName; |
|
47 | private int columnNumber; |
|
48 | private int lineNumber; |
|
49 | ||
50 | 0 | public ScriptTag(BSFEngine engine, BSFManager manager) { |
51 | 0 | this.engine = engine; |
52 | 0 | this.manager = manager; |
53 | 0 | } |
54 | ||
55 | // Tag interface |
|
56 | //------------------------------------------------------------------------- |
|
57 | public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException { |
|
58 | 0 | String text = getBodyText(); |
59 | ||
60 | 0 | log.debug(text); |
61 | ||
62 | // XXXX: unfortunately we must sychronize evaluations |
|
63 | // so that we can swizzle in the context. |
|
64 | // maybe we could create an expression from a context |
|
65 | // (and so create a BSFManager for a context) |
|
66 | 0 | synchronized (getRegistry()) { |
67 | 0 | getRegistry().setJellyContext(context); |
68 | ||
69 | try { |
|
70 | // XXXX: hack - there must be a better way!!! |
|
71 | 0 | for ( Iterator iter = context.getVariableNames(); iter.hasNext(); ) { |
72 | 0 | String name = (String) iter.next(); |
73 | 0 | Object value = context.getVariable( name ); |
74 | 0 | manager.declareBean( name, value, value.getClass() ); |
75 | } |
|
76 | 0 | engine.exec(fileName, lineNumber, columnNumber, text); |
77 | 0 | } |
78 | catch (BSFException e) { |
|
79 | 0 | throw new JellyTagException("Error occurred with script: " + e, e); |
80 | } |
|
81 | 0 | } |
82 | 0 | } |
83 | ||
84 | // Properties |
|
85 | //------------------------------------------------------------------------- |
|
86 | /** |
|
87 | * @return int |
|
88 | */ |
|
89 | public int getColumnNumber() { |
|
90 | 0 | return columnNumber; |
91 | } |
|
92 | ||
93 | /** |
|
94 | * @return String |
|
95 | */ |
|
96 | public String getElementName() { |
|
97 | 0 | return elementName; |
98 | } |
|
99 | ||
100 | /** |
|
101 | * @return BSFEngine |
|
102 | */ |
|
103 | public BSFEngine getEngine() { |
|
104 | 0 | return engine; |
105 | } |
|
106 | ||
107 | /** |
|
108 | * @return String |
|
109 | */ |
|
110 | public String getFileName() { |
|
111 | 0 | return fileName; |
112 | } |
|
113 | ||
114 | /** |
|
115 | * @return int |
|
116 | */ |
|
117 | public int getLineNumber() { |
|
118 | 0 | return lineNumber; |
119 | } |
|
120 | ||
121 | /** |
|
122 | * Sets the columnNumber. |
|
123 | * @param columnNumber The columnNumber to set |
|
124 | */ |
|
125 | public void setColumnNumber(int columnNumber) { |
|
126 | 0 | this.columnNumber = columnNumber; |
127 | 0 | } |
128 | ||
129 | /** |
|
130 | * Sets the elementName. |
|
131 | * @param elementName The elementName to set |
|
132 | */ |
|
133 | public void setElementName(String elementName) { |
|
134 | 0 | this.elementName = elementName; |
135 | 0 | } |
136 | ||
137 | /** |
|
138 | * Sets the engine. |
|
139 | * @param engine The engine to set |
|
140 | */ |
|
141 | public void setEngine(BSFEngine engine) { |
|
142 | 0 | this.engine = engine; |
143 | 0 | } |
144 | ||
145 | /** |
|
146 | * Sets the fileName. |
|
147 | * @param fileName The fileName to set |
|
148 | */ |
|
149 | public void setFileName(String fileName) { |
|
150 | 0 | this.fileName = fileName; |
151 | 0 | } |
152 | ||
153 | /** |
|
154 | * Sets the lineNumber. |
|
155 | * @param lineNumber The lineNumber to set |
|
156 | */ |
|
157 | public void setLineNumber(int lineNumber) { |
|
158 | 0 | this.lineNumber = lineNumber; |
159 | 0 | } |
160 | ||
161 | private JellyContextRegistry getRegistry() |
|
162 | { |
|
163 | 0 | return (JellyContextRegistry) this.manager.getObjectRegistry(); |
164 | } |
|
165 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |