KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > common > util > BasicDiagnostic


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: BasicDiagnostic.java,v 1.3 2005/06/08 06:19:08 nickb Exp $
16  */

17 package org.eclipse.emf.common.util;
18
19
20 import java.util.Collections JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.eclipse.core.runtime.IStatus;
25
26 import org.eclipse.emf.common.util.BasicEList;
27
28
29 /**
30  * A basic implementation of a diagostic that that also acts as a chain.
31  */

32 public class BasicDiagnostic implements Diagnostic, DiagnosticChain
33 {
34   /**
35    * The severity.
36    * @see #getSeverity
37    */

38   protected int severity;
39
40   /**
41    * The message.
42    * @see #getMessage
43    */

44   protected String JavaDoc message;
45
46   /**
47    * The message.
48    * @see #getMessage
49    */

50   protected List JavaDoc children;
51
52   /**
53    * The data.
54    * @see #getData
55    */

56   protected List JavaDoc data;
57
58   /**
59    * The source.
60    * @see #getSource
61    */

62   protected String JavaDoc source;
63
64   /**
65    * The code.
66    * @see #getCode
67    */

68   protected int code;
69
70   public BasicDiagnostic(String JavaDoc source, int code, String JavaDoc message, Object JavaDoc[] data)
71   {
72     this.source = source;
73     this.code = code;
74     this.message = message;
75     this.data = dataAsList(data);
76   }
77
78   public BasicDiagnostic(int severity, String JavaDoc source, int code, String JavaDoc message, Object JavaDoc[] data)
79   {
80     this(source, code, message, data);
81     this.severity = severity;
82   }
83
84   public BasicDiagnostic(String JavaDoc source, int code, List JavaDoc children, String JavaDoc message, Object JavaDoc[] data)
85   {
86     this(source, code, message, data);
87     if (children != null)
88     {
89       for (Iterator JavaDoc i = children.iterator(); i.hasNext(); )
90       {
91         add((Diagnostic)i.next());
92       }
93     }
94   }
95
96   protected List JavaDoc dataAsList(Object JavaDoc [] data)
97   {
98     if (data == null)
99     {
100       return Collections.EMPTY_LIST;
101     }
102     else
103     {
104       Object JavaDoc [] copy = new Object JavaDoc [data.length];
105       System.arraycopy(data, 0, copy, 0, data.length);
106       return new BasicEList.UnmodifiableEList(copy.length, copy);
107     }
108   }
109
110   public int getSeverity()
111   {
112     return severity;
113   }
114
115   public String JavaDoc getMessage()
116   {
117     return message;
118   }
119
120   public List JavaDoc getData()
121   {
122     return data;
123   }
124
125   public List JavaDoc getChildren()
126   {
127     return
128       children == null ?
129         Collections.EMPTY_LIST :
130         Collections.unmodifiableList(children);
131   }
132
133   public String JavaDoc getSource()
134   {
135     return source;
136   }
137
138   public int getCode()
139   {
140     return code;
141   }
142
143   public void add(Diagnostic diagnostic)
144   {
145     if (children == null)
146     {
147       children = new BasicEList();
148     }
149
150     children.add(diagnostic);
151     int childSeverity = diagnostic.getSeverity();
152     if (childSeverity > getSeverity())
153     {
154       severity = childSeverity;
155     }
156   }
157
158   public void addAll(Diagnostic diagnostic)
159   {
160     for (Iterator JavaDoc i = diagnostic.getChildren().iterator(); i.hasNext(); )
161     {
162       add((Diagnostic)i.next());
163     }
164   }
165
166   public void merge(Diagnostic diagnostic)
167   {
168     if (diagnostic.getChildren().isEmpty())
169     {
170       add(diagnostic);
171     }
172     else
173     {
174       addAll(diagnostic);
175     }
176   }
177
178   public int recomputeSeverity()
179   {
180     if (children != null)
181     {
182       severity = OK;
183       for (Iterator JavaDoc i = children.iterator(); i.hasNext(); )
184       {
185         Diagnostic child = (Diagnostic)i.next();
186         int childSeverity = child instanceof BasicDiagnostic ? ((BasicDiagnostic)child).recomputeSeverity() : child.getSeverity();
187         if (childSeverity > severity)
188         {
189           severity = childSeverity;
190         }
191       }
192     }
193
194     return severity;
195   }
196
197   private static class Wrapper implements IStatus
198   {
199     protected static final IStatus [] EMPTY_CHILDREN = new IStatus [0];
200
201     protected Diagnostic diagnostic;
202     protected IStatus [] wrappedChildren;
203
204     public Wrapper(Diagnostic diagnostic)
205     {
206       this.diagnostic = diagnostic;
207     }
208
209     public IStatus[] getChildren()
210     {
211       if (wrappedChildren == null)
212       {
213         List JavaDoc children = diagnostic.getChildren();
214         if (children.isEmpty())
215         {
216           wrappedChildren = EMPTY_CHILDREN;
217         }
218         else
219         {
220           wrappedChildren = new IStatus [children.size()];
221           for (int i = 0; i < wrappedChildren.length; ++i)
222           {
223             wrappedChildren[i] = toIStatus((Diagnostic)children.get(i));
224           }
225         }
226       }
227       return wrappedChildren;
228     }
229     
230     public int getCode()
231     {
232       return diagnostic.getCode();
233     }
234     
235     public Throwable JavaDoc getException()
236     {
237       for (Iterator JavaDoc i = diagnostic.getData().iterator(); i.hasNext(); )
238       {
239         Object JavaDoc data = i.next();
240         if (data instanceof Throwable JavaDoc)
241         {
242           return (Throwable JavaDoc)data;
243         }
244       }
245       return null;
246     }
247     
248     public String JavaDoc getMessage()
249     {
250       return diagnostic.getMessage();
251     }
252     
253     public String JavaDoc getPlugin()
254     {
255       return diagnostic.getSource();
256     }
257     
258     public int getSeverity()
259     {
260       return diagnostic.getSeverity();
261     }
262     
263     public boolean isMultiStatus()
264     {
265       return !diagnostic.getChildren().isEmpty();
266     }
267     
268     public boolean isOK()
269     {
270       return diagnostic.getSeverity() == OK;
271     }
272     
273     public boolean matches(int severityMask)
274     {
275       return (diagnostic.getSeverity() & severityMask ) != 0;
276     }
277
278     public String JavaDoc toString()
279     {
280       return diagnostic.toString();
281     }
282
283     public static IStatus create(Diagnostic diagnostic)
284     {
285       return new Wrapper(diagnostic);
286     }
287   }
288
289   /**
290    * Return the diagnostic viewed as an {@link IStatus}.
291    */

292   public static IStatus toIStatus(Diagnostic diagnostic)
293   {
294     return Wrapper.create(diagnostic);
295   }
296
297   public String JavaDoc toString()
298   {
299     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
300     result.append("Diagnostic ");
301     switch (severity)
302     {
303       case OK:
304       {
305         result.append("OK");
306         break;
307       }
308       case INFO:
309       {
310         result.append("INFO");
311         break;
312       }
313       case WARNING:
314       {
315         result.append("WARNING");
316         break;
317       }
318       case ERROR:
319       {
320         result.append("ERROR");
321         break;
322       }
323       case CANCEL:
324       {
325         result.append("CANCEL");
326         break;
327       }
328       default:
329       {
330         result.append(Integer.toHexString(severity));
331         break;
332       }
333     }
334
335     result.append(" source=");
336     result.append(source);
337
338     result.append(" code=");
339     result.append(code);
340
341     result.append(' ');
342     result.append(message);
343
344     if (data != null)
345     {
346       result.append(" data=");
347       result.append(data);
348     }
349     if (children != null)
350     {
351       result.append(' ');
352       result.append(children);
353     }
354
355     return result.toString();
356   }
357 }
358
Popular Tags