-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPocketGopher.java
More file actions
787 lines (706 loc) · 22.2 KB
/
Copy pathPocketGopher.java
File metadata and controls
787 lines (706 loc) · 22.2 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
// Pocket Gopher - a basic Gopher client for mobile devices
// 2010-10-31 Felix Pleșoianu <felixp7@yahoo.com>
// Some code and ideas by Nuno J. Silva <gopher://sdf-eu.org/1/users/njsg>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package ro.plesoianu;
import java.util.Vector;
import java.util.Stack;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class PocketGopher extends MIDlet
implements CommandListener, ItemCommandListener {
public final String[] itemTypeLabels = new String[] {
"0 Text file", "1 Directory", "7 Search Query",
"h Web page", "g GIF image", "I Image"
};
private Form top = new Form("Pocket Gopher");
private Command nav_cmd = new Command("Go to...", Command.SCREEN, 10);
private Command home_cmd = new Command("Home", Command.SCREEN, 10);
private Command stop_cmd = new Command("Stop", Command.STOP, 5);
private Command back_cmd = new Command("Back", Command.BACK, 5);
private Command hist_cmd = new Command("History", Command.SCREEN, 10);
private Command exit_cmd = new Command("Exit", Command.EXIT, 10);
private Command go_cmd = new Command("Go", Command.ITEM, 5);
private Command dir_pgup_cmd;
private Command dir_pgdn_cmd;
private ImageItem imgholder = null;
private Ticker loading_notification = new Ticker("Loading...");
private Alert item_fail = new Alert(
"Unsupported item type",
"Pocket Gopher does not handle binary files.",
null, AlertType.INFO);
private Alert net_fail = new Alert(
"Failure",
"Can't fetch the requested item",
null, AlertType.ERROR);
private Form bottom = null;
private Command btm_close_cmd;
private Command txt_pgup_cmd;
private Command txt_pgdn_cmd;
private Form navform = null;
private Command goto_cmd;
private Command nogo_cmd;
private TextField url_fld;
private TextField host_fld;
private TextField port_fld;
private ChoiceGroup type_fld;
private TextField selector_fld;
private StringItem nav_clear_fld;
private Command nav_clear_cmd;
private Form queryform = null;
private Command query_cmd;
private Command noqry_cmd;
private TextField query_fld;
private DirectoryItem queried_item;
private Vector previous_dir = new Vector();
private Vector current_dir = new Vector();
private int dir_page_num = 0;
private int dir_page_size = 25; // Arbitrary value; about half a page.
private int dir_page_count = 0;
private Stack history = new Stack();
private Thread loading = null;
private Vector current_text;
private int txt_page_num = 0;
private int txt_page_size = dir_page_size;
private int txt_page_count = 0;
public PocketGopher() { }
public void startApp() {
top.addCommand(nav_cmd);
top.addCommand(home_cmd);
top.addCommand(back_cmd);
top.addCommand(hist_cmd);
top.addCommand(exit_cmd);
top.setCommandListener(this);
Display.getDisplay(this).setCurrent(top);
goHome(); // Doing it after showing the window, for effect.
history.push(null);
dir_pgup_cmd =
new Command("PgUp", "Page Up", Command.SCREEN, 8);
dir_pgdn_cmd =
new Command("PgDn", "Page Down", Command.SCREEN, 8);
}
public void pauseApp() { }
public void destroyApp(boolean unconditional) {
stopLoading();
}
public void commandAction(Command c, Displayable s) {
if (c == exit_cmd) {
notifyDestroyed();
} else if (c == home_cmd) {
history.push(null);
previous_dir = current_dir;
goHome();
} else if (c == stop_cmd) {
stopLoading();
} else if (c == back_cmd) {
if (history.size() > 1) {
history.pop();
DirectoryItem prev =
(DirectoryItem) history.peek();
current_dir = previous_dir;
previous_dir = null;
if (prev == null) {
goHome();
} else if (current_dir != null) {
setUpDirPagination(current_dir, top);
top.setTitle(
prev.hostname
+ " " + String.valueOf(prev.port)
+ " " + prev.selector);
} else {
history.pop();
loadItem(prev);
}
}
} else if (c == hist_cmd) {
if (bottom == null) initSecondaryView();
bottom.deleteAll();
addDirectoryToForm(history, bottom);
bottom.setTitle("Session history");
Display.getDisplay(this).setCurrent(bottom);
} else if (c == btm_close_cmd) {
Display.getDisplay(this).setCurrent(top);
} else if (c == nav_cmd) {
if (navform == null) initNavForm();
Display.getDisplay(this).setCurrent(navform);
} else if (c == goto_cmd) {
Display.getDisplay(this).setCurrent(top);
char itemType = itemTypeLabels[
type_fld.getSelectedIndex()].charAt(0);
DirectoryItem tmp;
if (url_fld.getString().length() > 0) {
tmp = Data.parseGopherURL(url_fld.getString());
} else {
tmp = new DirectoryItem(
itemType, host_fld.getString(),
selector_fld.getString(),
host_fld.getString(),
Integer.parseInt(port_fld.getString()));
}
loadItem(tmp);
} else if (c == nogo_cmd) {
Display.getDisplay(this).setCurrent(top);
} else if (c == query_cmd) {
Display.getDisplay(this).setCurrent(top);
queried_item.selector += "\t" + query_fld.getString();
loadDirectory(queried_item);
} else if (c == noqry_cmd) {
Display.getDisplay(this).setCurrent(top);
} else if (c == txt_pgdn_cmd) {
if (txt_page_num < txt_page_count) {
bottom.deleteAll();
txt_page_num++;
paginateText(
current_text, bottom, txt_page_num);
}
} else if (c == txt_pgup_cmd) {
if (txt_page_num > 1) {
bottom.deleteAll();
txt_page_num--;
paginateText(
current_text, bottom, txt_page_num);
}
} else if (c == dir_pgdn_cmd) {
if (dir_page_num < dir_page_count) {
top.deleteAll();
dir_page_num++;
paginateDir(current_dir, top, dir_page_num);
}
} else if (c == dir_pgup_cmd) {
if (dir_page_num > 1) {
top.deleteAll();
dir_page_num--;
paginateDir(current_dir, top, dir_page_num);
}
}
}
public void commandAction(Command c, Item i) {
if (c == go_cmd) {
loadItem((DirectoryItem) i);
} else if (c == nav_clear_cmd) {
url_fld.setString("");
host_fld.setString("");
port_fld.setString("70");
type_fld.setSelectedIndex(1, true);
selector_fld.setString("");
}
}
public void loadItem(DirectoryItem di) {
stopLoading();
switch (di.getItemType()) {
case '0': loadTextFile(di); break;
case '1': loadDirectory(di); break;
case '7': loadQuery(di); break;
case 'h': getURL(di.selector); break;
case 'g':
case 'I': loadImage(di); break;
case 'i': break; // Shouldn't get here anyway.
default:
Display.getDisplay(this)
.setCurrent(item_fail);
}
}
public void goHome() {
stopLoading();
current_dir = Data.parseDirectory(Data.slurpInputStream(
this.getClass().getResourceAsStream("/home.txt")));
top.setTitle("Pocket Gopher");
top.deleteAll();
addDirectoryToForm(current_dir, top);
}
public void stopLoading() {
if (loading != null) {
loading.interrupt();
loading = null;
}
top.removeCommand(stop_cmd);
top.setTicker(null);
}
public void getURL(String url) {
try {
platformRequest(url);
} catch (ConnectionNotFoundException e) {
Display.getDisplay(this).setCurrent(net_fail);
}
}
public void show(Displayable d) { // Change screens from other threads.
Display.getDisplay(this).setCurrent(d);
}
public void loadTextFile(final DirectoryItem di) {
if (bottom == null) initSecondaryView();
loading = new Thread(new Runnable() {
public void run() {
top.setTicker(loading_notification);
top.addCommand(stop_cmd);
String tmp = Data.fetchText(
di.hostname, di.port, di.selector);
if (tmp != null) {
current_text = Data.splitString(
tmp.replace('\r', '\n'), '\n');
setUpTextPagination(
current_text, bottom);
bottom.setTitle(
di.hostname
+ " " + String.valueOf(di.port)
+ " " + di.selector);
show(bottom);
} else {
show(net_fail);
}
top.removeCommand(stop_cmd);
top.setTicker(null);
}
});
loading.start();
}
public void loadDirectory(final DirectoryItem di) {
loading = new Thread(new Runnable() {
public void run() {
top.setTicker(loading_notification);
top.addCommand(stop_cmd);
String content = Data.fetchText(
di.hostname, di.port, di.selector);
if (content != null) {
history.push(new DirectoryItem(di));
previous_dir = current_dir;
current_dir =
Data.parseDirectory(content);
setUpDirPagination(current_dir, top);
top.setTitle(di.hostname + " "
+ String.valueOf(di.port) + " "
+ di.selector);
show(top); // Not always redundant.
} else {
show(net_fail);
}
top.removeCommand(stop_cmd);
top.setTicker(null);
}
});
loading.start();
}
public void loadImage(final DirectoryItem di) {
if (bottom == null) initSecondaryView();
loading = new Thread(new Runnable() {
public void run() {
top.setTicker(loading_notification);
Image content = Data.fetchImage(
di.hostname, di.port, di.selector);
if (content != null) {
bottom.deleteAll();
bottom.setTitle(
di.hostname + " "
+ String.valueOf(di.port) + " "
+ di.selector);
if (imgholder == null)
initImageHolder(content);
else
imgholder.setImage(content);
bottom.append(imgholder);
show(bottom);
} else {
show(net_fail);
}
top.setTicker(null);
}
});
loading.start();
}
public void loadQuery(final DirectoryItem di) {
queried_item = new DirectoryItem (di);
if (queryform == null) initQueryForm();
queryform.setTitle(
"Query: " + di.hostname + " "
+ String.valueOf(di.port) + " "
+ di.selector);
Display.getDisplay(this).setCurrent(queryform);
}
public void addDirectoryToForm(final Vector dir, Form f) {
if (dir == null || f == null) return;
for (int i = 0; i < dir.size(); i++) {
addDirItemToForm((DirectoryItem) dir.elementAt(i), f);
}
}
public void addDirItemToForm(DirectoryItem di, Form f) {
if (di == null || f == null) return;
if (di.getItemType() != 'i' && di.getItemType() != '3') {
di.setDefaultCommand(go_cmd);
di.setItemCommandListener(this);
}
f.append(di);
}
public void setUpDirPagination(final Vector dir, Form f) {
dir_page_num = 1;
dir_page_count = Data.numPages(dir.size(), dir_page_size);
f.deleteAll();
paginateDir(dir, f, 1);
if (dir_page_count > 1) {
f.addCommand(dir_pgup_cmd);
f.addCommand(dir_pgdn_cmd);
} else {
f.removeCommand(dir_pgup_cmd);
f.removeCommand(dir_pgdn_cmd);
}
}
public void paginateDir(final Vector dir, Form f, int page) {
if (page < 1)
page = 1;
else if (page > dir_page_count)
page = dir_page_count;
final int start_offset = (page - 1) * dir_page_size;
int end_offset = start_offset + dir_page_size;
if (end_offset > dir.size()) end_offset = dir.size();
String pagecount = "Page " + String.valueOf(page)
+ " of " + String.valueOf(dir_page_count);
f.append(pagecount + "\n\n");
for (int i = start_offset; i < end_offset; i++)
addDirItemToForm((DirectoryItem) dir.elementAt(i), f);
f.append("\n\n" + pagecount);
}
public void setUpTextPagination(final Vector text, Form f) {
txt_page_num = 1;
txt_page_count = Data.numPages(text.size(), txt_page_size);
f.deleteAll();
paginateText(text, f, 1);
if (txt_page_count > 1) {
f.addCommand(txt_pgup_cmd);
f.addCommand(txt_pgdn_cmd);
} else {
f.removeCommand(txt_pgup_cmd);
f.removeCommand(txt_pgdn_cmd);
}
}
public void paginateText(final Vector text, Form f, int page) {
if (page < 1)
page = 1;
else if (page > txt_page_count)
page = txt_page_count;
final int start_offset = (page - 1) * txt_page_size;
int end_offset = start_offset + txt_page_size;
if (end_offset > text.size()) end_offset = text.size();
String pagecount = "Page " + String.valueOf(page)
+ " of " + String.valueOf(txt_page_count);
f.append(pagecount + "\n\n");
for (int i = start_offset; i < end_offset; i++)
f.append((String) text.elementAt(i));
f.append("\n\n" + pagecount);
}
public void initNavForm() {
navform = new Form("Navigate to...");
goto_cmd = new Command("Go there", Command.OK, 10);
nogo_cmd = new Command("Cancel", Command.CANCEL, 10);
url_fld = new TextField("Gopher URL", "", 140, TextField.URL);
host_fld = new TextField("Hostname", "", 140, TextField.URL);
port_fld = new TextField("Port", "70", 6, TextField.NUMERIC);
type_fld = new ChoiceGroup(
"Item type", Choice.POPUP, itemTypeLabels, null);
type_fld.setSelectedIndex(1, true);
selector_fld = new TextField(
"Selector (optional)", "", 140, TextField.URL);
nav_clear_fld = new StringItem(
null, "Clear form", StringItem.BUTTON);
nav_clear_cmd = new Command("Clear form", Command.SCREEN, 10);
nav_clear_fld.setDefaultCommand(nav_clear_cmd);
nav_clear_fld.setItemCommandListener(this);
navform.append(url_fld);
navform.append("Or else");
navform.append(host_fld);
navform.append(port_fld);
navform.append(type_fld);
navform.append(selector_fld);
navform.append(nav_clear_fld);
navform.addCommand(goto_cmd);
navform.addCommand(nogo_cmd);
navform.setCommandListener(this);
}
public void initSecondaryView() {
bottom = new Form("Pocket Gopher");
btm_close_cmd = new Command("Close", Command.BACK, 5);
txt_pgup_cmd =
new Command("PgUp", "Page Up", Command.SCREEN, 10);
txt_pgdn_cmd =
new Command("PgDn", "Page Down", Command.SCREEN, 10);
bottom.addCommand(btm_close_cmd);
bottom.setCommandListener(this);
}
public void initImageHolder(Image img) {
int layout = ImageItem.LAYOUT_CENTER
| ImageItem.LAYOUT_SHRINK
| ImageItem.LAYOUT_VSHRINK;
imgholder = new ImageItem(null, img, layout, "(image)");
}
public void initQueryForm() {
queryform = new Form("Query server");
query_cmd = new Command("Query", Command.OK, 10);
noqry_cmd = new Command("Cancel", Command.CANCEL, 10);
query_fld =
new TextField("Your query", "", 140, TextField.ANY);
queryform.append(query_fld);
queryform.addCommand(query_cmd);
queryform.addCommand(noqry_cmd);
queryform.setCommandListener(this);
}
}
class DirectoryItem extends StringItem {
private final int layout =
Item.LAYOUT_NEWLINE_BEFORE | Item.LAYOUT_NEWLINE_AFTER;
private char itemType;
public String selector;
public String hostname;
public int port;
public DirectoryItem(char itemType) {
super(null, "",
itemType == 'i' ? Item.PLAIN : Item.HYPERLINK);
this.itemType = itemType;
setProperLabel();
this.setLayout(layout);
}
public DirectoryItem(char itemType, String text) {
super(null, text,
itemType == 'i' ? Item.PLAIN : Item.HYPERLINK);
this.itemType = itemType;
setProperLabel();
this.setLayout(layout);
}
public DirectoryItem(char itemType, String text,
String selector, String hostname, int port) {
super(null, text,
itemType == 'i' ? Item.PLAIN : Item.HYPERLINK);
this.itemType = itemType;
setProperLabel();
this.setLayout(layout);
this.selector = selector;
this.hostname = hostname;
this.port = port;
}
public DirectoryItem(DirectoryItem original) {
super(null, original.getText(),
original.getItemType() == 'i' ?
Item.PLAIN : Item.HYPERLINK);
this.itemType = original.getItemType();
setProperLabel();
this.setLayout(layout);
this.selector = original.selector;
this.hostname = original.hostname;
this.port = original.port;
}
public void setProperLabel() {
switch (itemType) {
case '0': setLabel("[TXT]"); break;
case '1': setLabel("[DIR]"); break;
case '3': setLabel("[ERR]"); break;
case '5': setLabel("[ZIP]"); break;
case '7': setLabel("[QRY]"); break;
case '9': setLabel("[BIN]"); break;
case 'g': setLabel("[GIF]"); break;
case 'h': setLabel("[WWW]"); break;
case 'i': setLabel(""); break;
case 'I': setLabel("[IMG]"); break;
default: setLabel("[???]");
}
}
public char getItemType() { return itemType; }
}
class Data {
public static String fetchText(String hostname, int port, String selector) {
final String url = "socket://"
+ hostname + ":" + String.valueOf(Math.abs(port));
String content = null;
SocketConnection sc = null;
InputStream is = null;
OutputStream os = null;
try {
sc = (SocketConnection) Connector.open(
url, Connector.READ_WRITE, true);
is = sc.openInputStream();
os = sc.openOutputStream();
os.write((selector + "\r\n").getBytes());
os.flush();
content = slurpInputStream(is);
} catch (IOException e) {
System.err.println(e.toString());
} catch (SecurityException e) {
System.err.println(e.toString());
} finally {
if (is != null)
try { is.close(); } catch (IOException e) {}
if (os != null)
try { os.close(); } catch (IOException e) {}
if (sc != null)
try { sc.close(); } catch (IOException e) {}
}
return content;
}
public static Image fetchImage(String hostname, int port, String selector) {
final String url = "socket://"
+ hostname + ":" + String.valueOf(Math.abs(port));
Image content = null;
SocketConnection sc = null;
InputStream is = null;
OutputStream os = null;
try {
sc = (SocketConnection) Connector.open(
url, Connector.READ_WRITE, true);
is = sc.openInputStream();
os = sc.openOutputStream();
os.write((selector + "\r\n").getBytes());
os.flush();
content = Image.createImage(is);
} catch (IOException e) {
System.err.println(e.toString());
} catch (SecurityException e) {
System.err.println(e.toString());
} finally {
if (is != null)
try { is.close(); } catch (IOException e) {}
if (os != null)
try { os.close(); } catch (IOException e) {}
if (sc != null)
try { sc.close(); } catch (IOException e) {}
}
return content;
}
public static String slurpInputStream(InputStream is) {
if (is == null) return null;
InputStreamReader isr = new InputStreamReader(is);
final int buffer_size = 1024;
char[] buffer = new char[buffer_size];
StringBuffer output = new StringBuffer(buffer_size);
try {
int bytes_in = 0;
do {
bytes_in = isr.read(buffer);
if (bytes_in > -1)
output.append(buffer, 0, bytes_in);
} while (bytes_in > -1);
} catch (IOException e) {
System.err.println(e.toString());
} catch (OutOfMemoryError e) {
System.err.println(e.toString());
}
return output.toString();
}
public static Vector parseDirectory(String text) {
if (text == null || text.length() == 0) return new Vector();
final int len = text.length();
int mark = 0;
int pos = 0;
text = text.replace('\r', '\n');
Vector output = new Vector();
do {
while (pos < len && text.charAt(pos) != '\n') pos++;
output.addElement(
parseDirectoryItem(
text.substring(mark, pos)));
while (pos < len && text.charAt(pos) == '\n') pos++;
mark = pos;
} while (pos < len);
return output;
}
public static DirectoryItem parseDirectoryItem(String text) {
if (text == null || text.length() == 0) return null;
if (text.equals(".")) return null;
final Vector record = splitString(text, '\t');
final String label = (String) record.elementAt(0);
final char itemType = label.charAt(0);
DirectoryItem item =
new DirectoryItem(itemType, label.substring(1));
if (itemType == 'i' || itemType == '3' || record.size() < 2)
return item;
String selector = (String) record.elementAt(1);
if (itemType == 'h') {
if (selector.startsWith("URL:"))
item.selector = selector.substring(4);
} else {
item.selector = selector;
}
if (record.size() > 2)
item.hostname = (String) record.elementAt(2);
if (record.size() > 3) {
try {
item.port = Integer.parseInt(
(String) record.elementAt(3));
} catch (NumberFormatException e) {
item.port = 70;
}
}
return item;
}
public static Vector splitString(String text, char separator) {
Vector output = new Vector();
if (text == null || text.length() == 0) {
output.addElement("");
return output;
}
final int len = text.length();
int mark = 0;
int pos = 0;
do {
while (pos < len && text.charAt(pos) != separator)
pos++;
output.addElement(text.substring(mark, pos));
mark = ++pos; // Exactly one tab between each field.
} while (pos < len);
return output;
}
public static int numPages(int total_size, int page_size) {
if (total_size % page_size == 0)
return total_size / page_size;
else
return total_size / page_size + 1;
}
// Based on code by Nuno J. Silva <gopher://sdf-eu.org/1/users/njsg>
public static DirectoryItem parseGopherURL(String gopher_url) {
if (gopher_url == null) return null;
String hostname, selector;
int port = 70;
char type = '1';
int position = 0;
if (gopher_url.startsWith("gopher://")) position = 9;
{
int next_slash = gopher_url.indexOf('/', position);
int next_colon = gopher_url.indexOf(':', position);
int end_of_host;
if (next_slash == -1) next_slash = gopher_url.length();
if ((next_colon < next_slash) && (next_colon != -1)) {
// Then we have a port field
end_of_host = next_colon;
port = Integer.parseInt(
gopher_url.substring(
next_colon + 1, next_slash));
} else {
end_of_host = next_slash;
}
hostname = gopher_url.substring(position, end_of_host);
position = next_slash + 1;
}
if (position < gopher_url.length()) {
type = gopher_url.charAt(position);
position += 2; // Skip over type AND subsequent slash.
}
if (position < gopher_url.length())
selector = gopher_url.substring(position);
else
selector = "";
return new DirectoryItem(type, null, selector, hostname, port);
}
}