001: package org.apache.turbine.util.uri;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.util.Iterator;
023:
024: import org.apache.commons.lang.StringUtils;
025:
026: import org.apache.turbine.util.RunData;
027: import org.apache.turbine.util.ServerData;
028: import org.apache.turbine.util.parser.ParameterParser;
029:
030: /**
031: * This class allows you to keep all the information needed for a single
032: * link at one place. It keeps your query data, path info, the server
033: * scheme, name, port and the script path. It is tuned for usage with a
034: * Template System e.g. Velocity.
035: *
036: * If you must generate a Turbine Link in a Template System, use this class.
037: *
038: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
039: * @version $Id: TemplateURI.java 534527 2007-05-02 16:10:59Z tv $
040: */
041:
042: public class TemplateURI extends TurbineURI {
043: /**
044: * Empty C'tor. Uses Turbine.getDefaultServerData().
045: *
046: */
047: public TemplateURI() {
048: super ();
049: }
050:
051: /**
052: * Constructor with a RunData object
053: *
054: * @param runData A RunData object
055: */
056: public TemplateURI(RunData runData) {
057: super (runData);
058: }
059:
060: /**
061: * Constructor, set explicit redirection
062: *
063: * @param runData A RunData object
064: * @param redirect True if redirection allowed.
065: */
066: public TemplateURI(RunData runData, boolean redirect) {
067: super (runData, redirect);
068: }
069:
070: /**
071: * Constructor, set Template
072: *
073: * @param runData A RunData object
074: * @param template A Template Name
075: */
076: public TemplateURI(RunData runData, String template) {
077: super (runData);
078: setTemplate(template);
079: }
080:
081: /**
082: * Constructor, set Template, set explicit redirection
083: *
084: * @param runData A RunData object
085: * @param template A Template Name
086: * @param redirect True if redirection allowed.
087: */
088: public TemplateURI(RunData runData, String template,
089: boolean redirect) {
090: super (runData, redirect);
091: setTemplate(template);
092: }
093:
094: /**
095: * Constructor, set Template and Action
096: *
097: * @param runData A RunData object
098: * @param template A Template Name
099: * @param action An Action Name
100: */
101: public TemplateURI(RunData runData, String template, String action) {
102: this (runData, template);
103: setAction(action);
104: }
105:
106: /**
107: * Constructor, set Template and Action, set explicit redirection
108: *
109: * @param runData A RunData object
110: * @param template A Template Name
111: * @param action An Action Name
112: * @param redirect True if redirection allowed.
113: */
114: public TemplateURI(RunData runData, String template, String action,
115: boolean redirect) {
116: this (runData, template, redirect);
117: setAction(action);
118: }
119:
120: /**
121: * Constructor with a ServerData object
122: *
123: * @param serverData A ServerData object
124: */
125: public TemplateURI(ServerData serverData) {
126: super (serverData);
127: }
128:
129: /**
130: * Constructor, set explicit redirection
131: *
132: * @param serverData A ServerData object
133: * @param redirect True if redirection allowed.
134: */
135: public TemplateURI(ServerData serverData, boolean redirect) {
136: super (serverData, redirect);
137: }
138:
139: /**
140: * Constructor, set Template
141: *
142: * @param serverData A ServerData object
143: * @param template A Template Name
144: */
145: public TemplateURI(ServerData serverData, String template) {
146: super (serverData);
147: setTemplate(template);
148: }
149:
150: /**
151: * Constructor, set Template, set explicit redirection
152: *
153: * @param serverData A ServerData object
154: * @param template A Template Name
155: * @param redirect True if redirection allowed.
156: */
157: public TemplateURI(ServerData serverData, String template,
158: boolean redirect) {
159: super (serverData, redirect);
160: setTemplate(template);
161: }
162:
163: /**
164: * Constructor, set Template and Action
165: *
166: * @param serverData A ServerData object
167: * @param template A Template Name
168: * @param action An Action Name
169: */
170: public TemplateURI(ServerData serverData, String template,
171: String action) {
172: this (serverData, template);
173: setAction(action);
174: }
175:
176: /**
177: * Constructor, set Template and Action, set explicit redirection
178: *
179: * @param serverData A ServerData object
180: * @param template A Template Name
181: * @param action An Action Name
182: * @param redirect True if redirection allowed.
183: */
184: public TemplateURI(ServerData serverData, String template,
185: String action, boolean redirect) {
186: this (serverData, template, redirect);
187: setAction(action);
188: }
189:
190: /**
191: * Constructor, user Turbine.getDefaultServerData(), set Template and Action
192: *
193: * @param template A Template Name
194: * @param action An Action Name
195: */
196: public TemplateURI(String template, String action) {
197: this ();
198: setTemplate(template);
199: setAction(action);
200: }
201:
202: /**
203: * Sets the template= value for this URL.
204: *
205: * By default it adds the information to the path_info instead
206: * of the query data. An empty value (null or "") cleans out
207: * an existing value.
208: *
209: * @param template A String with the template value.
210: */
211: public void setTemplate(String template) {
212: if (StringUtils.isNotEmpty(template)) {
213: add(PATH_INFO, CGI_TEMPLATE_PARAM, template);
214: } else {
215: clearTemplate();
216: }
217: }
218:
219: /**
220: * Clears the template= value for this URL.
221: *
222: */
223: public void clearTemplate() {
224: removePathInfo(CGI_TEMPLATE_PARAM);
225: }
226:
227: /*
228: * ========================================================================
229: *
230: * Protected / Private Methods
231: *
232: * ========================================================================
233: *
234: */
235:
236: /**
237: * Method for a quick way to add all the parameters in a
238: * ParameterParser.
239: *
240: * <p>If the type is P (0), then add name/value to the pathInfo
241: * hashtable.
242: *
243: * <p>If the type is Q (1), then add name/value to the queryData
244: * hashtable.
245: *
246: * @param type Type of insertion (@see #add(char type, String name, String value))
247: * @param pp A ParameterParser.
248: */
249: protected void add(int type, ParameterParser pp) {
250: for (Iterator it = pp.keySet().iterator(); it.hasNext();) {
251: String key = (String) it.next();
252:
253: if (!key.equalsIgnoreCase(CGI_ACTION_PARAM)
254: && !key.equalsIgnoreCase(CGI_SCREEN_PARAM)
255: && !key.equalsIgnoreCase(CGI_TEMPLATE_PARAM)) {
256: String[] values = pp.getStrings(key);
257: if (values != null) {
258: for (int i = 0; i < values.length; i++) {
259: add(type, key, values[i]);
260: }
261: } else {
262: add(type, key, "");
263: }
264: }
265: }
266: }
267: }
|