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