View Javadoc

1   package org.apache.velocity.tools.view.tools;
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.HashSet;
23  import java.util.Map;
24  import javax.servlet.ServletContext;
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  import org.apache.velocity.tools.generic.ValueParser;
28  import org.apache.velocity.tools.view.context.ViewContext;
29  
30  /**
31   * @deprecated Use {@link org.apache.velocity.tools.view.LinkTool}
32   */
33  @Deprecated
34  public class LinkTool extends org.apache.velocity.tools.view.LinkTool
35  {
36      @Deprecated
37      public static final String SELF_ABSOLUTE_KEY = "self-absolute";
38      @Deprecated
39      public static final String SELF_INCLUDE_PARAMETERS_KEY = "self-include-parameters";
40      @Deprecated
41      public static final String AUTO_IGNORE_PARAMETERS_KEY = "auto-ignore-parameters";
42  
43      @Deprecated
44      protected ServletContext application;
45  
46      private HashSet<String> parametersToIgnore;
47      private boolean autoIgnore = true;
48  
49      @Override
50      protected void configure(ValueParser parser)
51      {
52          Boolean selfAbsolute = parser.getBoolean(SELF_ABSOLUTE_KEY);
53          if (selfAbsolute != null)
54          {
55              setForceRelative(!selfAbsolute.booleanValue());
56          }
57          Boolean selfParams = parser.getBoolean(SELF_INCLUDE_PARAMETERS_KEY);
58          if (selfParams != null)
59          {
60              setIncludeRequestParams(selfParams.booleanValue());
61          }
62          Boolean autoIgnoreParams = parser.getBoolean(AUTO_IGNORE_PARAMETERS_KEY);
63          if (autoIgnoreParams != null)
64          {
65              setAutoIgnoreParameters(autoIgnoreParams.booleanValue());
66          }
67  
68          super.configure(parser);
69      }
70  
71      @Deprecated
72      public void init(Object obj)
73      {
74          if (obj instanceof ViewContext)
75          {
76              ViewContext ctx = (ViewContext)obj;
77              setRequest(ctx.getRequest());
78              setResponse(ctx.getResponse());
79              this.application = ctx.getServletContext();
80              if (ctx.getVelocityEngine() != null)
81              {
82                  this.LOG = ctx.getVelocityEngine().getLog();
83              }
84          }
85      }
86  
87  
88      // --------------------------------------- Protected Methods -------------
89  
90      @Deprecated
91      public void setXhtml(boolean useXhtml)
92      {
93          setXHTML(useXhtml);
94      }
95  
96      /**
97       * @deprecated use {@link #setForceRelative} as <b>reversed</b>
98       *      replacement
99       */
100     @Deprecated
101     public void setSelfAbsolute(boolean selfAbsolute)
102     {
103         setForceRelative(!selfAbsolute);
104     }
105 
106     /**
107      * @deprecated use {@link #setIncludeRequestParams} instead
108      */
109     @Deprecated
110     public void setSelfIncludeParameters(boolean selfParams)
111     {
112         setIncludeRequestParams(selfParams);
113     }
114 
115     @Deprecated
116     public void setAutoIgnoreParameters(boolean autoIgnore)
117     {
118         this.autoIgnore = autoIgnore;
119     }
120 
121     @Deprecated
122     public void setRequest(HttpServletRequest request)
123     {
124         this.request = request;
125         setFromRequest(request);
126     }
127 
128     @Deprecated
129     public void setResponse(HttpServletResponse response)
130     {
131         this.response = response;
132         if (response != null)
133         {
134             setCharacterEncoding(response.getCharacterEncoding());
135         }
136     }
137 
138 
139     // --------------------------------------------- Template Methods -----------
140 
141     /**
142      * @deprecated use {@link #anchor(Object)} instead
143      */
144     @Deprecated
145     public LinkTool setAnchor(String anchor)
146     {
147         return (LinkTool)anchor(anchor);
148     }
149 
150     /**
151      * @deprecated use {@link #relative(Object)}
152      */
153     @Deprecated
154     public LinkTool setRelative(String uri)
155     {
156         return (LinkTool)relative(uri);
157     }
158 
159     /**
160      * @deprecated use {@link #absolute(Object)}
161      */
162     @Deprecated
163     public LinkTool setAbsolute(String uri)
164     {
165         return (LinkTool)absolute(uri);
166     }
167 
168     /**
169      * @deprecated use {@link #uri(Object)}
170      */
171     @Deprecated
172     public LinkTool setURI(String uri)
173     {
174         return (LinkTool)uri(uri);
175     }
176 
177     /**
178      * @deprecated use {@link #getPath}
179      */
180     @Deprecated
181     public String getURI()
182     {
183         return path;
184     }
185 
186     /**
187      * @deprecated use {@link #append(Object,Object)}
188      */
189     @Deprecated
190     public LinkTool addQueryData(String key, Object value)
191     {
192         return (LinkTool)append(key, value);
193     }
194 
195     /**
196      * @deprecated use {@link #params(Object)}
197      */
198     @Deprecated
199     public LinkTool addQueryData(Map parameters)
200     {
201         return (LinkTool)params(parameters);
202     }
203 
204     /**
205      * @deprecated use {@link #getQuery}
206      */
207     @Deprecated
208     public String getQueryData()
209     {
210         return getQuery();
211     }
212 
213     /**
214      * @deprecated use {@link #encode(Object)}
215      */
216     @Deprecated
217     public String encodeURL(String url)
218     {
219         return encode(url);
220     }
221 
222     /**
223      * If you do use this, then you must use {@link #addAllParameters()}
224      * and not the replacements for it, or these will not be honored.
225      *
226      * @deprecated use {@link #addRequestParams(String...)}
227      *             or {@link #addRequestParamsExcept(String...)}
228      *             or {@link #addMissingRequestParams(String...)}
229      */
230     @Deprecated
231     public LinkTool addIgnore(String parameterName)
232     {
233         LinkTool copy = (LinkTool)duplicate();
234         if (copy.parametersToIgnore == null)
235         {
236             copy.parametersToIgnore = new HashSet<String>(1);
237         }
238         copy.parametersToIgnore.add(parameterName);
239         return copy;
240     }
241 
242     /**
243      * @deprecated use {@link #addRequestParams(String...)}
244      *             or {@link #addRequestParamsExcept(String...)}
245      *             or {@link #addMissingRequestParams(String...)}
246      */
247     @Deprecated
248     public LinkTool addAllParameters()
249     {
250         if (this.parametersToIgnore != null)
251         {
252             String[] ignoreThese = new String[parametersToIgnore.size()];
253             return (LinkTool)addRequestParamsExcept(parametersToIgnore.toArray(ignoreThese));
254         }
255         else if (autoIgnore)
256         {
257             return (LinkTool)addMissingRequestParams();
258         }
259         else
260         {
261             return (LinkTool)addRequestParams();
262         }
263     }
264 
265     @Override
266     public void setParam(Object key, Object value, boolean append)
267     {
268         super.setParam(key, value, append);
269         if (autoIgnore)
270         {
271             if (parametersToIgnore == null)
272             {
273                 parametersToIgnore = new HashSet<String>(1);
274             }
275             parametersToIgnore.add(String.valueOf(key));
276         }
277     }
278 
279     @Override
280     public void setParams(Object obj, boolean append)
281     {
282         super.setParams(obj, append);
283         if (autoIgnore && obj instanceof Map)
284         {
285             Map params = (Map)obj;
286             if (!params.isEmpty())
287             {
288                 if (parametersToIgnore == null)
289                 {
290                     parametersToIgnore = new HashSet<String>(params.size());
291                 }
292                 for (Object e : ((Map)obj).entrySet())
293                 {
294                     Map.Entry entry = (Map.Entry)e;
295                     String key = String.valueOf(entry.getKey());
296                     parametersToIgnore.add(key);
297                 }
298             }
299         }
300     }
301 
302 }