-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVoyagers.java
More file actions
486 lines (460 loc) · 14 KB
/
Voyagers.java
File metadata and controls
486 lines (460 loc) · 14 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
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;
import java.io.*;
import javax.swing.JButton;
import javax.swing.JToolBar;
//The main class
/**
*
* @author meemr
*/
public class Voyagers implements ActionListener,Runnable
{
public final int SCROLLBARS_VERTICAL_ONLY = 1;
Thread thread;
String resource, host, file, url, ReloadReaderCount;
String PrevFileContents,NextFileContent, FileGoGetter;
HTTP webconnect;
TextField textfield;
Frame frame;
TextArea textarea;
JButton back,forward,home,stop,reload;
MenuItem open,save,exits;
int Eternal=2;
int TextAreaFileNameContent,SavedFileReaderCount;
int urlLength,slash,count,increment;
//The first function to be called.
//It creates an object that looks like the voyagers class.
public static void main(String s[])
{
Voyagers MyBrowser=new Voyagers();
MyBrowser.Intializer();
}
//This is the initialization method.
//It initializes the various components of the interface.
public void Intializer()
{
File creator=new File("UrlHistory");
creator.mkdir();
File OldFiles=new File("UrlHistory\\");
String ListOfOldFiles[] = OldFiles.list();
int NumberOfOldFiles = ListOfOldFiles.length;
for(int OldFilesCount=0; OldFilesCount< NumberOfOldFiles; OldFilesCount++)
{
File DeleteFiles=new File("UrlHistory\\" + ListOfOldFiles[OldFilesCount]);
DeleteFiles.delete();
}
frame = new Frame("VOYAGER");
frame.setLayout(new BorderLayout());
textfield=new TextField("127.0.0.1",20);
textarea =new TextArea("",10,50,SCROLLBARS_VERTICAL_ONLY);
textarea.setEditable(false);
MenuBar menubar=new MenuBar();
Menu menu = menubar.add(new Menu("File"));
open = (MenuItem)menu.add(new MenuItem("Open"));
open.addActionListener(this);
save = (MenuItem)menu.add(new MenuItem("Save"));
save.addActionListener(this);
exits = (MenuItem)menu.add(new MenuItem("Exit"));
exits.addActionListener(this);
menubar.add(menu);
JToolBar toolbar = new JToolBar();
back = (JButton)toolbar.add(new JButton(" <<= Back "));
back.addActionListener(this);
back.setEnabled(false);
forward = (JButton)toolbar.add(new JButton(" =>> Forward "));
forward.addActionListener(this);
forward.setEnabled(false);
home = (JButton)toolbar.add(new JButton(" <<=>>Home "));
home.addActionListener(this);
stop = (JButton)toolbar.add(new JButton(" << Stop >> "));
stop.addActionListener(this);
stop.setEnabled(false);
reload = (JButton)toolbar.add(new JButton(" <<=>> Reload <<=>>"));
reload.addActionListener(this);
reload.setEnabled(false);
frame.add(toolbar,"North");
frame.add(textfield,"South");
textfield.addActionListener(this);
frame.add(textarea,"Center");
frame.setMenuBar(menubar);
frame.resize(650,450);
frame.show();
HomePage();
}
//This method is self-explanatory.
//It traps all actions performed by the user on the
//various components of the application.
public void actionPerformed(ActionEvent e)
{
//This if condition is satisfied whenever any
//action is performed on the textfield component.
if(e.getSource() == textfield)
{
url = ((TextField)e.getSource()).getText();
urlLength = url.length();
thread=new Thread(this);
thread.start();
}
//This condition is satisfied whenever the
//user clicks on the back button on the user interface
else
if(e.getSource() == back)
{
{
increment =increment - 1;
if(increment == 1 || increment == 0)
back.setEnabled(false);
String TextAreaContent=textarea.getText();
int TextAreaContentLength=TextAreaContent.length();
textarea.replaceText("",0,TextAreaContentLength);
forward.setEnabled(true);
try
{
BufferedReader PrevFile = new BufferedReader(new FileReader("UrlHistory\\one"+increment+".html"));
while((PrevFileContents=PrevFile.readLine())!= null)
{
textarea.append(PrevFileContents);
}
}
catch (Exception ex)
{
}
}
//This condition is satisfied whenever the
//user clicks on the forward button on the user interface
if(e.getSource() == forward)
{
back.setEnabled(true);
String PrevFilesContentsDeleter=textarea.getText();
int PrevFilesContentsDeleterLength=PrevFilesContentsDeleter.length();
textarea.replaceText("",0,PrevFilesContentsDeleterLength);
try
{
increment = increment+1;
BufferedReader NextFile = new BufferedReader(new FileReader("UrlHistory\\one"+increment+".html"));
while((NextFileContent=NextFile.readLine())!= null)
{
textarea.append(NextFileContent);
}
if (increment == count)
forward.setEnabled(false);
}
catch (Exception ex)
{
}
}
//This condition is satisfied whenever the
//user clicks on the home button on the interface
else
if(e.getSource() == home)
{
String ScreenCleaner=textarea.getText();
int ScreenCleanersLength=ScreenCleaner.length();
textarea.replaceText("",0,ScreenCleanersLength);
try
{
int HomePageReaderCount;
String HomeIPAddress="127.0.0.1";
HTTP HomePageProtocol=new HTTP(HomeIPAddress);
String DefaultFile="\\";
String DefaultFileReaderString;
InputStream HomePageReader = HomePageProtocol.get(DefaultFile);
OutputStream HomePageSaver=new FileOutputStream("UrlHistory\\one.html");
while((HomePageReaderCount = HomePageReader.read()) != -1)
{
HomePageSaver.write((char)HomePageReaderCount);
}
BufferedReader HomePageWriter = new BufferedReader(new FileReader("UrlHistory\\one.html"));
while((DefaultFileReaderString=HomePageWriter.readLine())!= null)
{
textarea.append(DefaultFileReaderString);
}
}
catch (Exception s)
{
ExceptionHandler b = new ExceptionHandler();
b.ServerDown();
}
}
//This condition is satisfied whenever the
//user clicks on the stop button of the interface
else
if(e.getSource() == stop)
{
stop.setEnabled(false);
thread.stop();
}
//This condition is satisfied whenever the
//user clicks on the reload button on the interface
else
if(e.getSource() == reload)
{
url = textfield.getText();
urlLength = url.length();
try
{
resource=url.substring(0);
slash=resource.indexOf('/');
file=resource.substring(slash);
host=resource.substring(0,slash);
webconnect=new HTTP(host);
if(webconnect!=null)
{
count = count +1;
increment = count;
forward.setEnabled(false);
InputStream TextAreaFileName = webconnect.get(file);
OutputStream TextAreaFileNameSaver=new FileOutputStream("UrlHistory\\one"+increment+".html");
while((TextAreaFileNameContent= TextAreaFileName.read()) != -1)
{
TextAreaFileNameSaver.write((char)TextAreaFileNameContent);
}
String Blanker=textarea.getText();
int BlankerSize=Blanker.length();
textarea.replaceText("",0,BlankerSize);
BufferedReader ReloadReader = new BufferedReader(new FileReader("UrlHistory\\one"+increment+".html"));
while((ReloadReaderCount=ReloadReader.readLine())!= null)
{
back.setEnabled(true);
textarea.append(ReloadReaderCount);
}
}
}
catch(StringIndexOutOfBoundsException io)
{
ExceptionHandler b = new ExceptionHandler();
b.PutSlash();
}
catch(FileNotFoundException r)
{
}
catch(UnknownHostException h)
{
ExceptionHandler b = new ExceptionHandler();
b.ServerDown();
}
catch(IOException o)
{
ExceptionHandler b = new ExceptionHandler();
b.ServerDown();
}
}
//This condition is satisfied whenever the
//the user selects the menuoption open.
else
if (e.getSource() == open)
{
String Disintegrator=textarea.getText();
String directory;
int DisintegratorLength=Disintegrator.length();
textarea.replaceText("",0,DisintegratorLength);
Frame OpenFrame=new Frame("open");
FileDialog filedialog=new FileDialog(OpenFrame,"parameter",0);
filedialog.show();
directory=filedialog.getDirectory();
FileGoGetter=filedialog.getFile();
try
{
int FileOnDiskSize;
String FileOnDiskReader;
InputStream FileOnDisk=new FileInputStream(directory+FileGoGetter);
OutputStream FileOnDiskSaver=new FileOutputStream("UrlHistory\\parag.html");
while((FileOnDiskSize = FileOnDisk.read()) != -1)
{
FileOnDiskSaver.write((char)FileOnDiskSize);
}
BufferedReader FileOnDiskWriter = new BufferedReader(new FileReader("UrlHistory\\parag.html"));
while((FileOnDiskReader = FileOnDiskWriter.readLine())!= null)
{
textarea.append(FileOnDiskReader);
}
}
catch(IOException io)
{
}
}
//This condition is satisfied whenever the
//the user selects the menuoption save.
else
if (e.getSource() == save)
{
String p=new String();
Frame SaveFrame=new Frame("frames");
FileDialog SaveFileDialog=new FileDialog(SaveFrame,"p",1);
SaveFileDialog.show();
String SavedFileName;
String SavedFileDirectory;
SavedFileDirectory=SaveFileDialog.getDirectory();
SavedFileName=SaveFileDialog.getFile();
try
{
FileInputStream SavedFileReader=new FileInputStream("UrlHistory\\one"+increment+".html");
FileOutputStream SavedFileWriter=new FileOutputStream(SavedFileDirectory+SavedFileName);
while(( SavedFileReaderCount= SavedFileReader.read())!= -1)
{
SavedFileWriter.write((char)SavedFileReaderCount);
}
}
catch(IOException io)
{
}
}
//This condition is satisfied whenever the
//the user selects the menuoption exit.
else
if (e.getSource() == exits)
{
System.exit(0);
}
}
//This method is called whenever the user performs
//any action on the textfield component.This method starts
//by finding out the name of the file to be fetched from the server.
//It then connects to the server and gets the file requested by the user.
public void run()
{
try
{
resource=url.substring(0);
slash=resource.indexOf('/');
file=resource.substring(slash);
host=resource.substring(0,slash);
webconnect=new HTTP(host);
if(webconnect!=null)
{
count = count +1;
increment = count;
InputStream WWWFileReader = webconnect.get(file);
OutputStream WWWFileWriter=new FileOutputStream("UrlHistory\\one"+increment+".html");
while((TextAreaFileNameContent= WWWFileReader.read()) != -1)
{
WWWFileWriter.write((char)TextAreaFileNameContent);
}
BufferedReader WWWScreenWriter = new BufferedReader(new FileReader("UrlHistory\\one"+increment+".html"));
String WWWScreenCleaner=textarea.getText();
int WWWScreenCleanerSize=WWWScreenCleaner.length();
textarea.replaceText("",0,WWWScreenCleanerSize);
while((ReloadReaderCount=WWWScreenWriter.readLine())!= null)
{
forward.setEnabled(false);
stop.setEnabled(true);
reload.setEnabled(true);
textarea.append(ReloadReaderCount);
if(count == 2)
back.setEnabled(true);
}
}
}
catch(IOException io)
{
ExceptionHandler b = new ExceptionHandler();
b.ServerDown();
}
}
//This method is called explicitly to get the homepage
//from the localhost machine.It connects to the server running
//on the same machine as this application and gets it's default
//html file.
public void HomePage()
{
if(Eternal == 2)
{
int OurDefaultFileCount;
try
{
count=1;
increment=count;
String Ourselves="127.0.0.1";
HTTP OurUrl=new HTTP(Ourselves);
String OurDefaultFile="\\";
String DefaultFileStringReader;
InputStream OurDefaultFileReader = OurUrl.get(OurDefaultFile);
OutputStream OurDefaultFileWriter=new FileOutputStream("UrlHistory\\one"+increment+".html");
while((OurDefaultFileCount= OurDefaultFileReader.read()) != -1)
{
OurDefaultFileWriter.write((char)OurDefaultFileCount);
}
BufferedReader DefaultFileScreenWriter = new BufferedReader(new FileReader("UrlHistory\\one"+increment+".html"));
while((DefaultFileStringReader=DefaultFileScreenWriter.readLine())!= null)
{
textarea.append(DefaultFileStringReader);
}
}
catch (Exception e)
{
ExceptionHandler b = new ExceptionHandler();
b.ServerDown();
}
Eternal=3;
}
}
}
//This is the class which has the actual code for getting
//connected to any server.
class HTTP
{
public final static int HTTP_PORT=80;
InetAddress wwwserver;
DataInputStream FinalReader;
PrintStream FinalPrinter;
String response;
//This constructor is called automatically whenever any
//object of the said class is created. This constructor performs a simple
//thing.It gets the name of the server machine from which
//the html file has to be fetched.
public HTTP(String Server) throws UnknownHostException
{
wwwserver=InetAddress.getByName(Server);
}
//This method gets the html file from the server. It creates
//a socket which is then used to get connected to the server
//machine.Once connected,it makes use of the stream classes
//to get the contents of the html file.
public InputStream get(String filename) throws IOException
{
Socket socket;
InputStream InitialReader;
OutputStream InitialWriter;
socket= new Socket(wwwserver,HTTP_PORT);
if(socket==null)
{
return null;
}
InitialReader=socket.getInputStream();
InitialWriter=socket.getOutputStream();
FinalReader=new DataInputStream(InitialReader);
FinalPrinter=new PrintStream(InitialWriter);
if(InitialReader==null || InitialWriter==null)
{
return null;
}
FinalPrinter.println("GET " + filename + " HTTP/1.0\n");
while((response=FinalReader.readLine()).length() > 0)
{
if(response.startsWith("HTTP/1.0 404 Not Found"))
{
ExceptionHandler ErrorFile=new ExceptionHandler();
ErrorFile.FileNotFound();
}
}
return FinalReader;
}
}
//This class declares the functions which implement native code
//These functions are called whenever an exception occurs and a
//MessageBox with an appropriate error message is displayed.
class ExceptionHandler
{
public native void ServerDown();
public native void FileNotFound();
public native void PutSlash();
//This constructor loads the new.dll which contains
//the actual code for the above mentioned functions.
public ExceptionHandler()
{
System.loadLibrary("new");
}
}