]> git.somenet.org - irc/pjirc-ng.git/blob - src/main/java/irc/ListHandler.java
Pjirc 2.2.1 as available on the net, reformatted and made it compile.
[irc/pjirc-ng.git] / src / main / java / irc / ListHandler.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;\r
31 \r
32 import java.util.Hashtable;\r
33 import java.util.StringTokenizer;\r
34 \r
35 /**\r
36  * Handling for unbound list of items.\r
37  */\r
38 public class ListHandler {\r
39         private boolean _baseAll;\r
40         private Hashtable _list;\r
41         private String _orig;\r
42 \r
43         /**\r
44          * Create a new list of items.\r
45          * \r
46          * @param list\r
47          *          item list. Syntax is : "" or "none+item1+item2+item3+..." or\r
48          *          "all-item1-item2-item3-..." If list is empty, it is equivalent to\r
49          *          "all". Items containing + or - characters can be used by replacing\r
50          *          them by \+ or \-. The \ character may be entenred using \\ syntax.\r
51          */\r
52         public ListHandler(String list) {\r
53                 _orig = list;\r
54                 list = convert(list);\r
55                 _baseAll = true;\r
56                 _list = new Hashtable();\r
57                 StringTokenizer st = new StringTokenizer(list, "\1\2", true);\r
58                 if (!st.hasMoreTokens())\r
59                         return;\r
60                 String base = st.nextToken();\r
61                 if (base.equals("none"))\r
62                         _baseAll = false;\r
63                 while (st.hasMoreTokens()) {\r
64                         String mod = st.nextToken();\r
65                         if (mod.equals("\1"))\r
66                                 mod = "-";\r
67                         else if (mod.equals("\2"))\r
68                                 mod = "+";\r
69                         if (!st.hasMoreTokens())\r
70                                 break;\r
71                         String token = st.nextToken().toLowerCase(java.util.Locale.ENGLISH);\r
72                         _list.put(token, mod);\r
73                 }\r
74         }\r
75 \r
76         private String convert(String txt) {\r
77                 String ans = "";\r
78                 for (int i = 0; i < txt.length(); i++) {\r
79                         char c = txt.charAt(i);\r
80                         if (c == '-')\r
81                                 c = '\1';\r
82                         else if (c == '+')\r
83                                 c = '\2';\r
84                         else if (c == '\\' && (i != txt.length() - 1)) {\r
85                                 i++;\r
86                                 c = txt.charAt(i);\r
87                         }\r
88                         ans += c;\r
89                 }\r
90                 return ans;\r
91         }\r
92 \r
93         /**\r
94          * Test wether the given item is in the list.\r
95          * \r
96          * @param item\r
97          *          item to check.\r
98          * @return true if item is in the list, false otherwise.\r
99          */\r
100         public boolean inList(String item) {\r
101                 item = item.toLowerCase(java.util.Locale.ENGLISH);\r
102                 String mod = (String) _list.get(item);\r
103                 if (mod == null)\r
104                         return _baseAll;\r
105                 if (mod.equals("+"))\r
106                         return true;\r
107                 return false;\r
108         }\r
109 \r
110         @Override\r
111         public String toString() {\r
112                 return _orig;\r
113         }\r
114 }\r