Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.i18n / src / main / java / org / gvsig / i18n / MessagesClassLoader.java @ 40769

History | View | Annotate | Download (3.49 KB)

1
/**
2
 * 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
package org.gvsig.i18n;
25

    
26
import java.io.File;
27
import java.net.MalformedURLException;
28
import java.net.URL;
29
import java.net.URLClassLoader;
30
import java.util.ArrayList;
31
import java.util.List;
32
import java.util.StringTokenizer;
33

    
34
/**
35
 * <p>This class offers a class loader which is able to load files from the specified
36
 * directories.</p>
37
 * 
38
 * @author C?sar Mart?nez Izquierdo (cesar.martinez@iver.es)
39
 *
40
 */
41
public class MessagesClassLoader extends URLClassLoader {
42
        
43
        /**
44
         * <p>Creates a new class loader, which is able to load files from the directories
45
         * specified as parameter.</p>
46
         * 
47
         * @param urls The list of directories which will be used to load files.
48
         */
49
        public MessagesClassLoader(URL[] urls) {
50
                super(urls);
51
        }
52
        
53
        /**
54
         * Loads a resource using the specified file name.
55
         * 
56
         * @param res The name of the resource to be loaded.
57
         */
58
        public URL getResource(String res) {
59
                try {
60
                        ArrayList resource = new ArrayList();
61
                        StringTokenizer st = new StringTokenizer(res, "\\/");
62
                        
63
                        while (st.hasMoreTokens()) {
64
                                String token = st.nextToken();
65
                                resource.add(token);
66
                        }
67
                        URL ret = null;
68
                        int currentUrl;
69
                        URL[] urls = getURLs();
70
                
71
                        for (currentUrl=0; currentUrl< urls.length; currentUrl++) {
72
                                URL url = urls[currentUrl];
73
                                File file = new File(url.getFile());
74
                                if (url.getFile().endsWith("/"))
75
                                        ret = getResource(file, resource);
76
                        }
77
                        
78
                        if (ret != null) {
79
                                return ret;
80
                        }
81
                } catch (Exception e) {
82
                        e.printStackTrace();
83
                }
84
                
85
                return super.getResource(res);
86
        }
87
        
88
        /**
89
         * Busca recursivamente el recurso res en el directorio base. res es una
90
         * lista de String's con los directorios del path y base es el directorio
91
         * a partir del cual se busca dicho recurso. En cada ejecuci?n del m?todo
92
         * se toma el primer elemento de res y se busca dicho directorio en el
93
         * directorio base. Si se encuentra, ser? el directorio base para una
94
         * nueva llamada.
95
         *
96
         * @param base Directorio desde donde parte la b?squeda del recurso.
97
         * @param res Lista de strings con el path del recurso que se quiere
98
         *        encontrar
99
         *
100
         * @return URL con el recurso
101
         */
102
        private URL getResource(File base, List res) {
103
                File[] files = base.listFiles();
104
                
105
                String parte = (String) res.get(0);
106
                
107
                for (int i = 0; i < files.length; i++) {
108
                        if (files[i].getName().compareTo(parte) == 0) {
109
                                if (res.size() == 1) {
110
                                        try {
111
                                                return new URL("file:" + files[i].toString());
112
                                        } catch (MalformedURLException e) {
113
                                                return null;
114
                                        }
115
                                } else {
116
                                        return getResource(files[i], res.subList(1, res.size()));
117
                                }
118
                        }
119
                }
120
                
121
                return null;
122
        }
123
}
124

    
125