svn-gvsig-desktop / trunk / frameworks / _fwAndami / src / com / iver / andami / iconthemes / IconThemeManager.java @ 11002
History | View | Annotate | Download (5.85 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
|
42 |
package com.iver.andami.iconthemes; |
43 |
|
44 |
import java.io.File; |
45 |
import java.io.FileInputStream; |
46 |
import java.io.IOException; |
47 |
import java.io.InputStream; |
48 |
import java.util.ArrayList; |
49 |
import java.util.Enumeration; |
50 |
import java.util.zip.ZipEntry; |
51 |
import java.util.zip.ZipException; |
52 |
import java.util.zip.ZipFile; |
53 |
|
54 |
import org.kxml2.io.KXmlParser; |
55 |
import org.xmlpull.v1.XmlPullParserException; |
56 |
|
57 |
/**
|
58 |
* <p>This class deals with icon themes, it understands the on-disk theme
|
59 |
* format, it is able to list the availabe themes, to get the default
|
60 |
* theme and to change it.</p>
|
61 |
*
|
62 |
* <p>A XML theme description file should look similar to:
|
63 |
* <pre>
|
64 |
* <?xml version="1.0" encoding="utf-8"?>
|
65 |
* <theme>
|
66 |
* <name>Default gvSIG icon theme</name>
|
67 |
* <description>Clear fancy super great icon theme for gvSIG. Author: Salvador Dal?, <salvador.dali@art.org> </description>
|
68 |
* <version>1.2</version>
|
69 |
* </theme>
|
70 |
* </pre>
|
71 |
*
|
72 |
*
|
73 |
* @author Cesar Martinez Izquierdo <cesar.martinez@iver.es>
|
74 |
*
|
75 |
*/
|
76 |
public class IconThemeManager { |
77 |
protected File themesDir = null; |
78 |
protected final String themeDefinitionFile = "theme.xml"; |
79 |
|
80 |
/**
|
81 |
*
|
82 |
* @param themesDir
|
83 |
*/
|
84 |
public IconThemeManager(File themesDir) { |
85 |
this.themesDir = themesDir;
|
86 |
} |
87 |
|
88 |
/**
|
89 |
*
|
90 |
* @return
|
91 |
*/
|
92 |
public IconThemeInfo[] list() { |
93 |
File[] files = themesDir.listFiles(); |
94 |
if (files==null) return null; |
95 |
|
96 |
ArrayList themeList = new ArrayList(); |
97 |
IconThemeInfo info; |
98 |
for (int i=0; i<files.length; i++) { |
99 |
if (files[i].isDirectory()) {
|
100 |
info = readInfoFromDir(files[i]); |
101 |
if (info!=null) |
102 |
themeList.add(info); |
103 |
} |
104 |
else if (files[i].isFile()) { // ensure it's a regular file |
105 |
info = readInfoFromZip(files[i]); |
106 |
if (info!=null) |
107 |
themeList.add(info); |
108 |
} |
109 |
} |
110 |
|
111 |
return (IconThemeInfo[]) themeList.toArray(new IconThemeInfo[0]); |
112 |
} |
113 |
|
114 |
private IconThemeInfo readInfoFromDir(File dir){ |
115 |
File themeDefinition = new File(dir + File.separator + themeDefinitionFile); |
116 |
IconThemeInfo themeInfo; |
117 |
try {
|
118 |
// try reading the XML file
|
119 |
if (themeDefinition.exists() && themeDefinition.isFile()) {
|
120 |
themeInfo = readXML(new FileInputStream(themeDefinition)); |
121 |
themeInfo.setResource(dir); |
122 |
return themeInfo;
|
123 |
} |
124 |
} catch (IOException e) {} |
125 |
catch (XmlPullParserException e) {}
|
126 |
// the XML parsing failed, just show the dir name
|
127 |
themeInfo = new IconThemeInfo();
|
128 |
themeInfo.setName(dir.getName()); |
129 |
themeInfo.setDescription(dir.getName()); |
130 |
themeInfo.setResource(dir); |
131 |
return themeInfo;
|
132 |
} |
133 |
|
134 |
private IconThemeInfo readXML(InputStream xmlStream) throws XmlPullParserException, IOException { |
135 |
KXmlParser parser = new KXmlParser();
|
136 |
// we use null encoding, in this way kxml2 tries to detect the encoding
|
137 |
parser.setInput(xmlStream, null);
|
138 |
IconThemeInfo themeInfo = new IconThemeInfo();
|
139 |
for (parser.next(); parser.getEventType()!=KXmlParser.END_DOCUMENT; parser.next()) {
|
140 |
// este bucle externo recorre las etiquetas de primer y segundo nivel
|
141 |
if (parser.getEventType()==KXmlParser.START_TAG) {
|
142 |
if (parser.getName().equals("name")) { |
143 |
themeInfo.setName(parser.nextText()); |
144 |
} |
145 |
else if (parser.getName().equals("description")) { |
146 |
themeInfo.setDescription(parser.nextText()); |
147 |
} |
148 |
else if (parser.getName().equals("version")) { |
149 |
themeInfo.setVersion(parser.nextText()); |
150 |
} |
151 |
} |
152 |
} |
153 |
return themeInfo;
|
154 |
} |
155 |
|
156 |
private IconThemeInfo readInfoFromZip(File zipFile) { |
157 |
try {
|
158 |
ZipFile file = new ZipFile(zipFile); |
159 |
IconThemeInfo themeInfo; |
160 |
Enumeration entries = file.entries();
|
161 |
ZipEntry xmlEntry=null, dirEntry=null; |
162 |
// search for theme.xml and the directory names
|
163 |
while (entries.hasMoreElements() && (xmlEntry==null||dirEntry==null)) { |
164 |
ZipEntry entry = (ZipEntry) entries.nextElement(); |
165 |
if (entry.isDirectory()) {
|
166 |
dirEntry = entry; |
167 |
} |
168 |
if (entry.getName().equals(themeDefinitionFile)) {
|
169 |
xmlEntry = entry; |
170 |
} |
171 |
} |
172 |
|
173 |
try {
|
174 |
// try with the XML file
|
175 |
if (xmlEntry!=null) { |
176 |
themeInfo = readXML(file.getInputStream(xmlEntry)); |
177 |
themeInfo.setResource(zipFile); |
178 |
} |
179 |
} catch (XmlPullParserException e) {}
|
180 |
|
181 |
themeInfo = new IconThemeInfo();
|
182 |
themeInfo.setResource(zipFile); |
183 |
// now try with the directory
|
184 |
if (dirEntry!=null) { |
185 |
|
186 |
themeInfo.setName(dirEntry.getName()); |
187 |
themeInfo.setDescription(dirEntry.getName()); |
188 |
return themeInfo;
|
189 |
} |
190 |
else { // otherwise just use the zipName |
191 |
themeInfo.setName(zipFile.getName()); |
192 |
themeInfo.setDescription(zipFile.getName()); |
193 |
return themeInfo;
|
194 |
} |
195 |
|
196 |
} catch (ZipException e) { |
197 |
// TODO Auto-generated catch block
|
198 |
e.printStackTrace(); |
199 |
} catch (IOException e) { |
200 |
// TODO Auto-generated catch block
|
201 |
e.printStackTrace(); |
202 |
} |
203 |
return null; |
204 |
} |
205 |
|
206 |
|
207 |
} |