KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > component > RendererParameterTag


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.component;
14
15 import java.util.Map JavaDoc;
16
17 import javax.servlet.http.HttpServletRequest JavaDoc;
18 import javax.servlet.jsp.JspException JavaDoc;
19 import javax.servlet.jsp.JspTagException JavaDoc;
20 import javax.servlet.jsp.jstl.core.ConditionalTagSupport;
21
22 import org.apache.log4j.Logger;
23
24 import com.tonbeller.wcf.controller.RequestContext;
25 import com.tonbeller.wcf.expr.ExprUtils;
26
27 /**
28  * Adds an XSL parameter for the transformation. If used inside a
29  * render tag, the parameter is local to that transformation. Otherwise
30  * the scope attribute specifies the scope of the parameter, legal values
31  * are "request" (default), "session" and "application".
32  * <p></p>
33  * The value may be provided as literal, or as expression (starting with "${").
34  *
35  * @author av
36  */

37 public class RendererParameterTag extends ConditionalTagSupport {
38   private static Logger logger = Logger.getLogger(RendererParameterTag.class);
39
40   String JavaDoc name;
41   String JavaDoc value;
42   String JavaDoc scope;
43   String JavaDoc test;
44
45   public void setTest(String JavaDoc test) {
46     this.test = test;
47   }
48
49   /**
50    * adds a parameter to the parent RendererTag (local) or to the
51    * session-global xsl parameter map.
52    */

53   public int doStartTag() throws JspException JavaDoc {
54     logger.info("enter");
55     RequestContext context = RequestContext.instance();
56
57     String JavaDoc paramName = name;
58     String JavaDoc paramScope = (scope == null ? "request" : scope);
59     Object JavaDoc paramValue;
60     if (ExprUtils.isExpression(value))
61       paramValue = context.getModelReference(value);
62     else
63       paramValue = value;
64
65     RendererTag rt = (RendererTag) super.findAncestorWithClass(this, RendererTag.class);
66     if (rt != null) {
67       // parameter is local to this transformation
68
if (paramValue == null)
69         rt.removeParameter(paramName);
70       else
71         rt.addParameter(paramName, paramValue);
72     } else {
73       // scoped parameter
74
if (paramValue == null)
75         RendererParameters.removeParameter(context.getRequest(), paramName, paramValue, paramScope);
76       else
77         RendererParameters.setParameter(context.getRequest(), paramName, paramValue, paramScope);
78     }
79     logger.info("leave");
80     return super.doStartTag();
81   }
82
83   /**
84    * Sets the name.
85    * @param name The name to set
86    */

87   public void setName(String JavaDoc name) {
88     this.name = name;
89   }
90
91   /**
92    * Sets the value.
93    * @param value The value to set
94    */

95   public void setValue(String JavaDoc value) {
96     this.value = value;
97   }
98
99   /**
100    * @param string
101    */

102   public void setScope(String JavaDoc string) {
103     scope = string;
104   }
105
106   protected boolean condition() throws JspTagException JavaDoc {
107     if (test == null)
108       return false;
109
110     boolean b = true;
111     String JavaDoc v = value;
112     if (v != null && v.startsWith("!")) {
113       v = v.substring(1);
114       b = false;
115     }
116
117     HttpServletRequest JavaDoc hsr = (HttpServletRequest JavaDoc) pageContext.getRequest();
118     Map JavaDoc map = RendererParameters.getParameterMap(hsr);
119     String JavaDoc x = (String JavaDoc) map.get(test);
120     if (x == null)
121       return !b;
122     if (v == null)
123       return b;
124     if (v.equals(x))
125       return b;
126     return !b;
127   }
128
129 }
130
Popular Tags