svn-gvsig-desktop / tags / v1_1_Build_1008 / libraries / libInternationalization / src / org / gvsig / i18n / MessagesClassLoader.java @ 12520
History | View | Annotate | Download (2.57 KB)
1 |
/**
|
---|---|
2 |
*
|
3 |
*/
|
4 |
package org.gvsig.i18n; |
5 |
|
6 |
import java.io.File; |
7 |
import java.net.MalformedURLException; |
8 |
import java.net.URL; |
9 |
import java.net.URLClassLoader; |
10 |
import java.util.ArrayList; |
11 |
import java.util.List; |
12 |
import java.util.StringTokenizer; |
13 |
|
14 |
/**
|
15 |
* <p>This class offers a class loader which is able to load files from the specified
|
16 |
* directories.</p>
|
17 |
*
|
18 |
* @author C?sar Mart?nez Izquierdo (cesar.martinez@iver.es)
|
19 |
*
|
20 |
*/
|
21 |
public class MessagesClassLoader extends URLClassLoader { |
22 |
|
23 |
/**
|
24 |
* <p>Creates a new class loader, which is able to load files from the directories
|
25 |
* specified as parameter.</p>
|
26 |
*
|
27 |
* @param urls The list of directories which will be used to load files.
|
28 |
*/
|
29 |
public MessagesClassLoader(URL[] urls) { |
30 |
super(urls);
|
31 |
} |
32 |
|
33 |
/**
|
34 |
* Loads a resource using the specified file name.
|
35 |
*
|
36 |
* @param res The name of the resource to be loaded.
|
37 |
*/
|
38 |
public URL getResource(String res) { |
39 |
try {
|
40 |
ArrayList resource = new ArrayList(); |
41 |
StringTokenizer st = new StringTokenizer(res, "\\/"); |
42 |
|
43 |
while (st.hasMoreTokens()) {
|
44 |
String token = st.nextToken();
|
45 |
resource.add(token); |
46 |
} |
47 |
URL ret = null; |
48 |
int currentUrl;
|
49 |
URL[] urls = getURLs(); |
50 |
|
51 |
for (currentUrl=0; currentUrl< urls.length; currentUrl++) { |
52 |
URL url = urls[currentUrl];
|
53 |
File file = new File(url.getFile()); |
54 |
if (url.getFile().endsWith("/")) |
55 |
ret = getResource(file, resource); |
56 |
} |
57 |
|
58 |
if (ret != null) { |
59 |
return ret;
|
60 |
} |
61 |
} catch (Exception e) { |
62 |
e.printStackTrace(); |
63 |
} |
64 |
|
65 |
return super.getResource(res); |
66 |
} |
67 |
|
68 |
/**
|
69 |
* Busca recursivamente el recurso res en el directorio base. res es una
|
70 |
* lista de String's con los directorios del path y base es el directorio
|
71 |
* a partir del cual se busca dicho recurso. En cada ejecuci?n del m?todo
|
72 |
* se toma el primer elemento de res y se busca dicho directorio en el
|
73 |
* directorio base. Si se encuentra, ser? el directorio base para una
|
74 |
* nueva llamada.
|
75 |
*
|
76 |
* @param base Directorio desde donde parte la b?squeda del recurso.
|
77 |
* @param res Lista de strings con el path del recurso que se quiere
|
78 |
* encontrar
|
79 |
*
|
80 |
* @return URL con el recurso
|
81 |
*/
|
82 |
private URL getResource(File base, List res) { |
83 |
File[] files = base.listFiles(); |
84 |
|
85 |
String parte = (String) res.get(0); |
86 |
|
87 |
for (int i = 0; i < files.length; i++) { |
88 |
if (files[i].getName().compareTo(parte) == 0) { |
89 |
if (res.size() == 1) { |
90 |
try {
|
91 |
return new URL("file:" + files[i].toString()); |
92 |
} catch (MalformedURLException e) { |
93 |
return null; |
94 |
} |
95 |
} else {
|
96 |
return getResource(files[i], res.subList(1, res.size())); |
97 |
} |
98 |
} |
99 |
} |
100 |
|
101 |
return null; |
102 |
} |
103 |
} |
104 |
|
105 |
|