PageRenderTime 69ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/moo/src/main/java/net/rizon/moo/conf/Validator.java

https://gitlab.com/xMythycle/moo
Java | 291 lines | 133 code | 25 blank | 133 comment | 33 complexity | 9ca3d43a9456a5b8400895aa78aef799 MD5 | raw file
  1. package net.rizon.moo.conf;
  2. import java.io.File;
  3. import java.util.Collection;
  4. import java.util.List;
  5. import org.apache.commons.validator.routines.EmailValidator;
  6. import org.apache.commons.validator.routines.UrlValidator;
  7. public class Validator
  8. {
  9. private static EmailValidator ev = EmailValidator.getInstance(false);
  10. private static UrlValidator uv = new UrlValidator();
  11. /**
  12. * Validates entire list
  13. * @param <T> Class of list elements.
  14. * @param list List with validatable elements.
  15. * @throws ConfigurationException Thrown when an element is not valid.
  16. */
  17. public static <T extends Validatable> void validateList(List<T> list) throws ConfigurationException
  18. {
  19. if (list == null)
  20. return;
  21. for (T t : list)
  22. t.validate();
  23. }
  24. /**
  25. * Checks if object is defined.
  26. * @param <T> Class of object.
  27. * @param name Configuration item name.
  28. * @param obj Configuration item to validate.
  29. * @throws ConfigurationException Thrown when object is null.
  30. */
  31. public static <T> void validateNotNull(String name, T obj) throws ConfigurationException
  32. {
  33. if (obj == null)
  34. throw new ConfigurationException(name + " must be defined");
  35. }
  36. /**
  37. * Validates if the string is not empty.
  38. * @param name Configuration item name.
  39. * @param obj Configuration item.
  40. * @throws ConfigurationException Thrown when string is null or empty.
  41. */
  42. public static void validateNotEmpty(final String name, final String obj) throws ConfigurationException
  43. {
  44. validateNotNull(name, obj);
  45. if (obj.isEmpty())
  46. throw new ConfigurationException(name + " must not be empty");
  47. }
  48. /**
  49. * Validates if collection is not empty.
  50. * @param <T> Class of {@link Collection}.
  51. * @param name Configuration item name.
  52. * @param obj Collection.
  53. * @throws ConfigurationException Thrown when {@link Collection} is empty.
  54. */
  55. public static <T extends Collection<?>> void validateNotEmpty(final String name, final T obj) throws ConfigurationException
  56. {
  57. validateNotNull(name, obj);
  58. if (obj.isEmpty())
  59. throw new ConfigurationException(name + " must not be empty");
  60. }
  61. /**
  62. * Validates if integer is not zero.
  63. * @param name Configuration item name.
  64. * @param i Integer to validate.
  65. * @throws ConfigurationException Thrown when integer is 0.
  66. */
  67. public static void validateNotZero(final String name, final int i) throws ConfigurationException
  68. {
  69. if (i == 0)
  70. throw new ConfigurationException(name + " must be non zero");
  71. }
  72. /**
  73. * Validates if integer is positive (0 or higher)
  74. * @param name Configuration item name.
  75. * @param i Integer to validate
  76. * @throws ConfigurationException Throw when integer is smaller than 0.
  77. */
  78. public static void validatePositive(final String name, final int i) throws ConfigurationException
  79. {
  80. if (i < 0)
  81. throw new ConfigurationException(name + " must be 0 or higher");
  82. }
  83. /**
  84. * Validates if this integer is a valid port number.
  85. * @param name Configuration item name.
  86. * @param i Port number.
  87. * @param outgoing Specifies if this port is used to connect to another computer (true) or create a local socket (false).
  88. * @throws ConfigurationException Thrown when the integer is not a valid port.
  89. */
  90. public static void validatePort(final String name, final int i, final boolean outgoing) throws ConfigurationException
  91. {
  92. int lowerBound = outgoing ? 1 : 0;
  93. if (i < lowerBound || i > 65535)
  94. if (i == 0 && outgoing)
  95. throw new ConfigurationException(name + " (" + i + ") is not a valid port for outgoing connections");
  96. else
  97. throw new ConfigurationException(name + " (" + i + ") is not a valid port");
  98. }
  99. /**
  100. * Checks if the specified Host name or IP Address is valid.
  101. * @param name Configuration item name.
  102. * @param obj Host name or IP Address.
  103. * @throws ConfigurationException When Host name or IP Address is invalid.
  104. */
  105. public static void validateHost(final String name, final String obj) throws ConfigurationException
  106. {
  107. validateNotEmpty(name, obj);
  108. }
  109. public static void validateHostList(final String name, final String[] obj) throws ConfigurationException
  110. {
  111. validateNotNull(name, obj);
  112. for (String s : obj)
  113. validateHost(name, s);
  114. }
  115. /**
  116. * Checks if optional configuration item Host is not defined or valid if defined.
  117. * @param name Configuration item name.
  118. * @param obj Host name or IP Address. (Or null)
  119. * @throws ConfigurationException When Host name or IP Address is invalid if defined.
  120. */
  121. public static void validateNullOrHost(final String name, final String obj) throws ConfigurationException
  122. {
  123. if (obj != null)
  124. validateHost(name, obj);
  125. }
  126. /**
  127. * Checks if optional configuration item is not defined, or valid if defined.
  128. * @param name Configuration item name.
  129. * @param obj Item. (Or null)
  130. * @throws ConfigurationException When configuration item is not valid if defined.
  131. */
  132. public static void validateNullOrNotEmpty(final String name, final String obj) throws ConfigurationException
  133. {
  134. if (obj != null)
  135. Validator.validateNotEmpty(name, obj);
  136. }
  137. /**
  138. * Checks if optional configuration item is not defined, or valid if defined.
  139. * @param <T> Class of configuration item. (Must extend {@link Validatable})
  140. * @param name Configuration item name.
  141. * @param obj Object. (Or null)
  142. * @throws ConfigurationException When configuration item is not valid if defined.
  143. */
  144. public static <T extends Validatable> void validateNullOrValid(final String name, final T obj) throws ConfigurationException
  145. {
  146. if (obj != null)
  147. obj.validate();
  148. }
  149. /**
  150. * Checks if the IRCMask is RFC1459 compliant.
  151. * @param name Configuration item name.
  152. * @param mask IRC Mask.
  153. * @throws ConfigurationException When configuration item is null or not valid.
  154. */
  155. public static void validateIRCMask(final String name, final String mask) throws ConfigurationException
  156. {
  157. // XXX ?
  158. }
  159. /**
  160. * Checks if the list of channels is RFC1459 compliant.
  161. * @param name Configuration item name.
  162. * @param channels List of channels.
  163. * @throws ConfigurationException When a channel is invalid or not RFC1459 compliant.
  164. */
  165. public static void validateChannelList(final String name, final String[] channels) throws ConfigurationException
  166. {
  167. for (String c : channels)
  168. validateChannelName(name, c);
  169. }
  170. /**
  171. * Checks if channel name is RFC1459 compliant.
  172. * @param name Configuration item name.
  173. * @param channel Channel name.
  174. * @throws ConfigurationException When the channel name is invalid or not RFC compliant.
  175. */
  176. public static void validateChannelName(final String name, final String channel) throws ConfigurationException
  177. {
  178. validateNotEmpty(name, channel);
  179. // TODO: Better check :D
  180. if (channel.charAt(0) != '#')
  181. throw new ConfigurationException(name + " (" + channel + ") not a compliant channel name.");
  182. }
  183. /**
  184. * Checks if the list of email addresses is valid.
  185. * @param name Configuration item name.
  186. * @param emails List of email addresses.
  187. * @throws ConfigurationException When an email is invalid.
  188. */
  189. public static void validateEmailList(final String name, final List<String> emails) throws ConfigurationException
  190. {
  191. for (String email : emails)
  192. validateEmail(name, email);
  193. }
  194. /**
  195. * Checks if the email address is valid.
  196. * @param name Configuration item name.
  197. * @param email Email address.
  198. * @throws ConfigurationException When email is invalid.
  199. */
  200. public static void validateEmail(final String name, final String email) throws ConfigurationException
  201. {
  202. validateNotEmpty(name, email);
  203. if (!ev.isValid(email))
  204. throw new ConfigurationException(name + " (" + email + ") not a valid email address");
  205. }
  206. /**
  207. * Checks if specified path is correct for the current OS.
  208. * This sounds more fancy than it is, it actually just checks if it exists!
  209. * @param name Configuration item name.
  210. * @param path Path to check.
  211. * @throws ConfigurationException When path is non-existent.
  212. */
  213. public static void validatePath(final String name, final String path) throws ConfigurationException
  214. {
  215. validateNotNull(name, path);
  216. File f = new File(path);
  217. if (f.exists() && !f.canRead())
  218. throw new ConfigurationException(name + " (" + path + ") is not readable");
  219. }
  220. /**
  221. * Checks if all paths are correct for the current OS.
  222. * @param name Configuration item name.
  223. * @param paths Paths to check.
  224. * @throws ConfigurationException When path is non-existent.
  225. */
  226. public static void validatePathList(final String name, final String[] paths) throws ConfigurationException
  227. {
  228. validateNotNull(name, paths);
  229. for (String s : paths)
  230. validatePath(name, s);
  231. }
  232. /**
  233. * Checks if a list of strings does not contain empty elements.
  234. * @param name Configuration item name.
  235. * @param list List of strings.
  236. * @throws ConfigurationException When a string is empty.
  237. */
  238. public static void validateStringList(final String name, final List<String> list) throws ConfigurationException
  239. {
  240. for (String s : list)
  241. validateNotEmpty(name, s);
  242. }
  243. /**
  244. * Checks if the URL is valid.
  245. * @param name Configuration item name.
  246. * @param url URL to check.
  247. * @throws ConfigurationException When URL is not valid.
  248. */
  249. public static void validateURL(final String name, final String url) throws ConfigurationException
  250. {
  251. if (!uv.isValid(url))
  252. throw new ConfigurationException(name + " (" + url + ") is not a valid URL.");
  253. }
  254. /**
  255. * Checks if the list of URLs is valid.
  256. * @param name Configuration item name.
  257. * @param urls URLs to check.
  258. * @throws ConfigurationException When a URL is not valid.
  259. */
  260. public static void validateURLList(final String name, final List<String> urls) throws ConfigurationException
  261. {
  262. validateNotNull(name, urls);
  263. for (String s : urls)
  264. validateURL(name, s);
  265. }
  266. }