Coverage report

  %line %branch
org.apache.turbine.services.intake.model.FileItemField
0% 
0% 

 1  
 package org.apache.turbine.services.intake.model;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2005 The Apache Software Foundation.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License")
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 import org.apache.commons.fileupload.FileItem;
 20  
 
 21  
 import org.apache.turbine.services.intake.IntakeException;
 22  
 import org.apache.turbine.services.intake.validator.FileValidator;
 23  
 import org.apache.turbine.services.intake.validator.ValidationException;
 24  
 import org.apache.turbine.services.intake.xmlmodel.XmlField;
 25  
 import org.apache.turbine.util.TurbineRuntimeException;
 26  
 import org.apache.turbine.util.parser.ParameterParser;
 27  
 import org.apache.turbine.util.parser.ValueParser;
 28  
 
 29  
 /**
 30  
  * This Intake field is intended to represent a File input element in a HTML form.
 31  
  *
 32  
  * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
 33  
  * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
 34  
  * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
 35  
  * @version $Id: FileItemField.java 264148 2005-08-29 14:21:04Z henning $
 36  
  */
 37  
 public class FileItemField
 38  
         extends Field
 39  
 {
 40  
 
 41  
     /**
 42  
      * Constructor.
 43  
      *
 44  
      * @param field xml field definition object
 45  
      * @param group xml group definition object
 46  
      * @throws IntakeException thrown by superclass
 47  
      */
 48  
     public FileItemField(XmlField field, Group group)
 49  
             throws IntakeException
 50  
     {
 51  0
         super(field, group);
 52  0
     }
 53  
 
 54  
     /**
 55  
      * It is not possible to set the default value for this field type.
 56  
      * Calling this method with a non-null parameter will result in a
 57  
      * TurbineRuntimeException
 58  
      *
 59  
      * @param prop Parameter for the default values
 60  
      * @throws TurbineRuntimeException
 61  
      */
 62  
     public void setDefaultValue(String prop)
 63  
     {
 64  0
         if (prop != null)
 65  
         {
 66  0
             throw new TurbineRuntimeException(
 67  
                     "Default values are not valid for "
 68  
                     + this.getClass().getName());
 69  
         }
 70  
 
 71  0
         defaultValue = null;
 72  0
     }
 73  
 
 74  
     /**
 75  
      * It is not possible to set the empty value for this field type.
 76  
      * Calling this method with a non-null parameter will result in a
 77  
      * TurbineRuntimeException
 78  
      *
 79  
      * @param prop Parameter for the empty values
 80  
      * @throws TurbineRuntimeException
 81  
      */
 82  
     public void setEmptyValue(String prop)
 83  
     {
 84  0
         if (prop != null)
 85  
         {
 86  0
             throw new TurbineRuntimeException(
 87  
                     "Empty values are not valid for "
 88  
                     + this.getClass().getName());
 89  
         }
 90  
 
 91  0
         emptyValue = null;
 92  0
     }
 93  
 
 94  
     /**
 95  
      * A suitable validator.
 96  
      *
 97  
      * @return A suitable validator
 98  
      */
 99  
     protected String getDefaultValidator()
 100  
     {
 101  0
         return FileValidator.class.getName();
 102  
     }
 103  
 
 104  
     /**
 105  
      * Method called when this field (the group it belongs to) is
 106  
      * pulled from the pool.  The request data is searched to determine
 107  
      * if a value has been supplied for this field.  if so, the value
 108  
      * is validated.
 109  
      *
 110  
      * @param vp a <code>ValueParser</code> value
 111  
      * @return a <code>Field</code> value
 112  
      * @exception IntakeException if an error occurs
 113  
      */
 114  
     public Field init(ValueParser vp)
 115  
             throws IntakeException
 116  
     {
 117  
         try
 118  
         {
 119  0
             super.parser = (ParameterParser) vp;
 120  
         }
 121  0
         catch (ClassCastException e)
 122  
         {
 123  0
             throw new IntakeException(
 124  
                     "FileItemFields can only be used with ParameterParser");
 125  0
         }
 126  
 
 127  0
         validFlag = true;
 128  
 
 129  0
         FileItem [] fileItems = ((ParameterParser) parser).getFileItems(getKey());
 130  
 
 131  0
         if (fileItems != null)
 132  
         {
 133  0
             setFlag = true;
 134  0
             validate();
 135  
         }
 136  
 
 137  0
         initialized = true;
 138  0
         return this;
 139  
     }
 140  
 
 141  
     /**
 142  
      * Compares request data with constraints and sets the valid flag.
 143  
      *
 144  
      * @return the valid flag
 145  
      */
 146  
     protected boolean validate()
 147  
     {
 148  0
         ParameterParser pp = (ParameterParser) super.parser;
 149  0
         if (isMultiValued)
 150  
         {
 151  0
             FileItem[] ss = pp.getFileItems(getKey());
 152  
             // this definition of not set might need refined.  But
 153  
             // not sure the situation will arise.
 154  0
             if (ss.length == 0)
 155  
             {
 156  0
                 setFlag = false;
 157  
             }
 158  
 
 159  0
             if (validator != null)
 160  
             {
 161  0
                 for (int i = 0; i < ss.length; i++)
 162  
                 {
 163  
                     try
 164  
                     {
 165  0
                         ((FileValidator) validator).assertValidity(ss[i]);
 166  
                     }
 167  0
                     catch (ValidationException ve)
 168  
                     {
 169  0
                         setMessage(ve.getMessage());
 170  0
                     }
 171  
                 }
 172  
             }
 173  
 
 174  0
             if (setFlag && validFlag)
 175  
             {
 176  0
                 doSetValue();
 177  
             }
 178  
         }
 179  
         else
 180  
         {
 181  0
             FileItem s = pp.getFileItem(getKey());
 182  0
             if (s == null || s.getSize() == 0)
 183  
             {
 184  0
                 setFlag = false;
 185  
             }
 186  
 
 187  0
             if (validator != null)
 188  
             {
 189  
                 try
 190  
                 {
 191  0
                     ((FileValidator) validator).assertValidity(s);
 192  
 
 193  0
                     if (setFlag)
 194  
                     {
 195  0
                         doSetValue();
 196  
                     }
 197  
                 }
 198  0
                 catch (ValidationException ve)
 199  
                 {
 200  0
                     setMessage(ve.getMessage());
 201  0
                 }
 202  
             }
 203  0
             else if (setFlag)
 204  
             {
 205  0
                 doSetValue();
 206  
             }
 207  
         }
 208  
 
 209  0
         return validFlag;
 210  
     }
 211  
 
 212  
     /**
 213  
      * Sets the value of the field from data in the parser.
 214  
      */
 215  
     protected void doSetValue()
 216  
     {
 217  0
         ParameterParser pp = (ParameterParser) super.parser;
 218  0
         if (isMultiValued)
 219  
         {
 220  0
             setTestValue(pp.getFileItems(getKey()));
 221  
         }
 222  
         else
 223  
         {
 224  0
             setTestValue(pp.getFileItem(getKey()));
 225  
         }
 226  0
     }
 227  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.