InvalidXMLCharacterValidator

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class InvalidXMLCharacterValidator extends BaseValidator implements GracieValidator {
    
    public InvalidXMLCharacterValidator() {
        
    }
    
    /**
     * Validation Rule: Check the input text whether it contains invalid xml characters or not
     * @param inputText          Input string text
     * @return boolean           Return true when the input text contains invalid xml characters,
     *                           otherwise return false.
     */
    public static boolean containsInvalidCharacter(String inputText) {
        boolean contained = false;
        
        String invalidXMLCharacters = 
            ConfigureLoader.getInstance().getConfigureLoaderPropertiesValue(FieldValidationConstants.EXT_INVALID_XML_CHARACTERS_KEY);
        
        /*
        if (invalidXMLCharacters == null) {
            invalidXMLCharacters = FieldValidationConstants.INVALID_XML_CHARACTERS;
        }
        */
        
        if (!Util.isEmpty(inputText)) {
            Pattern pattern = Pattern.compile(invalidXMLCharacters);
            char[] array = inputText.toCharArray();
            for (int i = 0; i < array.length; i++) {
                Matcher matcher = pattern.matcher(String.valueOf(array[i]));
                boolean isValid = matcher.matches();
                if (isValid) {
                    contained = true;
                }
            }
        } 
        
        return contained;
    }
    
    /**
     * Validation Rule: Check the input text whether it contains invalid xml characters or not
     * @param inputText          Input string text
     * @param fieldErrorParam    Field error tag parameter
     * @param fieldErrorMessage  Field error message
     * @return boolean           Return true when the input text contains invalid xml characters,
     *                           otherwise return false.
     */
    public static boolean containsInvalidCharacter(String inputText, String fieldErrorParam, String fieldErrorMessage) {
        boolean contained = InvalidXMLCharacterValidator.containsInvalidCharacter(inputText);
        
        if (contained) {
            addFieldErrorMessage(fieldErrorParam, fieldErrorMessage);
        }
        
        return contained;
    }
    
    /**
     * Validation Rule: Check the input text whether it contains invalid xml characters or not
     * @param inputText          Input string text
     * @param fieldErrorParam    Field error tag parameter
     * @param bundle             Language property file id
     * @param key                Message key
     * @param defaultMessage     Default message
     * @param arguments          The arguments of message
     * @return boolean           Return true when the input text contains invalid xml characters,
     *                           otherwise return false.
     */
    public static boolean containsInvalidCharacter(String inputText, String fieldErrorParam, 
            String bundle, String key, String defaultMessage, String[] arguments) {
        boolean contained = InvalidXMLCharacterValidator.containsInvalidCharacter(inputText);
        
        if (contained) {
            String fieldErrorMessage = MessageUtil.getText(bundle, key, defaultMessage, arguments);
            addFieldErrorMessage(fieldErrorParam, fieldErrorMessage);
        }
        
        return contained;
    }

	public boolean isValid(String inputText, String fieldErrorParam, String bundle, String key, String defaultMessage, String[] arguments) {
		return InvalidXMLCharacterValidator.containsInvalidCharacter(inputText, fieldErrorParam, bundle, key, defaultMessage, arguments);
	}

	public boolean isValid(String inputText, String fieldErrorParam, String fieldErrorMessage) {
		return InvalidXMLCharacterValidator.containsInvalidCharacter(inputText, fieldErrorParam, fieldErrorMessage);
	}

	public boolean isValid(String inputText) {
		return InvalidXMLCharacterValidator.containsInvalidCharacter(inputText);
	}
    
	public boolean isValid(String inputText, Object[] objs) {
		return isValid(inputText);
	}

}

 

你可能感兴趣的:(InvalidXMLCharacterValidator)