From 48aee31524307c74cabd33fdc17a1eecd807d97d Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 3 May 2008 17:20:37 +0000 Subject: [PATCH] externalize verifier for variouos stuff git-svn-id: http://trooper/svn/projects/jSite/trunk@783 c3eda9e8-030b-0410-8277-bc7414b0a119 --- src/net/pterodactylus/jsite/core/Verifier.java | 89 ++++++++++++++++++++++ .../pterodactylus/jsite/gui/EditNodeDialog.java | 56 +------------- 2 files changed, 93 insertions(+), 52 deletions(-) create mode 100644 src/net/pterodactylus/jsite/core/Verifier.java diff --git a/src/net/pterodactylus/jsite/core/Verifier.java b/src/net/pterodactylus/jsite/core/Verifier.java new file mode 100644 index 0000000..ec9d348 --- /dev/null +++ b/src/net/pterodactylus/jsite/core/Verifier.java @@ -0,0 +1,89 @@ +/* + * 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 true if the name is okay, false 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 true if the hostname is not empty and can be + * resolved, false 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 + * 0 to 65535. + * + * @param portString + * The port number string + * @return true if the port number is okay, + * false 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; + } + +} diff --git a/src/net/pterodactylus/jsite/gui/EditNodeDialog.java b/src/net/pterodactylus/jsite/gui/EditNodeDialog.java index 260466e..2e49055 100644 --- a/src/net/pterodactylus/jsite/gui/EditNodeDialog.java +++ b/src/net/pterodactylus/jsite/gui/EditNodeDialog.java @@ -25,8 +25,6 @@ import java.awt.GridBagConstraints; 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; @@ -36,6 +34,7 @@ import javax.swing.JPanel; 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; @@ -246,65 +245,18 @@ public class EditNodeDialog extends JDialog implements I18nable { // /** - * Checks the name textfield for valid input. - * - * @return true if the name textfield seem okay, - * false if there is an error - */ - private boolean verifyName() { - return (nameTextField.getText().trim().length() != 0); - } - - /** - * Verifies the hostname textfield by resolving the given name. - * - * @return true if the hostname is not empty and can be - * resolved, false 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 - * 0 to 65535. - * - * @return true if the port number is okay, - * false 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; } -- 2.7.4