whitespace fixups
[jSite2.git] / src / net / pterodactylus / jsite / core / Verifier.java
1 /*
2  * jSite2 - Verifier.java -
3  * Copyright © 2008 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package net.pterodactylus.jsite.core;
21
22 import java.net.InetAddress;
23 import java.net.UnknownHostException;
24
25 /**
26  * Contains verifier for various settings.
27  *
28  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
29  */
30 public class Verifier {
31
32         /**
33          * Checks whether the given name is a valid node name.
34          *
35          * @param name
36          *            The name to verify
37          * @return <code>true</code> if the name is okay, <code>false</code> if
38          *         there is an error
39          */
40         public static boolean verifyNodeName(String name) {
41                 return (name != null) && (name.trim().length() != 0);
42         }
43
44         /**
45          * Verifies the given hostname by trying to resolve it.
46          *
47          * @param hostname
48          *            The hostname to verify
49          * @return <code>true</code> if the hostname is not empty and can be
50          *         resolved, <code>false</code> otherwise
51          */
52         public static boolean verifyHostname(String hostname) {
53                 if ((hostname == null) || (hostname.trim().length() == 0)) {
54                         return false;
55                 }
56                 try {
57                         InetAddress.getByName(hostname.trim());
58                         return true;
59                 } catch (UnknownHostException uhe1) {
60                         return false;
61                 }
62         }
63
64         /**
65          * Verifies that the port number is numeric and in the range from
66          * <code>0</code> to <code>65535</code>.
67          *
68          * @param portString
69          *            The port number string
70          * @return <code>true</code> if the port number is okay,
71          *         <code>false</code> otherwise
72          */
73         public static boolean verifyPort(String portString) {
74                 if ((portString == null) || (portString.trim().length() == 0)) {
75                         return false;
76                 }
77                 try {
78                         int portNumber = Integer.valueOf(portString.trim());
79                         if ((portNumber > 0) && (portNumber < 65536)) {
80                                 return true;
81                         }
82                 } catch (NumberFormatException nfe1) {
83                         /* ignore. */
84                 }
85                 return false;
86         }
87
88 }