source: trunk/jabberit_messenger/java_source/src/ConvertLogs.java @ 1001

Revision 1001, 6.8 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) 2004 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 or Jabber at jeti@jabber.org
21 *
22 *      Created on 2-mei-2004
23 */
24 
25import java.io.*;
26import java.net.URL;
27import java.net.URLDecoder;
28import java.text.ParseException;
29import java.text.SimpleDateFormat;
30import java.util.*;
31
32import javax.swing.JOptionPane;
33import javax.swing.ProgressMonitor;
34
35/**
36 * @author E.S. de Boer
37 *
38 */
39public class ConvertLogs
40{
41       
42        private static SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy",Locale.US);
43        private static Map  map = new HashMap();
44        private static String path;
45       
46        public static void main(String[] args)
47        {
48                getPath();
49                indexDirectory(new File(path + "logs"));
50                saveLogs();
51                System.exit(0);
52        }
53       
54        private static String getPath()
55        {
56                // Now, search for and get the URL for this class.
57                URL url = ConvertLogs.class.getResource("ConvertLogs.class");
58                //System.out.println(url);
59                String urlString = null;
60
61                try
62                { //remove %20 from program files etc
63                        urlString = URLDecoder.decode(url.toString(), "UTF8"); //encode if to url? probaly not
64                }
65                catch (Exception e)
66                {
67                        e.printStackTrace();
68                } //1.2 error
69
70                //System.out.println(url.getPath()  ); //werkt niet in 1.2
71                //System.out.println(url);
72                if (url.getProtocol().equals("jar"))
73                {
74                        // Strip off JarURL-specific syntax.
75                        urlString = urlString.substring(4);
76                        urlString = urlString.substring(0, urlString.lastIndexOf("!"));
77                        urlString = urlString.substring(0, urlString.lastIndexOf("/") + 1);
78                }
79                else urlString = ".....e:\\data\\java\\jeti\\";//urlString.substring(0, urlString.lastIndexOf("/jeti") + 1);
80                //System.out.println("df" + urlString);
81                path = urlString.substring(5);
82                return urlString;
83                /*
84                try
85                {
86                        localURL = new URL(urlString);
87                }
88                catch (MalformedURLException ex)
89                {
90                        ex.printStackTrace();
91                }
92                if(System.getProperty("file.separator").equals("/"))//unix? if mac then bug
93                {
94                        path = urlString.substring(5);
95                }
96                else path = urlString.substring(6);
97                path = path.replace('/',File.separatorChar);
98                System.out.println(path);
99                System.out.println(urlString);
100                System.out.println(localURL);
101                */
102        }
103
104        private static void indexDirectory(File dir)
105        {
106                File[] files = dir.listFiles();
107                if(files==null)System.exit(1);
108                ProgressMonitor progress = new ProgressMonitor(null,"Indexing log files","", 0, files.length);
109                for (int i=0; i < files.length; i++)
110            {
111                        if(progress.isCanceled()) break;
112                        File f = files[i];
113                        String name = f.getName();
114                        //System.out.println(name);
115                        if (name.endsWith(".txt"))
116                {
117                                int atLocation = name.indexOf("@");
118                                if (atLocation<0)
119                                {//no @ do nothing with it
120                                        continue;
121                                }
122                                progress.setNote(name);
123                        progress.setProgress(i);
124                        if(name.startsWith("msn.",atLocation+1))
125                        {
126                                readLog(f,name.substring(0,atLocation),"msn");
127                               
128                        }
129                        else if(name.startsWith("icq.",atLocation+1))
130                        {
131                                readLog(f,name.substring(0,atLocation),"icq");
132                               
133                        }
134                        else if(name.startsWith("aim.",atLocation+1))
135                        {
136                                readLog(f,name.substring(0,atLocation),"aim");
137                               
138                        }
139                        //else readLog(f,name.substring(0,atLocation),name.substring(atLocation+1,name.length()-4));
140                 }
141            }
142                progress.close();
143        }
144       
145        private static void readLog(File file, String adres, String type)
146        {
147                String name = adres +"@"+ type +".txt";
148               
149                BufferedReader log=null;
150                try
151                {
152                        log = new BufferedReader(new FileReader(file));
153                        parseLog(log,name);
154                }
155                catch (FileNotFoundException e2)
156                {
157                        return;
158                }
159                catch (IOException e2)
160                {
161                        e2.printStackTrace();
162                }
163                finally
164                {
165                        if(log!=null)
166                                try
167                                {
168                                        log.close();
169                                }
170                                catch (IOException e1)
171                                {
172                                        e1.printStackTrace();
173                                }
174                }
175                file.delete();
176        }
177       
178       
179       
180        private static void parseLog(BufferedReader reader, String name) throws IOException
181        {
182                StringBuffer text=null;
183                String line;
184                Date date=null;
185                List adresList =(List) map.get(name);
186                if(adresList==null)
187                {
188                        adresList = new ArrayList();
189                        map.put(name,adresList);
190                }
191                while((line = reader.readLine()) !=null)
192                {
193                        if(line.equals("")) continue;
194                        else if(line.startsWith("----------------") && line.length() > 50  && line.endsWith("----------------"))
195                        {
196                                String temp = line.substring(16);
197                                temp = temp.substring(0,temp.length()- 16);
198                                try
199                                {
200                                        Date newdate = dateFormat.parse(temp);
201                                        //System.out.println(date);
202                                        if(text!=null)
203                                        {
204                                                adresList.add(new DateText(date, text.toString()));
205                                        }
206                                        text = new StringBuffer();
207                                        date = newdate;
208                                } catch (ParseException e2)
209                                {
210                                        text.append(line + System.getProperty("line.separator"));
211                                }
212                                //System.out.println(date);
213                        }
214                        else text.append(line + System.getProperty("line.separator"));
215                }
216                adresList.add(new DateText(date, text.toString()));
217        }
218       
219        private static void saveLogs()
220        {
221                for(Iterator i=map.entrySet().iterator();i.hasNext();)
222                {
223                        Map.Entry entry = (Map.Entry)i.next();
224                        String filename = (String) entry.getKey();
225                        List texts = (List) entry.getValue();
226                        Collections.sort(texts);
227                       
228                        String file = path + "logs" + File.separator + filename;
229                        PrintWriter log=null;
230                        try
231                        {
232                                log = new PrintWriter(new BufferedOutputStream(new FileOutputStream(file, true)), true);
233                        }
234                        catch (IOException e2)
235                        {
236                                e2.printStackTrace();
237                                log.close();
238                                continue;
239                        }
240                        for(Iterator j = texts.iterator();j.hasNext();)
241                        {
242                                ((DateText)j.next()).save(log);
243                        }
244                        log.close();
245                }
246        }
247}
248
249class DateText implements Comparable
250{
251        private Date date;
252        private String text;
253       
254        public DateText(Date date, String text)
255        {
256                this.date = date;
257                this.text = text;
258        }
259       
260        public int compareTo(Object o)
261        {
262                return date.compareTo(((DateText)o).date);
263        }
264       
265        public void save(PrintWriter log)
266        {
267                log.println();
268                log.println("----------------" + date.toString() + "----------------");
269                log.println();
270                log.println(text);
271        }
272}
273/*
274 * Overrides for emacs
275 * Local variables:
276 * tab-width: 4
277 * End:
278 */
Note: See TracBrowser for help on using the repository browser.