1 |
/* |
/* |
2 |
* $Id$ |
* DirectoryListing.java |
3 |
* Copyright (C) 2003 The Free Software Foundation |
* Copyright (C) 2003 The Free Software Foundation |
4 |
* |
* |
5 |
* This file is part of GNU inetlib, a library. |
* This file is part of GNU inetlib, a library. |
50 |
* A gopher directory listing. |
* A gopher directory listing. |
51 |
* |
* |
52 |
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> |
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> |
|
* @version $Revision$ $Date$ |
|
53 |
*/ |
*/ |
54 |
public final class DirectoryListing implements Iterator |
public final class DirectoryListing |
55 |
|
implements Iterator |
56 |
{ |
{ |
57 |
|
|
58 |
private static final String DOT = "."; |
private static final String DOT = "."; |
61 |
private boolean doneRead = false; |
private boolean doneRead = false; |
62 |
private DirectoryEntry current; |
private DirectoryEntry current; |
63 |
|
|
64 |
DirectoryListing (InputStream in) |
DirectoryListing(InputStream in) |
65 |
{ |
{ |
66 |
this.in = new LineInputStream (in); |
this.in = new LineInputStream(in); |
67 |
} |
} |
68 |
|
|
69 |
/** |
/** |
70 |
* Indicates whether this listing contains more entries. |
* Indicates whether this listing contains more entries. |
71 |
*/ |
*/ |
72 |
public boolean hasNext () |
public boolean hasNext() |
73 |
{ |
{ |
74 |
try |
try |
75 |
{ |
{ |
76 |
fetch (); |
fetch(); |
77 |
} |
} |
78 |
catch (IOException e) |
catch (IOException e) |
79 |
{ |
{ |
80 |
return false; |
return false; |
81 |
} |
} |
82 |
return (current != null); |
return (current != null); |
83 |
} |
} |
84 |
|
|
85 |
/** |
/** |
86 |
* @see #nextEntry |
* @see #nextEntry |
87 |
*/ |
*/ |
88 |
public Object next () |
public Object next() |
89 |
{ |
{ |
90 |
try |
try |
91 |
{ |
{ |
92 |
return nextEntry (); |
return nextEntry(); |
93 |
} |
} |
94 |
catch (IOException e) |
catch (IOException e) |
95 |
{ |
{ |
96 |
throw new NoSuchElementException ("I/O error: " + e.getMessage ()); |
throw new NoSuchElementException("I/O error: " + e.getMessage()); |
97 |
} |
} |
98 |
} |
} |
99 |
|
|
100 |
/** |
/** |
101 |
* This iterator is read-only. |
* This iterator is read-only. |
102 |
*/ |
*/ |
103 |
public void remove () |
public void remove() |
104 |
{ |
{ |
105 |
throw new UnsupportedOperationException (); |
throw new UnsupportedOperationException(); |
106 |
} |
} |
107 |
|
|
108 |
/** |
/** |
109 |
* Returns the next entry in the directory listing. |
* Returns the next entry in the directory listing. |
110 |
*/ |
*/ |
111 |
public DirectoryEntry nextEntry () throws IOException |
public DirectoryEntry nextEntry() |
112 |
{ |
throws IOException |
113 |
fetch (); |
{ |
114 |
if (current == null) |
fetch(); |
115 |
{ |
if (current == null) |
116 |
throw new NoSuchElementException (); |
{ |
117 |
} |
throw new NoSuchElementException(); |
118 |
doneRead = false; |
} |
119 |
return current; |
doneRead = false; |
120 |
} |
return current; |
121 |
|
} |
122 |
void fetch () throws IOException |
|
123 |
{ |
void fetch() |
124 |
if (doneRead) |
throws IOException |
125 |
{ |
{ |
126 |
return; |
if (doneRead) |
127 |
} |
{ |
128 |
String line = in.readLine (); |
return; |
129 |
if (DOT.equals (line)) |
} |
130 |
{ |
String line = in.readLine(); |
131 |
current = null; |
if (DOT.equals(line)) |
132 |
} |
{ |
133 |
else |
current = null; |
134 |
{ |
} |
135 |
// Parse line |
else |
136 |
int type = DirectoryEntry.ERROR; |
{ |
137 |
switch (line.charAt (0)) |
// Parse line |
138 |
{ |
int type = DirectoryEntry.ERROR; |
139 |
case '0': |
switch(line.charAt(0)) |
140 |
type = DirectoryEntry.FILE; |
{ |
141 |
break; |
case '0': |
142 |
case '1': |
type = DirectoryEntry.FILE; |
143 |
type = DirectoryEntry.DIRECTORY; |
break; |
144 |
break; |
case '1': |
145 |
case '2': |
type = DirectoryEntry.DIRECTORY; |
146 |
type = DirectoryEntry.CSO_PHONE_BOOK; |
break; |
147 |
break; |
case '2': |
148 |
case '3': |
type = DirectoryEntry.CSO_PHONE_BOOK; |
149 |
type = DirectoryEntry.ERROR; |
break; |
150 |
break; |
case '3': |
151 |
case '4': |
type = DirectoryEntry.ERROR; |
152 |
type = DirectoryEntry.BINHEX; |
break; |
153 |
break; |
case '4': |
154 |
case '5': |
type = DirectoryEntry.BINHEX; |
155 |
type = DirectoryEntry.DOS_ARCHIVE; |
break; |
156 |
break; |
case '5': |
157 |
case '6': |
type = DirectoryEntry.DOS_ARCHIVE; |
158 |
type = DirectoryEntry.UUENCODED; |
break; |
159 |
break; |
case '6': |
160 |
case '7': |
type = DirectoryEntry.UUENCODED; |
161 |
type = DirectoryEntry.INDEX_SEARCH; |
break; |
162 |
break; |
case '7': |
163 |
case '8': |
type = DirectoryEntry.INDEX_SEARCH; |
164 |
type = DirectoryEntry.TELNET; |
break; |
165 |
break; |
case '8': |
166 |
case '9': |
type = DirectoryEntry.TELNET; |
167 |
type = DirectoryEntry.BINARY; |
break; |
168 |
break; |
case '9': |
169 |
case '+': |
type = DirectoryEntry.BINARY; |
170 |
type = DirectoryEntry.REDUNDANT; |
break; |
171 |
break; |
case '+': |
172 |
case 'T': |
type = DirectoryEntry.REDUNDANT; |
173 |
type = DirectoryEntry.TN3270; |
break; |
174 |
break; |
case 'T': |
175 |
case 'g': |
type = DirectoryEntry.TN3270; |
176 |
type = DirectoryEntry.GIF; |
break; |
177 |
break; |
case 'g': |
178 |
case 'I': |
type = DirectoryEntry.GIF; |
179 |
type = DirectoryEntry.IMAGE; |
break; |
180 |
break; |
case 'I': |
181 |
} |
type = DirectoryEntry.IMAGE; |
182 |
int start = 1; |
break; |
183 |
int end = line.indexOf ('\t', start); |
} |
184 |
if (end == -1) |
int start = 1; |
185 |
{ |
int end = line.indexOf('\t', start); |
186 |
throw new ProtocolException ("Invalid directory entry: " + line); |
if (end == -1) |
187 |
} |
{ |
188 |
String title = line.substring (start, end); |
throw new ProtocolException("Invalid directory entry: " + line); |
189 |
start = end + 1; |
} |
190 |
end = line.indexOf ('\t', start); |
String title = line.substring(start, end); |
191 |
if (end == -1) |
start = end + 1; |
192 |
{ |
end = line.indexOf('\t', start); |
193 |
throw new ProtocolException ("Invalid directory entry: " + line); |
if (end == -1) |
194 |
} |
{ |
195 |
String selector = line.substring (start, end); |
throw new ProtocolException("Invalid directory entry: " + line); |
196 |
start = end + 1; |
} |
197 |
end = line.indexOf ('\t', start); |
String selector = line.substring(start, end); |
198 |
if (end == -1) |
start = end + 1; |
199 |
{ |
end = line.indexOf('\t', start); |
200 |
throw new ProtocolException ("Invalid directory entry: " + line); |
if (end == -1) |
201 |
} |
{ |
202 |
String hostname = line.substring (start, end); |
throw new ProtocolException("Invalid directory entry: " + line); |
203 |
start = end + 1; |
} |
204 |
int port = Integer.parseInt (line.substring (start)); |
String hostname = line.substring(start, end); |
205 |
current = new DirectoryEntry (type, title, selector, hostname, port); |
start = end + 1; |
206 |
} |
int port = Integer.parseInt(line.substring(start)); |
207 |
} |
current = new DirectoryEntry(type, title, selector, hostname, port); |
208 |
|
} |
209 |
|
} |
210 |
|
|
211 |
} |
} |
212 |
|
|