source: trunk/jabberit_messenger/java_source/src/nu/fw/jeti/images/Icons.java @ 1014

Revision 1014, 7.5 KB checked in by alexandrecorreia, 15 years ago (diff)

Ticket #552 - Inclusão do projeto Java referente ao applet do módulo.

Line 
1/*
2 *      Jeti, a Java Jabber client, Copyright (C) 2001 E.S. de Boer 
3 *
4 *  This program is free software; you can redistribute it and/or modify
5 *  it under the terms of the GNU General Public License as published by
6 *  the Free Software Foundation; either version 2 of the License, or
7 *  (at your option) any later version.
8 *
9 *  This program is distributed in the hope that it will be useful,
10 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
11 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 *      GNU General Public License for more details.
13 *
14 *  You should have received a copy of the GNU General Public License
15 *  along with this program; if not, write to the Free Software
16 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 *
18 *      For questions, comments etc,
19 *      use the website at http://jeti.jabberstudio.org
20 *  or mail me at eric@jeti.tk
21 * 
22 *  30-12-2004
23 */
24
25package nu.fw.jeti.images;
26
27import java.io.*;
28import java.net.URL;
29import java.util.ArrayList;
30import java.util.Iterator;
31import java.util.List;
32import java.util.zip.ZipException;
33
34import javax.xml.parsers.FactoryConfigurationError;
35import javax.xml.parsers.ParserConfigurationException;
36import javax.xml.parsers.SAXParser;
37import javax.xml.parsers.SAXParserFactory;
38
39import nu.fw.jeti.backend.Start;
40import nu.fw.jeti.backend.XMLDataFile;
41import nu.fw.jeti.plugins.PluginsHandler;
42import nu.fw.jeti.util.Preferences;
43
44import org.xml.sax.InputSource;
45import org.xml.sax.SAXException;
46
47
48
49/**
50 * @author E.S. de Boer
51 */
52public class Icons extends XMLDataFile 
53{
54        //TODO combine with emoticons as classloader
55        private List iconList;//all rostericons/emoticons
56        private SAXParser parser;
57        private String iconType;
58
59        public Icons(String type)
60        {
61                iconList = Preferences.getPlugable(type);
62                iconType = type;
63        }
64
65        private void loadParser()
66        {
67                try{parser = SAXParserFactory.newInstance().newSAXParser();}
68                catch (FactoryConfigurationError ex){ex.printStackTrace();}
69                catch (SAXException ex){ex.printStackTrace();}
70                catch (ParserConfigurationException ex){ex.printStackTrace();}
71        }
72               
73        public Icons(SAXParser parser,String type) throws IOException
74        {
75                iconType = type;
76                iconList =Preferences.getPlugable(iconType);
77                this.parser = parser;
78                InputStream  data = null;
79                data = getClass().getResourceAsStream("/" + iconType+".xml");
80                if(data!=null)parseData(data);
81                try
82        {
83                        data = (new URL(Start.dataURL + "plugins/" + iconType + "/" + iconType+ ".xml")).openStream();
84                        parseData(data);
85                }
86                catch (IOException ex)
87                {
88                        if (new File(Start.path + "plugins" + File.separator + iconType).exists()) scanIcons();
89                }
90               
91                //if( iconList.isEmpty() ) throw new IOException("no " + iconType);
92               
93                for(Iterator i = iconList.iterator();i.hasNext();)
94                {
95                        //remove icons whitout description /deleted plugins still in preferences
96                        Object[] object = (Object[])i.next();
97                        if(object[2] == null)
98                        {
99                                System.out.println(object[0] + "not found or no description");
100                                i.remove();
101                        }
102                        else if(object[3] == null)
103                        {
104                                object[3] = getType((String)object[0]);
105                        }
106                }
107                parser = null;
108        }
109
110        private void parseData(InputStream data)
111        {
112                if(data != null)
113                {
114                        try
115                        {
116                                parser.parse(new InputSource(new InputStreamReader(data,"UTF-8")),new PluginsHandler(iconList));
117                        }
118                        catch (SAXException ex)
119                        {
120                                ex.printStackTrace();
121                        }
122                        catch (IOException ex)
123                        {
124                                ex.printStackTrace();
125                        }
126                }
127        }
128
129        public void scanIcons()
130        {
131                List oldrostericons = new ArrayList(iconList);
132                iconList.clear();
133                searchIcons();
134                //System.out.println(this.toString());
135                try
136                {
137                        BufferedWriter writer = new BufferedWriter(new FileWriter(Start.path + "plugins" + File.separator + iconType + File.separator + iconType +".xml"));
138                        writer.write(this.toString());
139                        writer.close();
140                        //new PrintWriter(new BufferedOutputStream(new FileOutputStream(Start.path +"plugins/plugins.xml"))).write(this.toString());
141                }
142                catch (IOException ex2)
143                {
144                        ex2.printStackTrace();
145                }
146                for(Iterator i = oldrostericons.iterator();i.hasNext();)
147                {
148                        Object[] oldPlugin =(Object[])i.next();
149                        for(Iterator j = iconList.iterator();j.hasNext();)
150                        {       
151                                Object[] newPlugin =(Object[])j.next();
152                                if(oldPlugin[0].equals(newPlugin[0]))
153                                {
154                                        newPlugin[1] = oldPlugin[1];
155                                        if(!oldPlugin[3].equals(""))newPlugin[3] = oldPlugin[3];
156                                        break;
157                                }
158                        }
159                }
160                InputStream data = getClass().getResourceAsStream("/" + iconType+".xml");
161                if(data!=null)
162                {
163                        if(parser ==null) loadParser();
164                        parseData(data);
165                }
166        }
167
168        private void searchIcons()
169        {//voeg samen met plugindata?
170                String urlString = Start.path + "plugins" + File.separator + iconType;
171               
172                //urlString += dir + "/";
173                //System.out.println(urlString);
174                File path = new File(urlString);
175                File file[] = path.listFiles();
176                if(file == null)
177                {
178                        //System.out.println("no rostericons");
179                        return;
180                }
181                for (int tel=0;tel<file.length;tel++)
182                {
183                        File currentFile = file[tel];
184                        //if(currentFile.toString().endsWith(".jisp") || currentFile.toString().endsWith(".jar") ||currentFile.toString().endsWith(".zip"))
185                        if(currentFile.isFile())
186                        {
187                                //System.out.println(currentFile);
188                                getIconInfo(currentFile);
189                        }
190                }
191        }
192
193        private void getIconInfo(File file)
194        {
195                //System.out.println(file);
196                try
197                {
198                        String name = file.getName().substring(0, file.getName().lastIndexOf("."));
199                        URL urlJar = new URL("jar:"+file.toURL() +"!/" + name + "/");
200                       
201                        //System.out.println(urlJar);
202                        Object[] data = new Object[6];
203                        if(parser ==null) loadParser();
204                        try{
205                                parser.parse(new URL(urlJar,"icondef.xml").openStream(),new RosterIconsHandler(urlJar,data));
206                                //parser.parse(stream,new rostericonsHandler(urlJar,data));
207                        } catch(ZipException e)
208                        {//no zip file so skip
209                                return;
210                        }
211//                      boolean found = false;
212//                      for(Iterator i = iconList.iterator();i.hasNext();)
213//                      {
214//                              Object[] temp = (Object[])i.next();
215//                              if(data[0].equals(temp[0]))
216//                              {
217//                                      temp[2] = data[2];
218//                                      temp[4] = file.getName();
219//                                      found = true;
220//                                      break;
221//                              }
222//                      }
223                        //if(!found)
224                        {//new plugin
225                                //System.out.println("data 0" + data[0]);
226                                data[1] = Boolean.TRUE;
227                                //temp[2] = data[2];
228                                data[3] = getType(file.getName());
229                                data[4] = file.getName();
230                                iconList.add(data);
231                        }
232                }
233                catch (IOException ex)
234                {
235                        System.err.println(ex.getMessage());
236                }catch (SAXException ex)
237                {
238                        System.err.println(ex.getMessage() + "while parsing " + file.getName());
239                       
240                }
241        }
242       
243        private String getType(String name)
244        {
245                name = name.toLowerCase();
246                if(name.indexOf("msn")!=-1) return "msn";
247                if(name.indexOf("icq")!=-1) return "icq";
248                if(name.indexOf("aim")!=-1) return "aim";
249                if(name.indexOf("aol")!=-1) return "aim";
250                if(name.indexOf("jabber")!=-1) return "jabber";
251                if(name.indexOf("yahoo")!=-1) return "yahoo";
252                if(name.indexOf("gadu")!=-1) return "gadu-gadu";
253                if(name.indexOf("sms")!=-1) return "sms";
254                if(name.indexOf("smtp")!=-1) return "smtp";
255                if(name.indexOf("mail")!=-1) return "smtp";
256                return "unknown";
257        }
258
259
260        public void appendToXML(StringBuffer xml)
261        {
262                appendHeader(xml);
263                appendOpenTag(xml,"<plugins>");
264                for(Iterator i = iconList.iterator();i.hasNext();)
265                {
266                        appendOpenTag(xml,"<plugin>");
267                        Object[] temp = (Object[]) i.next();
268                        appendElement(xml,"name",(String)temp[0]);
269                        appendElement(xml,"description",(String)temp[2]);
270                        appendElement(xml,"version",(String)temp[3]);
271                        appendElement(xml,"min_jeti_version",(String)temp[4]);//hackje
272                        appendCloseTag(xml,"</plugin>");
273                }
274                appendCloseTag(xml,"</plugins>");
275        }
276
277
278}
279/*
280 * Overrides for emacs
281 * Local variables:
282 * tab-width: 4
283 * End:
284 */
Note: See TracBrowser for help on using the repository browser.