root / branches / v2_0_0_prep / extensions / org.gvsig.mkmvnproject / templates / SPI+UIAPI / org.gvsig.fortunecookies / org.gvsig.fortunecookies.prov / org.gvsig.fortunecookies.prov.fileprovider / src / main / java / org / gvsig / fortunecookies / prov / fileprovider / impl / FortuneCookieFileProvider.java @ 32378
History | View | Annotate | Download (2.73 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
|
23 |
package org.gvsig.fortunecookies.prov.fileprovider.impl; |
24 |
|
25 |
import java.io.BufferedReader; |
26 |
import java.io.FileReader; |
27 |
import java.io.FileWriter; |
28 |
import java.net.URL; |
29 |
import java.util.ArrayList; |
30 |
import java.util.List; |
31 |
import java.util.Random; |
32 |
|
33 |
import org.gvsig.fortunecookies.lib.spi.FortuneCookieProvider; |
34 |
import org.gvsig.tools.service.spi.AbstractProvider; |
35 |
import org.gvsig.tools.service.spi.ProviderServices; |
36 |
|
37 |
public class FortuneCookieFileProvider extends AbstractProvider implements FortuneCookieProvider{ |
38 |
|
39 |
public FortuneCookieFileProvider(ProviderServices providerServices) {
|
40 |
super(providerServices);
|
41 |
} |
42 |
|
43 |
public String getMessage() throws Exception { |
44 |
FileWriter fichero = null; |
45 |
String message_text = ""; |
46 |
String sCadena;
|
47 |
|
48 |
List<String> messages = null; |
49 |
|
50 |
try
|
51 |
{ |
52 |
messages = new ArrayList<String>(); |
53 |
ClassLoader loader = this.getClass().getClassLoader(); |
54 |
URL url = loader.getResource("org/gvsig/fortunecookies/prov/fileprovider/impl/messages.txt"); |
55 |
FileReader fr = new FileReader(url.getFile()); |
56 |
BufferedReader bf = new BufferedReader(fr); |
57 |
sCadena = bf.readLine(); |
58 |
while (sCadena!=null){ |
59 |
while (sCadena!=null && sCadena.length()>0 && sCadena.charAt(0)!='\n'){ |
60 |
message_text = message_text +"\n"+ sCadena;
|
61 |
sCadena = bf.readLine(); |
62 |
} |
63 |
messages.add(message_text); |
64 |
message_text="";
|
65 |
sCadena = bf.readLine(); |
66 |
} |
67 |
|
68 |
} catch (Exception e) { |
69 |
e.printStackTrace(); |
70 |
} finally {
|
71 |
try {
|
72 |
if (null != fichero) |
73 |
fichero.close(); |
74 |
} catch (Exception e2) { |
75 |
e2.printStackTrace(); |
76 |
} |
77 |
} |
78 |
|
79 |
Random rand = new Random(); |
80 |
int index = rand.nextInt(messages.size());
|
81 |
|
82 |
return messages.get(index);
|
83 |
} |
84 |
|
85 |
|
86 |
|
87 |
} |