View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.plugin.opennap.net;
21  
22  import java.io.BufferedReader;
23  import java.io.FileReader;
24  import java.io.IOException;
25  
26  import org.xnap.util.PortRange;
27  import org.xnap.util.QuotedStringTokenizer;
28  
29  /***
30   * Creates {@link OpenNapServer} objects from a plain text file.
31   */
32  public class OpenNapServerFileReader implements OpenNapServerReader {
33  
34      //--- Data Field(s) ---
35  
36      private BufferedReader in = null;
37      private String filename;
38  
39      //--- Constructor(s) ---
40  	
41      public OpenNapServerFileReader(String filename)
42      {
43  		this.filename = filename;
44      }
45  
46      //--- Method(s) ---
47  
48      public void close()
49      {
50  		try {
51  			if (in != null) {
52  				in.close();
53  			}
54  		}	    
55  		catch (IOException e) {
56  		}
57      }
58  
59      public void open() throws IOException
60      {
61  		in = new BufferedReader(new FileReader(filename));
62      }
63  
64      public OpenNapServer read() throws IOException 
65      {
66  		String line;
67  
68  		while ((line = in.readLine()) != null) {
69  			try {
70  				QuotedStringTokenizer t
71  					= new QuotedStringTokenizer(line, " :");
72  		
73  				if (t.countTokens() < 2) {
74  					continue;
75  				}
76  		    
77  				String ip = t.nextToken();
78  				int port = Integer.parseInt(t.nextToken());
79  				if (port < PortRange.MIN_PORT || port > PortRange.MAX_PORT) {
80  					throw new NumberFormatException();
81  				}
82  				String network = (t.countTokens() > 0) ? t.nextToken() : "";
83  
84  				OpenNapServer s = new OpenNapServer(ip, port, network);
85  
86  				if (t.countTokens() >= 3) {
87  					String token = t.nextToken();
88  					s.setNick((token.length() > 0) ? token : null);
89  					token = t.nextToken();
90  					s.setPassword((token.length() > 0) ? token : null);
91  					token = t.nextToken();
92  					s.setEmail((token.length() > 0) ? token : null);
93  				}
94  					
95  				if (t.countTokens() >= 1) {
96  					s.setRedirector(t.nextToken().equals("true"));
97  				}
98  					
99  				if (t.countTokens() >= 1) {
100 					s.setAutoJoinChannels(t.nextToken());
101 				}
102 
103 				if (t.countTokens() >= 1) {
104 					s.setLastConnect(Long.parseLong(t.nextToken()));
105 				}
106 				
107 				if (t.countTokens() >= 1) {
108 					s.setAutoConnect(t.nextToken().equals("true"));
109 					
110 				}
111 				
112 				return s;
113 			} 
114 			catch (NumberFormatException e) {
115 			}
116 			catch (IllegalArgumentException e) {
117 			}
118 		}
119 
120         return null;
121     }
122 
123 }