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 package com.drazzib.netpisteur.monitor;
28
29 import java.io.BufferedWriter;
30 import java.io.FileWriter;
31 import java.io.IOException;
32 import java.io.PrintWriter;
33 import java.io.WriteAbortedException;
34 import java.net.MalformedURLException;
35 import java.net.URL;
36 import java.util.ArrayList;
37 import java.util.Calendar;
38 import java.util.GregorianCalendar;
39
40 import javax.swing.event.HyperlinkEvent;
41
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44
45 /**
46 * Represents the list of addresses visited that contains addresses keyboarded
47 * in the boxes, clicked links, precedents and following
48 *
49 * @author NetPisteur Team
50 */
51
52 public class ListHL {
53
54 private static Log log = LogFactory.getLog(ListHL.class);
55
56
57
58 private GregorianCalendar calen;
59
60
61
62 private ArrayList _adresse;
63
64 private ArrayList _adVisit;
65
66
67
68 private ArrayList _aSuivre;
69
70
71
72
73
74 private ArrayList _historique;
75
76 private URL _accueil;
77
78 private URL _adCour;
79
80
81
82 /**
83 * @param url :
84 * the home page
85 */
86 public ListHL(URL url) {
87 Address accueil = new Address(url, getTime());
88 this._accueil = accueil.getURL();
89 this._adresse = new ArrayList();
90 this._adVisit = new ArrayList();
91 this._aSuivre = new ArrayList();
92 this._historique = new ArrayList();
93 this._adCour = accueil.getURL();
94 add(this._accueil);
95 }
96
97 /**
98 * @param hl :
99 * the link that give the home page
100 */
101 public ListHL(HyperlinkEvent hl) {
102 Address accueil = new Address(hl.getURL(), getTime());
103 this._accueil = accueil.getURL();
104 this._adresse = new ArrayList();
105 this._adVisit = new ArrayList();
106 this._aSuivre = new ArrayList();
107 this._historique = new ArrayList();
108 this._adCour = accueil.getURL();
109 add(this._accueil);
110 }
111
112 public ListHL() {
113 Address accueil = new Address(this._accueil, null);
114 this._adresse = new ArrayList();
115 this._adVisit = new ArrayList();
116 this._aSuivre = new ArrayList();
117 this._historique = new ArrayList();
118 this._adCour = accueil.getURL();
119 try {
120 this._accueil = new URL("http://lan.drazzib.com/");
121 } catch (MalformedURLException ex) {
122 log.error("Acces impossible");
123 }
124 add(this._accueil);
125 }
126
127 /**
128 * This method allows to add to the history and the list of clicked links
129 * the addresse corresponding to the activated link
130 *
131 * @param hl :
132 * the clicked link
133 */
134 public void add(HyperlinkEvent hl) {
135 add(hl.getURL());
136 Address page = new Address(hl.getURL(), getTime());
137 this._adresse.add(page);
138 }
139
140 /**
141 * public void add(URL url) allows to add to the history and the list of
142 * precedents an address that is keyboarded in the box reserved for this
143 * purpose
144 *
145 * @param url :
146 * address that is keyboarded in the box reserved for this
147 * purpose
148 */
149 public void add(URL url) {
150 this._adCour = url;
151 Address pageCour = new Address(this._adCour, getTime());
152 if (this._historique.size() == 0) {
153 this._accueil = this._adCour;
154 this._adVisit.add(pageCour);
155 } else {
156 this._adVisit.add(pageCour);
157 }
158 this._aSuivre.clear();
159 this._historique.add(pageCour);
160 }
161
162 /**
163 * Method called when the button precedent is clicked
164 */
165 public void addPre() {
166 if (this._adVisit.size() > 1) {
167 Address pageSui = new Address(this._adCour, getTime());
168 this._aSuivre.add(pageSui);
169 this._adVisit.remove(this._adVisit.size() - 1);
170
171 this._adCour = ((Address) this._adVisit
172 .get(this._adVisit.size() - 1)).getURL();
173 Address pageCour = new Address(this._adCour, getTime());
174 this._historique.add(pageCour);
175 }
176 }
177
178 /**
179 * Method called when the button following is clicked
180 */
181 public void addSui() {
182 if (this._aSuivre.size() > 0) {
183
184 this._adCour = ((Address) this._aSuivre
185 .get(this._adVisit.size() - 1)).getURL();
186 Address pagePre = new Address(this._adCour, getTime());
187 this._historique.add(pagePre);
188 this._aSuivre.remove(this._aSuivre.size() - 1);
189 Address pageCour = new Address(this._adCour, getTime());
190 this._adVisit.add(pageCour);
191 }
192 }
193
194 /**
195 * public URL getURLSuivante() allows to recup url of the page visited after
196 * the current page This method is used for the button following
197 *
198 * @return URL that follow the current address
199 */
200
201 public URL getURLSuivante() {
202 URL ret = this._adCour;
203 if (this._aSuivre.size() > 0) {
204 Address pageSui = (Address) this._aSuivre
205 .get(this._aSuivre.size() - 1);
206
207 ret = pageSui.getURL();
208 }
209 return ret;
210 }
211
212 /**
213 * public URL getURLPrecedente() allows to recup url of the page visited
214 * before the current page This method is used for the button precedent
215 *
216 * @return URL that precede the current address
217 */
218 public URL getURLPrecedente() {
219 URL ret = this._adCour;
220 if (this._adVisit.size() > 1) {
221 int i = this._adVisit.size() - 1;
222 i--;
223 Address pagePre = (Address) this._adVisit.get(i);
224 ret = pagePre.getURL();
225 }
226 return ret;
227 }
228
229 /**
230 * public URL getURLCourante() allows to recup url of the current page This
231 * method is used for the button reload
232 *
233 * @return URL current
234 */
235 public URL getURLCourante() {
236 return this._adCour;
237 }
238
239 /**
240 * public URL getURLAccueil() allows to recup url of the home page This
241 * method is used for the button home
242 *
243 * @return URL home page
244 */
245 public URL getURLAccueil() {
246 return this._accueil;
247 }
248
249 /**
250 * public String getTime() allows to recup the hour to which the page was
251 * loaded
252 *
253 * @return String
254 */
255 public String getTime() {
256 this.calen = new GregorianCalendar();
257 String ret;
258 ret = getHours() + ":" + getMinutes() + ":" + getSeconds();
259 return ret;
260 }
261
262 /**
263 * public int getHours()
264 *
265 * Allows to recup system hours
266 *
267 * @return String
268 */
269 public String getHours() {
270 String ret;
271 int tmp = this.calen.get(Calendar.HOUR_OF_DAY);
272 if (tmp > -1 && tmp < 10) {
273 ret = "0" + tmp;
274 } else {
275 ret = Integer.toString(tmp);
276 }
277 return ret;
278 }
279
280 /**
281 * public int getMinutes()
282 *
283 * Allows to recup system minutes
284 *
285 * @return String
286 */
287 public String getMinutes() {
288 String ret;
289 int tmp = this.calen.get(Calendar.MINUTE);
290 if (tmp > -1 && tmp < 10) {
291 ret = "0" + tmp;
292 } else {
293 ret = Integer.toString(tmp);
294 }
295 return ret;
296 }
297
298 /**
299 * public int getSeconds()
300 *
301 * Allows to recup system seconds
302 *
303 * @return String
304 */
305 public String getSeconds() {
306 String ret;
307 int tmp = this.calen.get(Calendar.SECOND);
308 if (tmp > -1 && tmp < 10) {
309 ret = "0" + tmp;
310 } else {
311 ret = Integer.toString(tmp);
312 }
313 return ret;
314 }
315
316 /**
317 * public boolean writeListeHLToDisk()
318 *
319 * method that allows to write all URL visited in a file
320 *
321 * @return boolean (true if there is no problem with the write. else it's
322 * false )
323 * @param file :
324 * the file where record measures
325 * @param sep :
326 * the separator of the measure
327 */
328 public boolean writeListeHLToDisk(String file, String sep) {
329 PrintWriter out;
330 boolean retour = false;
331 URL recupUrl;
332 String recupTime;
333
334 try {
335 out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
336 out.println("# Historique des pages visites : ");
337
338 for (int i = 0; i < this._historique.size(); i++) {
339
340
341 Address cour = (Address) this._historique.get(i);
342 recupUrl = cour.getURL();
343 recupTime = cour.getTime();
344 out.println("" + i + sep + recupUrl + sep + recupTime);
345 }
346 out.close();
347 retour = true;
348
349 } catch (WriteAbortedException e) {
350 log.error("Erreur d'écriture du fichier", e);
351 }
352
353 catch (IOException e) {
354 log.error("Erreur physique de lecture du fichier", e);
355 }
356
357 return (retour);
358
359 }
360
361 /**
362 * public boolean writeHLClicToDisk()
363 *
364 * method that allows to write all the addresses of the clicked links in a
365 * file
366 *
367 * @return boolean (true if there is no problem with the write. else it's
368 * false )
369 * @param file :
370 * the file where record measures
371 * @param sep :
372 * the separator of the measure
373 */
374 public boolean writeHLClicToDisk(String file, String sep) {
375 PrintWriter out;
376 boolean retour = false;
377 URL recupUrl;
378 String recupTime;
379
380 try {
381 out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
382 out.println("# Liste des liens cliqus : ");
383
384 for (int i = 0; i < this._adresse.size(); i++) {
385
386
387 Address cour = (Address) this._adresse.get(i);
388 recupUrl = cour.getURL();
389 recupTime = cour.getTime();
390 out.println("" + i + sep + recupUrl + sep + recupTime);
391 }
392 out.close();
393 retour = true;
394
395 } catch (WriteAbortedException e) {
396 log.error("Erreur d'écriture du fichier", e);
397 }
398
399 catch (IOException e) {
400 log.error("Erreur physique de lecture du fichier", e);
401 }
402
403 return (retour);
404
405 }
406
407 /**
408 * Allows to return the contents of the address vector on a String
409 */
410 public String toString() {
411 StringBuffer ret = new StringBuffer();
412 for (int i = 0; i < this._adresse.size(); i++) {
413 Address cour = (Address) this._adresse.get(i);
414 ret.append("\n" + cour);
415 }
416 return ret.toString();
417 }
418 }
419