View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/util/ParseUtil.java,v 1.29 2004/10/17 07:02:30 hkollmann Exp $
3    * $Revision: 1.29 $
4    * $Date: 2004/10/17 07:02:30 $
5    *
6    * DbForms - a Rapid Application Development Framework
7    * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8    *
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation; either
12   * version 2.1 of the License, or (at your option) any later version.
13   *
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   */
23  
24  package org.dbforms.util;
25  
26  
27  import java.util.Enumeration;
28  import java.util.Vector;
29  
30  import javax.servlet.http.HttpServletRequest;
31  
32  
33  
34  /***
35   * This utility-class provides convenience methods for parsing strings and
36   * generating certain data structures
37   *
38   * @author Joe Peer
39   */
40  public class ParseUtil {
41     /***
42      * Returns a FileHolder object for the specified file pending around in the
43      * current request
44      *
45      * @param request the request object
46      * @param name the file name.
47      *
48      * @return a FileHolder object for the named file.
49      */
50     public static FileHolder getFileHolder(HttpServletRequest request,
51                                            String             name) {
52        MultipartRequest multipartRequest = (MultipartRequest) request
53                                            .getAttribute("multipartRequest");
54  
55        return (multipartRequest == null) ? null
56                                          : multipartRequest.getFileHolder(name);
57     }
58  
59  
60  
61     /***
62      * Get the name of the parameter starting with the input string.
63      *
64      * @param request the request object
65      * @param str the string to check for
66      *
67      * @return the name of the parameter starting with the input string
68      */
69     public static String getFirstParameterStartingWith(HttpServletRequest request,
70                                                        String             str) {
71        MultipartRequest multipartRequest = (MultipartRequest) request
72                                            .getAttribute("multipartRequest");
73  
74        Enumeration      e = (multipartRequest == null)
75                                ? request.getParameterNames()
76                                : multipartRequest.getParameterNames();
77  
78        while (e.hasMoreElements()) {
79           String param = (String) e.nextElement();
80  
81           if (param.startsWith(str)) {
82              return param;
83           }
84        }
85  
86        e = request.getAttributeNames();
87  
88        while (e.hasMoreElements()) {
89           String param = (String) e.nextElement();
90  
91           if (param.startsWith(str)) {
92              return param;
93           }
94        }
95  
96        return null;
97     }
98  
99  
100    /***
101     * Returns the value of the named parameter as a String, or the default if
102     * the parameter was not sent or was sent without a value.  The value is
103     * guaranteed to be in its normal, decoded form.  If the parameter has
104     * multiple values, only the first (!!!) one is returned
105     * For parameters with multiple values, it's possible the
106     * first "value" may be null.
107     *
108     * @param request the request object
109     * @param name    the parameter name
110     * @param def     the default value
111     *
112     * @return the parameter value.
113     */
114    public static String getParameter(HttpServletRequest request,
115                                      String             name,
116                                      String             def) {
117       String s = getParameter(request, name);
118 
119       if (Util.isNull(s)) {
120          s = def;
121       }
122 
123       return s;
124    }
125 
126 
127    /***
128     * Returns the value of the named parameter as a String, or null if the
129     * parameter was not sent or was sent without a value.  The value is
130     * guaranteed to be in its normal, decoded form.  If the parameter has
131     * multiple values, only the first (!!!) one is returned
132     * For parameters with multiple values, it's possible the
133     * first "value" may be null.
134     *
135     * @param request the request object
136     * @param name the parameter name.
137     *
138     * @return the parameter value.
139     */
140    public static String getParameter(HttpServletRequest request,
141                                      String             name) {
142       String           res;
143       MultipartRequest multipartRequest = (MultipartRequest) request
144                                           .getAttribute("multipartRequest");
145 
146       //patch by borghi
147       if (request.getAttribute(name) != null) {
148          res = request.getAttribute(name)
149                       .toString();
150       } else {
151          //end patch
152          res = (multipartRequest == null) ? request.getParameter(name)
153                                           : multipartRequest.getParameter(name);
154       }
155 
156       return res;
157    }
158 
159 
160    /***
161     * Returns the names of all the parameters as an Enumeration of Strings.  It
162     * returns an empty Enumeration if there are no parameters.
163     *
164     * @param request the request object
165     *
166     * @return the names of all the parameters as an Enumeration of Strings.
167     */
168    public static Enumeration getParameterNames(HttpServletRequest request) {
169       MultipartRequest multipartRequest = (MultipartRequest) request
170                                           .getAttribute("multipartRequest");
171 
172       return (multipartRequest == null) ? request.getParameterNames()
173                                         : multipartRequest.getParameterNames();
174    }
175 
176 
177    /***
178     * Returns the values of the named parameter as a String array, or null if
179     * the parameter was not sent.  The array has one entry for each parameter
180     * field sent.  If any field was sent without a value that entry is stored
181     * in the array as a null.  The values are guaranteed to be in their
182     * normal, decoded form.  A single value is returned as a one-element
183     * array.
184     *
185     * @param request the request object
186     * @param name the parameter name.
187     *
188     * @return the parameter values.
189     */
190    public static String[] getParameterValues(HttpServletRequest request,
191                                              String             name) {
192       MultipartRequest multipartRequest = (MultipartRequest) request
193                                           .getAttribute("multipartRequest");
194 
195       return (multipartRequest == null) ? request.getParameterValues(name)
196                                         : multipartRequest.getParameterValues(name);
197    }
198 
199 
200    /***
201      * Get a Vector object containing all the request parameters
202      * starting with the input string.
203      *
204      * @param request the request object
205      * @param str the string to check for
206      *
207      * @return a Vector object containing all the request parameters
208      *         starting with the input string.
209      */
210    public static Vector getParametersStartingWith(HttpServletRequest request,
211                                                   String             str) {
212       MultipartRequest multipartRequest = (MultipartRequest) request
213                                           .getAttribute("multipartRequest");
214       Vector           result = new Vector();
215       Enumeration      e   = (multipartRequest == null)
216                                 ? request.getParameterNames()
217                                 : multipartRequest.getParameterNames();
218 
219       while (e.hasMoreElements()) {
220          String param = (String) e.nextElement();
221 
222          if (param.startsWith(str)) {
223             if (!result.contains(param)) {
224                result.addElement(param);
225             }
226          }
227       }
228 
229       e = request.getAttributeNames();
230 
231       while (e.hasMoreElements()) {
232          String param = (String) e.nextElement();
233 
234          if (param.startsWith(str)) {
235             if (!result.contains(param)) {
236                result.addElement(param);
237             }
238          }
239       }
240 
241       return result;
242    }
243 }