svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.i18n / src / main / java / org / gvsig / i18n / MessagesClassLoader.java @ 40559
History | View | Annotate | Download (3.51 KB)
1 | 40435 | jjdelcerro | /**
|
---|---|---|---|
2 | 40559 | jjdelcerro | * gvSIG. Desktop Geographic Information System.
|
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
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 3
|
||
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 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | /**
|
||
25 | 40435 | jjdelcerro | *
|
26 | */
|
||
27 | package org.gvsig.i18n; |
||
28 | |||
29 | import java.io.File; |
||
30 | import java.net.MalformedURLException; |
||
31 | import java.net.URL; |
||
32 | import java.net.URLClassLoader; |
||
33 | import java.util.ArrayList; |
||
34 | import java.util.List; |
||
35 | import java.util.StringTokenizer; |
||
36 | |||
37 | /**
|
||
38 | * <p>This class offers a class loader which is able to load files from the specified
|
||
39 | * directories.</p>
|
||
40 | *
|
||
41 | * @author C?sar Mart?nez Izquierdo (cesar.martinez@iver.es)
|
||
42 | *
|
||
43 | */
|
||
44 | public class MessagesClassLoader extends URLClassLoader { |
||
45 | |||
46 | /**
|
||
47 | * <p>Creates a new class loader, which is able to load files from the directories
|
||
48 | * specified as parameter.</p>
|
||
49 | *
|
||
50 | * @param urls The list of directories which will be used to load files.
|
||
51 | */
|
||
52 | public MessagesClassLoader(URL[] urls) { |
||
53 | super(urls);
|
||
54 | } |
||
55 | |||
56 | /**
|
||
57 | * Loads a resource using the specified file name.
|
||
58 | *
|
||
59 | * @param res The name of the resource to be loaded.
|
||
60 | */
|
||
61 | public URL getResource(String res) { |
||
62 | try {
|
||
63 | ArrayList resource = new ArrayList(); |
||
64 | StringTokenizer st = new StringTokenizer(res, "\\/"); |
||
65 | |||
66 | while (st.hasMoreTokens()) {
|
||
67 | String token = st.nextToken();
|
||
68 | resource.add(token); |
||
69 | } |
||
70 | URL ret = null; |
||
71 | int currentUrl;
|
||
72 | URL[] urls = getURLs(); |
||
73 | |||
74 | for (currentUrl=0; currentUrl< urls.length; currentUrl++) { |
||
75 | URL url = urls[currentUrl];
|
||
76 | File file = new File(url.getFile()); |
||
77 | if (url.getFile().endsWith("/")) |
||
78 | ret = getResource(file, resource); |
||
79 | } |
||
80 | |||
81 | if (ret != null) { |
||
82 | return ret;
|
||
83 | } |
||
84 | } catch (Exception e) { |
||
85 | e.printStackTrace(); |
||
86 | } |
||
87 | |||
88 | return super.getResource(res); |
||
89 | } |
||
90 | |||
91 | /**
|
||
92 | * Busca recursivamente el recurso res en el directorio base. res es una
|
||
93 | * lista de String's con los directorios del path y base es el directorio
|
||
94 | * a partir del cual se busca dicho recurso. En cada ejecuci?n del m?todo
|
||
95 | * se toma el primer elemento de res y se busca dicho directorio en el
|
||
96 | * directorio base. Si se encuentra, ser? el directorio base para una
|
||
97 | * nueva llamada.
|
||
98 | *
|
||
99 | * @param base Directorio desde donde parte la b?squeda del recurso.
|
||
100 | * @param res Lista de strings con el path del recurso que se quiere
|
||
101 | * encontrar
|
||
102 | *
|
||
103 | * @return URL con el recurso
|
||
104 | */
|
||
105 | private URL getResource(File base, List res) { |
||
106 | File[] files = base.listFiles(); |
||
107 | |||
108 | String parte = (String) res.get(0); |
||
109 | |||
110 | for (int i = 0; i < files.length; i++) { |
||
111 | if (files[i].getName().compareTo(parte) == 0) { |
||
112 | if (res.size() == 1) { |
||
113 | try {
|
||
114 | return new URL("file:" + files[i].toString()); |
||
115 | } catch (MalformedURLException e) { |
||
116 | return null; |
||
117 | } |
||
118 | } else {
|
||
119 | return getResource(files[i], res.subList(1, res.size())); |
||
120 | } |
||
121 | } |
||
122 | } |
||
123 | |||
124 | return null; |
||
125 | } |
||
126 | } |
||
127 |