PageRenderTime 54ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/portal-service/src/com/liferay/portal/kernel/util/Validator.java

https://github.com/viktorkovacs/liferay-portal-trunk
Java | 976 lines | 473 code | 131 blank | 372 comment | 190 complexity | fc911d4c48d69661925ed842dca734e4 MD5 | raw file
  1. /**
  2. * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.portal.kernel.util;
  15. import java.net.MalformedURLException;
  16. import java.net.URL;
  17. import java.util.regex.Matcher;
  18. import java.util.regex.Pattern;
  19. /**
  20. * Provides utility methods related to data validation and format checking.
  21. *
  22. * @author Brian Wing Shun Chan
  23. * @author Alysa Carver
  24. */
  25. public class Validator {
  26. /**
  27. * Determines if the booleans are equal.
  28. *
  29. * @param boolean1 the first boolean
  30. * @param boolean2 the second boolean
  31. * @return <code>true</code> if the booleans are equal; <code>false</code>
  32. * otherwise
  33. */
  34. public static boolean equals(boolean boolean1, boolean boolean2) {
  35. if (boolean1 == boolean2) {
  36. return true;
  37. }
  38. else {
  39. return false;
  40. }
  41. }
  42. /**
  43. * Determines if the bytes are equal.
  44. *
  45. * @param byte1 the first byte
  46. * @param byte2 the second byte
  47. * @return <code>true</code> if the bytes are equal; <code>false</code>
  48. * otherwise
  49. */
  50. public static boolean equals(byte byte1, byte byte2) {
  51. if (byte1 == byte2) {
  52. return true;
  53. }
  54. else {
  55. return false;
  56. }
  57. }
  58. /**
  59. * Determines if the characters are equal.
  60. *
  61. * @param char1 the first character
  62. * @param char2 the second character
  63. * @return <code>true</code> if the characters are equal; <code>false</code>
  64. * otherwise
  65. */
  66. public static boolean equals(char char1, char char2) {
  67. if (char1 == char2) {
  68. return true;
  69. }
  70. else {
  71. return false;
  72. }
  73. }
  74. /**
  75. * Determines if the doubles are equal.
  76. *
  77. * @param double1 the first double
  78. * @param double2 the second double
  79. * @return <code>true</code> if the doubles are equal; <code>false</code>
  80. * otherwise
  81. */
  82. public static boolean equals(double double1, double double2) {
  83. if (Double.compare(double1, double2) == 0) {
  84. return true;
  85. }
  86. else {
  87. return false;
  88. }
  89. }
  90. /**
  91. * Determines if the floats are equal.
  92. *
  93. * @param float1 the first float
  94. * @param float2 the second float
  95. * @return <code>true</code> if the floats are equal; <code>false</code>
  96. * otherwise
  97. */
  98. public static boolean equals(float float1, float float2) {
  99. if (Float.compare(float1, float2) == 0) {
  100. return true;
  101. }
  102. else {
  103. return false;
  104. }
  105. }
  106. /**
  107. * Determines if the integers are equal.
  108. *
  109. * @param int1 the first integer
  110. * @param int2 the second integer
  111. * @return <code>true</code> if the integers are equal; <code>false</code>
  112. * otherwise
  113. */
  114. public static boolean equals(int int1, int int2) {
  115. if (int1 == int2) {
  116. return true;
  117. }
  118. else {
  119. return false;
  120. }
  121. }
  122. /**
  123. * Determines if the long integers are equal.
  124. *
  125. * @param long1 the first long integer
  126. * @param long2 the second long integer
  127. * @return <code>true</code> if the long integers are equal;
  128. * <code>false</code> otherwise
  129. */
  130. public static boolean equals(long long1, long long2) {
  131. if (long1 == long2) {
  132. return true;
  133. }
  134. else {
  135. return false;
  136. }
  137. }
  138. /**
  139. * Determines if the objects are either equal, the same instance, or both
  140. * <code>null</code>.
  141. *
  142. * @param obj1 the first object
  143. * @param obj2 the second object
  144. * @return <code>true</code> if the objects are either equal, the same
  145. * instance, or both <code>null</code>; <code>false</code> otherwise
  146. */
  147. public static boolean equals(Object obj1, Object obj2) {
  148. if ((obj1 == null) && (obj2 == null)) {
  149. return true;
  150. }
  151. else if ((obj1 == null) || (obj2 == null)) {
  152. return false;
  153. }
  154. else {
  155. return obj1.equals(obj2);
  156. }
  157. }
  158. /**
  159. * Determines if the short integers are equal.
  160. *
  161. * @param short1 the first short integer
  162. * @param short2 the second short integer
  163. * @return <code>true</code> if the short integers are equal;
  164. * <code>false</code> otherwise
  165. */
  166. public static boolean equals(short short1, short short2) {
  167. if (short1 == short2) {
  168. return true;
  169. }
  170. else {
  171. return false;
  172. }
  173. }
  174. /**
  175. * Determines if the string is an email address. The only requirements are
  176. * that the string consist of two parts separated by an @ symbol, and that
  177. * it contain no whitespace.
  178. *
  179. * @param address the string to check
  180. * @return <code>true</code> if the string is an email address;
  181. * <code>false</code> otherwise
  182. */
  183. public static boolean isAddress(String address) {
  184. if (isNull(address)) {
  185. return false;
  186. }
  187. String[] tokens = address.split(StringPool.AT);
  188. if (tokens.length != 2) {
  189. return false;
  190. }
  191. for (String token : tokens) {
  192. for (char c : token.toCharArray()) {
  193. if (Character.isWhitespace(c)) {
  194. return false;
  195. }
  196. }
  197. }
  198. return true;
  199. }
  200. /**
  201. * Determines if the string is an alphanumeric name, meaning it contains
  202. * nothing but English letters, numbers, and spaces.
  203. *
  204. * @param name the string to check
  205. * @return <code>true</code> if the string is an Alphanumeric name;
  206. * <code>false</code> otherwise
  207. */
  208. public static boolean isAlphanumericName(String name) {
  209. if (isNull(name)) {
  210. return false;
  211. }
  212. for (char c : name.trim().toCharArray()) {
  213. if (!isChar(c) && !isDigit(c) && !Character.isWhitespace(c)) {
  214. return false;
  215. }
  216. }
  217. return true;
  218. }
  219. /**
  220. * Determines if the character is in the ASCII character set. This includes
  221. * characters with integer values between 32 and 126 (inclusive).
  222. *
  223. * @param c the character to check
  224. * @return <code>true</code> if the character is in the ASCII character set;
  225. * <code>false</code> otherwise
  226. */
  227. public static boolean isAscii(char c) {
  228. int i = c;
  229. if ((i >= 32) && (i <= 126)) {
  230. return true;
  231. }
  232. else {
  233. return false;
  234. }
  235. }
  236. /**
  237. * Determines if the character is an upper or lower case English letter.
  238. *
  239. * @param c the character to check
  240. * @return <code>true</code> if the character is an upper or lower case
  241. * English letter; <code>false</code> otherwise
  242. */
  243. public static boolean isChar(char c) {
  244. int x = c;
  245. if ((x >= _CHAR_BEGIN) && (x <= _CHAR_END)) {
  246. return true;
  247. }
  248. return false;
  249. }
  250. /**
  251. * Determines if string consists only of upper and lower case English
  252. * letters.
  253. *
  254. * @param s the string to check
  255. * @return <code>true</code> if the string consists only of upper and lower
  256. * case English letters
  257. */
  258. public static boolean isChar(String s) {
  259. if (isNull(s)) {
  260. return false;
  261. }
  262. for (char c : s.toCharArray()) {
  263. if (!isChar(c)) {
  264. return false;
  265. }
  266. }
  267. return true;
  268. }
  269. /**
  270. * Determines if the date is valid in the Gregorian calendar.
  271. *
  272. * @param month the month to check
  273. * @param day the day to check
  274. * @return <code>true</code> if the date is valid in the Gregorian calendar;
  275. * <code>false</code> otherwise
  276. */
  277. public static boolean isDate(int month, int day, int year) {
  278. return isGregorianDate(month, day, year);
  279. }
  280. /**
  281. * Determines if the character is a digit between 0 and 9 (inclusive).
  282. *
  283. * @param c the character to check
  284. * @return <code>true</code> if the character is a digit between 0 and 9
  285. * (inclusive); <code>false</code> otherwise
  286. */
  287. public static boolean isDigit(char c) {
  288. int x = c;
  289. if ((x >= _DIGIT_BEGIN) && (x <= _DIGIT_END)) {
  290. return true;
  291. }
  292. return false;
  293. }
  294. /**
  295. * Determines if the string consists of only digits between 0 and 9
  296. * (inclusive).
  297. *
  298. * @param s the string to check
  299. * @return <code>true</code> if the string consists of only digits between 0
  300. * and 9 (inclusive); <code>false</code> otherwise
  301. */
  302. public static boolean isDigit(String s) {
  303. if (isNull(s)) {
  304. return false;
  305. }
  306. for (char c : s.toCharArray()) {
  307. if (!isDigit(c)) {
  308. return false;
  309. }
  310. }
  311. return true;
  312. }
  313. /**
  314. * Determines if the string is a valid domain name. See RFC-1034 (section
  315. * 3), RFC-1123 (section 2.1), and RFC-952 (section B. Lexical grammar).
  316. *
  317. * @param domainName the string to check
  318. * @return <code>true</code> if the string is a valid domain name;
  319. * <code>false</code> otherwise
  320. */
  321. public static boolean isDomain(String domainName) {
  322. // See RFC-1034 (section 3), RFC-1123 (section 2.1), and RFC-952
  323. // (section B. Lexical grammar)
  324. if (isNull(domainName)) {
  325. return false;
  326. }
  327. if (domainName.length() > 255) {
  328. return false;
  329. }
  330. String[] domainNameArray = StringUtil.split(
  331. domainName, StringPool.PERIOD);
  332. for (String domainNamePart : domainNameArray) {
  333. char[] domainNamePartCharArray = domainNamePart.toCharArray();
  334. for (int i = 0; i < domainNamePartCharArray.length; i++) {
  335. char c = domainNamePartCharArray[i];
  336. if ((i == 0) && (c == CharPool.DASH)) {
  337. return false;
  338. }
  339. else if ((i == (domainNamePartCharArray.length - 1)) &&
  340. (c == CharPool.DASH)) {
  341. return false;
  342. }
  343. else if ((!isChar(c)) && (!isDigit(c)) &&
  344. (c != CharPool.DASH)) {
  345. return false;
  346. }
  347. }
  348. }
  349. return true;
  350. }
  351. /**
  352. * Determines if the string is a valid email address.
  353. *
  354. * @param emailAddress the string to check
  355. * @return <code>true</code> if the string is a valid email address;
  356. * <code>false</code> otherwise
  357. */
  358. public static boolean isEmailAddress(String emailAddress) {
  359. Matcher matcher = _emailAddressPattern.matcher(emailAddress);
  360. return matcher.matches();
  361. }
  362. /**
  363. * Determines if the character is a special character in an email address.
  364. *
  365. * @param c the character to check
  366. * @return <code>true</code> if the character is a special character in an
  367. * email address; <code>false</code> otherwise
  368. */
  369. public static boolean isEmailAddressSpecialChar(char c) {
  370. // LEP-1445
  371. for (int i = 0; i < _EMAIL_ADDRESS_SPECIAL_CHAR.length; i++) {
  372. if (c == _EMAIL_ADDRESS_SPECIAL_CHAR[i]) {
  373. return true;
  374. }
  375. }
  376. return false;
  377. }
  378. /**
  379. * Determines if the date is valid in the Gregorian calendar.
  380. *
  381. * @param month the month (0-based, meaning 0 for January)
  382. * @param day the day of the month
  383. * @param year the year
  384. * @return <code>true</code> if the date is valid; <code>false</code>
  385. * otherwise
  386. */
  387. public static boolean isGregorianDate(int month, int day, int year) {
  388. if ((month < 0) || (month > 11)) {
  389. return false;
  390. }
  391. int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  392. if (month == 1) {
  393. int febMax = 28;
  394. if (((year % 4) == 0) && ((year % 100) != 0) ||
  395. ((year % 400) == 0)) {
  396. febMax = 29;
  397. }
  398. if ((day < 1) || (day > febMax)) {
  399. return false;
  400. }
  401. }
  402. else if ((day < 1) || (day > months[month])) {
  403. return false;
  404. }
  405. return true;
  406. }
  407. /**
  408. * Determines if the string is a hexidecimal number. At present the only
  409. * requirement is that the string is not <code>null</code>; it does not
  410. * actually check the format of the string.
  411. *
  412. * @param s the string to check
  413. * @return <code>true</code> if the string is a hexidecimal number;
  414. * <code>false</code> otherwise
  415. * @see #isNull(String)
  416. */
  417. public static boolean isHex(String s) {
  418. if (isNull(s)) {
  419. return false;
  420. }
  421. return true;
  422. }
  423. /**
  424. * Determines if the string is an HTML document. The only requirement is
  425. * that it contain the opening and closing html tags.
  426. *
  427. * @param s the string to check
  428. * @return <code>true</code> if the string is an HTML document;
  429. * <code>false</code> otherwise
  430. */
  431. public static boolean isHTML(String s) {
  432. if (isNull(s)) {
  433. return false;
  434. }
  435. if (((s.indexOf("<html>") != -1) || (s.indexOf("<HTML>") != -1)) &&
  436. ((s.indexOf("</html>") != -1) || (s.indexOf("</HTML>") != -1))) {
  437. return true;
  438. }
  439. return false;
  440. }
  441. /**
  442. * Determines if the string is a valid IPv4 IP address.
  443. *
  444. * @param ipAddress the string to check
  445. * @return <code>true</code> if the string is an IPv4 IP address;
  446. * <code>false</code> otherwise
  447. */
  448. public static boolean isIPAddress(String ipAddress) {
  449. Matcher matcher = _ipAddressPattern.matcher(ipAddress);
  450. return matcher.matches();
  451. }
  452. /**
  453. * Determines if the date is valid in the Julian calendar.
  454. *
  455. * @param month the month (0-based, meaning 0 for January)
  456. * @param day the day of the month
  457. * @param year the year
  458. * @return <code>true</code> if the date is valid; <code>false</code>
  459. * otherwise
  460. */
  461. public static boolean isJulianDate(int month, int day, int year) {
  462. if ((month < 0) || (month > 11)) {
  463. return false;
  464. }
  465. int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  466. if (month == 1) {
  467. int febMax = 28;
  468. if ((year % 4) == 0) {
  469. febMax = 29;
  470. }
  471. if ((day < 1) || (day > febMax)) {
  472. return false;
  473. }
  474. }
  475. else if ((day < 1) || (day > months[month])) {
  476. return false;
  477. }
  478. return true;
  479. }
  480. /**
  481. * Determines if the string contains a valid number according to the Luhn
  482. * algorithm, commonly used to validate credit card numbers.
  483. *
  484. * @param number the string to check
  485. * @return <code>true</code> if the string contains a valid number according
  486. * to the Luhn algorithm; <code>false</code> otherwise
  487. */
  488. public static boolean isLUHN(String number) {
  489. if (number == null) {
  490. return false;
  491. }
  492. number = StringUtil.reverse(number);
  493. int total = 0;
  494. for (int i = 0; i < number.length(); i++) {
  495. int x = 0;
  496. if (((i + 1) % 2) == 0) {
  497. x = Integer.parseInt(number.substring(i, i + 1)) * 2;
  498. if (x >= 10) {
  499. String s = String.valueOf(x);
  500. x = Integer.parseInt(s.substring(0, 1)) +
  501. Integer.parseInt(s.substring(1, 2));
  502. }
  503. }
  504. else {
  505. x = Integer.parseInt(number.substring(i, i + 1));
  506. }
  507. total = total + x;
  508. }
  509. if ((total % 10) == 0) {
  510. return true;
  511. }
  512. else {
  513. return false;
  514. }
  515. }
  516. /**
  517. * Determines if the string is a name, meaning it contains nothing but
  518. * English letters and spaces.
  519. *
  520. * @param name the string to check
  521. * @return <code>true</code> if the string is a name; <code>false</code>
  522. * otherwise
  523. */
  524. public static boolean isName(String name) {
  525. if (isNull(name)) {
  526. return false;
  527. }
  528. for (char c : name.trim().toCharArray()) {
  529. if (!isChar(c) && !Character.isWhitespace(c)) {
  530. return false;
  531. }
  532. }
  533. return true;
  534. }
  535. /**
  536. * Determines if the long number object is not <code>null</code>, meaning it
  537. * is neither a <code>null</code> reference or zero.
  538. *
  539. * @param l the long number object to check
  540. * @return <code>true</code> if the long number object is not
  541. * <code>null</code>; <code>false</code> otherwise
  542. */
  543. public static boolean isNotNull(Long l) {
  544. return !isNull(l);
  545. }
  546. /**
  547. * Determines if the object is not <code>null</code>, using the rules from
  548. * {@link #isNotNull(Long)} or {@link #isNotNull(String)} if the object is
  549. * one of these types.
  550. *
  551. * @param obj the object to check
  552. * @return <code>true</code> if the object is not <code>null</code>;
  553. * <code>false</code> otherwise
  554. */
  555. public static boolean isNotNull(Object obj) {
  556. return !isNull(obj);
  557. }
  558. /**
  559. * Determines if the array is not <code>null</code>, meaning it is neither a
  560. * <code>null</code> reference or empty.
  561. *
  562. * @param array the array to check
  563. * @return <code>true</code> if the array is not <code>null</code>;
  564. * <code>false</code> otherwise
  565. */
  566. public static boolean isNotNull(Object[] array) {
  567. return !isNull(array);
  568. }
  569. /**
  570. * Determines if the string is not <code>null</code>, meaning it is not a
  571. * <code>null</code> reference, nothing but spaces, or the string
  572. * "<code>null</code>".
  573. *
  574. * @param s the string to check
  575. * @return <code>true</code> if the string is not <code>null</code>;
  576. * <code>false</code> otherwise
  577. */
  578. public static boolean isNotNull(String s) {
  579. return !isNull(s);
  580. }
  581. /**
  582. * Determines if the long number object is <code>null</code>, meaning it is
  583. * either a <code>null</code> reference or zero.
  584. *
  585. * @param l the long number object to check
  586. * @return <code>true</code> if the long number object is <code>null</code>;
  587. * <code>false</code> otherwise
  588. */
  589. public static boolean isNull(Long l) {
  590. if ((l == null) || (l.longValue() == 0)) {
  591. return true;
  592. }
  593. else {
  594. return false;
  595. }
  596. }
  597. /**
  598. * Determines if the object is <code>null</code>, using the rules from
  599. * {@link #isNull(Long)} or {@link #isNull(String)} if the object is one of
  600. * these types.
  601. *
  602. * @param obj the object to check
  603. * @return <code>true</code> if the object is <code>null</code>;
  604. * <code>false</code> otherwise
  605. */
  606. public static boolean isNull(Object obj) {
  607. if (obj instanceof Long) {
  608. return isNull((Long)obj);
  609. }
  610. else if (obj instanceof String) {
  611. return isNull((String)obj);
  612. }
  613. else if (obj == null) {
  614. return true;
  615. }
  616. else {
  617. return false;
  618. }
  619. }
  620. /**
  621. * Determines if the array is <code>null</code>, meaning it is either a
  622. * <code>null</code> reference or empty.
  623. *
  624. * @param array the array to check
  625. * @return <code>true</code> if the array is <code>null</code>;
  626. * <code>false</code> otherwise
  627. */
  628. public static boolean isNull(Object[] array) {
  629. if ((array == null) || (array.length == 0)) {
  630. return true;
  631. }
  632. else {
  633. return false;
  634. }
  635. }
  636. /**
  637. * Determines if the string is <code>null</code>, meaning it is a
  638. * <code>null</code> reference, nothing but spaces, or the string
  639. * "<code>null</code>".
  640. *
  641. * @param s the string to check
  642. * @return <code>true</code> if the string is <code>null</code>;
  643. * <code>false</code> otherwise
  644. */
  645. public static boolean isNull(String s) {
  646. if (s == null) {
  647. return true;
  648. }
  649. int counter = 0;
  650. for (int i = 0; i < s.length(); i++) {
  651. char c = s.charAt(i);
  652. if (c == CharPool.SPACE) {
  653. continue;
  654. }
  655. else if (counter > 3) {
  656. return false;
  657. }
  658. if (counter == 0) {
  659. if (c != CharPool.LOWER_CASE_N) {
  660. return false;
  661. }
  662. }
  663. else if (counter == 1) {
  664. if (c != CharPool.LOWER_CASE_U) {
  665. return false;
  666. }
  667. }
  668. else if ((counter == 2) || (counter == 3)) {
  669. if (c != CharPool.LOWER_CASE_L) {
  670. return false;
  671. }
  672. }
  673. counter++;
  674. }
  675. if ((counter == 0) || (counter == 4)) {
  676. return true;
  677. }
  678. return false;
  679. }
  680. /**
  681. * Determines if the string is a decimal integer number, meaning it contains
  682. * nothing but decimal digits.
  683. *
  684. * @param number the string to check
  685. * @return <code>true</code> if the string is a decimal integer number;
  686. * <code>false</code> otherwise
  687. */
  688. public static boolean isNumber(String number) {
  689. if (isNull(number)) {
  690. return false;
  691. }
  692. for (char c : number.toCharArray()) {
  693. if (!isDigit(c)) {
  694. return false;
  695. }
  696. }
  697. return true;
  698. }
  699. /**
  700. * Determines if the string is a valid password, meaning it is at least four
  701. * characters long and contains only letters and decimal digits.
  702. *
  703. * @return <code>true</code> if the string is a valid password;
  704. * <code>false</code> otherwise
  705. */
  706. public static boolean isPassword(String password) {
  707. if (isNull(password)) {
  708. return false;
  709. }
  710. if (password.length() < 4) {
  711. return false;
  712. }
  713. for (char c : password.toCharArray()) {
  714. if (!isChar(c) && !isDigit(c)) {
  715. return false;
  716. }
  717. }
  718. return true;
  719. }
  720. /**
  721. * Determines if the string is a valid phone number. The only requirement is
  722. * that there are decimal digits in the string; length and format are not
  723. * checked.
  724. *
  725. * @param phoneNumber the string to check
  726. * @return <code>true</code> if the string is a valid phone number;
  727. * <code>false</code> otherwise
  728. */
  729. public static boolean isPhoneNumber(String phoneNumber) {
  730. return isNumber(StringUtil.extractDigits(phoneNumber));
  731. }
  732. /**
  733. * Determines if the string is a valid URL based on the rules in {@link
  734. * java.net.URL}.
  735. *
  736. * @param url the string to check
  737. * @return <code>true</code> if the string is a valid URL;
  738. * <code>false</code> otherwise
  739. */
  740. public static boolean isUrl(String url) {
  741. if (Validator.isNotNull(url)) {
  742. try {
  743. new URL(url);
  744. return true;
  745. }
  746. catch (MalformedURLException murle) {
  747. }
  748. }
  749. return false;
  750. }
  751. /**
  752. * Determines if the string is a valid variable name in Java.
  753. *
  754. * @param variableName the string to check
  755. * @return <code>true</code> if the string is a valid variable name in Java;
  756. * <code>false</code> otherwise
  757. */
  758. public static boolean isVariableName(String variableName) {
  759. if (isNull(variableName)) {
  760. return false;
  761. }
  762. Matcher matcher = _variableNamePattern.matcher(variableName);
  763. if (matcher.matches()) {
  764. return true;
  765. }
  766. else {
  767. return false;
  768. }
  769. }
  770. /**
  771. * Determines if the string is a valid variable term, meaning it begins with
  772. * "[$" and ends with "$]".
  773. *
  774. * @param s the string to check
  775. * @return <code>true</code> if the string is a valid variable term;
  776. * <code>false</code> otherwise
  777. */
  778. public static boolean isVariableTerm(String s) {
  779. if (s.startsWith(_VARIABLE_TERM_BEGIN) &&
  780. s.endsWith(_VARIABLE_TERM_END)) {
  781. return true;
  782. }
  783. else {
  784. return false;
  785. }
  786. }
  787. /**
  788. * Determines if the character is whitespace, meaning it is either the
  789. * <code>null</code> character '0' or whitespace according to {@link
  790. * java.lang.Character#isWhitespace(char)}.
  791. *
  792. * @param c the character to check
  793. * @return <code>true</code> if the character is whitespace;
  794. * <code>false</code> otherwise
  795. */
  796. public static boolean isWhitespace(char c) {
  797. int i = c;
  798. if ((i == 0) || Character.isWhitespace(c)) {
  799. return true;
  800. }
  801. else {
  802. return false;
  803. }
  804. }
  805. /**
  806. * Determines if the string is an XML document. The only requirement is that
  807. * it contain either the xml start tag "<?xml" or the empty document tag
  808. * "<root />".
  809. *
  810. * @param s the string to check
  811. * @return <code>true</code> if the string is an XML document;
  812. * <code>false</code> otherwise
  813. */
  814. public static boolean isXml(String s) {
  815. if (s.startsWith(_XML_BEGIN) || s.startsWith(_XML_EMPTY)) {
  816. return true;
  817. }
  818. else {
  819. return false;
  820. }
  821. }
  822. private static final int _CHAR_BEGIN = 65;
  823. private static final int _CHAR_END = 122;
  824. private static final int _DIGIT_BEGIN = 48;
  825. private static final int _DIGIT_END = 57;
  826. private static final char[] _EMAIL_ADDRESS_SPECIAL_CHAR = new char[] {
  827. '.', '!', '#', '$', '%', '&', '\'', '*', '+', '-', '/', '=', '?', '^',
  828. '_', '`', '{', '|', '}', '~'
  829. };
  830. private static final String _VARIABLE_TERM_BEGIN = "[$";
  831. private static final String _VARIABLE_TERM_END = "$]";
  832. private static final String _XML_BEGIN = "<?xml";
  833. private static final String _XML_EMPTY = "<root />";
  834. private static Pattern _emailAddressPattern = Pattern.compile(
  835. "([\\w!#%&-/=_`~\\Q.$*+?^{|}\\E]+)*@([\\w-]+\\.)+[A-Za-z]+");
  836. private static Pattern _ipAddressPattern = Pattern.compile(
  837. "\\b" +
  838. "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." +
  839. "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." +
  840. "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." +
  841. "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])" +
  842. "\\b");
  843. private static Pattern _variableNamePattern = Pattern.compile(
  844. "[_a-zA-Z]+[_a-zA-Z0-9]*");
  845. }