001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.kernel.util;
022:
023: import com.liferay.portal.kernel.log.Log;
024: import com.liferay.portal.kernel.log.LogFactoryUtil;
025:
026: /**
027: * <a href="Validator.java.html"><b><i>View Source</i></b></a>
028: *
029: * @author Brian Wing Shun Chan
030: * @author Alysa Carver
031: *
032: */
033: public class Validator {
034:
035: public static boolean equals(String s1, String s2) {
036: if ((s1 == null) && (s2 == null)) {
037: return true;
038: } else if ((s1 == null) || (s2 == null)) {
039: return false;
040: } else {
041: return s1.equals(s2);
042: }
043: }
044:
045: public static boolean isAddress(String address) {
046: if (isNull(address)) {
047: return false;
048: }
049:
050: String[] tokens = address.split(StringPool.AT);
051:
052: if (tokens.length != 2) {
053: return false;
054: }
055:
056: for (int i = 0; i < tokens.length; i++) {
057: char[] c = tokens[i].toCharArray();
058:
059: for (int j = 0; j < c.length; j++) {
060: if (Character.isWhitespace(c[j])) {
061: return false;
062: }
063: }
064: }
065:
066: return true;
067: }
068:
069: public static boolean isChar(char c) {
070: return Character.isLetter(c);
071: }
072:
073: public static boolean isChar(String s) {
074: if (isNull(s)) {
075: return false;
076: }
077:
078: char[] c = s.toCharArray();
079:
080: for (int i = 0; i < c.length; i++) {
081: if (!isChar(c[i])) {
082: return false;
083: }
084: }
085:
086: return true;
087: }
088:
089: public static boolean isDigit(char c) {
090: int x = (int) c;
091:
092: if ((x >= 48) && (x <= 57)) {
093: return true;
094: }
095:
096: return false;
097: }
098:
099: public static boolean isDigit(String s) {
100: if (isNull(s)) {
101: return false;
102: }
103:
104: char[] c = s.toCharArray();
105:
106: for (int i = 0; i < c.length; i++) {
107: if (!isDigit(c[i])) {
108: return false;
109: }
110: }
111:
112: return true;
113: }
114:
115: public static boolean isHex(String s) {
116: if (isNull(s)) {
117: return false;
118: }
119:
120: return true;
121: }
122:
123: public static boolean isHTML(String s) {
124: if (isNull(s)) {
125: return false;
126: }
127:
128: if (((s.indexOf("<html>") != -1) || (s.indexOf("<HTML>") != -1))
129: && ((s.indexOf("</html>") != -1) || (s
130: .indexOf("</HTML>") != -1))) {
131:
132: return true;
133: }
134:
135: return false;
136: }
137:
138: public static boolean isLUHN(String number) {
139: if (number == null) {
140: return false;
141: }
142:
143: number = StringUtil.reverse(number);
144:
145: int total = 0;
146:
147: for (int i = 0; i < number.length(); i++) {
148: int x = 0;
149:
150: if (((i + 1) % 2) == 0) {
151: x = Integer.parseInt(number.substring(i, i + 1)) * 2;
152:
153: if (x >= 10) {
154: String s = Integer.toString(x);
155:
156: x = Integer.parseInt(s.substring(0, 1))
157: + Integer.parseInt(s.substring(1, 2));
158: }
159: } else {
160: x = Integer.parseInt(number.substring(i, i + 1));
161: }
162:
163: total = total + x;
164: }
165:
166: if ((total % 10) == 0) {
167: return true;
168: } else {
169: return false;
170: }
171: }
172:
173: public static boolean isDate(int month, int day, int year) {
174: return isGregorianDate(month, day, year);
175: }
176:
177: public static boolean isGregorianDate(int month, int day, int year) {
178: if ((month < 0) || (month > 11)) {
179: return false;
180: }
181:
182: int[] months = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
183:
184: if (month == 1) {
185: int febMax = 28;
186:
187: if (((year % 4) == 0) && ((year % 100) != 0)
188: || ((year % 400) == 0)) {
189:
190: febMax = 29;
191: }
192:
193: if ((day < 1) || (day > febMax)) {
194: return false;
195: }
196: } else if ((day < 1) || (day > months[month])) {
197: return false;
198: }
199:
200: return true;
201: }
202:
203: public static boolean isJulianDate(int month, int day, int year) {
204: if ((month < 0) || (month > 11)) {
205: return false;
206: }
207:
208: int[] months = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
209:
210: if (month == 1) {
211: int febMax = 28;
212:
213: if ((year % 4) == 0) {
214: febMax = 29;
215: }
216:
217: if ((day < 1) || (day > febMax)) {
218: return false;
219: }
220: } else if ((day < 1) || (day > months[month])) {
221: return false;
222: }
223:
224: return true;
225: }
226:
227: public static boolean isEmailAddress(String emailAddress) {
228: Boolean valid = null;
229:
230: try {
231: valid = (Boolean) PortalClassInvoker.invoke(
232: "com.liferay.util.mail.InternetAddressUtil",
233: "isValid", emailAddress);
234: } catch (Exception e) {
235: if (_log.isWarnEnabled()) {
236: _log.warn(e);
237: }
238: }
239:
240: if (valid == null) {
241: return false;
242: } else {
243: return valid.booleanValue();
244: }
245: }
246:
247: public static boolean isEmailAddressSpecialChar(char c) {
248:
249: // LEP-1445
250:
251: for (int i = 0; i < _EMAIL_ADDRESS_SPECIAL_CHAR.length; i++) {
252: if (c == _EMAIL_ADDRESS_SPECIAL_CHAR[i]) {
253: return true;
254: }
255: }
256:
257: return false;
258: }
259:
260: /**
261: * @deprecated Use <code>isEmailAddress</code>.
262: */
263: public static boolean isValidEmailAddress(String ea) {
264: return isEmailAddress(ea);
265: }
266:
267: public static boolean isName(String name) {
268: if (isNull(name)) {
269: return false;
270: }
271:
272: char[] c = name.trim().toCharArray();
273:
274: for (int i = 0; i < c.length; i++) {
275: if (((!isChar(c[i])) && (!Character.isWhitespace(c[i])))
276: || (c[i] == ',')) {
277:
278: return false;
279: }
280: }
281:
282: return true;
283: }
284:
285: public static boolean isNumber(String number) {
286: if (isNull(number)) {
287: return false;
288: }
289:
290: char[] c = number.toCharArray();
291:
292: for (int i = 0; i < c.length; i++) {
293: if (!isDigit(c[i])) {
294: return false;
295: }
296: }
297:
298: return true;
299: }
300:
301: public static boolean isNull(Object obj) {
302: if (obj instanceof Long) {
303: return isNull((Long) obj);
304: } else if (obj instanceof String) {
305: return isNull((String) obj);
306: } else if (obj == null) {
307: return true;
308: } else {
309: return false;
310: }
311: }
312:
313: public static boolean isNull(Long l) {
314: if ((l == null) || l.longValue() == 0) {
315: return true;
316: } else {
317: return false;
318: }
319: }
320:
321: public static boolean isNull(String s) {
322: if (s == null) {
323: return true;
324: }
325:
326: s = s.trim();
327:
328: if ((s.equals(StringPool.NULL)) || (s.equals(StringPool.BLANK))) {
329: return true;
330: }
331:
332: return false;
333: }
334:
335: public static boolean isNull(Object[] array) {
336: if ((array == null) || (array.length == 0)) {
337: return true;
338: } else {
339: return false;
340: }
341: }
342:
343: public static boolean isNotNull(Object obj) {
344: return !isNull(obj);
345: }
346:
347: public static boolean isNotNull(Long l) {
348: return !isNull(l);
349: }
350:
351: public static boolean isNotNull(String s) {
352: return !isNull(s);
353: }
354:
355: public static boolean isNotNull(Object[] array) {
356: return !isNull(array);
357: }
358:
359: public static boolean isPassword(String password) {
360: if (isNull(password)) {
361: return false;
362: }
363:
364: if (password.length() < 4) {
365: return false;
366: }
367:
368: char[] c = password.toCharArray();
369:
370: for (int i = 0; i < c.length; i++) {
371: if ((!isChar(c[i])) && (!isDigit(c[i]))) {
372:
373: return false;
374: }
375: }
376:
377: return true;
378: }
379:
380: public static boolean isPhoneNumber(String phoneNumber) {
381: return isNumber(StringUtil.extractDigits(phoneNumber));
382: }
383:
384: public static boolean isVariableTerm(String s) {
385: if (s.startsWith("[$") && s.endsWith("$]")) {
386: return true;
387: } else {
388: return false;
389: }
390: }
391:
392: private static char[] _EMAIL_ADDRESS_SPECIAL_CHAR = new char[] {
393: '.', '!', '#', '$', '%', '&', '\'', '*', '+', '-', '/',
394: '=', '?', '^', '_', '`', '{', '|', '}', '~' };
395:
396: private static Log _log = LogFactoryUtil.getLog(Validator.class);
397:
398: }
|