InputValidationUtils.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.core.mvc.properties.provider;

  21. /**    
  22.  * InputValidationUtils
  23.  *
  24.  * @author Poli Andrea (poli@link.it)
  25.  * @author $Author$
  26.  * @version $Rev$, $Date$
  27.  */
  28. public class InputValidationUtils {
  29.    
  30.     private InputValidationUtils() {}
  31.    
  32.     public static void validateTextInput(String input, String fieldName) throws ProviderValidationException {
  33.         validateTextAreaInput(input, fieldName, true, false, false);
  34.     }
  35.     public static void validateTextInput(String input, String fieldName,boolean spaceEnabled) throws ProviderValidationException {
  36.         validateTextAreaInput(input, fieldName, spaceEnabled, false, false);
  37.     }
  38.    
  39.     public static void validateTextAreaInput(String input, String fieldName) throws ProviderValidationException {
  40.         validateTextAreaInput(input, fieldName, false, false, false);
  41.     }
  42.     public static void validateTextAreaInput(String input, String fieldName,boolean spaceEnabled) throws ProviderValidationException {
  43.         validateTextAreaInput(input, fieldName, spaceEnabled, false, false);
  44.     }
  45.     public static void validateTextAreaInput(String input, String fieldName,
  46.             boolean spaceEnabled, boolean tabEnabled, boolean newLineEnabled) throws ProviderValidationException {
  47.        
  48.         String prefix = "Il valore indicato nel campo '";
  49.        
  50.         if(input==null || "".equals(input)) {
  51.             throw new ProviderValidationException("Non รจ stato fornito un valore nel campo '"+fieldName+"'");
  52.         }
  53.         if(spaceEnabled) {
  54.             if(input.startsWith(" ")) {
  55.                 throw new ProviderValidationException(prefix+fieldName+"' non deve iniziare con uno spazio");
  56.             }
  57.             if(input.endsWith(" ")) {
  58.                 throw new ProviderValidationException(prefix+fieldName+"' non deve terminare con uno spazio");
  59.             }
  60.         }
  61.         else {
  62.             if(input.contains(" ")) {
  63.                 throw new ProviderValidationException(prefix+fieldName+"' non deve contenere spazi");
  64.             }
  65.         }
  66.         if(!tabEnabled && input.contains("\t")) {
  67.             throw new ProviderValidationException(prefix+fieldName+"' non deve contenere tab (\\t)");
  68.         }
  69.         if(!newLineEnabled &&
  70.                 (input.contains("\n") || input.contains("\r"))) {
  71.             throw new ProviderValidationException(prefix+fieldName+"' non deve contenere ritorni a capo (\\n o \\r)");
  72.         }
  73.     }
  74.    
  75. }