/src/mpv5/utils/text/TypeConversion.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/ · Java · 114 lines · 52 code · 16 blank · 46 comment · 12 complexity · 9be7b9f033fa4c151be182705980cbd4 MD5 · raw file

  1. /*
  2. * This file is part of YaBS.
  3. *
  4. * YaBS is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * YaBS is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with YaBS. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package mpv5.utils.text;
  18. //~--- JDK imports ------------------------------------------------------------
  19. import java.net.InetAddress;
  20. import java.util.Locale;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import java.util.regex.Pattern;
  24. import javax.mail.internet.AddressException;
  25. import javax.mail.internet.InternetAddress;
  26. /**
  27. *
  28. *
  29. */
  30. public class TypeConversion {
  31. private static final Pattern rfc2822 = Pattern.compile(
  32. "^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$");
  33. /**
  34. * Converts a boolean value into either "0" (false) or "1" (true)
  35. *
  36. * @param bool
  37. * @return The string value
  38. */
  39. public static String booleanToString(boolean bool) {
  40. String booleans = "0";
  41. if (bool) {
  42. booleans = "1";
  43. }
  44. return booleans;
  45. }
  46. /**
  47. * Converts a String into a boolean value. Everything except the String "1"
  48. * or "true" will return false.
  49. *
  50. * @param string
  51. * @return
  52. */
  53. public static boolean stringToBoolean(String string) {
  54. boolean val = false;
  55. if (string != null) {
  56. if (string.equals("1") || Boolean.valueOf(string)) {
  57. val = true;
  58. }
  59. }
  60. return val;
  61. }
  62. /**
  63. * Converts a String into a Locale
  64. *
  65. * @param localestring In Format "de" or "de_DE"
  66. * @return
  67. */
  68. public static Locale stringToLocale(String localestring) {
  69. String[] data = localestring.split("_");
  70. String lang = localestring.substring(0, 2);
  71. String country = null;
  72. if ((data != null) && (data.length > 1)) {
  73. country = data[1];
  74. }
  75. if (country != null) {
  76. return new Locale(lang.toLowerCase(), country.toUpperCase());
  77. } else {
  78. return new Locale(lang.toLowerCase());
  79. }
  80. }
  81. /**
  82. *
  83. * @param email
  84. * @return
  85. */
  86. public static InternetAddress stringToMail(String email) {
  87. if (!rfc2822.matcher(email).matches()) {
  88. return null;
  89. }
  90. try {
  91. return new InternetAddress(email);
  92. } catch (AddressException ex) {
  93. Logger.getLogger(TypeConversion.class.getName()).log(Level.SEVERE, null, ex);
  94. return null;
  95. }
  96. }
  97. }
  98. //~ Formatted by Jindent --- http://www.jindent.com