]> git.somenet.org - irc/pjirc-ng.git/blob - src/main/java/irc/StreamParameterProvider.java
Pjirc 2.2.1 as available on the net, reformatted and made it compile.
[irc/pjirc-ng.git] / src / main / java / irc / StreamParameterProvider.java
1 /*****************************************************/
2 /*          This java file is a part of the          */
3 /*                                                   */
4 /*           -  Plouf's Java IRC Client  -           */
5 /*                                                   */
6 /*   Copyright (C)  2002 - 2004 Philippe Detournay   */
7 /*                                                   */
8 /*         All contacts : theplouf@yahoo.com         */
9 /*                                                   */
10 /*  PJIRC is free software; you can redistribute     */
11 /*  it and/or modify it under the terms of the GNU   */
12 /*  General Public License as published by the       */
13 /*  Free Software Foundation; version 2 or later of  */
14 /*  the License.                                     */
15 /*                                                   */
16 /*  PJIRC is distributed in the hope that it will    */
17 /*  be useful, but WITHOUT ANY WARRANTY; without     */
18 /*  even the implied warranty of MERCHANTABILITY or  */
19 /*  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   */
20 /*  General Public License for more details.         */
21 /*                                                   */
22 /*  You should have received a copy of the GNU       */
23 /*  General Public License along with PJIRC; if      */
24 /*  not, write to the Free Software Foundation,      */
25 /*  Inc., 59 Temple Place, Suite 330, Boston,        */
26 /*  MA  02111-1307  USA                              */
27 /*                                                   */
28 /*****************************************************/
29
30 package irc;
31
32 import java.io.BufferedReader;
33 import java.io.InputStream;
34 import java.io.InputStreamReader;
35 import java.util.Hashtable;
36
37 /**
38  * Parameter provider from a text stream.
39  */
40 public class StreamParameterProvider implements ParameterProvider {
41         private Hashtable _table;
42
43         /**
44          * Create a new StreamParameterProvider loading the given input stream.
45          * 
46          * @param is
47          *          the input stream to load from. If is is null, then the parameter
48          *          list will be empty.
49          */
50         public StreamParameterProvider(InputStream is) {
51                 _table = new Hashtable();
52                 if (is == null)
53                         return;
54
55                 try {
56                         BufferedReader reader = new BufferedReader(new InputStreamReader(is));
57                         String line = reader.readLine();
58                         while (line != null) {
59                                 line = line.trim();
60                                 if (line.length() > 0) {
61                                         if (line.charAt(0) != '#') {
62                                                 parse(line);
63                                         }
64                                 }
65                                 line = reader.readLine();
66                         }
67                         reader.close();
68                 } catch (Exception ex) {
69                         ex.printStackTrace();
70                 }
71         }
72
73         private void parse(String str) {
74                 int pos = str.indexOf('=');
75                 if (pos < 0)
76                         return;
77                 String before = str.substring(0, pos).trim().toLowerCase(java.util.Locale.ENGLISH);
78                 String after = str.substring(pos + 1).trim();
79                 _table.put(before, after);
80         }
81
82         @Override
83         public String getParameter(String name) {
84                 return (String) _table.get(name.toLowerCase(java.util.Locale.ENGLISH));
85         }
86 }