001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.midp.lcdui;
028:
029: import javax.microedition.lcdui.Font;
030: import javax.microedition.lcdui.TextField;
031:
032: /**
033: * Class to handle conforming text to specified constraints
034: */
035: public class TextPolicy {
036:
037: /**
038: * Check is this is a valid decimal
039: *
040: * @param array string to check
041: * @return true if this is a valid string
042: */
043: private static boolean checkDecimal(String array) {
044:
045: int len = array.length();
046:
047: /*
048: * If the whole content is "-", ".", or "-.",
049: * this is invalid.
050: */
051: if ((len == 1 && array.charAt(0) == '-')
052: || (len == 1 && array.charAt(0) == '.')
053: || (len == 2 && array.charAt(0) == '-' && array
054: .charAt(1) == '.')) {
055: return false;
056: }
057:
058: /*
059: * For decimal constraint, it is probably easier to re-validate the
060: * whole content, than to try to validate the inserted data in
061: * relation to the existing data around it.
062: */
063: boolean hasSeparator = false;
064: for (int i = 0; i < len; i++) {
065: char c = array.charAt(i);
066:
067: /*
068: * valid characters are
069: * [0-9],
070: * '-' at the first pos,
071: * '.' as the decimal separator.
072: */
073: if (c == '.') {
074: if (!hasSeparator) {
075: hasSeparator = true;
076: } else {
077: return false;
078: }
079: } else if (((c < '0') || (c > '9')) && (c != '-' || i != 0)) {
080: return false;
081: }
082: }
083: return true;
084:
085: }
086:
087: /**
088: * Check is this is a valid email
089: *
090: * @param array string to check
091: * @return true if this is a valid string
092: */
093: private static boolean checkEmail(String array) {
094: return true;
095: }
096:
097: /**
098: * Check is this is a valid numeric
099: *
100: * @param array string to check
101: * @return true if this is a valid string
102: */
103: private static boolean checkNumeric(String array) {
104:
105: int len = array.length();
106:
107: /* If the whole content is just a minus sign, this is invalid. */
108: if (len == 1 && array.charAt(0) == '-') {
109: return false;
110: }
111:
112: int offset = 0;
113:
114: //
115: // if first character is a minus sign then don't let the loop
116: // below see it.
117: //
118: if (array.charAt(0) == '-') {
119: offset++;
120: }
121:
122: /*
123: * Now we can just validate the inserted data. If we see a minus
124: * sign then it must be in the wrong place because of the check
125: * above
126: */
127: for (; offset < len; offset++) {
128: char c = array.charAt(offset);
129: if (((c < '0') || (c > '9'))) {
130: return false;
131: }
132: }
133: return true;
134: }
135:
136: /**
137: * Check is this is a valid phone number
138: *
139: * @param array string to check
140: * @return true if this is a valid string
141: */
142: private static boolean checkPhoneNumber(String array) {
143: int len = array.length();
144: for (int i = 0; i < len; i++) {
145: char c = array.charAt(i);
146: if (((c < '0') || (c > '9'))
147: && (!(c == '#' || c == '*' || c == '+'))) {
148: return false;
149: }
150: }
151: return true;
152: }
153:
154: /**
155: * Check is this is a valid url
156: *
157: * @param array string to check
158: * @return true if this is a valid string
159: */
160: private static boolean checkURL(String array) {
161: return true;
162: }
163:
164: /**
165: * Check is this is a valid string given the constraints
166: *
167: * @param dca string to check
168: * @param constraints the constraints
169: * @return true if this is a valid string
170: */
171: public static boolean isValidString(DynamicCharacterArray dca,
172: int constraints) {
173:
174: if (dca.length() == 0) {
175: return true;
176: }
177:
178: switch (constraints & TextField.CONSTRAINT_MASK) {
179: case TextField.ANY:
180: return true;
181: case TextField.DECIMAL:
182: return checkDecimal(dca.toString());
183: case TextField.EMAILADDR:
184: return checkEmail(dca.toString());
185: case TextField.NUMERIC:
186: return checkNumeric(dca.toString());
187: case TextField.PHONENUMBER:
188: return checkPhoneNumber(dca.toString());
189: case TextField.URL:
190: return checkURL(dca.toString());
191: }
192: return false;
193: }
194:
195: }
|