##### Coverage Report - org.webmacro.servlet.Form

Classes in this File | Line Coverage | Branch Coverage | Complexity
Form |
21% | 7/32
0% | 0/12
| 3.0;3

1 |  |
     /*
2 |  |
      * Copyright (C) 1998-2000 Semiotek Inc.  All Rights Reserved.
3 |  |
      *
4 |  |
      * Redistribution and use in source and binary forms, with or without
5 |  |
      * modification, are permitted under the terms of either of the following
6 |  |
      * Open Source licenses:
7 |  |
      *
8 |  |
      * The GNU General Public License, version 2, or any later version, as
9 |  |
      * published by the Free Software Foundation
10 |  |
      * (http://www.fsf.org/copyleft/gpl.html);
11 |  |
      *
12 |  |
      *  or
13 |  |
      *
14 |  |
      * The Semiotek Public License (http://webmacro.org/LICENSE.)
15 |  |
      *
16 |  |
      * This software is provided "as is", with NO WARRANTY, not even the
17 |  |
      * implied warranties of fitness to purpose, or merchantability. You
18 |  |
      * assume all risks and liabilities associated with its use.
19 |  |
      *
20 |  |
      * See www.webmacro.org for more information on the WebMacro project.
21 |  |
      *
22 |  |
      */
23 |  |
24 |  |
     package org.webmacro.servlet;
25 |  |
26 |  |
     import java.util.Enumeration;
27 |  |
28 |  |
     import javax.servlet.http.HttpServletRequest;
29 |  |
30 |  |
     import org.webmacro.UnsettableException;
31 |  |
     import org.webmacro.util.Bag;
32 |  |
33 |  |
34 |  |
     /**
35 |  |
      * Provide access to form variables.
36 |  |
      *
37 |  |
      *  @author Marcel Huijkman
38 |  |
      *  @version    15-07-2002
39 |  |
      */
40 |  |
     final public class Form implements Bag
41 |  |
     {
42 |  |
43 |  |
         /**
44 |  |
          * This is the request object from the WebContext.
45 |  |
          */
46 |  |
         final HttpServletRequest _request;
47 |  |
48 |  |
         /**
49 |  |
          * Read the form data from the supplied Request object.
50 |  |
          */
51 |  |
         Form(final HttpServletRequest r)
52 | 1040 |
         {
53 | 1040 |
             _request = r;
54 | 1040 |
         }
55 |  |
56 |  |
         /**
57 |  |
          * Get a form value.
58 |  |
          */
59 |  |
         final public Object get(String field)
60 |  |
         {
61 | 6096 |
             String[] values = _request.getParameterValues(field);
62 |  |
             try
63 |  |
             {
64 | 6096 |
                 return values[0];
65 |  |
             }
66 | 5194 |
             catch (NullPointerException ne)
67 |  |
             {
68 | 5194 |
                 return null;
69 |  |
             }
70 | 0 |
             catch (ArrayIndexOutOfBoundsException e)
71 |  |
             {
72 |  |
                 // Seems to be a bug in Jetty
73 | 0 |
                 return null;
74 |  |
             }
75 |  |
         }
76 |  |
77 |  |
         /**
78 |  |
          * Try to get a form value.
79 |  |
          *
80 |  |
          * @param strKey  The form key that we're looking for.
81 |  |
          *
82 |  |
          * @return        The value of that form key if found, else null
83 |  |
          *
84 |  |
          **/
85 |  |
         final public Object getPossibleForm(String strKey)
86 |  |
         {
87 |  |
             String strElement;
88 |  |
             Object obValue;
89 |  |
90 |  |
             Enumeration obEnumeration;
91 |  |
92 |  |
             //--- end of var's declaration ---//
93 |  |
94 | 0 |
             obValue = get(strKey);
95 | 0 |
             if (obValue == null)
96 |  |
             {
97 | 0 |
                 obEnumeration = _request.getParameterNames();
98 | 0 |
                 while (obEnumeration.hasMoreElements())
99 |  |
                 {
100 | 0 |
                     strElement = obEnumeration.nextElement().toString();
101 | 0 |
                     if (strElement.equalsIgnoreCase(strKey))
102 |  |
                     {
103 | 0 |
                         obValue = get(strElement);
104 | 0 |
                         break;
105 |  |
                     }
106 |  |
                 }
107 |  |
             }
108 |  |
109 | 0 |
             return obValue;
110 |  |
         }
111 |  |
112 |  |
113 |  |
         /**
114 |  |
          * Get a form value as an array.
115 |  |
          */
116 |  |
         final public Object[] getList(String field)
117 |  |
         {
118 |  |
             try
119 |  |
             {
120 | 0 |
                 return _request.getParameterValues(field);
121 |  |
             }
122 | 0 |
             catch (NullPointerException ne)
123 |  |
             {
124 | 0 |
                 return null;
125 |  |
             }
126 |  |
         }
127 |  |
128 |  |
         /**
129 |  |
          * Unsupported.
130 |  |
          */
131 |  |
         final public void put(String key, Object value)
132 |  |
                 throws UnsettableException
133 |  |
         {
134 | 0 |
             throw new UnsettableException("Cannot set a form property");
135 |  |
         }
136 |  |
137 |  |
138 |  |
         /**
139 |  |
          * Unsupported.
140 |  |
          */
141 |  |
         final public void remove (String key)
142 |  |
                 throws UnsettableException
143 |  |
         {
144 | 0 |
             throw new UnsettableException("Cannot unset a form property");
145 |  |
         }
146 |  |
147 |  |
         /**
148 |  |
          * Return a String listing all the HTTP request parameter names and their values.
149 |  |
          */
150 |  |
         final public String toString ()
151 |  |
         {
152 | 0 |
             StringBuffer sb = new StringBuffer();
153 | 0 |
             String eol = java.lang.System.getProperty("line.separator");
154 | 0 |
             for (Enumeration params = _request.getParameterNames(); params.hasMoreElements(); )
155 |  |
             {
156 | 0 |
                 String key = (String) params.nextElement();
157 | 0 |
                 String[] value = _request.getParameterValues(key);
158 |  |
159 | 0 |
                 for (int x = 0; value != null && x < value.length; x++)
160 |  |
                 {
161 | 0 |
                     sb.append(key).append("=").append(value[x]).append(eol);
162 |  |
                 }
163 | 0 |
             }
164 | 0 |
             return sb.toString();
165 |  |
         }
166 |  |
167 |  |
     }
168 |  |