Преглед изворни кода

Add servername validator

CLIENT-164

Change-Id: If8fa77b51d50a0355ac3297aa0ef884908f05f8c
Reviewed-on: http://gerrit.dmdirc.com/1773
Automatic-Compile: DMDirc Local Commits <dmdirc@googlemail.com>
Reviewed-by: Chris Smith <chris@dmdirc.com>
tags/0.6.5^0
Greg Holmes пре 13 година
родитељ
комит
941b88b7c1

+ 51
- 0
src/com/dmdirc/util/validators/ServerNameValidator.java Прегледај датотеку

@@ -0,0 +1,51 @@
1
+/*
2
+ * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.util.validators;
24
+
25
+import java.net.URI;
26
+import java.net.URISyntaxException;
27
+
28
+/**
29
+ * Validates a server name, prepending an irc protocol if no protocol is
30
+ * specified.
31
+ */
32
+public class ServerNameValidator implements Validator<String> {
33
+
34
+    /** {@inheritDoc} */
35
+    @Override
36
+    public ValidationResponse validate(final String object) {
37
+        try {
38
+            URI uri = new URI(object);
39
+            if (uri.getHost() == null || uri.getHost().isEmpty()) {
40
+                uri = new URI("irc://" + object);
41
+            }
42
+            if (uri.getHost() == null || uri.getHost().isEmpty()) {
43
+                return new ValidationResponse("Address requires a hostname.");
44
+            } else {
45
+                return new ValidationResponse();
46
+            }
47
+        } catch (URISyntaxException ex) {
48
+            return new ValidationResponse(ex.getReason());
49
+        }
50
+    }
51
+}

+ 0
- 1
test/com/dmdirc/util/validators/PermissiveValidatorTest.java Прегледај датотеку

@@ -21,7 +21,6 @@
21 21
  */
22 22
 package com.dmdirc.util.validators;
23 23
 
24
-import com.dmdirc.util.validators.PermissiveValidator;
25 24
 import org.junit.Test;
26 25
 import static org.junit.Assert.*;
27 26
 

Loading…
Откажи
Сачувај