View Javadoc

1   /*
2    * This file is part of NetPisteur.
3    * 
4    * Copyright 2001-2002 : Olivier CORNEC, David BOUANCHEAU, Loic LOPEZ,
5    *                       Gaetan BOUDARD, Mael LE LANNOU, Julien ROBINEAU
6    * 
7    * Copyright 2002-2003 : Olivier BRIENS, Simon DEZE, Florence FRIGOULT,
8    *                       Olivier JOURNEAULT, Francois MARTINIER, Damien RAUDE-MORVAN
9    * 
10   * Copyright 2004-2007 : Damien RAUDE-MORVAN
11   * 
12   * NetPisteur is free software; you can redistribute it and/or modify
13   * it under the terms of the GNU General Public License as published by
14   * the Free Software Foundation; either version 2 of the License, or
15   * (at your option) any later version.
16   *
17   * NetPisteur is distributed in the hope that it will be useful,
18   * but WITHOUT ANY WARRANTY; without even the implied warranty of
19   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20   * GNU General Public License for more details.
21   *
22   * You should have received a copy of the GNU General Public License
23   * along with NetPisteur; if not, write to the Free Software
24   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
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  	// private attributes of the class
57  
58  	private GregorianCalendar calen; // give hour
59  
60  	// attributes
61  
62  	private ArrayList _adresse; // Store addresses of clicked links
63  
64  	private ArrayList _adVisit; // Store visited addresses before the current
65  
66  	// address(list of precedents + current address)
67  
68  	private ArrayList _aSuivre; // Store pages on which the user is returned
69  
70  	// (list
71  
72  	// of following)
73  
74  	private ArrayList _historique; // Store all addresses visited
75  
76  	private URL _accueil; // addresse of the home page
77  
78  	private URL _adCour; // current address
79  
80  	// constructor
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  	} // ListHL()
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 	} // ListHL()
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 	} // ListHL()
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 	} // add()
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 	} // add()
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 			// _historique.add((Adress)_adVisit.lastElement());
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 	} // addPre()
177 
178 	/**
179 	 * Method called when the button following is clicked
180 	 */
181 	public void addSui() {
182 		if (this._aSuivre.size() > 0) {
183 			// _historique.add((Adress)_aSuivre.lastElement());
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 	} // addSui()
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 	} // getURLSuivante()
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 	} // getURLPrecedente()
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 	} // getURLCourante()
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 	} // getURLAccueil()
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 	} // getTime()
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 	} // getHours()
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 	} // getMinutes()
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 	} // getSeconds()
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 				// renvoi le numero d'ordre de visite de la page et l'adresse
340 				// url
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 	} // writeListToDisk()
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 				// renvoi le numero d'ordre de visite de la page et l'adresse
386 				// url
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 	} // writeListToDisk()
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 	} // toString()
418 } // Class ListHL
419