1 /*****************************************************/
\r
2 /* This java file is a part of the */
\r
4 /* - Plouf's Java IRC Client - */
\r
6 /* Copyright (C) 2002 - 2004 Philippe Detournay */
\r
8 /* All contacts : theplouf@yahoo.com */
\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
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
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
28 /*****************************************************/
\r
33 * Combined IRC mode handler.
\r
35 public class MultiModeHandler {
\r
36 private String[] _parameters;
\r
37 private char[][] _modes;
\r
38 private char[] _prefix;
\r
39 private int _paramIndex;
\r
40 private int _codeIndex;
\r
41 private boolean _positive;
\r
43 private String _currentMode;
\r
44 private char _currentModeChar;
\r
45 private boolean _currentHasParameter;
\r
46 private String _currentParameter;
\r
47 private boolean _currentPrefix;
\r
50 * Create a new ModeHandler with empty mode.
\r
55 * A,B,C,D know modes. This is an array of four arrays of chars.
\r
59 public MultiModeHandler(String code, char[][] modes, char prefix[]) {
\r
62 _parameters = (new StringParser()).parseString(code);
\r
69 * Check whether there is more modes to handle.
\r
71 * @return true if there is more modes to handle, false otherwise.
\r
73 public boolean terminated() {
\r
74 return (_codeIndex == _parameters[0].length());
\r
78 * Handle the next mode. This method must be called before first call to any
\r
79 * other getter's method.
\r
81 public void next() {
\r
84 char c = _parameters[0].charAt(_codeIndex++);
\r
99 _currentModeChar = c;
\r
100 if (hasParameter(_positive, c) && _paramIndex < _parameters.length) {
\r
101 _currentHasParameter = true;
\r
102 _currentParameter = _parameters[_paramIndex++];
\r
104 _currentHasParameter = false;
\r
105 _currentParameter = "";
\r
107 _currentPrefix = inside(_prefix, c);
\r
110 private boolean inside(char[] list, char c) {
\r
111 for (int i = 0; i < list.length; i++)
\r
118 * Get current mode to handle.
\r
122 public String getMode() {
\r
123 return _currentMode;
\r
127 * Check whether the current mode has a parameter.
\r
129 * @return true if the mode has a parameter, false otherwise.
\r
131 public boolean hasParameter() {
\r
132 return _currentHasParameter;
\r
136 * Get the current parameter, if defined.
\r
138 * @return the current parameter, if defined.
\r
140 public String getParameter() {
\r
141 return _currentParameter;
\r
145 * Check wether the current mode comes from the prefix list instead of the
\r
148 * @return true if the mode is a prefix mode.
\r
150 public boolean isPrefix() {
\r
151 return _currentPrefix;
\r
155 * Check wether the current mode comes from the A list.
\r
157 * @return true if the mode is a A code.
\r
159 public boolean isModeA() {
\r
160 return inside(_modes[0], _currentModeChar);
\r
163 private boolean hasParameter(boolean positive, char c) {
\r
164 if (inside(_prefix, c))
\r
166 else if (inside(_modes[0], c))
\r
168 else if (inside(_modes[1], c))
\r
170 else if (inside(_modes[2], c))
\r