Coverage Report - org.melati.servlet.Form
 
Classes in this File Line Coverage Branch Coverage Complexity
Form
27%
16/60
31%
11/36
2.357
 
 1  
 /*
 2  
  * $Source: /usr/cvsroot/melati/melati/src/main/java/org/melati/servlet/Form.java,v $
 3  
  * $Revision: 1.8 $
 4  
  *
 5  
  * Copyright (C) 2000 Tim Joyce
 6  
  *
 7  
  * Part of Melati (http://melati.org), a framework for the rapid
 8  
  * development of clean, maintainable web applications.
 9  
  *
 10  
  * Melati is free software; Permission is granted to copy, distribute
 11  
  * and/or modify this software under the terms either:
 12  
  *
 13  
  * a) the GNU General Public License as published by the Free Software
 14  
  *    Foundation; either version 2 of the License, or (at your option)
 15  
  *    any later version,
 16  
  *
 17  
  *    or
 18  
  *
 19  
  * b) any version of the Melati Software License, as published
 20  
  *    at http://melati.org
 21  
  *
 22  
  * You should have received a copy of the GNU General Public License and
 23  
  * the Melati Software License along with this program;
 24  
  * if not, write to the Free Software Foundation, Inc.,
 25  
  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
 26  
  * GNU General Public License and visit http://melati.org to obtain the
 27  
  * Melati Software License.
 28  
  *
 29  
  * Feel free to contact the Developers of Melati (http://melati.org),
 30  
  * if you would like to work out a different arrangement than the options
 31  
  * outlined here.  It is our intention to allow Melati to be used by as
 32  
  * wide an audience as possible.
 33  
  *
 34  
  * This program is distributed in the hope that it will be useful,
 35  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 36  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 37  
  * GNU General Public License for more details.
 38  
  *
 39  
  * Contact details for copyright holder:
 40  
  *
 41  
  *     Tim Joyce <timj At paneris.org>
 42  
  *     http://paneris.org/
 43  
  *     68 Sandbanks Rd, Poole, Dorset. BH14 8BY. UK
 44  
  */
 45  
 
 46  
 package org.melati.servlet;
 47  
 
 48  
 import java.util.Enumeration;
 49  
 import java.util.Hashtable;
 50  
 
 51  
 
 52  
 import javax.servlet.http.HttpServletRequest;
 53  
 
 54  
 import org.melati.poem.Persistent;
 55  
 import org.melati.poem.Column;
 56  
 import org.melati.template.ServletTemplateContext;
 57  
 import org.melati.template.TempletAdaptor;
 58  
 import org.melati.template.TempletAdaptorConstructionMelatiException;
 59  
 import org.melati.util.UTF8URLEncoder;
 60  
 
 61  
 /**
 62  
  * An object to hold useful static methods for manipulation of a Form in a 
 63  
  * {@link ServletTemplateContext}.
 64  
  */
 65  
 
 66  
 public final class Form {
 67  
   
 68  1
   static Hashtable adaptorCache = new Hashtable();
 69  
 
 70  0
   private Form() {}
 71  
   
 72  
   /**
 73  
    * Retrieve updated persistent fields from a context modified in a template.
 74  
    * <p>
 75  
    * The context can specify an adaptor for each field using another HTML
 76  
    * field with name suffix &quot;-adaptor&quot; and value the classname of
 77  
    * a <code>TempletAdaptor</code>.
 78  
    * Hence the templet that renders the field can specify how
 79  
    * the result is parsed. 
 80  
    * This is currently used for dates.
 81  
    *
 82  
    * @param context the current {@link ServletTemplateContext} to get values from
 83  
    * @param object  the {@link Persistent} to update
 84  
    */
 85  
   public static void extractFields(ServletTemplateContext context, 
 86  
                                    Persistent object) {
 87  0
     for (Enumeration c = object.getTable().columns(); c.hasMoreElements();) {
 88  0
       Column column = (Column)c.nextElement();
 89  0
       String formFieldName = "field_" + column.getName();
 90  0
       String rawString = context.getForm(formFieldName);
 91  
 
 92  0
       String adaptorFieldName = formFieldName + "-adaptor";
 93  0
       String adaptorName = context.getForm(adaptorFieldName);
 94  0
       if (adaptorName != null) {
 95  0
         TempletAdaptor adaptor = getAdaptor(adaptorFieldName, adaptorName);
 96  0
         column.setRaw(object, adaptor.rawFrom(context, formFieldName));
 97  0
       } else {
 98  0
         if (rawString != null) {
 99  0
           rawString = rawString.trim();
 100  0
           if (rawString.equals("")) {
 101  0
             if (column.getType().getNullable())
 102  0
               column.setRaw(object, null);
 103  
             else
 104  0
               column.setRawString(object, "");
 105  
           } else {
 106  0
             column.setRawString(object, rawString);
 107  
           }
 108  
         }
 109  
       }
 110  0
     }
 111  0
   }
 112  
 
 113  
   /**
 114  
    * Fill in value of a Field from a ServletTemplateContext.
 115  
    *
 116  
    * @param context    the current {@link ServletTemplateContext} to get values from
 117  
    * @param fieldName  the name of the field to extract
 118  
    * @return the value of the field 
 119  
    * @throws TempletAdaptorConstructionMelatiException 
 120  
    *             if there is a problem, for example with the class name
 121  
    */
 122  
   public static Object extractField(ServletTemplateContext context, String fieldName)
 123  
       throws TempletAdaptorConstructionMelatiException {
 124  
 
 125  0
     String rawString = context.getForm(fieldName);
 126  
 
 127  0
     String adaptorFieldName = fieldName + "-adaptor";
 128  0
     String adaptorName = context.getForm(adaptorFieldName);
 129  
 
 130  0
     if (adaptorName != null) {
 131  0
       TempletAdaptor adaptor = getAdaptor(adaptorFieldName, adaptorName);
 132  0
       return adaptor.rawFrom(context, fieldName);
 133  
     }
 134  0
     return rawString;
 135  
   }
 136  
 
 137  
 
 138  
   private static TempletAdaptor getAdaptor(String adaptorFieldName, String adaptorName) {
 139  0
     TempletAdaptor adaptor = (TempletAdaptor)adaptorCache.get(adaptorName);
 140  0
     if(adaptor == null)
 141  
       try {
 142  0
         adaptor = (TempletAdaptor)Class.forName(adaptorName).newInstance();
 143  0
         adaptorCache.put(adaptorName, adaptor);
 144  
       }
 145  0
       catch (Exception e) {
 146  0
         throw new TempletAdaptorConstructionMelatiException(
 147  
         adaptorFieldName, adaptorName, e);
 148  0
       }
 149  
 
 150  0
     return adaptor;
 151  
   }
 152  
   
 153  
   /**
 154  
   * A utility method that gets a value from the Form.  It will return
 155  
   * null if the value is "" or not present.
 156  
   *
 157  
   * @param context - a template context
 158  
   * @param field - the name of the field to get
 159  
   *
 160  
   * @return - the value of the field requested
 161  
   */
 162  
   public static String getFieldNulled(ServletTemplateContext context, String field) {
 163  5
     return getField(context, field, null);
 164  
   }
 165  
   
 166  
     
 167  
   /**
 168  
   * A utility method that gets a value from the Form.  It will return
 169  
   * the default if the value is "" or not present.
 170  
   *
 171  
   * @param context - a template context
 172  
   * @param field - the name of the field to get
 173  
   * @param defaultValue - the default value if the field is "" or not present
 174  
   *
 175  
   * @return - the value of the field requested
 176  
   */
 177  
   public static String getField(ServletTemplateContext context, String field, 
 178  
                                String defaultValue) {
 179  5
     String val = context.getForm(field);
 180  5
     if (val == null) return defaultValue;
 181  1
     return val.trim().equals("") ? defaultValue : val;
 182  
   }
 183  
 
 184  
   /**
 185  
   * A utility method that gets a value from the Form as an Integer.  
 186  
   * It will return null if the value is "" or not present.
 187  
   *
 188  
   * @param context - a template context
 189  
   * @param field - the name of the field to get
 190  
   * @param defaultValue - the default value if the field is "" or not present
 191  
   *
 192  
   * @return - the value of the field requested
 193  
   */
 194  
   public static Integer getIntegerField(ServletTemplateContext context, String field, 
 195  
                                 Integer defaultValue) {
 196  0
     String val = getFieldNulled(context, field);
 197  0
     return val == null ? defaultValue : new Integer(val);
 198  
   }
 199  
 
 200  
   /**
 201  
   * A utility method that gets a value from the Form as an Integer.  
 202  
   * It will return null if the value is "" or not present.
 203  
   *
 204  
   * @param context - a template context
 205  
   * @param field - the name of the field to get
 206  
   *
 207  
   * @return - the value of the field requested
 208  
   */
 209  
   public static Integer getIntegerField(ServletTemplateContext context, String field) {
 210  0
     return getIntegerField(context, field, null);
 211  
   }
 212  
 
 213  
   /**
 214  
   * A utility method that tests whether a field is present in a Form,
 215  
   * returning a Boolean.  
 216  
   *
 217  
   * @param context - a template context
 218  
   * @param field - the name of the field to get
 219  
   *
 220  
   * @return - TRUE or FALSE depending if the field is present
 221  
   */
 222  
   public static Boolean getBooleanField(ServletTemplateContext context, String field) {
 223  0
     return getFieldNulled(context, field) ==  null ? 
 224  
                                              Boolean.FALSE : Boolean.TRUE;
 225  
   }
 226  
   
 227  
   /**
 228  
    * Modify or add a form parameter setting (query string component) in a URL.
 229  
    *
 230  
    * @param uri     A URI
 231  
    * @param query   A query string
 232  
    * @param field   The parameter's name
 233  
    * @param value   The new value for the parameter (unencoded)
 234  
    * @return        <TT><I>uri</I>?<I>query</I></TT> with <TT>field=value</TT>.
 235  
    *                If there is already a binding for <TT>field</TT> in the
 236  
    *                query string it is replaced, not duplicated.
 237  
    */
 238  
 
 239  
   public static String sameURLWith(String uri, String query,
 240  
                                    String field, String value) {
 241  3
     return uri + "?" + sameQueryWith(query, field, value);
 242  
   }
 243  
 
 244  
   /**
 245  
    * Modify or add a form parameter setting (query string component) in the URL
 246  
    * for a servlet request.
 247  
    *
 248  
    * @param request A servlet request
 249  
    * @param field   The parameter's name
 250  
    * @param value   The new value for the parameter (unencoded)
 251  
    * @return        The request's URL with <TT>field=value</TT>.  If there is
 252  
    *                already a binding for <TT>field</TT> in the query string
 253  
    *                it is replaced, not duplicated.  If there is no query
 254  
    *                string, one is added.
 255  
    */
 256  
 
 257  
   public static String sameURLWith(HttpServletRequest request,
 258  
                                    String field, String value) {
 259  3
     return sameURLWith(request.getRequestURI(), request.getQueryString(),
 260  
                        field, value);
 261  
   }
 262  
 
 263  
   /**
 264  
    * Modify or add a form parameter setting (query string component) in a query
 265  
    * string.
 266  
    * Note this uses the default encoding. 
 267  
    * 
 268  
    * @param qs      A query string
 269  
    * @param field   The parameter's name
 270  
    * @param value   The new value for the parameter (unencoded)
 271  
    * @return        <TT>qs</TT> with <TT>field=value</TT>.
 272  
    *                If there is already a binding for <TT>field</TT> in the
 273  
    *                query string it is replaced, not duplicated.
 274  
    */
 275  
   public static String sameQueryWith(String qs, String field, String value) {
 276  
     
 277  6
     String fenc = UTF8URLEncoder.encode(field);
 278  6
     String fenceq = fenc + '=';
 279  6
     String fev = fenceq + UTF8URLEncoder.encode(value);
 280  
 
 281  6
     if (qs == null || qs.equals("")) return fev;
 282  
 
 283  
     int i;
 284  2
     if (qs.startsWith(fenceq)) i = 0;
 285  
     else {
 286  1
       i = qs.indexOf('&' + fenceq);
 287  1
       if (i == -1) return qs + '&' + fev;
 288  0
       ++i;
 289  
     }
 290  
 
 291  1
     int a = qs.indexOf('&', i);
 292  1
     return qs.substring(0, i) + fev + (a == -1 ? "" : qs.substring(a));
 293  
   }
 294  
   
 295  
 
 296  
   /**
 297  
   * A utility method that gets a value from the Form.  It will return
 298  
   * null if the value is "" or not present.
 299  
   *
 300  
   * @param context - a template context
 301  
   * @param field - the name of the field to get
 302  
   *
 303  
   * @return - the value of the field requested
 304  
   */
 305  
   public static String getFormNulled(ServletTemplateContext context, String field) {
 306  0
     return getForm(context, field, null);
 307  
   }
 308  
 
 309  
  /**
 310  
   * A utility method that gets a value from the Form.  It will return
 311  
   * the default if the value is "" or not present.
 312  
   *
 313  
   * @param context - a template context
 314  
   * @param field - the name of the field to get
 315  
   * @param def - the default value if the field is "" or not present
 316  
   *
 317  
   * @return - the value of the field requested
 318  
   */
 319  
   public static String getForm(ServletTemplateContext context, String field, 
 320  
                                String def) {
 321  0
     String val = context.getForm(field);
 322  0
     if (val == null) return def;
 323  0
     return val.trim().equals("") ? def : val;
 324  
   }
 325  
 
 326  
 }
 327  
 
 328  
 
 329  
 
 330  
 
 331  
 
 332  
 
 333  
 
 334  
 
 335