KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > tag > common > core > ParamSupport


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.standard.tag.common.core;
18
19 import java.util.LinkedList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import javax.servlet.jsp.JspException JavaDoc;
23 import javax.servlet.jsp.JspTagException JavaDoc;
24 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
25 import javax.servlet.jsp.tagext.Tag JavaDoc;
26
27 import org.apache.taglibs.standard.resources.Resources;
28
29 /**
30  * <p>Support for tag handlers for &lt;param&gt;, the URL parameter
31  * subtag for &lt;import&gt; in JSTL 1.0.</p>
32  *
33  * @see ParamParent, ImportSupport, URLEncodeSupport
34  * @author Shawn Bayern
35  */

36
37 public abstract class ParamSupport extends BodyTagSupport JavaDoc {
38
39     //*********************************************************************
40
// Protected state
41

42     protected String JavaDoc name; // 'name' attribute
43
protected String JavaDoc value; // 'value' attribute
44

45     /**
46      * There used to be an 'encode' attribute; I've left this as a
47      * vestige in case custom subclasses want to use our functionality
48      * but NOT encode parameters.
49      */

50     protected boolean encode = true;
51
52     //*********************************************************************
53
// Constructor and initialization
54

55     public ParamSupport() {
56     super();
57     init();
58     }
59
60     private void init() {
61     name = value = null;
62     }
63
64     //*********************************************************************
65
// Tag logic
66

67     // simply send our name and value to our appropriate ancestor
68
public int doEndTag() throws JspException JavaDoc {
69     Tag JavaDoc t = findAncestorWithClass(this, ParamParent.class);
70     if (t == null)
71         throw new JspTagException JavaDoc(
72         Resources.getMessage("PARAM_OUTSIDE_PARENT"));
73
74     // take no action for null or empty names
75
if (name == null || name.equals(""))
76         return EVAL_PAGE;
77
78     // send the parameter to the appropriate ancestor
79
ParamParent parent = (ParamParent) t;
80     String JavaDoc value = this.value;
81     if (value == null) {
82         if (bodyContent == null || bodyContent.getString() == null)
83         value = "";
84         else
85         value = bodyContent.getString().trim();
86     }
87         if (encode) {
88             // FIXME: revert to java.net.URLEncoder.encode(s, enc) once
89
// we have a dependency on J2SE 1.4+.
90
String JavaDoc enc = pageContext.getResponse().getCharacterEncoding();
91             parent.addParameter(
92             Util.URLEncode(name, enc), Util.URLEncode(value, enc));
93         } else {
94             parent.addParameter(name, value);
95         }
96     return EVAL_PAGE;
97     }
98
99     // Releases any resources we may have (or inherit)
100
public void release() {
101     init();
102     }
103
104     //*********************************************************************
105
// Support for parameter management
106

107     /**
108      * Provides support for aggregating query parameters in URLs.
109      * Specifically, accepts a series of parameters, ensuring that
110      * - newer parameters will precede older ones in the output URL
111      * - all supplied parameters precede those in the input URL
112      */

113     public static class ParamManager {
114
115         //*********************************
116
// Private state
117

118     private List JavaDoc names = new LinkedList JavaDoc();
119         private List JavaDoc values = new LinkedList JavaDoc();
120     private boolean done = false;
121         
122     //*********************************
123
// Public interface
124

125     /** Adds a new parameter to the list. */
126         public void addParameter(String JavaDoc name, String JavaDoc value) {
127         if (done)
128         throw new IllegalStateException JavaDoc();
129         if (name != null) {
130             names.add(name);
131             if (value != null)
132             values.add(value);
133             else
134             values.add("");
135         }
136     }
137
138     /**
139          * Produces a new URL with the stored parameters, in the appropriate
140          * order.
141          */

142     public String JavaDoc aggregateParams(String JavaDoc url) {
143         /*
144              * Since for efficiency we're destructive to the param lists,
145              * we don't want to run multiple times.
146              */

147         if (done)
148         throw new IllegalStateException JavaDoc();
149         done = true;
150
151         //// reverse the order of our two lists
152
// Collections.reverse(this.names);
153
// Collections.reverse(this.values);
154

155         // build a string from the parameter list
156
StringBuffer JavaDoc newParams = new StringBuffer JavaDoc();
157         for (int i = 0; i < names.size(); i++) {
158         newParams.append(names.get(i) + "=" + values.get(i));
159         if (i < (names.size() - 1))
160             newParams.append("&");
161         }
162
163         // insert these parameters into the URL as appropriate
164
if (newParams.length() > 0) {
165             int questionMark = url.indexOf('?');
166             if (questionMark == -1) {
167             return (url + "?" + newParams);
168             } else {
169             StringBuffer JavaDoc workingUrl = new StringBuffer JavaDoc(url);
170             workingUrl.insert(questionMark + 1, (newParams + "&"));
171             return workingUrl.toString();
172             }
173         } else {
174         return url;
175         }
176     }
177     }
178 }
179
Popular Tags