1 17 package org.eclipse.emf.common.util; 18 19 20 import java.util.Collections ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 import org.eclipse.core.runtime.IStatus; 25 26 import org.eclipse.emf.common.util.BasicEList; 27 28 29 32 public class BasicDiagnostic implements Diagnostic, DiagnosticChain 33 { 34 38 protected int severity; 39 40 44 protected String message; 45 46 50 protected List children; 51 52 56 protected List data; 57 58 62 protected String source; 63 64 68 protected int code; 69 70 public BasicDiagnostic(String source, int code, String message, Object [] 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 source, int code, String message, Object [] data) 79 { 80 this(source, code, message, data); 81 this.severity = severity; 82 } 83 84 public BasicDiagnostic(String source, int code, List children, String message, Object [] data) 85 { 86 this(source, code, message, data); 87 if (children != null) 88 { 89 for (Iterator i = children.iterator(); i.hasNext(); ) 90 { 91 add((Diagnostic)i.next()); 92 } 93 } 94 } 95 96 protected List dataAsList(Object [] data) 97 { 98 if (data == null) 99 { 100 return Collections.EMPTY_LIST; 101 } 102 else 103 { 104 Object [] copy = new Object [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 getMessage() 116 { 117 return message; 118 } 119 120 public List getData() 121 { 122 return data; 123 } 124 125 public List getChildren() 126 { 127 return 128 children == null ? 129 Collections.EMPTY_LIST : 130 Collections.unmodifiableList(children); 131 } 132 133 public String 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 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 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 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 getException() 236 { 237 for (Iterator i = diagnostic.getData().iterator(); i.hasNext(); ) 238 { 239 Object data = i.next(); 240 if (data instanceof Throwable ) 241 { 242 return (Throwable )data; 243 } 244 } 245 return null; 246 } 247 248 public String getMessage() 249 { 250 return diagnostic.getMessage(); 251 } 252 253 public String 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 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 292 public static IStatus toIStatus(Diagnostic diagnostic) 293 { 294 return Wrapper.create(diagnostic); 295 } 296 297 public String toString() 298 { 299 StringBuffer result = new StringBuffer (); 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 |