FileDocCategorySizeDatePackage
TextPolicy.javaAPI DocphoneME MR2 API (J2ME)5600Wed May 02 18:00:24 BST 2007com.sun.midp.lcdui

TextPolicy

public class TextPolicy extends Object
Class to handle conforming text to specified constraints

Fields Summary
Constructors Summary
Methods Summary
private static booleancheckDecimal(java.lang.String array)
Check is this is a valid decimal

param
array string to check
return
true if this is a valid string


        int len = array.length();

        /*
         * If the whole content is "-", ".", or "-.",
         * this is invalid.
         */
        if ((len == 1 && array.charAt(0) == '-") ||
            (len == 1 && array.charAt(0) == '.") ||
            (len == 2 && array.charAt(0) == '-" && array.charAt(1) == '.")) {
            return false;
        }

        /*
         * For decimal constraint, it is probably easier to re-validate the
         * whole content, than to try to validate the inserted data in
         * relation to the existing data around it.
         */
        boolean hasSeparator = false;
        for (int i = 0; i < len; i++) {
            char    c = array.charAt(i);

            /*
             * valid characters are
             *   [0-9],
             *   '-' at the first pos,
             *   '.' as the decimal separator.
             */
            if (c == '.") {
                if (!hasSeparator) {
                    hasSeparator = true;
                } else {
                    return false;
                }
            } else if (((c < '0") || (c > '9")) &&
                       (c != '-"  || i != 0)) {
                return false;
            }
        }
        return true;

    
private static booleancheckEmail(java.lang.String array)
Check is this is a valid email

param
array string to check
return
true if this is a valid string

        return true;
    
private static booleancheckNumeric(java.lang.String array)
Check is this is a valid numeric

param
array string to check
return
true if this is a valid string


        int len = array.length();

        /* If the whole content is just a minus sign, this is invalid. */
        if (len == 1 && array.charAt(0) == '-") {
            return false;
        }

        int offset = 0;

        //
        // if first character is a minus sign then don't let the loop
        // below see it.
        //
        if (array.charAt(0) == '-") {
            offset++;
        }

        /*
         * Now we can just validate the inserted data. If we see a minus
         * sign then it must be in the wrong place because of the check
         * above
         */
        for (; offset < len; offset++) {
            char c = array.charAt(offset);
            if (((c < '0") || (c > '9"))) {
                return false;
            }
        }
        return true;
    
private static booleancheckPhoneNumber(java.lang.String array)
Check is this is a valid phone number

param
array string to check
return
true if this is a valid string

        int len = array.length();
        for (int i = 0; i < len; i++) {
            char c = array.charAt(i);
            if (((c < '0") || (c > '9")) &&
                (!(c == '#" || c == '*" || c == '+"))) {
                return false;
            }
        }
        return true;
    
private static booleancheckURL(java.lang.String array)
Check is this is a valid url

param
array string to check
return
true if this is a valid string

        return true;
    
public static booleanisValidString(DynamicCharacterArray dca, int constraints)
Check is this is a valid string given the constraints

param
dca string to check
param
constraints the constraints
return
true if this is a valid string

 

        if (dca.length() == 0) {
            return true;
        }

        switch (constraints & TextField.CONSTRAINT_MASK) {
            case TextField.ANY:         return true;
            case TextField.DECIMAL:     return checkDecimal(dca.toString());
            case TextField.EMAILADDR:   return checkEmail(dca.toString());
            case TextField.NUMERIC:     return checkNumeric(dca.toString());
            case TextField.PHONENUMBER: return checkPhoneNumber(dca.toString());
            case TextField.URL:         return checkURL(dca.toString());
        }
        return false;