1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
36
37
38
39 private BufferedReader in = null;
40 private String filename;
41 private String lastNetwork = null;
42 private String lastType = null;
43
44
45
46 public WSXFileReader(String filename)
47 {
48 this.filename = filename;
49 }
50
51
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
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