/scm-core/src/main/java/sonia/scm/util/ValidationUtil.java

https://bitbucket.org/sdorra/scm-manager/ · Java · 190 lines · 68 code · 27 blank · 95 comment · 13 complexity · a89cb2e5cf94e52a469942a9a97ce7f4 MD5 · raw file

  1. /**
  2. * Copyright (c) 2010, Sebastian Sdorra
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. Neither the name of SCM-Manager; nor the names of its
  14. * contributors may be used to endorse or promote products derived from this
  15. * software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  24. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * http://bitbucket.org/sdorra/scm-manager
  29. *
  30. */
  31. package sonia.scm.util;
  32. //~--- non-JDK imports --------------------------------------------------------
  33. import com.google.common.base.Splitter;
  34. import sonia.scm.Validateable;
  35. //~--- JDK imports ------------------------------------------------------------
  36. import java.util.regex.Pattern;
  37. /**
  38. *
  39. * @author Sebastian Sdorra
  40. */
  41. public final class ValidationUtil
  42. {
  43. /** Field description */
  44. private static final String REGEX_MAIL =
  45. "^[A-z0-9][\\w.-]*@[A-z0-9][\\w\\-\\.]+\\.[A-z0-9]{2,6}$";
  46. /** Field description */
  47. private static final String REGEX_NAME =
  48. "^[A-z0-9\\.\\-_@]|[^ ]([A-z0-9\\.\\-_@ ]*[A-z0-9\\.\\-_@]|[^ ])?$";
  49. /** Field description */
  50. private static final String REGEX_REPOSITORYNAME =
  51. "(?!^\\.\\.$)(?!^\\.$)(?!.*[\\\\\\[\\]])^[A-z0-9\\.][A-z0-9\\.\\-_/]*$";
  52. //~--- constructors ---------------------------------------------------------
  53. /**
  54. * Constructs ...
  55. *
  56. */
  57. private ValidationUtil() {}
  58. //~--- get methods ----------------------------------------------------------
  59. /**
  60. * Method description
  61. *
  62. *
  63. * @param value
  64. *
  65. * @return
  66. */
  67. public static boolean isFilenameValid(String value)
  68. {
  69. return Util.isNotEmpty(value) && isNotContaining(value, "/", "\\", ":");
  70. }
  71. /**
  72. * Method description
  73. *
  74. *
  75. * @param value
  76. *
  77. * @return
  78. */
  79. public static boolean isMailAddressValid(String value)
  80. {
  81. return Util.isNotEmpty(value) && value.matches(REGEX_MAIL);
  82. }
  83. /**
  84. * Method description
  85. *
  86. *
  87. * @param name
  88. *
  89. * @return
  90. */
  91. public static boolean isNameValid(String name)
  92. {
  93. return Util.isNotEmpty(name) && name.matches(REGEX_NAME);
  94. }
  95. /**
  96. * Method description
  97. *
  98. *
  99. * @param value
  100. * @param notAllowedStrings
  101. *
  102. * @return
  103. */
  104. public static boolean isNotContaining(String value,
  105. String... notAllowedStrings)
  106. {
  107. boolean result = Util.isNotEmpty(value);
  108. if (result && (notAllowedStrings != null))
  109. {
  110. for (String nas : notAllowedStrings)
  111. {
  112. if (value.indexOf(nas) >= 0)
  113. {
  114. result = false;
  115. break;
  116. }
  117. }
  118. }
  119. return result;
  120. }
  121. /**
  122. * Method description
  123. *
  124. *
  125. * @param name
  126. * @since 1.9
  127. *
  128. * @return
  129. */
  130. public static boolean isRepositoryNameValid(String name)
  131. {
  132. Pattern pattern = Pattern.compile(REGEX_REPOSITORYNAME);
  133. boolean result = true;
  134. if (Util.isNotEmpty(name))
  135. {
  136. for (String p : Splitter.on('/').split(name))
  137. {
  138. if (!pattern.matcher(p).matches())
  139. {
  140. result = false;
  141. break;
  142. }
  143. }
  144. }
  145. else
  146. {
  147. result = false;
  148. }
  149. return result;
  150. }
  151. /**
  152. * Method description
  153. *
  154. *
  155. * @param validateable
  156. *
  157. * @return
  158. */
  159. public static boolean isValid(Validateable validateable)
  160. {
  161. return (validateable != null) && validateable.isValid();
  162. }
  163. }