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