KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > runtime > logview > LogEntry


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.runtime.logview;
12
13 import java.io.PrintWriter JavaDoc;
14 import java.io.StringWriter JavaDoc;
15 import com.ibm.icu.text.DateFormat;
16 import com.ibm.icu.text.SimpleDateFormat;
17
18 import java.text.ParseException JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Date JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.PlatformObject;
25 import org.eclipse.jface.resource.ImageDescriptor;
26 import org.eclipse.pde.internal.runtime.PDERuntimeMessages;
27 import org.eclipse.ui.model.IWorkbenchAdapter;
28
29 public class LogEntry extends PlatformObject implements IWorkbenchAdapter {
30     
31     public static final String JavaDoc F_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS"; //$NON-NLS-1$
32

33     private ArrayList JavaDoc children;
34     private LogEntry parent;
35     private String JavaDoc pluginId;
36     private int severity;
37     private int code;
38     private Date JavaDoc fDate;
39     private String JavaDoc message;
40     private String JavaDoc stack;
41     private LogSession session;
42
43     public LogEntry() {
44     }
45
46     public LogSession getSession() {
47         return session;
48     }
49
50     void setSession(LogSession session) {
51         this.session = session;
52     }
53
54     public LogEntry(IStatus status) {
55         processStatus(status);
56     }
57     public int getSeverity() {
58         return severity;
59     }
60
61     public boolean isOK() {
62         return severity == IStatus.OK;
63     }
64     public int getCode() {
65         return code;
66     }
67     public String JavaDoc getPluginId() {
68         return pluginId;
69     }
70     public String JavaDoc getMessage() {
71         return message;
72     }
73     public String JavaDoc getStack() {
74         return stack;
75     }
76     public Date JavaDoc getDate() {
77         if (fDate == null)
78             fDate = new Date JavaDoc(0); // unknown date - return epoch
79
return fDate;
80     }
81     public String JavaDoc getSeverityText() {
82         return getSeverityText(severity);
83     }
84     public boolean hasChildren() {
85         return children != null && children.size() > 0;
86     }
87     public String JavaDoc toString() {
88         return getSeverityText();
89     }
90     /**
91      * @see IWorkbenchAdapter#getChildren(Object)
92      */

93     public Object JavaDoc[] getChildren(Object JavaDoc parent) {
94         if (children == null)
95             return new Object JavaDoc[0];
96         return children.toArray();
97     }
98
99     /**
100      * @see IWorkbenchAdapter#getImageDescriptor(Object)
101      */

102     public ImageDescriptor getImageDescriptor(Object JavaDoc arg0) {
103         return null;
104     }
105
106     /**
107      * @see IWorkbenchAdapter#getLabel(Object)
108      */

109     public String JavaDoc getLabel(Object JavaDoc obj) {
110         return getSeverityText();
111     }
112
113     /**
114      * @see IWorkbenchAdapter#getParent(Object)
115      */

116     public Object JavaDoc getParent(Object JavaDoc obj) {
117         return parent;
118     }
119
120     void setParent(LogEntry parent) {
121         this.parent = parent;
122     }
123
124     private String JavaDoc getSeverityText(int severity) {
125         switch (severity) {
126             case IStatus.ERROR :
127                 return PDERuntimeMessages.LogView_severity_error;
128             case IStatus.WARNING :
129                 return PDERuntimeMessages.LogView_severity_warning;
130             case IStatus.INFO :
131                 return PDERuntimeMessages.LogView_severity_info;
132             case IStatus.OK :
133                 return PDERuntimeMessages.LogView_severity_ok;
134         }
135         return "?"; //$NON-NLS-1$
136
}
137
138
139     void processEntry(String JavaDoc line) {
140         //!ENTRY <pluginID> <severity> <code> <date>
141
//!ENTRY <pluginID> <date> if logged by the framework!!!
142
StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(line, " "); //$NON-NLS-1$
143
int tokenCount = stok.countTokens();
144         boolean noSeverity = stok.countTokens() < 5;
145         
146         // no severity means it should be represented as OK
147
if (noSeverity) {
148             severity = 0;
149             code = 0;
150         }
151         StringBuffer JavaDoc dateBuffer = new StringBuffer JavaDoc();
152         for (int i = 0; i < tokenCount; i++) {
153             String JavaDoc token = stok.nextToken();
154             switch (i) {
155                 case 0:
156                     break;
157                 case 1:
158                     pluginId = token;
159                     break;
160                 case 2:
161                     if (noSeverity) {
162                         if (dateBuffer.length() > 0)
163                             dateBuffer.append(" "); //$NON-NLS-1$
164
dateBuffer.append(token);
165                     } else {
166                         severity = parseInteger(token);
167                     }
168                     break;
169                 case 3:
170                     if (noSeverity) {
171                         if (dateBuffer.length() > 0)
172                             dateBuffer.append(" "); //$NON-NLS-1$
173
dateBuffer.append(token);
174                     } else
175                         code = parseInteger(token);
176                     break;
177                 default:
178                     if (dateBuffer.length() > 0)
179                         dateBuffer.append(" "); //$NON-NLS-1$
180
dateBuffer.append(token);
181             }
182         }
183         DateFormat formatter = new SimpleDateFormat(F_DATE_FORMAT);
184         try {
185             Date JavaDoc date = formatter.parse(dateBuffer.toString());
186             if (date != null)
187                 fDate = date;
188         } catch (ParseException JavaDoc e) {
189         }
190     }
191     
192     int processSubEntry(String JavaDoc line) {
193         //!SUBENTRY <depth> <pluginID> <severity> <code> <date>
194
//!SUBENTRY <depth> <pluginID> <date>if logged by the framework!!!
195
StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(line, " "); //$NON-NLS-1$
196
int tokenCount = stok.countTokens();
197         boolean byFrameWork = stok.countTokens() < 5;
198         
199         StringBuffer JavaDoc dateBuffer = new StringBuffer JavaDoc();
200         int depth = 0;
201         for (int i = 0; i < tokenCount; i++) {
202             String JavaDoc token = stok.nextToken();
203             switch (i) {
204                 case 0:
205                     break;
206                 case 1:
207                     depth = parseInteger(token);
208                     break;
209                 case 2:
210                     pluginId = token;
211                     break;
212                 case 3:
213                     if (byFrameWork) {
214                         if (dateBuffer.length() > 0)
215                             dateBuffer.append(" "); //$NON-NLS-1$
216
dateBuffer.append(token);
217                     } else {
218                         severity = parseInteger(token);
219                     }
220                     break;
221                 case 4:
222                     if (byFrameWork) {
223                         if (dateBuffer.length() > 0)
224                             dateBuffer.append(" "); //$NON-NLS-1$
225
dateBuffer.append(token);
226                     } else
227                         code = parseInteger(token);
228                     break;
229                 default:
230                     if (dateBuffer.length() > 0)
231                         dateBuffer.append(" "); //$NON-NLS-1$
232
dateBuffer.append(token);
233             }
234         }
235         DateFormat formatter = new SimpleDateFormat(F_DATE_FORMAT);
236         try {
237             Date JavaDoc date = formatter.parse(dateBuffer.toString());
238             if (date != null)
239                 fDate = date;
240         } catch (ParseException JavaDoc e) {
241         }
242         return depth;
243     }
244     
245     private int parseInteger(String JavaDoc token) {
246         try {
247             return Integer.parseInt(token);
248         } catch (NumberFormatException JavaDoc e) {
249             return 0;
250         }
251     }
252
253     void setStack(String JavaDoc stack) {
254         this.stack = stack;
255     }
256     void setMessage(String JavaDoc message) {
257         this.message = message;
258     }
259
260     private void processStatus(IStatus status) {
261         pluginId = status.getPlugin();
262         severity = status.getSeverity();
263         code = status.getCode();
264         fDate = new Date JavaDoc();
265         message = status.getMessage();
266         Throwable JavaDoc throwable = status.getException();
267         if (throwable != null) {
268             StringWriter JavaDoc swriter = new StringWriter JavaDoc();
269             PrintWriter JavaDoc pwriter = new PrintWriter JavaDoc(swriter);
270             throwable.printStackTrace(pwriter);
271             pwriter.flush();
272             pwriter.close();
273             stack = swriter.toString();
274         }
275         IStatus[] schildren = status.getChildren();
276         if (schildren.length > 0) {
277             children = new ArrayList JavaDoc();
278             for (int i = 0; i < schildren.length; i++) {
279                 LogEntry child = new LogEntry(schildren[i]);
280                 addChild(child);
281             }
282         }
283     }
284     void addChild(LogEntry child) {
285         if (children == null)
286             children = new ArrayList JavaDoc();
287         children.add(child);
288         child.setParent(this);
289     }
290     public void write(PrintWriter JavaDoc writer) {
291         if (session != null)
292             writer.println(session.getSessionData());
293         writer.println(getSeverityText());
294         if (fDate != null)
295             writer.println(getDate());
296         
297         if (message != null)
298             writer.println(getMessage());
299     
300         if (stack != null) {
301             writer.println();
302             writer.println(stack);
303         }
304     }
305 }
306
Popular Tags