summaryrefslogtreecommitdiff
path: root/java/ImageProcessing/framework/ListFiles.java
blob: 7abe067341c61023e572f7c8a03494928227cd54 (plain)
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
package imaging.framework;

import java.net.*;
import java.io.*;
import java.applet.*;

public class ListFiles
{
  public ListFiles ()
  {
  }

  public ListFiles (FileBrowser browser, Applet parent)
  {
    this.browser_ = browser;
    try
      {
	this.fileIcon_ = new Icon (parent.getCodeBase () + 
				   "../ImageProcessing/framework/" +
				   "doc01.gif", 
				   parent);
	this.dirIcon_ = new Icon (parent.getCodeBase () + 
				  "../ImageProcessing/framework/" +
				  "file03.gif", 
				  parent);
      }
    catch (MalformedURLException e)
      {
      }
  }

  public String stripProtocolHeader (String url)
  {
    if (url.startsWith ("http://"))
      {
	return url.substring (7);
      }
    else
      return url;
  }
    
  public int listFiles (String url, FileNode fileNode)
  {
    String s = this.stripProtocolHeader (url);
    String hostname = s;
    String directory = null;
    int index = -1;

    if ((index = s.indexOf ("/")) != -1)
      {
	hostname = s.substring (0, index);
	directory = s.substring (index);
      }
    return this.listFiles (hostname, directory, fileNode);
  }

  public int listFiles (String url, String directory, FileNode fileNode)
  {
    boolean validDirectory = false;
    int count = 0;
    String hostname = this.stripProtocolHeader (url);
    this.url_ = url;
    this.directory_ = directory;
    try
      {
	Socket sock = new Socket (hostname, 80);
	PrintStream out = new PrintStream (sock.getOutputStream ());
	DataInputStream in = new DataInputStream (sock.getInputStream ());
	System.out.println ("Connected to: " + hostname);

	String request = null;
	if (directory.endsWith ("/"))
	  request = "GET " + directory + "\n\n";
	else
	  request = "GET " + directory + "/\n\n";
    
	System.out.println ("Sending request: " + request);

	// Send the request
	out.println (request);
	
	String reply = null;
	// Receive the reply

	// Read all the data in a loop. Search for "Parent Directory"
	// to verify that this indeed is a directory. If we encounter
	// the string "<HTML>" then assume that this is an HTML page
	// and therefore the directory contained "index.html"
	while ((reply = in.readLine ()) != null)
	  {
	    if (validDirectory)
	      this.parse (reply, fileNode);
	    else 
	      {
		// Take a guess at the type of data we get back
		if (reply.indexOf ("Parent Directory") != -1)
		  validDirectory = true;
		else if ((reply.toUpperCase ().indexOf ("<HTML>") != -1) ||
			 (reply.toUpperCase ().indexOf ("<P>") != -1) ||
			 (reply.toUpperCase ().indexOf ("<TABLE") != -1))
		  return 0;
	      }
	  }
      }
    catch (MalformedURLException e)
      {
	System.err.println (e);
      }
    catch (IOException e)
      {
	System.err.println (e);
      }
    if (validDirectory == false)
      return -1;
    return 1;
  }

  private int parse (String s, FileNode fileNode)
  {
    int i= -1;
    int j = -1;
    int startIndex = -1;
    int endIndex = -1;
    boolean isFile = true;
    String name = null;

    if ((i = s.indexOf ("HREF=")) != -1)
      startIndex = i + 6;
    else
      return -1;

    if ((j = s.indexOf (">", i)) != -1)
      endIndex = j - 1;
    else
      return -1;

    // Check if this is a directory
    if (s.charAt (endIndex - 1) == '/')
      isFile = false;

    if (endIndex >= startIndex)
      {
	name = s.substring (startIndex, endIndex);
	if (browser_ != null)
	  {
	    //	    System.out.println (name);
	    if (isFile)	      
	      fileNode.addEntry (new FileNode (name, this.fileIcon_, null,
					       fileNode.app ()));
	    else
	      {
		FileNode f = new FileNode (name, this.dirIcon_, this,
					   fileNode.app ());
		fileNode.addEntry (f);
		f.setExpandable (true);
	      }
	  }
      }
    return 0;
  }

  private FileBrowser browser_ = null;
  private String url_ = null;
  private String directory_ = null;
  private Icon fileIcon_;
  private Icon dirIcon_;
}