View Javadoc

1   /*
2    * 04/29/2003
3    * 
4    * This file is now part of XNap, but is based on:
5    *
6    * WSXServerlistReader.java
7    * Copyright (C) 2003 Frederik Zimmer
8    * tristian@users.sourceforge.net
9    * http://sourceforge.net/projects/ziga/
10   *
11   * This program is free software; you can redistribute it and/or
12   * modify it under the terms of the GNU General Public License
13   * as published by the Free Software Foundation; either version 2
14   * of the License, or any later version.
15   *
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   *
21   * You should have received a copy of the GNU General Public License
22   * along with this program; if not, write to the Free Software
23   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24   */
25  package org.xnap.plugin.opennap.net;
26  
27  import java.io.BufferedReader;
28  import java.io.FileReader;
29  import java.io.IOException;
30  
31  import org.xnap.util.PortRange;
32  
33  public class WSXFileReader implements OpenNapServerReader, WSXFile {
34  
35  	//--- Constant(s) ---
36  
37  	//--- Data Field(s) ---
38  
39      private BufferedReader in = null;
40      private String filename;
41  	private String lastNetwork = null;
42  	private String lastType = null;
43  
44      //--- Constructor(s) ---
45  	
46      public WSXFileReader(String filename)
47      {
48  		this.filename = filename;
49      }
50  
51      //--- Method(s) ---
52  
53      public void close()
54      {
55  		try {
56  			if (in != null) {
57  				in.close();
58  			}
59  		}	    
60  		catch (IOException e) {
61  		}
62      }
63  
64      public void open() throws IOException
65      {
66  		in = new BufferedReader(new FileReader(filename));
67      }
68  
69      public OpenNapServer read() throws IOException 
70      {
71  		String address = null;
72  		int port = -1;
73  		String username = null;
74  		String password = null;
75  
76  		String line;		
77  		while ((line = in.readLine()) != null) {
78  			if (line.length() == 0 || line.charAt(0) == '#') {
79  				continue;
80  			}
81  
82  			int i = line.indexOf(':');
83  			if (i > 0) {
84  				String key = line.substring(0, i);
85  				String value = line.substring(i + 1).trim();
86  				if (key.equals(NETWORK)) {
87  					if (!value.equalsIgnoreCase(NOT_AVAILABLE)
88  						&& value.length() > 0) {
89  						lastNetwork = value;
90  					}
91  				} 
92  				else if (key.equals(PORT)) {
93  					try {
94  						port = Integer.parseInt(value);
95  					} 
96  					catch (NumberFormatException e) {
97  					}
98  				} 
99  				else if (key.equals(TYPE)) {
100 					lastType = value;
101 				} 
102 				else if (key.equals(USERNAME)) {
103 					username = value;
104 				}
105 				else if (key.equals(USER_PASSWORD)) {
106 					password = value;
107 				}
108 				else if (key.equals(ADDRESS)) {
109 					if (value.length() > 0) {
110 						address = value;
111 					}
112 
113 					if (port == -1) {
114 						int index = address.lastIndexOf(':');
115 						if (index != -1) {
116 							try {
117 								port = Integer.parseInt
118 									(address.substring(index + 1));
119 								address = address.substring(0, index);
120 							} 
121 							catch (NumberFormatException e) {
122 							}
123 						}
124 					}
125 					if (port == -1) {
126 						port = (lastType != null && lastType.equals(TYPE_META)) 
127 							? OpenNapServer.DEFAULT_META_PORT
128 							: OpenNapServer.DEFAULT_PORT;
129 					}
130 
131 					if (address != null && port >= PortRange.MIN_PORT 
132 						&& port <= PortRange.MAX_PORT) {
133 						OpenNapServer server = new OpenNapServer(address, port);
134 			
135 						if (lastNetwork != null) {
136 							server.setNetworkName(lastNetwork);
137 						}
138 		
139 						if (lastType != null && lastType.equals(TYPE_META)) {
140 							server.setRedirector(true);
141 						}
142 
143 						return server;
144 					}
145 					else {
146 						// reinitialize values
147 						address = null;
148 						port = -1;
149 						username = null;
150 						password = null;
151 					}
152 				} 
153 			}
154 		}
155 
156 		return null;
157 	}
158 
159 }
160 
161 
162 
163 
164