View Javadoc
1   /*
2    * #%L
3    * Nuiton Validator
4    * %%
5    * Copyright (C) 2013 - 2014 Code Lutin, Tony Chemit
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  package org.nuiton.validator.xwork2.field;
23  
24  import com.opensymphony.xwork2.validator.ValidationException;
25  
26  import java.io.File;
27  
28  /**
29   * <!-- START SNIPPET: javadoc -->
30   * RequiredFileFieldValidator checks that a File field is not null nor have an empty filename.
31   * <!-- END SNIPPET: javadoc -->
32   * <!-- START SNIPPET: parameters -->
33   * <ul>
34   * <li>fieldName - The field name this validator is validating. Required if using Plain-Validator Syntax otherwise not required</li>
35   * </ul>
36   * <!-- END SNIPPET: parameters -->
37   * <pre>
38   * <!-- START SNIPPET: examples -->
39   *     &lt;validators&gt;
40   *         &lt;!-- Plain-Validator Syntax --&gt;
41   *         &lt;validator type="requiredFile"&gt;
42   *             &lt;param name="fieldName"&gt;tmp&lt;/param&gt;
43   *             &lt;message&gt;tmp is required&lt;/message&gt;
44   *         &lt;/validator&gt;
45   *
46   *         &lt;!-- Field-Validator Syntax --&gt;
47   *         &lt;field name="tmp"&gt;
48   *         	  &lt;field-validator type="requiredFile"&gt;
49   *                 &lt;message&gt;tmp is required&lt;/message&gt;
50   *            &lt;/field-validator&gt;
51   *         &lt;/field&gt;
52   *     &lt;/validators&gt;
53   * <!-- END SNIPPET: examples -->
54   * </pre>
55   *
56   * @author Tony Chemit - chemit@codelutin.com
57   */
58  public class RequiredFileFieldValidator extends NuitonFieldValidatorSupport {
59  
60      @Override
61      public void validateWhenNotSkip(Object object) throws ValidationException {
62          String fieldName = getFieldName();
63          Object value = getFieldValue(fieldName, object);
64          if (value == null) {
65              // no value defined
66              addFieldError(fieldName, object);
67              return;
68          }
69          File f;
70          if (value instanceof File) {
71              f = (File) value;
72          } else if (value instanceof String) {
73              f = new File((String) value);
74          } else {
75              addFieldError(fieldName, object);
76              return;
77          }
78  
79          if (f.getPath().trim().isEmpty()) {
80              // f is not a directory nor exists
81              addFieldError(fieldName, object);
82          }
83      }
84  
85      @Override
86      public String getValidatorType() {
87          return "requiredFile";
88      }
89  }