View Javadoc

1   package org.apache.turbine.util.uri;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Iterator;
23  
24  import org.apache.commons.lang.StringUtils;
25  
26  import org.apache.turbine.util.RunData;
27  import org.apache.turbine.util.ServerData;
28  import org.apache.turbine.util.parser.ParameterParser;
29  
30  /***
31   * This class allows you to keep all the information needed for a single
32   * link at one place. It keeps your query data, path info, the server
33   * scheme, name, port and the script path. It is tuned for usage with a
34   * Template System e.g. Velocity.
35   *
36   * If you must generate a Turbine Link in a Template System, use this class.
37   *
38   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
39   * @version $Id: TemplateURI.java 534527 2007-05-02 16:10:59Z tv $
40   */
41  
42  public class TemplateURI
43          extends TurbineURI
44  {
45      /***
46       * Empty C'tor. Uses Turbine.getDefaultServerData().
47       *
48       */
49      public TemplateURI()
50      {
51          super();
52      }
53  
54      /***
55       * Constructor with a RunData object
56       *
57       * @param runData A RunData object
58       */
59      public TemplateURI(RunData runData)
60      {
61          super(runData);
62      }
63  
64      /***
65       * Constructor, set explicit redirection
66       *
67       * @param runData A RunData object
68       * @param redirect True if redirection allowed.
69       */
70      public TemplateURI(RunData runData, boolean redirect)
71      {
72          super(runData, redirect);
73      }
74  
75      /***
76       * Constructor, set Template
77       *
78       * @param runData A RunData object
79       * @param template A Template Name
80       */
81      public TemplateURI(RunData runData, String template)
82      {
83          super(runData);
84          setTemplate(template);
85      }
86  
87      /***
88       * Constructor, set Template, set explicit redirection
89       *
90       * @param runData A RunData object
91       * @param template A Template Name
92       * @param redirect True if redirection allowed.
93       */
94      public TemplateURI(RunData runData, String template, boolean redirect)
95      {
96          super(runData, redirect);
97          setTemplate(template);
98      }
99  
100     /***
101      * Constructor, set Template and Action
102      *
103      * @param runData A RunData object
104      * @param template A Template Name
105      * @param action An Action Name
106      */
107     public TemplateURI(RunData runData, String template, String action)
108     {
109         this(runData, template);
110         setAction(action);
111     }
112 
113     /***
114      * Constructor, set Template and Action, set explicit redirection
115      *
116      * @param runData A RunData object
117      * @param template A Template Name
118      * @param action An Action Name
119      * @param redirect True if redirection allowed.
120      */
121     public TemplateURI(RunData runData, String template, String action, boolean redirect)
122     {
123         this(runData, template, redirect);
124         setAction(action);
125     }
126 
127     /***
128      * Constructor with a ServerData object
129      *
130      * @param serverData A ServerData object
131      */
132     public TemplateURI(ServerData serverData)
133     {
134         super(serverData);
135     }
136 
137     /***
138      * Constructor, set explicit redirection
139      *
140      * @param serverData A ServerData object
141      * @param redirect True if redirection allowed.
142      */
143     public TemplateURI(ServerData serverData, boolean redirect)
144     {
145         super(serverData, redirect);
146     }
147 
148     /***
149      * Constructor, set Template
150      *
151      * @param serverData A ServerData object
152      * @param template A Template Name
153      */
154     public TemplateURI(ServerData serverData, String template)
155     {
156         super(serverData);
157         setTemplate(template);
158     }
159 
160     /***
161      * Constructor, set Template, set explicit redirection
162      *
163      * @param serverData A ServerData object
164      * @param template A Template Name
165      * @param redirect True if redirection allowed.
166      */
167     public TemplateURI(ServerData serverData, String template, boolean redirect)
168     {
169         super(serverData, redirect);
170         setTemplate(template);
171     }
172 
173     /***
174      * Constructor, set Template and Action
175      *
176      * @param serverData A ServerData object
177      * @param template A Template Name
178      * @param action An Action Name
179      */
180     public TemplateURI(ServerData serverData, String template, String action)
181     {
182         this(serverData, template);
183         setAction(action);
184     }
185 
186     /***
187      * Constructor, set Template and Action, set explicit redirection
188      *
189      * @param serverData A ServerData object
190      * @param template A Template Name
191      * @param action An Action Name
192      * @param redirect True if redirection allowed.
193      */
194     public TemplateURI(ServerData serverData, String template, String action, boolean redirect)
195     {
196         this(serverData, template, redirect);
197         setAction(action);
198     }
199 
200     /***
201      * Constructor, user Turbine.getDefaultServerData(), set Template and Action
202      *
203      * @param template A Template Name
204      * @param action An Action Name
205      */
206     public TemplateURI(String template, String action)
207     {
208         this();
209         setTemplate(template);
210         setAction(action);
211     }
212 
213     /***
214      * Sets the template= value for this URL.
215      *
216      * By default it adds the information to the path_info instead
217      * of the query data. An empty value (null or "") cleans out
218      * an existing value.
219      *
220      * @param template A String with the template value.
221      */
222     public void setTemplate(String template)
223     {
224         if(StringUtils.isNotEmpty(template))
225         {
226             add(PATH_INFO, CGI_TEMPLATE_PARAM, template);
227         }
228         else
229         {
230             clearTemplate();
231         }
232     }
233 
234     /***
235      * Clears the template= value for this URL.
236      *
237      */
238     public void clearTemplate()
239     {
240         removePathInfo(CGI_TEMPLATE_PARAM);
241     }
242 
243     /*
244      * ========================================================================
245      *
246      * Protected / Private Methods
247      *
248      * ========================================================================
249      *
250      */
251 
252     /***
253      * Method for a quick way to add all the parameters in a
254      * ParameterParser.
255      *
256      * <p>If the type is P (0), then add name/value to the pathInfo
257      * hashtable.
258      *
259      * <p>If the type is Q (1), then add name/value to the queryData
260      * hashtable.
261      *
262      * @param type Type of insertion (@see #add(char type, String name, String value))
263      * @param pp A ParameterParser.
264      */
265     protected void add(int type,
266             ParameterParser pp)
267     {
268         for(Iterator it = pp.keySet().iterator(); it.hasNext();)
269         {
270             String key = (String) it.next();
271 
272             if (!key.equalsIgnoreCase(CGI_ACTION_PARAM) &&
273                     !key.equalsIgnoreCase(CGI_SCREEN_PARAM) &&
274                     !key.equalsIgnoreCase(CGI_TEMPLATE_PARAM))
275             {
276                 String[] values = pp.getStrings(key);
277                 if(values != null)
278                 {
279                     for (int i = 0; i < values.length; i++)
280                     {
281                         add(type, key, values[i]);
282                     }
283                 }
284                 else
285                 {
286                     add(type, key, "");
287                 }
288             }
289         }
290     }
291 }