Advertisement
pisio

Validation class - JAVA

Apr 23rd, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.03 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package System;
  6.  
  7. import java.util.Iterator;
  8. import java.util.Map;
  9. import java.util.TreeMap;
  10.  
  11. /**
  12.  *
  13.  * @author pisio
  14.  */
  15. public class Validation {
  16.  
  17.     private static Validation validation = null;
  18.     private TreeMap<String, String> rules;
  19.     private TreeMap<String, String> dataRule;
  20.     private TreeMap<String, String> errors;
  21.  
  22.     private Validation() {
  23.         rules = new TreeMap<>();
  24.         dataRule = new TreeMap<>();
  25.         errors = new TreeMap<>();
  26.     }
  27.  
  28.     public static Validation Init() {
  29.         if (validation == null) {
  30.             Validation.validation = new Validation();
  31.         }
  32.         return Validation.validation;
  33.     }
  34.  
  35.     public void set_rule(String key, String rule, String data) {
  36.         rules.put(key, rule);
  37.         dataRule.put(key, data);
  38.     }
  39.  
  40.     public boolean validate_run() {
  41.         if (rules.size() != dataRule.size()) {
  42.             return false;
  43.         }
  44.  
  45.         for (Map.Entry entry : rules.entrySet()) {
  46.             String key = entry.getKey().toString();
  47.             String rule = entry.getValue().toString();
  48.             String data = dataRule.get(key);
  49.             _doValidate(key, rule, data);
  50.         }
  51.  
  52.         if (errors.size() == 0) {
  53.             return true;
  54.         }
  55.         return false;
  56.  
  57.     }
  58.  
  59.     private boolean _doValidate(String key, String rule, String data) {
  60.         String[] r = rule.split("#");
  61.         for (String s : r) {
  62.             String[] line = s.split(":");
  63.             if (line[0].equals("max_lenght")) {
  64.                 if (this.__lenght(data, Integer.parseInt(line[1]), 1) == false) {
  65.                     errors.put(key, data);
  66. //                    System.out.println(key + " -> fail");
  67.                 }
  68.             }
  69.             if (line[0].equals("min_lenght")) {
  70.                 if (this.__lenght(data, Integer.parseInt(line[1]), 2) == false) {
  71.                     errors.put(key, data);
  72. //                    System.out.println(key + " -> fail");
  73.                 }
  74.             }
  75.             if (line[0].equals("valid_mail")) {
  76.                 if (this.__validMail(data) == false) {
  77.                     errors.put(key, data);
  78. //                    System.out.println(key + " -> fail");
  79.                 }
  80.             }
  81.         }
  82.         return true;
  83.     }
  84.  
  85.     //Validadate  Require input. if have more of 1 char. Using __lenght
  86.     private boolean __isRequare(String data) {
  87.         return this.__lenght(data, 1, 1);
  88.     }
  89.  
  90.     //Validate email
  91.     private boolean __validMail(String mail) {
  92.         return mail.matches("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
  93.     }
  94.  
  95.     //Cheking lenght of  string
  96.     private boolean __lenght(String str, int range, int type) {
  97. //        System.out.format("%s  =  %d   =>  %d \n\r ", str, range, type);
  98.         if (type == 1) {
  99.             if (str.length() < range) {
  100.                 return true;
  101.             }
  102.         } else if (type == 2) {
  103.             if (str.length() > range) {
  104.                 return true;
  105.             }
  106.         }
  107.         return false;
  108.     }
  109.  
  110.     //Returns error for  <strong> key </strong>
  111.     public String errorsList(String key) {
  112.         String er = errors.get(key);
  113.         // errors.remove(key);
  114.         return String.format("%s did not pass validation", er);
  115.     }
  116.  
  117. // Using errorsList  for  each key in  errors Tree
  118.     public String all_errors() {
  119.         StringBuilder sb = new StringBuilder();
  120.         Iterator<String> iterator = errors.keySet().iterator();
  121.         while (iterator.hasNext()) {
  122.             sb.append(errorsList(iterator.next())).append("\n\r");
  123.         }
  124.  
  125.         return sb.toString();
  126.     }
  127.  
  128.     // clear  Rules and  Data
  129.     private void __clear() {
  130.         rules.clear();
  131.         dataRule.clear();
  132.     }
  133.  
  134.     public void flushAll() {
  135.         __clear();
  136.         __flushErrors();
  137.     }
  138.  
  139.     public void __flushErrors() {
  140.         errors.clear();
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement