Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4dc7353e1750af077e4e06a8600431d183129722 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*******************************************************************************
 * Copyright (c) 2000, 2004 QNX Software Systems and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     QNX Software Systems - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.core.model;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICModelStatus;
import org.eclipse.cdt.core.model.ICModelStatusConstants;
import org.eclipse.core.resources.IResourceStatus;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;

/**
 * @see ICModelStatus
 */

public class CModelStatus extends Status implements ICModelStatus, ICModelStatusConstants, IResourceStatus {

	/**
	 * The elements related to the failure, or <code>null</code> if no
	 * elements are involved.
	 */
	protected ICElement[] fElements;

	protected final static ICElement[] EmptyElement = new ICElement[]{};
	/**
	 * The path related to the failure, or <code>null</code> if no path is
	 * involved.
	 */
	protected IPath fPath;
	/**
	 * The <code>String</code> related to the failure, or <code>null</code>
	 * if no <code>String</code> is involved.
	 */
	protected String fString;
	protected final static String EMPTY_STRING = ""; //$NON-NLS-1$

	/**
	 * Empty children
	 */
	protected final static IStatus[] fgEmptyChildren = new IStatus[]{};
	protected IStatus[] fChildren = fgEmptyChildren;
	protected final static String DEFAULT_STRING = "CModelStatus"; //$NON-NLS-1$;

	/**
	 * Singleton OK object
	 */
	public static final ICModelStatus VERIFIED_OK = new CModelStatus(OK, OK, org.eclipse.cdt.internal.core.Util.bind("status.OK")); //$NON-NLS-1$

	/**
	 * Constructs an C model status with no corresponding elements.
	 */
	public CModelStatus() {
		// no code for an multi-status
		this(0);
	}

	/**
	 * Constructs an C model status with no corresponding elements.
	 */
	public CModelStatus(int code) {
		this(code, CElement.NO_ELEMENTS);
	}

	/**
	 * Constructs an C model status with the given corresponding elements.
	 */
	public CModelStatus(int code, ICElement[] elements) {
		super(ERROR, CCorePlugin.PLUGIN_ID, code, DEFAULT_STRING, null);
		fElements = elements;
		fPath = Path.EMPTY;
	}

	/**
	 * Constructs an C model status with no corresponding elements.
	 */
	public CModelStatus(int code, String string) {
		this(ERROR, code, string);
	}

	public CModelStatus(int severity, int code, String string) {
		super(severity, CCorePlugin.PLUGIN_ID, code, DEFAULT_STRING, null);
		fElements = CElement.NO_ELEMENTS;
		fPath = Path.EMPTY;
		fString = string;
	}

	/**
	 * Constructs an C model status with no corresponding elements.
	 */
	public CModelStatus(int code, IPath path) {
		super(ERROR, CCorePlugin.PLUGIN_ID, code, DEFAULT_STRING, null);
		fElements = CElement.NO_ELEMENTS;
		fPath = path;
	}

	/**
	 * Constructs an C model status with the given corresponding element.
	 */
	public CModelStatus(int code, ICElement element) {
		this(code, new ICElement[]{element});
	}

	/**
	 * Constructs an C model status with the given corresponding element and
	 * string
	 */
	public CModelStatus(int code, ICElement element, String string) {
		this(code, new ICElement[]{element});
		fString = string;
	}

	public CModelStatus(int code, ICElement element, IPath path) {
		this(code, new ICElement[]{element});
		fPath = path;
	}

	/**
	 * Constructs an C model status with no corresponding elements.
	 */
	public CModelStatus(CoreException coreException) {
		this(CORE_EXCEPTION, coreException);
	}

	/**
	 * Constructs an C model status with no corresponding elements.
	 */
	public CModelStatus(int code, Throwable throwable) {
		super(ERROR, CCorePlugin.PLUGIN_ID, code, DEFAULT_STRING, throwable);
		fElements = CElement.NO_ELEMENTS;
		fPath = Path.EMPTY;
	}

	protected int getBits() {
		int severity = 1 << (getCode() % 100 / 33);
		int category = 1 << ( (getCode() / 100) + 3);
		return severity | category;
	}

	/**
	 * @see IStatus
	 */
	public IStatus[] getChildren() {
		return fChildren;
	}

	/**
	 * @see ICModelStatus
	 */
	public ICElement[] getElements() {
		return fElements;
	}

	/**
	 * Returns the message that is relevant to the code of this status.
	 */
	public String getMessage() {
		Throwable exception = getException();
		if (isMultiStatus()) {
			StringBuffer sb = new StringBuffer();
			IStatus[] children = getChildren();
			if (children != null && children.length > 0) {
				for (int i = 0; i < children.length; ++i) {
					sb.append(children[i].getMessage()).append(',');
				}
			}
			return sb.toString();
		}
		if (exception == null) {
			switch (getCode()) {
				case CORE_EXCEPTION :
					return CoreModelMessages.getFormattedString("status.coreException"); //$NON-NLS-1$

				case DEVICE_PATH :
					return CoreModelMessages.getFormattedString("status.cannotUseDeviceOnPath", getPath().toString()); //$NON-NLS-1$

				case PARSER_EXCEPTION :
					return CoreModelMessages.getFormattedString("status.ParserError"); //$NON-NLS-1$

				case ELEMENT_DOES_NOT_EXIST :
					return CoreModelMessages.getFormattedString("element.doesNotExist", getFirstElementName()); //$NON-NLS-1$

				case EVALUATION_ERROR :
					return CoreModelMessages.getFormattedString("status.evaluationError", getString()); //$NON-NLS-1$

				case INDEX_OUT_OF_BOUNDS :
					return CoreModelMessages.getFormattedString("status.indexOutOfBounds"); //$NON-NLS-1$

				case INVALID_CONTENTS :
					return CoreModelMessages.getFormattedString("status.invalidContents"); //$NON-NLS-1$

				case INVALID_DESTINATION :
					return CoreModelMessages.getFormattedString("status.invalidDestination", getFirstElementName()); //$NON-NLS-1$

				case INVALID_ELEMENT_TYPES :
					StringBuffer buff = new StringBuffer(CoreModelMessages.getFormattedString("operation.notSupported")); //$NON-NLS-1$
					for (int i = 0; i < fElements.length; i++) {
						if (i > 0) {
							buff.append(", "); //$NON-NLS-1$
						}
						buff.append( (fElements[i]).toString());
					}
					return buff.toString();

				case INVALID_NAME :
					return CoreModelMessages.getFormattedString("status.invalidName", getString()); //$NON-NLS-1$

				case INVALID_PATH :
					String path = getPath() == null ? "null" : getPath().toString(); //$NON-NLS-1$
					return CoreModelMessages.getFormattedString("status.invalidPath", new Object[]{path, getString()}); //$NON-NLS-1$

				case INVALID_PATHENTRY :
					return CoreModelMessages.getFormattedString("status.invalidPathEntry", getString()); //$NON-NLS-1$

				case INVALID_PROJECT :
					return CoreModelMessages.getFormattedString("status.invalidProject", getString()); //$NON-NLS-1$

				case INVALID_RESOURCE :
					return CoreModelMessages.getFormattedString("status.invalidResource", getString()); //$NON-NLS-1$

				case INVALID_RESOURCE_TYPE :
					return CoreModelMessages.getFormattedString("status.invalidResourceType", getString()); //$NON-NLS-1$

				case INVALID_SIBLING :
					if (fString != null) {
						return CoreModelMessages.getFormattedString("status.invalidSibling", getString()); //$NON-NLS-1$
					}
					return CoreModelMessages.getFormattedString("status.invalidSibling", getFirstElementName()); //$NON-NLS-1$

				case IO_EXCEPTION :
					return CoreModelMessages.getFormattedString("status.IOException"); //$NON-NLS-1$

				case NAME_COLLISION :
					StringBuffer sb = new StringBuffer();
					if (fElements != null && fElements.length > 0) {
						ICElement element = fElements[0];
						sb.append(element.getElementName()).append(' ');
					}
					if (fString != null) {
						return fString;
					}
					return CoreModelMessages.getFormattedString("status.nameCollision", sb.toString()); //$NON-NLS-1$ //$NON-NLS-2$

				case NO_ELEMENTS_TO_PROCESS :
					return CoreModelMessages.getFormattedString("operation.needElements"); //$NON-NLS-1$

				case NULL_NAME :
					return CoreModelMessages.getFormattedString("operation.needName"); //$NON-NLS-1$

				case NULL_PATH :
					return CoreModelMessages.getFormattedString("operation.needPath"); //$NON-NLS-1$

				case NULL_STRING :
					return CoreModelMessages.getFormattedString("operation.needString"); //$NON-NLS-1$

				case PATH_OUTSIDE_PROJECT :
					return CoreModelMessages.getFormattedString(
							"operation.pathOutsideProject", new String[]{getString(), getFirstElementName()}); //$NON-NLS-1$

				case READ_ONLY :
					return CoreModelMessages.getFormattedString("status.readOnly", getFirstElementName()); //$NON-NLS-1$

				case RELATIVE_PATH :
					return CoreModelMessages.getFormattedString("operation.needAbsolutePath", getPath().toString()); //$NON-NLS-1$

				case UPDATE_CONFLICT :
					return CoreModelMessages.getFormattedString("status.updateConflict"); //$NON-NLS-1$

				case NO_LOCAL_CONTENTS :
					return CoreModelMessages.getFormattedString("status.noLocalContents", getPath().toString()); //$NON-NLS-1$

				case INVALID_CONTAINER_ENTRY :
					return CoreModelMessages.getFormattedString(
							"pathentry.invalidContainer", new String[]{getString(), getFirstElementName()}); //$NON-NLS-1$

				case VARIABLE_PATH_UNBOUND :
					return CoreModelMessages.getFormattedString("pathentry.unboundVariablePath", //$NON-NLS-1$
							new String[]{getPath().makeRelative().toString(), getFirstElementName()});

				case PATHENTRY_CYCLE :
					return CoreModelMessages.getFormattedString("pathentry.cycle", getFirstElementName()); //$NON-NLS-1$
			}
			return getString();
		}
		String message = exception.getMessage();
		if (message != null) {
			return message;
		}
		return exception.toString();
	}

	/**
	 * @see IOperationStatus
	 */
	public IPath getPath() {
		if (fPath == null) {
			return Path.EMPTY;
		}
		return fPath;
	}

	/**
	 * @see IStatus
	 */
	public int getSeverity() {
		if (fChildren == fgEmptyChildren)
			return super.getSeverity();
		int severity = -1;
		for (int i = 0, max = fChildren.length; i < max; i++) {
			int childrenSeverity = fChildren[i].getSeverity();
			if (childrenSeverity > severity) {
				severity = childrenSeverity;
			}
		}
		return severity;
	}

	/**
	 * @see ICModelStatus
	 */
	public String getString() {
		if (fString == null) {
			return EMPTY_STRING;
		}
		return fString;
	}

	public String getFirstElementName() {
		if (fElements != null && fElements.length > 0) {
			return fElements[0].getElementName();
		}
		return EMPTY_STRING;
	}

	/**
	 * @see ICModelStatus
	 */
	public boolean doesNotExist() {
		return getCode() == ELEMENT_DOES_NOT_EXIST;
	}

	/**
	 * @see IStatus
	 */
	public boolean isMultiStatus() {
		return fChildren != fgEmptyChildren;
	}

	/**
	 * @see ICModelStatus
	 */
	public boolean isOK() {
		return getCode() == OK;
	}

	/**
	 * @see IStatus#matches
	 */
	public boolean matches(int mask) {
		if (!isMultiStatus()) {
			return matches(this, mask);
		}
		for (int i = 0, max = fChildren.length; i < max; i++) {
			if (matches((CModelStatus)fChildren[i], mask))
				return true;
		}
		return false;
	}

	/**
	 * Helper for matches(int).
	 */
	protected boolean matches(CModelStatus status, int mask) {
		int severityMask = mask & 0x7;
		int categoryMask = mask & ~0x7;
		int bits = status.getBits();
		return ( (severityMask == 0) || (bits & severityMask) != 0) && ( (categoryMask == 0) || (bits & categoryMask) != 0);
	}

	/**
	 * Creates and returns a new <code>ICModelStatus</code> that is a a
	 * multi-status status.
	 * 
	 * @see IStatus#.isMultiStatus()
	 */
	public static ICModelStatus newMultiStatus(ICModelStatus[] children) {
		CModelStatus jms = new CModelStatus();
		jms.fChildren = children;
		return jms;
	}

	/**
	 * Creates and returns a new <code>ICModelStatus</code> that is a a
	 * multi-status status.
	 * 
	 * @see IStatus#.isMultiStatus()
	 */
	public static ICModelStatus newMultiStatus(int code, ICModelStatus[] children) {
		CModelStatus jms = new CModelStatus(code);
		jms.fChildren = children;
		return jms;
	}

	/**
	 * Returns a printable representation of this exception for debugging
	 * purposes.
	 */
	public String toString() {
		if (this == VERIFIED_OK) {
			return "CModelStatus[OK]"; //$NON-NLS-1$
		}
		StringBuffer buffer = new StringBuffer();
		buffer.append("C Model Status ["); //$NON-NLS-1$
		buffer.append(getMessage());
		buffer.append("]"); //$NON-NLS-1$
		return buffer.toString();
	}
}

Back to the top