source: trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/util/HTMLLoader.java@ 6897

Last change on this file since 6897 was 5800, checked in by cs025, 21 years ago

Adding gs3build

  • Property svn:keywords set to Author Date Id Revision
File size: 3.7 KB
Line 
1package org.greenstone.gsdl3.gs3build.util;
2
3
4
5import java.net.*;
6
7import java.io.*;
8
9
10
11public class HTMLLoader implements Runnable
12
13{ URL from;
14
15 StringBuffer HTML;
16
17 boolean loaded;
18
19 boolean error;
20
21 boolean notFound;
22
23 Thread thread;
24
25 static int running;
26
27
28
29 /**
30
31 * Constructor to load given URL
32
33 */
34
35 public HTMLLoader(URL from)
36
37 { this.from = from;
38
39 this.HTML = new StringBuffer();
40
41 this.loaded = false;
42
43 this.error = false;
44
45 this.thread = null;
46 }
47
48
49
50 /**
51
52 * @return <code>0</code> if finished running; any other value if
53
54 * still executing
55
56 */
57
58 public static synchronized int running()
59
60 { return running;
61
62 }
63
64
65
66 /**
67
68 * Starts the thread; the method <code>load</code> is a synonym for this
69
70 */
71
72 public synchronized void start()
73
74 { if (thread == null)
75
76 { thread = new Thread(this);
77
78 thread.start();
79
80 running ++;
81
82 }
83
84// System.out.println("Running: "+running);
85
86 }
87
88
89
90 /**
91
92 * Do the actual loading; this will multi-thread with calling task
93
94 */
95
96 public void run()
97
98 { URLConnection connect;
99
100 int length, i, c;
101
102 byte inbuffer[];
103
104 InputStream in;
105
106
107
108 inbuffer = new byte[128];
109
110
111
112 this.notFound = false;
113// System.out.println("Running loader");
114
115
116
117 // get connection and test length
118
119 try
120
121 { connect = this.from.openConnection();
122
123 }
124
125 catch (IOException ioex)
126
127 { this.loaded = true;
128
129 this.error = true;
130
131 running --;
132 System.out.println(ioex);
133
134 return;
135
136 }
137
138
139 length = connect.getContentLength();
140
141 if (length == 0)
142
143 { this.loaded = true;
144
145 running --;
146 System.out.println("Empty");
147
148 return;
149
150 }
151
152
153
154 // get an inputstream to the document body
155
156 try
157
158 { in = connect.getInputStream();
159
160 }
161
162 catch (IOException ioex)
163
164 { this.error = true;
165
166 this.loaded = true;
167
168 running --;
169 System.out.println("Open failed" + ioex);
170
171 return;
172
173 }
174
175
176
177 if (length < 0)
178
179 { if (connect.getHeaderField(0) != null &&
180
181 connect.getHeaderField(0).indexOf("404") >= 0)
182
183 { this.notFound = true;
184
185 this.error = true;
186
187 this.loaded = true;
188
189 running --;
190 System.out.println("Not found");
191
192 return;
193
194 }
195
196 }
197
198// System.out.println("Ready to read " + length);
199
200
201
202 // read in the document body
203
204 i = 0;
205
206 c = 0;
207
208 while (true || i < length)
209
210 { // read in 128 characters (abort on IO Exception)
211
212 try
213
214 { c = in.read(inbuffer);
215
216 }
217
218 catch (IOException ioex)
219
220 { break;
221
222 }
223
224 // System.out.println("read " + c);
225
226
227
228 // if no characters were read, end, otherwise add it to the body
229
230 if (c != -1)
231
232 { if (c > 0)
233
234 { this.HTML.append(new String(inbuffer, 0, c));
235
236 i += c;
237
238 }
239
240 }
241
242 else
243
244 { break;
245
246 }
247
248 // if an incomplete buffer was read (ie no characters remain), end
249
250/* if (c < inbuffer.length && i >= length)
251
252 { break;
253
254 }*/
255
256 }
257
258
259
260 // Close file
261
262 try
263
264 { in.close();
265
266 }
267
268 catch (IOException ioex)
269
270 { //System.out.println("Closed except "+ioex);
271
272 }
273
274
275 this.loaded = true;
276
277 running --;
278
279 }
280
281
282
283 /**
284
285 * Do the actual loading (calls a Start on the Thread)
286
287 */
288
289 public void load()
290
291 { this.start();
292
293// this.run();
294
295 }
296
297
298
299 /**
300
301 * @return HTML text as a <code>String</code>.
302
303 */
304
305 public synchronized String HTML()
306
307 { if (this.error)
308
309 { return "Error";
310
311 }
312
313 return this.HTML.toString();
314
315 }
316
317
318
319 /**
320
321 * @return <code>true</code> if the file has successfully loaded
322
323 */
324
325 public boolean loaded()
326
327 { return this.loaded;
328
329 }
330
331
332
333 protected void finalize() throws Throwable
334
335 { this.thread = null;
336
337 this.from = null;
338
339 this.HTML = null;
340
341 super.finalize();
342
343 }
344
345
346
347 static
348
349 { running = 0;
350
351 }
352
353}
Note: See TracBrowser for help on using the repository browser.