PageRenderTime 57ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/spreddy/liferay-portal
Java | 988 lines | 477 code | 132 blank | 379 comment | 192 complexity | 6688748929d626e1cbad37ccef7ecf0e 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. * Returns <code>true</code> 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. * Returns <code>true</code> 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. * Returns <code>true</code> 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. * Returns <code>true</code> 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. * Returns <code>true</code> 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. * Returns <code>true</code> 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. * Returns <code>true</code> 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. * Returns <code>true</code> if the objects are either equal, the same
  140. * instance, or both <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. * Returns <code>true</code> 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. * Returns <code>true</code> if the string is an email address. The only
  176. * requirements are that the string consist of two parts separated by an @
  177. * symbol, and that 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. * Returns <code>true</code> if the string is an alphanumeric name, meaning
  202. * it contains 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. * Returns <code>true</code> if the character is in the ASCII character set.
  221. * This includes characters with integer values between 32 and 126
  222. * (inclusive).
  223. *
  224. * @param c the character to check
  225. * @return <code>true</code> if the character is in the ASCII character set;
  226. * <code>false</code> otherwise
  227. */
  228. public static boolean isAscii(char c) {
  229. int i = c;
  230. if ((i >= 32) && (i <= 126)) {
  231. return true;
  232. }
  233. else {
  234. return false;
  235. }
  236. }
  237. /**
  238. * Returns <code>true</code> if the character is an upper or lower case
  239. * English letter.
  240. *
  241. * @param c the character to check
  242. * @return <code>true</code> if the character is an upper or lower case
  243. * English letter; <code>false</code> otherwise
  244. */
  245. public static boolean isChar(char c) {
  246. int x = c;
  247. if ((x >= _CHAR_BEGIN) && (x <= _CHAR_END)) {
  248. return true;
  249. }
  250. return false;
  251. }
  252. /**
  253. * Returns <code>true</code> if string consists only of upper and lower case
  254. * English letters.
  255. *
  256. * @param s the string to check
  257. * @return <code>true</code> if the string consists only of upper and lower
  258. * case English letters
  259. */
  260. public static boolean isChar(String s) {
  261. if (isNull(s)) {
  262. return false;
  263. }
  264. for (char c : s.toCharArray()) {
  265. if (!isChar(c)) {
  266. return false;
  267. }
  268. }
  269. return true;
  270. }
  271. /**
  272. * Returns <code>true</code> if the date is valid in the Gregorian calendar.
  273. *
  274. * @param month the month to check
  275. * @param day the day to check
  276. * @return <code>true</code> if the date is valid in the Gregorian calendar;
  277. * <code>false</code> otherwise
  278. */
  279. public static boolean isDate(int month, int day, int year) {
  280. return isGregorianDate(month, day, year);
  281. }
  282. /**
  283. * Returns <code>true</code> if the character is a digit between 0 and 9
  284. * (inclusive).
  285. *
  286. * @param c the character to check
  287. * @return <code>true</code> if the character is a digit between 0 and 9
  288. * (inclusive); <code>false</code> otherwise
  289. */
  290. public static boolean isDigit(char c) {
  291. int x = c;
  292. if ((x >= _DIGIT_BEGIN) && (x <= _DIGIT_END)) {
  293. return true;
  294. }
  295. return false;
  296. }
  297. /**
  298. * Returns <code>true</code> if the string consists of only digits between 0
  299. * and 9 (inclusive).
  300. *
  301. * @param s the string to check
  302. * @return <code>true</code> if the string consists of only digits between 0
  303. * and 9 (inclusive); <code>false</code> otherwise
  304. */
  305. public static boolean isDigit(String s) {
  306. if (isNull(s)) {
  307. return false;
  308. }
  309. for (char c : s.toCharArray()) {
  310. if (!isDigit(c)) {
  311. return false;
  312. }
  313. }
  314. return true;
  315. }
  316. /**
  317. * Returns <code>true</code> if the string is a valid domain name. See
  318. * RFC-1034 (section 3), RFC-1123 (section 2.1), and RFC-952 (section B.
  319. * Lexical grammar).
  320. *
  321. * @param domainName the string to check
  322. * @return <code>true</code> if the string is a valid domain name;
  323. * <code>false</code> otherwise
  324. */
  325. public static boolean isDomain(String domainName) {
  326. // See RFC-1034 (section 3), RFC-1123 (section 2.1), and RFC-952
  327. // (section B. Lexical grammar)
  328. if (isNull(domainName)) {
  329. return false;
  330. }
  331. if (domainName.length() > 255) {
  332. return false;
  333. }
  334. String[] domainNameArray = StringUtil.split(
  335. domainName, CharPool.PERIOD);
  336. for (String domainNamePart : domainNameArray) {
  337. char[] domainNamePartCharArray = domainNamePart.toCharArray();
  338. for (int i = 0; i < domainNamePartCharArray.length; i++) {
  339. char c = domainNamePartCharArray[i];
  340. if ((i == 0) && (c == CharPool.DASH)) {
  341. return false;
  342. }
  343. else if ((i == (domainNamePartCharArray.length - 1)) &&
  344. (c == CharPool.DASH)) {
  345. return false;
  346. }
  347. else if ((!isChar(c)) && (!isDigit(c)) &&
  348. (c != CharPool.DASH)) {
  349. return false;
  350. }
  351. }
  352. }
  353. return true;
  354. }
  355. /**
  356. * Returns <code>true</code> if the string is a valid email address.
  357. *
  358. * @param emailAddress the string to check
  359. * @return <code>true</code> if the string is a valid email address;
  360. * <code>false</code> otherwise
  361. */
  362. public static boolean isEmailAddress(String emailAddress) {
  363. Matcher matcher = _emailAddressPattern.matcher(emailAddress);
  364. return matcher.matches();
  365. }
  366. /**
  367. * Returns <code>true</code> if the character is a special character in an
  368. * email address.
  369. *
  370. * @param c the character to check
  371. * @return <code>true</code> if the character is a special character in an
  372. * email address; <code>false</code> otherwise
  373. */
  374. public static boolean isEmailAddressSpecialChar(char c) {
  375. // LEP-1445
  376. for (int i = 0; i < _EMAIL_ADDRESS_SPECIAL_CHAR.length; i++) {
  377. if (c == _EMAIL_ADDRESS_SPECIAL_CHAR[i]) {
  378. return true;
  379. }
  380. }
  381. return false;
  382. }
  383. /**
  384. * Returns <code>true</code> if the date is valid in the Gregorian calendar.
  385. *
  386. * @param month the month (0-based, meaning 0 for January)
  387. * @param day the day of the month
  388. * @param year the year
  389. * @return <code>true</code> if the date is valid; <code>false</code>
  390. * otherwise
  391. */
  392. public static boolean isGregorianDate(int month, int day, int year) {
  393. if ((month < 0) || (month > 11)) {
  394. return false;
  395. }
  396. int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  397. if (month == 1) {
  398. int febMax = 28;
  399. if (((year % 4) == 0) && ((year % 100) != 0) ||
  400. ((year % 400) == 0)) {
  401. febMax = 29;
  402. }
  403. if ((day < 1) || (day > febMax)) {
  404. return false;
  405. }
  406. }
  407. else if ((day < 1) || (day > months[month])) {
  408. return false;
  409. }
  410. return true;
  411. }
  412. /**
  413. * Returns <code>true</code> if the string is a hexidecimal number. At
  414. * present the only requirement is that the string is not <code>null</code>;
  415. * it does not actually check the format of the string.
  416. *
  417. * @param s the string to check
  418. * @return <code>true</code> if the string is a hexidecimal number;
  419. * <code>false</code> otherwise
  420. * @see #isNull(String)
  421. */
  422. public static boolean isHex(String s) {
  423. if (isNull(s)) {
  424. return false;
  425. }
  426. return true;
  427. }
  428. /**
  429. * Returns <code>true</code> if the string is an HTML document. The only
  430. * requirement is that it contain the opening and closing html tags.
  431. *
  432. * @param s the string to check
  433. * @return <code>true</code> if the string is an HTML document;
  434. * <code>false</code> otherwise
  435. */
  436. public static boolean isHTML(String s) {
  437. if (isNull(s)) {
  438. return false;
  439. }
  440. if (((s.indexOf("<html>") != -1) || (s.indexOf("<HTML>") != -1)) &&
  441. ((s.indexOf("</html>") != -1) || (s.indexOf("</HTML>") != -1))) {
  442. return true;
  443. }
  444. return false;
  445. }
  446. /**
  447. * Returns <code>true</code> if the string is a valid IPv4 IP address.
  448. *
  449. * @param ipAddress the string to check
  450. * @return <code>true</code> if the string is an IPv4 IP address;
  451. * <code>false</code> otherwise
  452. */
  453. public static boolean isIPAddress(String ipAddress) {
  454. Matcher matcher = _ipAddressPattern.matcher(ipAddress);
  455. return matcher.matches();
  456. }
  457. /**
  458. * Returns <code>true</code> if the date is valid in the Julian calendar.
  459. *
  460. * @param month the month (0-based, meaning 0 for January)
  461. * @param day the day of the month
  462. * @param year the year
  463. * @return <code>true</code> if the date is valid; <code>false</code>
  464. * otherwise
  465. */
  466. public static boolean isJulianDate(int month, int day, int year) {
  467. if ((month < 0) || (month > 11)) {
  468. return false;
  469. }
  470. int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  471. if (month == 1) {
  472. int febMax = 28;
  473. if ((year % 4) == 0) {
  474. febMax = 29;
  475. }
  476. if ((day < 1) || (day > febMax)) {
  477. return false;
  478. }
  479. }
  480. else if ((day < 1) || (day > months[month])) {
  481. return false;
  482. }
  483. return true;
  484. }
  485. /**
  486. * Returns <code>true</code> if the string contains a valid number according
  487. * to the Luhn algorithm, commonly used to validate credit card numbers.
  488. *
  489. * @param number the string to check
  490. * @return <code>true</code> if the string contains a valid number according
  491. * to the Luhn algorithm; <code>false</code> otherwise
  492. */
  493. public static boolean isLUHN(String number) {
  494. if (number == null) {
  495. return false;
  496. }
  497. number = StringUtil.reverse(number);
  498. int total = 0;
  499. for (int i = 0; i < number.length(); i++) {
  500. int x = 0;
  501. if (((i + 1) % 2) == 0) {
  502. x = Integer.parseInt(number.substring(i, i + 1)) * 2;
  503. if (x >= 10) {
  504. String s = String.valueOf(x);
  505. x = Integer.parseInt(s.substring(0, 1)) +
  506. Integer.parseInt(s.substring(1, 2));
  507. }
  508. }
  509. else {
  510. x = Integer.parseInt(number.substring(i, i + 1));
  511. }
  512. total = total + x;
  513. }
  514. if ((total % 10) == 0) {
  515. return true;
  516. }
  517. else {
  518. return false;
  519. }
  520. }
  521. /**
  522. * Returns <code>true</code> if the string is a name, meaning it contains
  523. * nothing but English letters and spaces.
  524. *
  525. * @param name the string to check
  526. * @return <code>true</code> if the string is a name; <code>false</code>
  527. * otherwise
  528. */
  529. public static boolean isName(String name) {
  530. if (isNull(name)) {
  531. return false;
  532. }
  533. for (char c : name.trim().toCharArray()) {
  534. if (!isChar(c) && !Character.isWhitespace(c)) {
  535. return false;
  536. }
  537. }
  538. return true;
  539. }
  540. /**
  541. * Returns <code>true</code> if the long number object is not
  542. * <code>null</code>, meaning it is neither a <code>null</code> reference or
  543. * zero.
  544. *
  545. * @param l the long number object to check
  546. * @return <code>true</code> if the long number object is not
  547. * <code>null</code>; <code>false</code> otherwise
  548. */
  549. public static boolean isNotNull(Long l) {
  550. return !isNull(l);
  551. }
  552. /**
  553. * Returns <code>true</code> if the object is not <code>null</code>, using
  554. * the rules from {@link #isNotNull(Long)} or {@link #isNotNull(String)} if
  555. * the object is one of these types.
  556. *
  557. * @param obj the object to check
  558. * @return <code>true</code> if the object is not <code>null</code>;
  559. * <code>false</code> otherwise
  560. */
  561. public static boolean isNotNull(Object obj) {
  562. return !isNull(obj);
  563. }
  564. /**
  565. * Returns <code>true</code> if the array is not <code>null</code>, meaning
  566. * it is neither a <code>null</code> reference or empty.
  567. *
  568. * @param array the array to check
  569. * @return <code>true</code> if the array is not <code>null</code>;
  570. * <code>false</code> otherwise
  571. */
  572. public static boolean isNotNull(Object[] array) {
  573. return !isNull(array);
  574. }
  575. /**
  576. * Returns <code>true</code> if the string is not <code>null</code>, meaning
  577. * it is not a <code>null</code> reference, nothing but spaces, or the
  578. * string "<code>null</code>".
  579. *
  580. * @param s the string to check
  581. * @return <code>true</code> if the string is not <code>null</code>;
  582. * <code>false</code> otherwise
  583. */
  584. public static boolean isNotNull(String s) {
  585. return !isNull(s);
  586. }
  587. /**
  588. * Returns <code>true</code> if the long number object is <code>null</code>,
  589. * meaning it is either a <code>null</code> reference or zero.
  590. *
  591. * @param l the long number object to check
  592. * @return <code>true</code> if the long number object is <code>null</code>;
  593. * <code>false</code> otherwise
  594. */
  595. public static boolean isNull(Long l) {
  596. if ((l == null) || (l.longValue() == 0)) {
  597. return true;
  598. }
  599. else {
  600. return false;
  601. }
  602. }
  603. /**
  604. * Returns <code>true</code> if the object is <code>null</code>, using the
  605. * rules from {@link #isNull(Long)} or {@link #isNull(String)} if the object
  606. * is one of these types.
  607. *
  608. * @param obj the object to check
  609. * @return <code>true</code> if the object is <code>null</code>;
  610. * <code>false</code> otherwise
  611. */
  612. public static boolean isNull(Object obj) {
  613. if (obj instanceof Long) {
  614. return isNull((Long)obj);
  615. }
  616. else if (obj instanceof String) {
  617. return isNull((String)obj);
  618. }
  619. else if (obj == null) {
  620. return true;
  621. }
  622. else {
  623. return false;
  624. }
  625. }
  626. /**
  627. * Returns <code>true</code> if the array is <code>null</code>, meaning it
  628. * is either a <code>null</code> reference or empty.
  629. *
  630. * @param array the array to check
  631. * @return <code>true</code> if the array is <code>null</code>;
  632. * <code>false</code> otherwise
  633. */
  634. public static boolean isNull(Object[] array) {
  635. if ((array == null) || (array.length == 0)) {
  636. return true;
  637. }
  638. else {
  639. return false;
  640. }
  641. }
  642. /**
  643. * Returns <code>true</code> if the string is <code>null</code>, meaning it
  644. * is a <code>null</code> reference, nothing but spaces, or the string
  645. * "<code>null</code>".
  646. *
  647. * @param s the string to check
  648. * @return <code>true</code> if the string is <code>null</code>;
  649. * <code>false</code> otherwise
  650. */
  651. public static boolean isNull(String s) {
  652. if (s == null) {
  653. return true;
  654. }
  655. int counter = 0;
  656. for (int i = 0; i < s.length(); i++) {
  657. char c = s.charAt(i);
  658. if (c == CharPool.SPACE) {
  659. continue;
  660. }
  661. else if (counter > 3) {
  662. return false;
  663. }
  664. if (counter == 0) {
  665. if (c != CharPool.LOWER_CASE_N) {
  666. return false;
  667. }
  668. }
  669. else if (counter == 1) {
  670. if (c != CharPool.LOWER_CASE_U) {
  671. return false;
  672. }
  673. }
  674. else if ((counter == 2) || (counter == 3)) {
  675. if (c != CharPool.LOWER_CASE_L) {
  676. return false;
  677. }
  678. }
  679. counter++;
  680. }
  681. if ((counter == 0) || (counter == 4)) {
  682. return true;
  683. }
  684. return false;
  685. }
  686. /**
  687. * Returns <code>true</code> if the string is a decimal integer number,
  688. * meaning it contains nothing but decimal digits.
  689. *
  690. * @param number the string to check
  691. * @return <code>true</code> if the string is a decimal integer number;
  692. * <code>false</code> otherwise
  693. */
  694. public static boolean isNumber(String number) {
  695. if (isNull(number)) {
  696. return false;
  697. }
  698. for (char c : number.toCharArray()) {
  699. if (!isDigit(c)) {
  700. return false;
  701. }
  702. }
  703. return true;
  704. }
  705. /**
  706. * Returns <code>true</code> if the string is a valid password, meaning it
  707. * is at least four characters long and contains only letters and decimal
  708. * digits.
  709. *
  710. * @return <code>true</code> if the string is a valid password;
  711. * <code>false</code> otherwise
  712. */
  713. public static boolean isPassword(String password) {
  714. if (isNull(password)) {
  715. return false;
  716. }
  717. if (password.length() < 4) {
  718. return false;
  719. }
  720. for (char c : password.toCharArray()) {
  721. if (!isChar(c) && !isDigit(c)) {
  722. return false;
  723. }
  724. }
  725. return true;
  726. }
  727. /**
  728. * Returns <code>true</code> if the string is a valid phone number. The only
  729. * requirement is that there are decimal digits in the string; length and
  730. * format are not checked.
  731. *
  732. * @param phoneNumber the string to check
  733. * @return <code>true</code> if the string is a valid phone number;
  734. * <code>false</code> otherwise
  735. */
  736. public static boolean isPhoneNumber(String phoneNumber) {
  737. return isNumber(StringUtil.extractDigits(phoneNumber));
  738. }
  739. /**
  740. * Returns <code>true</code> if the string is a valid URL based on the rules
  741. * in {@link java.net.URL}.
  742. *
  743. * @param url the string to check
  744. * @return <code>true</code> if the string is a valid URL;
  745. * <code>false</code> otherwise
  746. */
  747. public static boolean isUrl(String url) {
  748. if (Validator.isNotNull(url)) {
  749. if (url.indexOf(CharPool.COLON) == -1) {
  750. return false;
  751. }
  752. try {
  753. new URL(url);
  754. return true;
  755. }
  756. catch (MalformedURLException murle) {
  757. }
  758. }
  759. return false;
  760. }
  761. /**
  762. * Returns <code>true</code> if the string is a valid variable name in Java.
  763. *
  764. * @param variableName the string to check
  765. * @return <code>true</code> if the string is a valid variable name in Java;
  766. * <code>false</code> otherwise
  767. */
  768. public static boolean isVariableName(String variableName) {
  769. if (isNull(variableName)) {
  770. return false;
  771. }
  772. Matcher matcher = _variableNamePattern.matcher(variableName);
  773. if (matcher.matches()) {
  774. return true;
  775. }
  776. else {
  777. return false;
  778. }
  779. }
  780. /**
  781. * Returns <code>true</code> if the string is a valid variable term, meaning
  782. * it begins with "[$" and ends with "$]".
  783. *
  784. * @param s the string to check
  785. * @return <code>true</code> if the string is a valid variable term;
  786. * <code>false</code> otherwise
  787. */
  788. public static boolean isVariableTerm(String s) {
  789. if (s.startsWith(_VARIABLE_TERM_BEGIN) &&
  790. s.endsWith(_VARIABLE_TERM_END)) {
  791. return true;
  792. }
  793. else {
  794. return false;
  795. }
  796. }
  797. /**
  798. * Returns <code>true</code> if the character is whitespace, meaning it is
  799. * either the <code>null</code> character '0' or whitespace according to
  800. * {@link java.lang.Character#isWhitespace(char)}.
  801. *
  802. * @param c the character to check
  803. * @return <code>true</code> if the character is whitespace;
  804. * <code>false</code> otherwise
  805. */
  806. public static boolean isWhitespace(char c) {
  807. int i = c;
  808. if ((i == 0) || Character.isWhitespace(c)) {
  809. return true;
  810. }
  811. else {
  812. return false;
  813. }
  814. }
  815. /**
  816. * Returns <code>true</code> if the string is an XML document. The only
  817. * requirement is that it contain either the xml start tag "<?xml" or the
  818. * empty document tag "<root />".
  819. *
  820. * @param s the string to check
  821. * @return <code>true</code> if the string is an XML document;
  822. * <code>false</code> otherwise
  823. */
  824. public static boolean isXml(String s) {
  825. if (s.startsWith(_XML_BEGIN) || s.startsWith(_XML_EMPTY)) {
  826. return true;
  827. }
  828. else {
  829. return false;
  830. }
  831. }
  832. private static final int _CHAR_BEGIN = 65;
  833. private static final int _CHAR_END = 122;
  834. private static final int _DIGIT_BEGIN = 48;
  835. private static final int _DIGIT_END = 57;
  836. private static final char[] _EMAIL_ADDRESS_SPECIAL_CHAR = new char[] {
  837. '.', '!', '#', '$', '%', '&', '\'', '*', '+', '-', '/', '=', '?', '^',
  838. '_', '`', '{', '|', '}', '~'
  839. };
  840. private static final String _VARIABLE_TERM_BEGIN = "[$";
  841. private static final String _VARIABLE_TERM_END = "$]";
  842. private static final String _XML_BEGIN = "<?xml";
  843. private static final String _XML_EMPTY = "<root />";
  844. private static Pattern _emailAddressPattern = Pattern.compile(
  845. "[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@" +
  846. "(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?");
  847. private static Pattern _ipAddressPattern = Pattern.compile(
  848. "\\b" +
  849. "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." +
  850. "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." +
  851. "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." +
  852. "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])" +
  853. "\\b");
  854. private static Pattern _variableNamePattern = Pattern.compile(
  855. "[_a-zA-Z]+[_a-zA-Z0-9]*");
  856. }