--- /dev/null
+/*
+ * jSite2 - Verifier.java -
+ * Copyright © 2008 David Roden
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+package net.pterodactylus.jsite.core;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+/**
+ * Contains verifier for various settings.
+ *
+ * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
+ * @version $Id$
+ */
+public class Verifier {
+
+ /**
+ * Checks whether the given name is a valid node name.
+ *
+ * @param name
+ * The name to verify
+ * @return <code>true</code> if the name is okay, <code>false</code> if
+ * there is an error
+ */
+ public static boolean verifyNodeName(String name) {
+ return (name != null) && (name.trim().length() != 0);
+ }
+
+ /**
+ * Verifies the given hostname by trying to resolve it.
+ *
+ * @param hostname
+ * The hostname to verify
+ * @return <code>true</code> if the hostname is not empty and can be
+ * resolved, <code>false</code> otherwise
+ */
+ public static boolean verifyHostname(String hostname) {
+ if ((hostname == null) || (hostname.trim().length() == 0)) {
+ return false;
+ }
+ try {
+ InetAddress.getByName(hostname.trim());
+ return true;
+ } catch (UnknownHostException uhe1) {
+ return false;
+ }
+ }
+
+ /**
+ * Verifies that the port number is numeric and in the range from
+ * <code>0</code> to <code>65535</code>.
+ *
+ * @param portString
+ * The port number string
+ * @return <code>true</code> if the port number is okay,
+ * <code>false</code> otherwise
+ */
+ public static boolean verifyPort(String portString) {
+ if ((portString == null) || (portString.trim().length() == 0)) {
+ return false;
+ }
+ try {
+ int portNumber = Integer.valueOf(portString.trim());
+ if ((portNumber > 0) && (portNumber < 65536)) {
+ return true;
+ }
+ } catch (NumberFormatException nfe1) {
+ /* ignore. */
+ }
+ return false;
+ }
+
+}
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
-import java.net.InetAddress;
-import java.net.UnknownHostException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
+import net.pterodactylus.jsite.core.Verifier;
import net.pterodactylus.jsite.i18n.I18n;
import net.pterodactylus.jsite.i18n.I18nable;
import net.pterodactylus.jsite.i18n.gui.I18nAction;
//
/**
- * Checks the name textfield for valid input.
- *
- * @return <code>true</code> if the name textfield seem okay,
- * <code>false</code> if there is an error
- */
- private boolean verifyName() {
- return (nameTextField.getText().trim().length() != 0);
- }
-
- /**
- * Verifies the hostname textfield by resolving the given name.
- *
- * @return <code>true</code> if the hostname is not empty and can be
- * resolved, <code>false</code> otherwise
- */
- private boolean verifyHostname() {
- if (hostnameTextField.getText().trim().length() == 0) {
- return false;
- }
- try {
- InetAddress.getByName(hostnameTextField.getText().trim());
- return true;
- } catch (UnknownHostException uhe1) {
- return false;
- }
- }
-
- /**
- * Verifies that the port number is numeric and in the range from
- * <code>0</code> to <code>65535</code>.
- *
- * @return <code>true</code> if the port number is okay,
- * <code>false</code> otherwise
- */
- private boolean verifyPort() {
- try {
- int portNumber = Integer.valueOf(portTextField.getText().trim());
- if ((portNumber > 0) && (portNumber < 65536)) {
- return true;
- }
- } catch (NumberFormatException nfe1) {
- /* ignore. */
- }
- return false;
- }
-
- /**
* Confirms the node settings and closes the dialog.
*/
private void confirm() {
- if (!verifyName()) {
+ if (!Verifier.verifyNodeName(nameTextField.getText())) {
JOptionPane.showMessageDialog(this, I18n.get("editNodeDialog.error.name.message"), I18n.get("editNodeDialog.error.name.title"), JOptionPane.ERROR_MESSAGE);
return;
}
- if (!verifyHostname()) {
+ if (!Verifier.verifyHostname(hostnameTextField.getText())) {
JOptionPane.showMessageDialog(this, I18n.get("editNodeDialog.error.hostname.message"), I18n.get("editNodeDialog.error.hostname.title"), JOptionPane.ERROR_MESSAGE);
return;
}
- if (!verifyPort()) {
+ if (!Verifier.verifyPort(portTextField.getText())) {
JOptionPane.showMessageDialog(this, I18n.get("editNodeDialog.error.port.message"), I18n.get("editNodeDialog.error.port.title"), JOptionPane.ERROR_MESSAGE);
return;
}