1 /*
2 * EWCTag.java
3 *
4 * $Author: mballesteros $ $Date: 2004/03/01 12:19:33 $ $Revision: 1.10 $
5 */
6 package net.sf.ewc.taglib;
7
8 import java.io.IOException;
9 import java.util.Stack;
10 import java.util.logging.Level;
11 import java.util.logging.Logger;
12
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.jsp.JspException;
15 import javax.servlet.jsp.JspWriter;
16 import javax.servlet.jsp.PageContext;
17 import javax.servlet.jsp.tagext.BodyTagSupport;
18 import javax.servlet.jsp.tagext.Tag;
19
20 import net.sf.ewc.EWCSession;
21 import net.sf.ewc.EWCSessionFactory;
22
23 /***
24 * This is the base class that EWC tags should extend. It provide common
25 * services to easily develop new tags.
26 */
27 public class EWCTag extends BodyTagSupport {
28
29 /***
30 * Class logger
31 */
32 private static Logger logger = Logger.getLogger(EWCTag.class.getName());
33
34 /***
35 * The JSP writter
36 */
37 protected JspWriter out;
38
39 /***
40 * The page context
41 */
42 protected PageContext pageContext;
43
44 /***
45 * The tag parent
46 */
47 protected Tag parent;
48
49 /***
50 * EWCSession
51 */
52 protected EWCSession session;
53
54 /***
55 * Default constructor.
56 */
57 public EWCTag() {
58 super();
59 }
60
61 /***
62 * Returns the current {@link EWCSession}
63 *
64 * @return The current {@link EWCSession}
65 */
66 private EWCSession getEWCSession() {
67 return session;
68 }
69
70 /***
71 * Returns the {@link EWCSession}for the pageContext
72 *
73 * @return The {@link EWCSession}
74 */
75 public static EWCSession getEWCSession(PageContext pageContext) {
76 return EWCSessionFactory.getEWCSession(pageContext.getSession());
77 }
78
79 /***
80 * Returns the application context path, where the webapp has been hooked.
81 * Use this method instead of hardcoding the webapp name in your code.
82 *
83 * @return The application context path
84 */
85 protected String getContextPath() {
86 return ((HttpServletRequest) pageContext.getRequest()).getContextPath();
87 }
88
89 /***
90 * Returns the context stack for the request
91 *
92 * @param pageContext
93 * @param stackName
94 * @return The context {@link java.util.Stack}
95 */
96 protected static Stack getStack(PageContext pageContext, String stackName) {
97 Stack stack = (Stack) pageContext.getRequest().getAttribute(
98 stackName + "Stack");
99 if (stack == null) {
100 stack = new Stack();
101 pageContext.getRequest().setAttribute(stackName + "Stack", stack);
102 }
103 return stack;
104 }
105
106 /***
107 * Returns the context stack for the request
108 *
109 * @param stackName
110 * @return The context {@link java.util.Stack}
111 */
112 protected Stack getStack(String stackName) {
113 return getStack(pageContext, stackName);
114 }
115
116 /***
117 * Returns the context stack for the request
118 *
119 * @return The context {@link java.util.Stack}
120 */
121 protected static Stack getCtxStack(PageContext pageContext) {
122 return getStack(pageContext, "ctxStack");
123 }
124
125 /***
126 * Returns the context stack for the request
127 *
128 * @return The context {@link java.util.Stack}
129 */
130 protected Stack getCtxStack() {
131 return getCtxStack(pageContext);
132 }
133
134 /***
135 * Pushes a new context on the stack
136 *
137 * @param className
138 * The new context class name
139 */
140 protected static void pushCtx(PageContext pageContext, String className) {
141 if (logger.isLoggable(Level.FINEST)) {
142 logger.finest("New eval context: " + className);
143 }
144 EWCSession session = EWCSessionFactory.getEWCSession(pageContext
145 .getSession());
146 Object ctx = session.getContext(className);
147 getCtxStack(pageContext).push(
148 new Object[] { ctx, session.getFullExpression(ctx, ""), null});
149 }
150
151 /***
152 * Pushes a new context on the stack
153 *
154 * @param className
155 * The new context class name
156 */
157 protected void pushCtx(String className) {
158 pushCtx(pageContext, className);
159 }
160
161 /***
162 * Pushes a new field context on the stack
163 *
164 * @param ctx
165 * The new context
166 * @param src
167 * The relative path to navigate from the last context to the
168 * new one
169 */
170 protected static void pushCtx(PageContext pageContext, Object ctx,
171 String src) {
172 if (logger.isLoggable(Level.FINEST)) {
173 logger.finest("New eval context: src=" + src + ", ctx=" + ctx);
174 }
175 getCtxStack(pageContext).push(
176 new Object[] {
177 ctx,
178 getEWCSession(pageContext).getComposedExpression(
179 getCtxPath(pageContext), src)});
180 }
181
182 /***
183 * Pushes a new field context on the stack
184 *
185 * @param ctx
186 * The new context
187 * @param src
188 * The relative path to navigate from the last context to the
189 * new one
190 */
191 protected void pushCtx(Object ctx, String src) {
192 pushCtx(pageContext, ctx, src);
193 }
194
195 /***
196 * Returns the context info stored on the top of the stack, without poping
197 * it
198 *
199 * @return The TOS of the context stack
200 */
201 protected static Object[] peekCtx(PageContext pageContext) {
202 Stack stk = getCtxStack(pageContext);
203 if (stk.size() == 0) { throw new IllegalStateException(
204 "ERROR: Trying to get a field value without having defined a context. Use \"ewc:ctx\" tag before."); }
205 return (Object[]) stk.peek();
206 }
207
208 /***
209 * Returns the context info stored on the top of the stack, without poping
210 * it
211 *
212 * @return The TOS of the context stack
213 */
214 protected Object[] peekCtx() {
215 return peekCtx(pageContext);
216 }
217
218 /***
219 * Returns the context info stored on the top of the stack, without poping
220 * it
221 *
222 * @return The TOS of the context stack
223 */
224 private Object[] peekCtx(int i) {
225 Stack stk = getCtxStack();
226 if (stk.size() == 0) { throw new IllegalStateException(
227 "ERROR: Trying to get a field value without having defined a context. Use \"ewc:ctx\" tag before."); }
228 return (Object[]) stk.get(stk.size() - i - 1);
229 }
230
231 /***
232 * @return
233 */
234 protected Object[] popCtx() {
235 Object[] out = (Object[]) getCtxStack().pop();
236 if (logger.isLoggable(Level.FINEST)) {
237 logger.finest("Popping context: src=" + out[1] + ", ctx=" + out[0]);
238 }
239 return out;
240 }
241
242 /***
243 * Returns the current context object
244 *
245 * @param pageContext
246 * @return The current context object
247 */
248 public static Object getCtx(PageContext pageContext) {
249 return peekCtx(pageContext)[0];
250 }
251
252 /***
253 * Returns the current context object
254 *
255 * @return The current context object
256 */
257 protected Object getCtx() {
258 return getCtx(pageContext);
259 }
260
261 /***
262 * Returns the current context object
263 *
264 * @return The current context object
265 */
266 protected Object getCtx(int i) {
267 return peekCtx(i)[0];
268 }
269
270 /***
271 * Returns the current context path
272 *
273 * @return
274 */
275 protected static String getCtxPath(PageContext pageContext) {
276 return peekCtx(pageContext)[1].toString();
277 }
278
279 /***
280 * Returns the current context path
281 *
282 * @return
283 */
284 protected String getCtxPath() {
285 return getCtxPath(pageContext);
286 }
287
288 /***
289 * Returns the current context path
290 *
291 * @return
292 */
293 protected String getCtxPath(int i) {
294 return peekCtx(i)[1].toString();
295 }
296
297 /***
298 * Returns the String value of a field expression evaluated on the given
299 * context
300 *
301 * @param fieldExpr
302 * The field expression to evaluate
303 * @return The result as String of the expression evaluation
304 */
305 protected Object getExpressionValue(Object ctx, String fieldExpr) {
306 if (this.parent != null && parent instanceof EWCTag) {
307 fieldExpr = ((EWCTag) parent).substituteParentParameters(fieldExpr);
308 }
309 return getEWCSession().getExpressionValue(ctx, fieldExpr);
310 }
311
312 /***
313 * Returns the String value of a field expression evaluated on the given
314 * context
315 *
316 * @param fieldExpr
317 * The field expression to evaluate
318 * @return The result as String of the expression evaluation
319 */
320 protected String getExpressionValueAsString(Object ctx, String fieldExpr) {
321 Object out = getExpressionValue(ctx, fieldExpr);
322 return (out == null ? "" : out.toString());
323 }
324
325 /***
326 * Returns the value of a field expression evaluated on the current
327 * context. This current context can be modified using "../"
328 * subexpressions. Thus, "../.name" is conceptually equivalent to
329 * "getParent().getName()".
330 *
331 * @param fieldExpr
332 * The field expression to evaluate
333 * @return The result of the expression evaluation
334 */
335 protected Object getExpressionValue(String fieldExpr) {
336 int i;
337 for (i = 0; fieldExpr.startsWith("../"); i++) {
338 fieldExpr = fieldExpr.substring(3);
339 }
340 return getExpressionValue(getCtx(i), fieldExpr);
341 }
342
343 /***
344 * Returns the String value of a field expression evaluated on the current
345 * context
346 *
347 * @param fieldExpr
348 * The field expression to evaluate
349 * @return The result as String of the expression evaluation
350 */
351 protected String getExpressionValueAsString(String fieldExpr) {
352 Object out = getExpressionValue(fieldExpr);
353 return (out == null ? "" : out.toString());
354 }
355
356 /***
357 * Transforms the user expression, substituting the context parameters
358 *
359 * @return
360 */
361 protected String substituteParentParameters(String exp) {
362 if (exp.indexOf('@') > 0 && this.parent != null
363 && parent instanceof EWCTag) {
364 return ((EWCTag) parent).substituteParentParameters(exp);
365 } else {
366 return exp;
367 }
368 }
369
370 /***
371 * Returns the component's property value, or the default value if it
372 * wasn't set
373 *
374 * @param property
375 * The property name
376 * @param defValue
377 * The default value
378 * @return The property value
379 */
380 protected String getComponentPropertyValue(String property, String defValue) {
381 return getEWCSession().getComponentPropertyValue(getCtx(), property,
382 defValue);
383 }
384
385 /***
386 * Returns the expression that access to a component's property
387 *
388 * @param property
389 * The property
390 * @return
391 */
392 protected String getComponentPropertyFullExpression(String property) {
393 return getEWCSession().getComponentPropertyFullExpression(
394 getCtx(),
395 property);
396 }
397
398 /***
399 * Composes two expressions in such a way that <code>eval(ctx,compose(exp1,exp2))==eval(eval(ctx,exp1),exp2)</code>
400 *
401 * @param exp1
402 * The first expression
403 * @param exp2
404 * The secont expression
405 * @return The composed expression
406 */
407 protected String getFullExpression(String exp) {
408 int i;
409 for (i = 0; exp.startsWith("../"); i++) {
410 exp = exp.substring(3);
411 }
412 return getEWCSession().getComposedExpression(getCtxPath(i), exp);
413 }
414
415 /***
416 * Composes two expressions in such a way that <code>eval(ctx,compose(exp1,exp2))==eval(eval(ctx,exp1),exp2)</code>
417 *
418 * @param exp1
419 * The first expression
420 * @param exp2
421 * The secont expression
422 * @return The composed expression
423 */
424 protected String getComposedExpression(String exp1, String exp2) {
425 return getEWCSession().getComposedExpression(exp1, exp2);
426 }
427
428 /***
429 * Returns an indexed expression so <code>eval(ctx, indexed(exp1,exp2))==eval(ctx,exp1)[eval(ctx,exp2)]</code>
430 *
431 * @param exp1
432 * The first expression
433 * @param exp2
434 * The secont expression
435 * @return The indexed expression
436 */
437 protected String getIndexedExpression(String exp1, String exp2) {
438 return getEWCSession().getIndexedExpression(exp1, exp2);
439 }
440
441 /***
442 * Returns the event String that should be sent to the controller. It just
443 * adds the context.
444 *
445 * @param event
446 * @return
447 */
448 protected String getEvent(String event) {
449 if (event == null) return null;
450 int i;
451 for (i = 0; event.startsWith("../"); i++) {
452 event = event.substring(3);
453 }
454 if (this.parent != null && parent instanceof EWCTag) {
455 event = ((EWCTag) parent).substituteParentParameters(event);
456 }
457 return getEWCSession().getEventFullExpression(getCtxPath(i), event);
458 }
459
460 /***
461 * @return Returns the next processing action
462 * @throws JspException
463 */
464 public int doStartTag() throws JspException {
465 return EVAL_BODY_INCLUDE;
466 }
467
468 /***
469 * @return Returns the next processing action
470 * @throws JspException
471 */
472 public int doEndTag() throws JspException {
473 return EVAL_PAGE;
474 }
475
476 /***
477 *
478 */
479 public void release() {
480 // Do nothing
481 }
482
483 /***
484 * Sets the page context
485 *
486 * @param pageContext
487 * The page context
488 */
489 public void setPageContext(PageContext pageContext) {
490 this.pageContext = pageContext;
491 this.out = pageContext.getOut();
492 this.session = getEWCSession(pageContext);
493 }
494
495 /***
496 * Sets the parent tag
497 *
498 * @param parent
499 * The parent tag
500 */
501 public void setParent(Tag parent) {
502 this.parent = parent;
503 }
504
505 /***
506 * Returns the parent tag
507 *
508 * @return The parent tag
509 */
510 public Tag getParent() {
511 return parent;
512 }
513
514 /***
515 * Prints <code><tagName</code> through the JSP writer
516 *
517 * @param tagName
518 * The tag name
519 * @throws IOException
520 */
521 protected void printBeginTag(String tagName) throws IOException {
522 out.print("<");
523 out.print(tagName);
524 }
525
526 /***
527 * Prints <code> attName="attValue"</code> through the JSP writer
528 *
529 * @param attName
530 * The attribute name
531 * @param attValue
532 * The attribute value
533 * @throws IOException
534 */
535 protected void printAttribute(String attName, String attValue)
536 throws IOException {
537 if (attValue != null) {
538 out.print(' ');
539 out.print(attName);
540 out.print("=\"");
541 out.print(attValue);
542 out.print('"');
543 }
544 }
545
546 /***
547 * Prints <code> ></code> through the JSP writer
548 *
549 * @throws IOException
550 */
551 protected void printEndBeginTag() throws IOException {
552 out.print(">");
553 }
554
555 /***
556 * Prints <code> /></code> through the JSP writer
557 *
558 * @throws IOException
559 */
560 protected void printEndTag() throws IOException {
561 out.print("/>");
562 }
563
564 /***
565 * Prints <code></tagName></code> through the JSP writer
566 *
567 * @param tagName
568 * The tag name
569 * @throws IOException
570 */
571 protected void printEndTag(String tagName) throws IOException {
572 out.print("</");
573 out.print(tagName);
574 out.print(">");
575 }
576 }
This page was automatically generated by Maven