]> git.somenet.org - irc/pjirc-ng.git/blob - src/main/java/irc/style/URLRecognizer.java
Pjirc 2.2.1 as available on the net, reformatted and made it compile.
[irc/pjirc-ng.git] / src / main / java / irc / style / URLRecognizer.java
1 /*****************************************************/\r
2 /*          This java file is a part of the          */\r
3 /*                                                   */\r
4 /*           -  Plouf's Java IRC Client  -           */\r
5 /*                                                   */\r
6 /*   Copyright (C)  2002 - 2004 Philippe Detournay   */\r
7 /*                                                   */\r
8 /*         All contacts : theplouf@yahoo.com         */\r
9 /*                                                   */\r
10 /*  PJIRC is free software; you can redistribute     */\r
11 /*  it and/or modify it under the terms of the GNU   */\r
12 /*  General Public License as published by the       */\r
13 /*  Free Software Foundation; version 2 or later of  */\r
14 /*  the License.                                     */\r
15 /*                                                   */\r
16 /*  PJIRC is distributed in the hope that it will    */\r
17 /*  be useful, but WITHOUT ANY WARRANTY; without     */\r
18 /*  even the implied warranty of MERCHANTABILITY or  */\r
19 /*  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   */\r
20 /*  General Public License for more details.         */\r
21 /*                                                   */\r
22 /*  You should have received a copy of the GNU       */\r
23 /*  General Public License along with PJIRC; if      */\r
24 /*  not, write to the Free Software Foundation,      */\r
25 /*  Inc., 59 Temple Place, Suite 330, Boston,        */\r
26 /*  MA  02111-1307  USA                              */\r
27 /*                                                   */\r
28 /*****************************************************/\r
29 \r
30 package irc.style;\r
31 \r
32 /**\r
33  * The URLRecognizer.\r
34  */\r
35 public class URLRecognizer implements WordRecognizer {\r
36         private boolean isAlpha(String s) {\r
37                 s = s.toLowerCase(java.util.Locale.ENGLISH);\r
38                 for (int i = 0; i < s.length(); i++)\r
39                         if ((s.charAt(i) < 'a') || (s.charAt(i) > 'z'))\r
40                                 return false;\r
41                 return true;\r
42         }\r
43 \r
44         @Override\r
45         public boolean recognize(String word) {\r
46                 if (word.startsWith("http://"))\r
47                         return true;\r
48                 if (word.startsWith("ftp://"))\r
49                         return true;\r
50                 if (word.startsWith("www."))\r
51                         return true;\r
52                 if (word.startsWith("ftp."))\r
53                         return true;\r
54                 int a = word.indexOf('.');\r
55                 if (a == -1)\r
56                         return false;\r
57                 int b = word.lastIndexOf('.');\r
58                 if (a == b)\r
59                         return false;\r
60                 String ext = word.substring(b + 1);\r
61                 if (!isAlpha(ext))\r
62                         return false;\r
63                 if ((ext.length() == 2) || (ext.length() == 3))\r
64                         return true;\r
65                 return false;\r
66         }\r
67 \r
68         @Override\r
69         public String getType() {\r
70                 return "url";\r
71         }\r
72 \r
73 }\r