KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > input > tags > VariablesTag


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.input.tags;
21
22 import java.util.Collection JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24
25 import javax.servlet.jsp.JspException JavaDoc;
26 import javax.servlet.jsp.PageContext JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.struts.taglib.TagUtils;
31 import org.apache.struts.taglib.html.BaseFieldTag;
32 import org.apache.struts.util.MessageResources;
33
34 import com.sslexplorer.boot.PropertyClass;
35 import com.sslexplorer.boot.PropertyClassManager;
36 import com.sslexplorer.boot.PropertyDefinition;
37 import com.sslexplorer.core.CoreUtil;
38 import com.sslexplorer.properties.attributes.AttributeDefinition;
39 import com.sslexplorer.properties.attributes.AttributesPropertyClass;
40
41 /**
42  * Tag this inserts a component that allows the user to select a replacement
43  * variable to be inserted into an input component.
44  *
45  * @author Brett Smith <brett@3sp.com>
46  */

47 public class VariablesTag extends BaseFieldTag {
48
49     final static Log log = LogFactory.getLog(VariablesTag.class);
50
51     // Protected instance variables
52

53     protected String JavaDoc variables;
54     protected String JavaDoc fragment;
55     protected String JavaDoc inputId;
56     protected boolean includeUserAttributes;
57     protected boolean includeRequestAttributes;
58     protected boolean includeSession;
59     protected String JavaDoc titleKey;
60     protected boolean disabled;
61
62     /**
63      * The message resources for this package.
64      */

65     protected static MessageResources messages = MessageResources.getMessageResources("org.apache.struts.taglib.bean.LocalStrings");
66
67     /**
68      * Constructor
69      */

70     public VariablesTag() {
71         includeUserAttributes = true;
72         includeSession = true;
73         includeRequestAttributes = false;
74         disabled = false;
75     }
76
77     /**
78      * Set the messsage resources key to use for the title
79      *
80      * @param titleKey title message resources key
81      */

82     public void setTitleKey(String JavaDoc titleKey) {
83         this.titleKey = titleKey;
84     }
85
86     /**
87      * Set the id of the component to insert the chosen variable into.
88      *
89      * @param inputId input component id
90      */

91     public void setInputId(String JavaDoc inputId) {
92         this.inputId = inputId;
93     }
94
95     /**
96      * Set the comma separated list of variables names
97      *
98      * @param variables variables.
99      */

100     public void setVariables(String JavaDoc variables) {
101         this.variables = variables;
102     }
103
104     /**
105      * Set whether all of the user attributes should be added to the list of
106      * variables
107      *
108      * @param includeUserAttributes include user attributes
109      *
110      */

111     public void setIncludeUserAttributes(boolean includeUserAttributes) {
112         this.includeUserAttributes = includeUserAttributes;
113     }
114
115     /**
116      * Set whether all of the <i>session</i> replacement variables
117      *
118      * @param includeSession include session
119      *
120      */

121     public void setIncludeSession(boolean includeSession) {
122         this.includeSession = includeSession;
123     }
124
125     /**
126      * Set whether all of the <i>request</i> replacement variables
127      *
128      * @param includeRequest include session
129      *
130      */

131     public void setIncludeRequest(boolean includeRequest) {
132         this.includeRequestAttributes = includeRequest;
133     }
134     /*
135      * (non-Javadoc)
136      *
137      * @see javax.servlet.jsp.tagext.BodyTagSupport#doEndTag()
138      */

139     public int doEndTag() throws JspException JavaDoc {
140         if(fragment != null) {
141             TagUtils.getInstance().write(pageContext, fragment);
142         }
143         return (EVAL_PAGE);
144     }
145
146     /*
147      * (non-Javadoc)
148      *
149      * @see javax.servlet.jsp.tagext.BodyTagSupport#release()
150      */

151     public void release() {
152         super.release();
153         fragment = null;
154         variables = null;
155         inputId = null;
156         includeUserAttributes = true;
157         includeSession = true;
158         includeRequestAttributes = false;
159         disabled = false;
160     }
161
162     /*
163      * (non-Javadoc)
164      *
165      * @see javax.servlet.jsp.tagext.BodyTagSupport#doStartTag()
166      */

167     public int doStartTag() throws JspException JavaDoc {
168         if (disabled)
169             return SKIP_PAGE;
170             
171         if(getBundle() == null || getBundle().equals("")) {
172             setBundle("navigation");
173         }
174         fragment = null;
175         if (inputId != null) {
176             fragment = generateReplacementVariableChooserFragment(titleKey, pageContext, getBundle(), getLocale(), inputId, variables, includeSession, includeUserAttributes, includeRequestAttributes);
177         } else {
178             log.warn("Both variables and inputId attributes must be specified for variables tag");
179             return SKIP_PAGE;
180         }
181         return (EVAL_BODY_AGAIN);
182     }
183     
184     public static String JavaDoc generateReplacementVariableChooserFragment(String JavaDoc titleKey, PageContext JavaDoc pageContext, String JavaDoc bundle, String JavaDoc locale, String JavaDoc inputId, String JavaDoc variables, boolean includeSession, boolean includeUserAttributes, boolean includeRequest) throws JspException JavaDoc {
185         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
186       
187         String JavaDoc title = null;
188         if (titleKey != null) {
189             title = TagUtils.getInstance().message(pageContext, bundle, locale, titleKey, new String JavaDoc[] {});
190         }
191         if (title == null) {
192             title = TagUtils.getInstance().message(pageContext, bundle, locale,
193                             "replacementVariablesChooser.title", new String JavaDoc[] {});
194             if(title == null) {
195                 title = "Replacement Variables";
196             }
197         }
198         buf.append("<div class=\"component_replacementVariablesToggle\">");
199         
200 // buf.append("<input type=\"button\" onclick=\"toggleAndPositionBelow(document.getElementById('replacementVariablesChooser");
201
// buf.append(inputId);
202
// buf.append("'), document.getElementById('");
203
// buf.append(inputId);
204
// buf.append("'))\" ");
205
// buf.append("value=\"${}\"/>");
206

207         buf.append("<img onclick=\"togglePopupBelowLeft(document.getElementById('replacementVariablesChooser");
208         buf.append(inputId);
209         buf.append("'), document.getElementById('");
210         buf.append(inputId);
211         buf.append("'))\" ");
212         buf.append("src=\"");
213         buf.append(CoreUtil.getThemePath(pageContext.getSession()));
214         buf.append("/images/variables.gif\"/>");
215         
216         buf.append("</div>");
217         buf.append("<div id=\"replacementVariablesChooser");
218         buf.append(inputId);
219         buf.append("\" style=\"position:absolute;display: none;overflow:visible;top:4px;left:4px;z-index:900;\">");
220         buf.append("<div class=\"replacementVariablesMain\">");
221         buf.append("<div class=\"replacementVariablesTitleBar\">");
222         buf.append("<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");
223         buf.append("<tr><td class=\"title\">");
224         buf.append(title);
225         buf.append("</td><td class=\"close\"><img SRC=\"");
226         buf.append(CoreUtil.getThemePath(pageContext.getSession()));
227         buf.append("/images/actions/erase.gif\" ");
228         buf.append("onclick=\"document.getElementById('");
229         buf.append(inputId);
230         buf.append("').value = ''\"/><img SRC=\"");
231         buf.append(CoreUtil.getThemePath(pageContext.getSession()));
232         buf.append("/images/actions/closeReplacementVariables.gif\" ");
233         buf.append("onclick=\"togglePopup(document.getElementById('replacementVariablesChooser");
234         buf.append(inputId);
235         buf.append("'))\"/>");
236         buf.append("</td></tr></table></div><div class=\"replacementVariablesContent\">");
237         buf.append("<ul>");
238         if(variables != null) {
239             StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(variables, ",");
240             while (t.hasMoreTokens()) {
241                 String JavaDoc n = t.nextToken();
242                 addVariable(n, n, buf, pageContext, bundle, locale, inputId);
243             }
244         }
245         if(includeSession) {
246             addVariable("session:username", null, buf, pageContext, bundle, locale, inputId);
247             addVariable("session:password", null, buf, pageContext, bundle, locale, inputId);
248             addVariable("session:email", null, buf, pageContext, bundle, locale, inputId);
249             addVariable("session:fullname", null, buf, pageContext, bundle, locale, inputId);
250             addVariable("session:clientProxyURL", null, buf, pageContext, bundle, locale, inputId);
251         }
252         
253         if(includeRequest) {
254             addVariable("request:serverName", null, buf, pageContext, bundle, locale, inputId);
255             addVariable("request:serverPort", null, buf, pageContext, bundle, locale, inputId);
256             addVariable("request:userAgent", null, buf, pageContext, bundle, locale, inputId);
257         }
258         
259         if (includeUserAttributes) {
260             Collection JavaDoc<PropertyDefinition> l;
261             try {
262                 for (PropertyClass propertyClass : PropertyClassManager.getInstance().getPropertyClasses()) {
263                     if (propertyClass instanceof AttributesPropertyClass) {
264                         l = propertyClass.getDefinitions();
265                         for (PropertyDefinition d : l) {
266                             AttributeDefinition def = (AttributeDefinition)d;
267                             if (def.isReplaceable()) {
268                                 addVariable(def.getPropertyClass().getName() + ":" + def.getName(), def.getDescription(), buf, pageContext, bundle, locale, inputId);
269                             }
270                         }
271                     }
272                 }
273             } catch (Exception JavaDoc e) {
274                 log.error("Failed to get user attribute definitions.");
275             }
276         }
277         buf.append("</ul>");
278         buf.append("</div>");
279         buf.append("</div>");
280         return buf.toString();
281     }
282
283     static void addVariable(String JavaDoc variable, String JavaDoc desc, StringBuffer JavaDoc buf, PageContext JavaDoc pageContext, String JavaDoc bundle, String JavaDoc locale, String JavaDoc inputId) throws JspException JavaDoc {
284         buf.append("<li><a onmouseover=\"return escape('");
285         if(desc == null) {
286             desc = TagUtils.getInstance().message(pageContext, bundle, locale, "replacementVariable." + variable + ".description", new String JavaDoc[] {});
287             if(desc == null) {
288                 desc = "${" + variable + "}";
289             }
290         }
291         buf.append(desc);
292         buf.append("')\" HREF=\"#\" onclick=\"document.getElementById('");
293         buf.append(inputId);
294         buf.append("').value = document.getElementById('");
295         buf.append(inputId);
296         buf.append("').value + '${");
297         buf.append(variable);
298         buf.append("}'; togglePopup(document.getElementById('replacementVariablesChooser");
299         buf.append(inputId);
300         buf.append("'))\">");
301         buf.append(variable);
302         buf.append("</a></li>");
303     }
304
305     /* (non-Javadoc)
306      * @see org.apache.struts.taglib.html.BaseHandlerTag#setDisabled(boolean)
307      */

308     public void setDisabled(boolean disabled) {
309         this.disabled = disabled;
310     }
311 }
312
Popular Tags