root / trunk / frameworks / _fwAndami / src / com / iver / andami / iconthemes / IconThemeManager.java @ 11003
History | View | Annotate | Download (6.25 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.FileNotFoundException; |
47 |
import java.io.IOException; |
48 |
import java.io.InputStream; |
49 |
import java.util.ArrayList; |
50 |
import java.util.Enumeration; |
51 |
import java.util.zip.ZipEntry; |
52 |
import java.util.zip.ZipException; |
53 |
import java.util.zip.ZipFile; |
54 |
|
55 |
import org.kxml2.io.KXmlParser; |
56 |
import org.xmlpull.v1.XmlPullParserException; |
57 |
|
58 |
/**
|
59 |
* <p>This class deals with icon themes, it understands the on-disk theme
|
60 |
* format, it is able to list the availabe themes, to get the default
|
61 |
* theme and to change it.</p>
|
62 |
*
|
63 |
* <p>A XML theme description file should look similar to:
|
64 |
* <pre>
|
65 |
* <?xml version="1.0" encoding="utf-8"?>
|
66 |
* <theme>
|
67 |
* <name>Default gvSIG icon theme</name>
|
68 |
* <description>Clear fancy super great icon theme for gvSIG. Author: Salvador Dal?, &lt;salvador.dali@art.org&gt;</description>
|
69 |
* <version>1.2</version>
|
70 |
* </theme>
|
71 |
* </pre>
|
72 |
*
|
73 |
*
|
74 |
* @author Cesar Martinez Izquierdo <cesar.martinez@iver.es>
|
75 |
*
|
76 |
*/
|
77 |
public class IconThemeManager { |
78 |
protected File themesDir = null; |
79 |
protected final String themeDefinitionFile = "theme.xml"; |
80 |
|
81 |
/**
|
82 |
*
|
83 |
* @param themesDir
|
84 |
*/
|
85 |
public IconThemeManager(File themesDir) throws FileNotFoundException { |
86 |
if (!themesDir.exists())
|
87 |
throw new FileNotFoundException(); |
88 |
this.themesDir = themesDir;
|
89 |
} |
90 |
|
91 |
/**
|
92 |
*
|
93 |
* @return
|
94 |
*/
|
95 |
public IconThemeInfo[] list() { |
96 |
File[] files = themesDir.listFiles(); |
97 |
if (files==null) return null; |
98 |
|
99 |
ArrayList themeList = new ArrayList(); |
100 |
IconThemeInfo info; |
101 |
for (int i=0; i<files.length; i++) { |
102 |
if (files[i].isDirectory()) {
|
103 |
info = readInfoFromDir(files[i]); |
104 |
if (info!=null) |
105 |
themeList.add(info); |
106 |
} |
107 |
else if (files[i].isFile()) { // ensure it's a regular file |
108 |
info = readInfoFromZip(files[i]); |
109 |
if (info!=null) |
110 |
themeList.add(info); |
111 |
} |
112 |
} |
113 |
|
114 |
return (IconThemeInfo[]) themeList.toArray(new IconThemeInfo[0]); |
115 |
} |
116 |
|
117 |
private IconThemeInfo readInfoFromDir(File dir){ |
118 |
File themeDefinition = new File(dir + File.separator + themeDefinitionFile); |
119 |
IconThemeInfo themeInfo; |
120 |
try {
|
121 |
// try reading the XML file
|
122 |
if (themeDefinition.exists() && themeDefinition.isFile()) {
|
123 |
themeInfo = readXML(new FileInputStream(themeDefinition)); |
124 |
themeInfo.setResource(dir); |
125 |
return themeInfo;
|
126 |
} |
127 |
} catch (IOException e) {} |
128 |
catch (XmlPullParserException e) {
|
129 |
e.printStackTrace(); |
130 |
} |
131 |
// the XML parsing failed, just show the dir name
|
132 |
themeInfo = new IconThemeInfo();
|
133 |
themeInfo.setName(dir.getName()); |
134 |
themeInfo.setDescription(dir.getName()); |
135 |
themeInfo.setResource(dir); |
136 |
return themeInfo;
|
137 |
} |
138 |
|
139 |
private IconThemeInfo readXML(InputStream xmlStream) throws XmlPullParserException, IOException { |
140 |
KXmlParser parser = new KXmlParser();
|
141 |
// we use null encoding, in this way kxml2 tries to detect the encoding
|
142 |
parser.setInput(xmlStream, null);
|
143 |
IconThemeInfo themeInfo = new IconThemeInfo();
|
144 |
for (parser.next(); parser.getEventType()!=KXmlParser.END_DOCUMENT; parser.next()) {
|
145 |
// este bucle externo recorre las etiquetas de primer y segundo nivel
|
146 |
if (parser.getEventType()==KXmlParser.START_TAG) {
|
147 |
if (parser.getName().equals("name")) { |
148 |
themeInfo.setName(parser.nextText()); |
149 |
} |
150 |
else if (parser.getName().equals("description")) { |
151 |
themeInfo.setDescription(parser.nextText()); |
152 |
} |
153 |
else if (parser.getName().equals("version")) { |
154 |
themeInfo.setVersion(parser.nextText()); |
155 |
} |
156 |
} |
157 |
} |
158 |
return themeInfo;
|
159 |
} |
160 |
|
161 |
private IconThemeInfo readInfoFromZip(File zipFile) { |
162 |
try {
|
163 |
ZipFile file = new ZipFile(zipFile); |
164 |
IconThemeInfo themeInfo; |
165 |
Enumeration entries = file.entries();
|
166 |
ZipEntry xmlEntry=null, dirEntry=null; |
167 |
// search for theme.xml and the directory names
|
168 |
while (entries.hasMoreElements() && (xmlEntry==null||dirEntry==null)) { |
169 |
ZipEntry entry = (ZipEntry) entries.nextElement(); |
170 |
if (entry.isDirectory()) {
|
171 |
dirEntry = entry; |
172 |
} |
173 |
if (basename(entry.getName()).equals(themeDefinitionFile)) {
|
174 |
xmlEntry = entry; |
175 |
} |
176 |
} |
177 |
|
178 |
try {
|
179 |
// try with the XML file
|
180 |
if (xmlEntry!=null) { |
181 |
themeInfo = readXML(file.getInputStream(xmlEntry)); |
182 |
themeInfo.setResource(zipFile); |
183 |
return themeInfo;
|
184 |
} |
185 |
} catch (XmlPullParserException e) {
|
186 |
e.printStackTrace(); |
187 |
System.out.println(file.getName());
|
188 |
} |
189 |
|
190 |
themeInfo = new IconThemeInfo();
|
191 |
themeInfo.setResource(zipFile); |
192 |
// now try with the directory
|
193 |
if (dirEntry!=null) { |
194 |
|
195 |
themeInfo.setName(dirEntry.getName()); |
196 |
themeInfo.setDescription(dirEntry.getName()); |
197 |
return themeInfo;
|
198 |
} |
199 |
else { // otherwise just use the zipName |
200 |
themeInfo.setName(zipFile.getName()); |
201 |
themeInfo.setDescription(zipFile.getName()); |
202 |
return themeInfo;
|
203 |
} |
204 |
|
205 |
} catch (ZipException e) { |
206 |
// TODO Auto-generated catch block
|
207 |
e.printStackTrace(); |
208 |
} catch (IOException e) { |
209 |
// TODO Auto-generated catch block
|
210 |
e.printStackTrace(); |
211 |
} |
212 |
return null; |
213 |
} |
214 |
|
215 |
private String basename(String fullname) { |
216 |
String[] parts = fullname.split(File.separator+"|/"); |
217 |
return parts[parts.length-1]; |
218 |
} |
219 |
|
220 |
|
221 |
} |