You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by vg...@apache.org on 2005/08/10 23:10:57 UTC

svn commit: r231328 - /cocoon/blocks/forms/trunk/java/org/apache/cocoon/forms/formmodel/Upload.java

Author: vgritsenko
Date: Wed Aug 10 14:10:53 2005
New Revision: 231328

URL: http://svn.apache.org/viewcvs?rev=231328&view=rev
Log:
refactor mime type loop

Modified:
    cocoon/blocks/forms/trunk/java/org/apache/cocoon/forms/formmodel/Upload.java

Modified: cocoon/blocks/forms/trunk/java/org/apache/cocoon/forms/formmodel/Upload.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/forms/trunk/java/org/apache/cocoon/forms/formmodel/Upload.java?rev=231328&r1=231327&r2=231328&view=diff
==============================================================================
--- cocoon/blocks/forms/trunk/java/org/apache/cocoon/forms/formmodel/Upload.java (original)
+++ cocoon/blocks/forms/trunk/java/org/apache/cocoon/forms/formmodel/Upload.java Wed Aug 10 14:10:53 2005
@@ -1,12 +1,12 @@
 /*
- * Copyright 1999-2004 The Apache Software Foundation.
- * 
+ * Copyright 1999-2005 The Apache Software Foundation.
+ *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -31,14 +31,15 @@
 import org.xml.sax.SAXException;
 
 /**
- * A file-uploading Widget. This widget gives access via Cocoon Forms, to Cocoon's 
+ * A file-uploading Widget. This widget gives access via Cocoon Forms, to Cocoon's
  * file upload functionality.
- * 
+ *
  * @author <a href="mailto:uv@upaya.co.uk">Upayavira</a>
  * @author <a href="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
  * @version $Id$
  */
-public class Upload extends AbstractWidget implements ValidationErrorAware {
+public class Upload extends AbstractWidget
+                    implements ValidationErrorAware {
 
     private static final String UPLOAD_EL = "upload";
     private static final String VALUE_EL = "value";
@@ -48,6 +49,7 @@
     private Part part;
     private ValidationError validationError;
 
+
     public Upload(UploadDefinition uploadDefinition) {
         super(uploadDefinition);
         this.uploadDefinition = uploadDefinition;
@@ -75,8 +77,9 @@
     }
 
     public void readFromRequest(FormContext formContext) {
-        if (!getCombinedState().isAcceptingInputs())
+        if (!getCombinedState().isAcceptingInputs()) {
             return;
+        }
 
         Object obj = formContext.getRequest().get(getRequestParameterName());
 
@@ -109,6 +112,24 @@
         // And keep the current state if the parameter doesn't exist or is null
     }
 
+    private boolean validateMimeType() {
+        String mimeTypes = this.uploadDefinition.getMimeTypes();
+        if (mimeTypes != null) {
+            StringTokenizer tok = new StringTokenizer(mimeTypes, ", ");
+            String contentType = this.part.getMimeType();
+            boolean found = false;
+            while(tok.hasMoreTokens()) {
+                if (tok.nextToken().equals(contentType)) {
+                    return true;
+                }
+            }
+            return false;
+        }
+
+        // No mime type restriction
+        return true;
+    }
+
     public boolean validate() {
         if (!getCombinedState().isValidatingValues()) {
             this.wasValid = true;
@@ -121,26 +142,10 @@
                 newError = new ValidationError(new I18nMessage("general.field-required", Constants.I18N_CATALOGUE));
                 getForm().addWidgetUpdate(this);
             }
-        } else {
-            String mimeTypes = this.uploadDefinition.getMimeTypes();
-            if (mimeTypes != null) {
-                StringTokenizer tok = new StringTokenizer(this.uploadDefinition.getMimeTypes(), ", ");
-                String contentType = this.part.getMimeType();
-                boolean found = false;
-                while(tok.hasMoreTokens()) {
-                    if (tok.nextToken().equals(contentType)) {
-                        found = true;
-                        break;
-                    }
-                }
-                if (!found) {
-                    newError = new ValidationError(new I18nMessage("upload.invalid-type", Constants.I18N_CATALOGUE));
-                }
-            } else {
-                newError = null;
-            }
+        } else if (!validateMimeType()) {
+            newError = new ValidationError(new I18nMessage("upload.invalid-type", Constants.I18N_CATALOGUE));
         }
-        
+
         if (!ObjectUtils.equals(this.validationError, newError)) {
             setValidationError(newError);
         }
@@ -159,7 +164,7 @@
     /**
      * Set a validation error on this field. This allows fields to be externally marked as invalid by
      * application logic.
-     * 
+     *
      * @param error the validation error
      */
     public void setValidationError(ValidationError error) {
@@ -173,7 +178,7 @@
     public String getXMLElementName() {
         return UPLOAD_EL;
     }
-    
+
     /**
      * Adds attributes @required, @mime-types
      */
@@ -202,5 +207,4 @@
             contentHandler.endElement(Constants.INSTANCE_NS, VALIDATION_MSG_EL, Constants.INSTANCE_PREFIX_COLON + VALIDATION_MSG_EL);
         }
     }
-
 }