Source code: com/aendvari/tethys/tag/basic/DefineTag.java
1 /*
2 * DefineTag.java
3 *
4 * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5 *
6 * See the file LICENSE for terms of use.
7 *
8 */
9
10 package com.aendvari.tethys.tag.basic;
11
12 import java.io.IOException;
13 import javax.servlet.http.*;
14 import javax.servlet.jsp.*;
15 import javax.servlet.jsp.tagext.*;
16
17 import com.aendvari.common.model.*;
18
19 import com.aendvari.tethys.*;
20 import com.aendvari.tethys.context.*;
21 import com.aendvari.tethys.tag.*;
22 import com.aendvari.tethys.tag.model.*;
23
24 import com.aendvari.common.util.ServletUtil;
25
26
27 /**
28 * <p>Implementation class for the "define" tag.</p>
29 *
30 * @author Scott Milne
31 *
32 */
33
34 public class DefineTag extends ModelTreeTag
35 {
36 /* Variables */
37
38 /** The type of the define (path or value). */
39 public String type;
40
41 /** the scope in which to save the define */
42 protected String scope;
43
44 /** true/false if the variable should be saved into page scope. See DefineTEI. */
45 protected String bean;
46
47
48 /* Attributes */
49
50 public String getType() { return type; }
51 public void setType( String type ) { this.type = type; }
52
53 public String getScope() { return scope ;}
54 public void setScope( String scope ) { this.scope = scope; }
55
56 public String getBean() { return bean ;}
57 public void setBean( String setBean ) { this.bean = setBean; }
58
59
60 /* Constructor */
61
62 public DefineTag()
63 {
64 super();
65
66 type = null;
67 scope = null;
68 bean = null;
69 }
70
71 public int doStartTag() throws JspTagException
72 {
73 // check for the scope tag
74 if (
75 (scope.compareToIgnoreCase(ServletUtil.Scope.Session)!=0) &&
76 (scope.compareToIgnoreCase(ServletUtil.Scope.Page)!=0) &&
77 (scope.compareToIgnoreCase(ServletUtil.Scope.Request)!=0)
78 )
79 {
80 throw new JspTagException("DefineTag: \"scope\" must be either \"session\", \"page\", \"application\" or \"request\"");
81 }
82
83 // check for the type tag. It's not required, but must be valid
84 if (type != null)
85 {
86 if (!type.equals("path") && !type.equals("value"))
87 {
88 throw new JspTagException("DefineTag: \"type\" (not required) must be either \"value\" or \"path\"");
89 }
90 }
91 else
92 {
93 type = "value";
94 }
95
96 // establish tag context
97 establishModelContext();
98
99 // get the value from the path
100 ModelNode modelNode = getModelNode(path, true);
101
102 String attributeValue = "";
103
104 if (modelNode != null)
105 {
106 if (type.equals("value"))
107 {
108 // set the tag value to return
109 attributeValue = modelNode.getNodeValue();
110 }
111 else
112 {
113 // set the tag value to return
114 attributeValue = modelContext.extendModelPath(path);
115
116 // for visual purposes, convert all "." into "/"
117 // OSM supports both, but it looks better to have all the same
118 attributeValue = attributeValue.replace('.', '/');
119
120 // trim it just in case
121 attributeValue = attributeValue.trim();
122 }
123 }
124
125 // get the request
126 HttpServletRequest request =
127 (HttpServletRequest)pageContext.getRequest();
128
129 // get the session
130 HttpSession session = request.getSession(true);
131
132 int inScope = PageContext.PAGE_SCOPE;
133
134 // store the value where it's needed
135 if( (scope.compareToIgnoreCase(ServletUtil.Scope.Session)==0) )
136 {
137 inScope = PageContext.SESSION_SCOPE;
138 }
139 else if( (scope.compareToIgnoreCase(ServletUtil.Scope.Request)==0) )
140 {
141 inScope = PageContext.REQUEST_SCOPE;
142 }
143 else if( (scope.compareToIgnoreCase(ServletUtil.Scope.Page)==0) )
144 {
145 inScope = PageContext.PAGE_SCOPE;
146 }
147 else if( (scope.compareToIgnoreCase(ServletUtil.Scope.Application)==0) )
148 {
149 inScope = PageContext.APPLICATION_SCOPE;
150 }
151
152 // Expose this value as a scripting variable
153 pageContext.setAttribute(getName(), attributeValue, inScope);
154
155 // skip the body of the tag
156 return EVAL_BODY_INCLUDE;
157 }
158
159 /**
160 * Release all allocated resources.
161 *
162 */
163
164 public void release()
165 {
166 super.release();
167
168 type = null;
169 scope = null;
170 bean = null;
171 }
172 }
173