Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/aendvari/tethys/tag/data/TagData.java


1   /*
2    * TagData.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.data;
11  
12  import javax.servlet.*;
13  import javax.servlet.http.*;
14  import javax.servlet.jsp.*;
15  
16  import com.aendvari.common.model.*;
17  import com.aendvari.common.util.ServletUtil;
18  
19  import com.aendvari.tethys.tag.*;
20  
21  /**
22   * <p>Provides access to tag data. This is an abstract class to provide basic
23   * functionality for more specific tag data.</p>
24   *
25   * <p>The get/set methods are intentionally named <code>get/setTagData</code> to allow
26   * the subclass to wrap them with the name <code>get/setData</code>.</p>
27   *
28   * @author Trevor Milne
29   *
30   */
31  
32  public abstract class TagData
33  {
34    /* Constants */
35  
36  
37    /** Constants for servlet locations of tag data objects. */
38    protected final static String TagDataBase = "satyr/tag";
39  
40  
41    /* Constructors */
42  
43  
44      /**
45       * Constructs an empty <code>TagData</code>.
46       *
47       */
48  
49    public TagData()
50    {
51    }
52  
53  
54    /* Access */
55  
56  
57    /**
58     * Returns the servlet attribute key for this tag data.
59     * This method is to be implemented by subclasses.
60     *
61     * @return                  The servlet attribute location for this tag data.
62     *
63     */
64  
65    protected abstract String getDataKey();
66  
67  
68    /**
69     * Returns a new instance of this tag data.
70     * This method is to be implemented by subclasses.
71     *
72     * @return                  The new tag data object.
73     *
74     */
75  
76    protected abstract TagData createObject();
77  
78  
79    /* Servlet access */
80  
81  
82    /**
83     * Returns the tag data that is stored in the request or session.
84     * If one does not exist, a new instance is created and stored.
85     *
86       * @param    request            The servlet request being processed.
87     *
88     * @return                  The specified {@link TagData}, null if not available.
89     *
90     */
91  
92    protected TagData getTagData(HttpServletRequest request)
93    {
94      // get the tag data
95      TagData data = (TagData)ServletUtil.getAttribute(request, getDataKey());
96  
97      // create instance if necessary
98      if (data == null)
99      {
100       data = createObject();
101 
102       setTagData(request, data);
103     }
104 
105     return data;
106   }
107 
108   /**
109    * Returns the tag data that is stored in the session.
110    * If one does not exist, a new instance is created and stored.
111    *
112      * @param    session            The servlet session being processed.
113    *
114    * @return                  The specified {@link TagData}, null if not available.
115    *
116    */
117 
118   protected TagData getTagData(HttpSession session)
119   {
120     // get the tag data
121     TagData data = (TagData)session.getAttribute(getDataKey());
122 
123     // create instance if necessary
124     if (data == null)
125     {
126       data = createObject();
127 
128       setTagData(session, data);
129     }
130 
131     return data;
132   }
133 
134   /**
135    * Returns the tag data that is stored in the servlet context.
136    * If one does not exist, a new instance is created and stored.
137    *
138      * @param    context            The servlet context being processed.
139    *
140    * @return                  The specified {@link TagData}, null if not available.
141    *
142    */
143 
144   protected TagData getTagData(ServletContext context)
145   {
146     // get the tag data
147     TagData data = (TagData)context.getAttribute(getDataKey());
148 
149     // create instance if necessary
150     if (data == null)
151     {
152       data = createObject();
153 
154       setTagData(context, data);
155     }
156 
157     return data;
158   }
159 
160   /**
161    * Returns the tag data that is stored in the page context.
162    * If one does not exist, a new instance is created and stored.
163    *
164    * @param    context            The <code>pageContext</code> value from within a taglet.
165    *
166    * @return                  The specified {@link TagData}, null if not available.
167    *
168    */
169 
170   protected TagData getTagData(PageContext context)
171   {
172     // get the tag data
173     TagData data = (TagData)context.getAttribute(getDataKey());
174 
175     // create instance if necessary
176     if (data == null)
177     {
178       data = createObject();
179       context.setAttribute(getDataKey(), data);
180     }
181 
182     return data;
183   }
184 
185   /**
186    * Returns the tag data that is stored in the specified scope.
187    * If one does not exist, a new instance is created and stored.
188    *
189    * @param    context            The <code>pageContext</code> value from within a taglet.
190    * @param    scope            The scope to search.
191    *
192    * @return                  The specified {@link TagData}, null if not available.
193    *
194    */
195 
196   protected TagData getTagData(PageContext context, String scope)
197   {
198     // get the tag data
199     TagData data = (TagData)ServletUtil.getAttribute(context, scope, getDataKey());
200 
201     // create instance if necessary
202     if (data == null)
203     {
204       data = createObject();
205 
206       setTagData(context, scope, data);
207     }
208 
209     return data;
210   }
211 
212   /**
213    * Sets the tag data in the request.
214    *
215      * @param    request            The servlet request to store the data into.
216      * @param    data            The {@link TagData} to store.
217    *
218    */
219 
220   protected void setTagData(HttpServletRequest request, TagData data)
221   {
222     request.setAttribute(getDataKey(), data);
223   }
224 
225   /**
226    * Sets the tag data in the session.
227    *
228      * @param    session            The servlet session to store the data into.
229      * @param    data            The {@link TagData} to store.
230    *
231    */
232 
233   protected void setTagData(HttpSession session, TagData data)
234   {
235     session.setAttribute(getDataKey(), data);
236   }
237 
238   /**
239    * Sets the tag data in the servlet context.
240    *
241      * @param    context            The servlet context to store the data into.
242      * @param    data            The {@link TagData} to store.
243    *
244    */
245 
246   protected void setTagData(ServletContext context, TagData data)
247   {
248     context.setAttribute(getDataKey(), data);
249   }
250 
251   /**
252    * Sets the tag data into the specified scope.
253    *
254    * @param    context            The <code>pageContext</code> value from within a taglet.
255    * @param    scope            The scope to place the data.
256    *
257    * @return                  A {@link TagData} instance.
258    *
259    */
260 
261   protected void setTagData(PageContext context, String scope, TagData data)
262   {
263     ServletUtil.setAttribute(context, scope, getDataKey(), data);
264   }
265 }
266