Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package System;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.TreeMap;
- /**
- *
- * @author pisio
- */
- public class Validation {
- private static Validation validation = null;
- private TreeMap<String, String> rules;
- private TreeMap<String, String> dataRule;
- private TreeMap<String, String> errors;
- private Validation() {
- rules = new TreeMap<>();
- dataRule = new TreeMap<>();
- errors = new TreeMap<>();
- }
- public static Validation Init() {
- if (validation == null) {
- Validation.validation = new Validation();
- }
- return Validation.validation;
- }
- public void set_rule(String key, String rule, String data) {
- rules.put(key, rule);
- dataRule.put(key, data);
- }
- public boolean validate_run() {
- if (rules.size() != dataRule.size()) {
- return false;
- }
- for (Map.Entry entry : rules.entrySet()) {
- String key = entry.getKey().toString();
- String rule = entry.getValue().toString();
- String data = dataRule.get(key);
- _doValidate(key, rule, data);
- }
- if (errors.size() == 0) {
- return true;
- }
- return false;
- }
- private boolean _doValidate(String key, String rule, String data) {
- String[] r = rule.split("#");
- for (String s : r) {
- String[] line = s.split(":");
- if (line[0].equals("max_lenght")) {
- if (this.__lenght(data, Integer.parseInt(line[1]), 1) == false) {
- errors.put(key, data);
- // System.out.println(key + " -> fail");
- }
- }
- if (line[0].equals("min_lenght")) {
- if (this.__lenght(data, Integer.parseInt(line[1]), 2) == false) {
- errors.put(key, data);
- // System.out.println(key + " -> fail");
- }
- }
- if (line[0].equals("valid_mail")) {
- if (this.__validMail(data) == false) {
- errors.put(key, data);
- // System.out.println(key + " -> fail");
- }
- }
- }
- return true;
- }
- //Validadate Require input. if have more of 1 char. Using __lenght
- private boolean __isRequare(String data) {
- return this.__lenght(data, 1, 1);
- }
- //Validate email
- private boolean __validMail(String mail) {
- return mail.matches("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
- }
- //Cheking lenght of string
- private boolean __lenght(String str, int range, int type) {
- // System.out.format("%s = %d => %d \n\r ", str, range, type);
- if (type == 1) {
- if (str.length() < range) {
- return true;
- }
- } else if (type == 2) {
- if (str.length() > range) {
- return true;
- }
- }
- return false;
- }
- //Returns error for <strong> key </strong>
- public String errorsList(String key) {
- String er = errors.get(key);
- // errors.remove(key);
- return String.format("%s did not pass validation", er);
- }
- // Using errorsList for each key in errors Tree
- public String all_errors() {
- StringBuilder sb = new StringBuilder();
- Iterator<String> iterator = errors.keySet().iterator();
- while (iterator.hasNext()) {
- sb.append(errorsList(iterator.next())).append("\n\r");
- }
- return sb.toString();
- }
- // clear Rules and Data
- private void __clear() {
- rules.clear();
- dataRule.clear();
- }
- public void flushAll() {
- __clear();
- __flushErrors();
- }
- public void __flushErrors() {
- errors.clear();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement