Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 71357539c7c61ef8913c32d37ce472d3bca887c7 (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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/*******************************************************************************
 * Copyright (c) 2005, 2012 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.pde.internal.core.builders;

import java.io.File;
import java.util.*;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.osgi.util.NLS;
import org.eclipse.pde.core.build.IBuild;
import org.eclipse.pde.core.plugin.*;
import org.eclipse.pde.internal.core.*;
import org.eclipse.pde.internal.core.ischema.*;
import org.eclipse.pde.internal.core.project.PDEProject;
import org.eclipse.pde.internal.core.schema.SchemaRegistry;
import org.eclipse.pde.internal.core.util.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

public class ExtensionsErrorReporter extends ManifestErrorReporter {

	private IPluginModelBase fModel;
	private IBuild fBuildModel;

	public ExtensionsErrorReporter(IFile file) {
		super(file);
		fModel = PluginRegistry.findModel(file.getProject());
		try {
			if (fModel != null && fModel.getUnderlyingResource() != null)
				fBuildModel = ClasspathUtilCore.getBuild(fModel);
		} catch (CoreException e) {
		}
	}

	/**
	 * @throws SAXException  
	 */
	public void characters(char[] characters, int start, int length) throws SAXException {
	}

	public void validateContent(IProgressMonitor monitor) {
		Element element = getDocumentRoot();
		if (element == null)
			return;
		String elementName = element.getNodeName();
		if (!"plugin".equals(elementName) && !"fragment".equals(elementName)) { //$NON-NLS-1$ //$NON-NLS-2$
			reportIllegalElement(element, CompilerFlags.ERROR);
		} else {
			int severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_DEPRECATED);
			if (severity != CompilerFlags.IGNORE) {
				NamedNodeMap attrs = element.getAttributes();
				for (int i = 0; i < attrs.getLength(); i++) {
					reportUnusedAttribute(element, attrs.item(i).getNodeName(), severity);
				}
			}

			NodeList children = element.getChildNodes();
			for (int i = 0; i < children.getLength(); i++) {
				if (monitor.isCanceled())
					break;
				Element child = (Element) children.item(i);
				String name = child.getNodeName();
				if (name.equals("extension")) { //$NON-NLS-1$
					validateExtension(child);
				} else if (name.equals("extension-point")) { //$NON-NLS-1$
					validateExtensionPoint(child);
				} else {
					if (!name.equals("runtime") && !name.equals("requires")) { //$NON-NLS-1$ //$NON-NLS-2$
						severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_UNKNOWN_ELEMENT);
						if (severity != CompilerFlags.IGNORE)
							reportIllegalElement(child, severity);
					} else {
						severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_DEPRECATED);
						if (severity != CompilerFlags.IGNORE)
							reportUnusedElement(child, severity);
					}
				}
			}

			IExtensions extensions = fModel.getExtensions();
			if (extensions != null && extensions.getExtensions().length == 0 && extensions.getExtensionPoints().length == 0)
				report(PDECoreMessages.Builders_Manifest_useless_file, -1, IMarker.SEVERITY_WARNING, PDEMarkerFactory.P_USELESS_FILE, PDEMarkerFactory.CAT_OTHER);
		}
	}

	protected void validateExtension(Element element) {
		if (!assertAttributeDefined(element, "point", CompilerFlags.ERROR)) //$NON-NLS-1$
			return;
		String pointID = element.getAttribute("point"); //$NON-NLS-1$
		if (!PDECore.getDefault().getExtensionsRegistry().hasExtensionPoint(pointID)) {
			int severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_UNRESOLVED_EX_POINTS);
			if (severity != CompilerFlags.IGNORE) {
				report(NLS.bind(PDECoreMessages.Builders_Manifest_ex_point, pointID), getLine(element, "point"), severity, PDEMarkerFactory.CAT_OTHER); //$NON-NLS-1$
			}
		} else {
			SchemaRegistry reg = PDECore.getDefault().getSchemaRegistry();
			ISchema schema = reg.getSchema(pointID);
			if (schema != null) {
				validateElement(element, schema, true);
			}
		}
	}

	/**
	 * @param parentElement
	 * @param childElement
	 * @param severity
	 */
	private void reportMaxOccurenceViolation(ElementOccurrenceResult result, int severity) {
		Element childElement = result.getElement();
		String allowedOccurrences = new Integer(result.getAllowedOccurrences()).toString();
		String message = NLS.bind(PDECoreMessages.ExtensionsErrorReporter_maxOccurrence, new String[] {allowedOccurrences, childElement.getNodeName()});
		report(message, getLine(childElement), severity, PDEMarkerFactory.P_ILLEGAL_XML_NODE, childElement, null, PDEMarkerFactory.CAT_FATAL);
	}

	/**
	 * @param parentElement
	 * @param childElement
	 * @param severity
	 */
	private void reportMinOccurenceViolation(Element parentElement, ElementOccurrenceResult result, int severity) {

		ISchemaElement childElement = result.getSchemaElement();
		String allowedOccurrences = new Integer(result.getAllowedOccurrences()).toString();
		String message = NLS.bind(PDECoreMessages.ExtensionsErrorReporter_minOccurrence, new String[] {allowedOccurrences, childElement.getName()});
		report(message, getLine(parentElement), severity, PDEMarkerFactory.CAT_FATAL);
	}

	protected void validateElement(Element element, ISchema schema, boolean isTopLevel) {
		String elementName = element.getNodeName();
		ISchemaElement schemaElement = schema.findElement(elementName);

		// Validate element occurrence violations
		if ((schemaElement != null) && (schemaElement.getType() instanceof ISchemaComplexType)) {
			validateMaxElementMult(element, schemaElement);
			validateMinElementMult(element, schemaElement);
		}

		ISchemaElement parentSchema = null;
		if (!"extension".equals(elementName)) { //$NON-NLS-1$
			Node parent = element.getParentNode();
			parentSchema = schema.findElement(parent.getNodeName());
		} else if (isTopLevel == false) {
			// This is an "extension" element; but, not a top level one.  
			// It is nested within another "extension" element somewhere
			// e.g. "extension" element is a child element of another element
			// that is not a "plugin" elment 
			// element
			// Report illegal element
			int severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_UNKNOWN_ELEMENT);
			reportIllegalElement(element, severity);
			return;
		}

		if (parentSchema != null) {
			int severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_UNKNOWN_ELEMENT);
			if (severity != CompilerFlags.IGNORE) {
				HashSet<String> allowedElements = new HashSet<String>();
				computeAllowedElements(parentSchema.getType(), allowedElements);
				if (!allowedElements.contains(elementName)) {
					reportIllegalElement(element, severity);
					return;
				}
			}

		}
		if (schemaElement == null && parentSchema != null) {
			ISchemaAttribute attr = parentSchema.getAttribute(elementName);
			if (attr != null && attr.getKind() == IMetaAttribute.JAVA) {
				if (attr.isDeprecated())
					reportDeprecatedAttribute(element, element.getAttributeNode("class")); //$NON-NLS-1$
				validateJavaAttribute(element, element.getAttributeNode("class")); //$NON-NLS-1$				
			}
		} else {
			if (schemaElement != null) {
				validateRequiredExtensionAttributes(element, schemaElement);
				validateExistingExtensionAttributes(element, element.getAttributes(), schemaElement);
				validateInternalExtensionAttribute(element, schemaElement);
				if (schemaElement.isDeprecated()) {
					if (schemaElement instanceof ISchemaRootElement)
						reportDeprecatedRootElement(element, ((ISchemaRootElement) schemaElement).getDeprecatedSuggestion());
					else
						reportDeprecatedElement(element);
				}
				if (schemaElement.hasTranslatableContent())
					validateTranslatableElementContent(element);
				// Bug 213457 - look up elements based on the schema in which the parent is found
				schema = schemaElement.getSchema();
			}
			NodeList children = element.getChildNodes();
			for (int i = 0; i < children.getLength(); i++) {
				validateElement((Element) children.item(i), schema, false);
			}
		}
	}

	private void validateInternalExtensionAttribute(Element element, ISchemaElement schemaElement) {
		int severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_INTERNAL);
		if (severity == CompilerFlags.IGNORE)
			return;

		if (schemaElement instanceof ISchemaRootElement) {
			ISchemaRootElement rootElement = (ISchemaRootElement) schemaElement;
			String epid = schemaElement.getSchema().getPluginId();
			String pid = fModel.getPluginBase().getId();
			if (epid == null || pid == null)
				return;
			if (rootElement.isInternal() && !epid.equals(pid)) {
				String point = element.getAttribute("point"); //$NON-NLS-1$
				if (point == null)
					return; // should never come to this...
				report(NLS.bind(PDECoreMessages.Builders_Manifest_internal_rootElement, point), getLine(element, "point"), severity, PDEMarkerFactory.CAT_DEPRECATION); //$NON-NLS-1$
			}
		}
	}

	/**
	 * @param element
	 * @param schemaElement
	 */
	private void validateMinElementMult(Element element, ISchemaElement schemaElement) {
		// Validate min element occurence violations
		int minSeverity = CompilerFlags.getFlag(fProject, CompilerFlags.P_UNKNOWN_ELEMENT);
		if (minSeverity != CompilerFlags.IGNORE) {
			HashSet<?> minElementSet = ElementOccurenceChecker.findMinOccurenceViolations(schemaElement, element);
			Iterator<?> minIterator = minElementSet.iterator();

			while (minIterator.hasNext()) {
				reportMinOccurenceViolation(element, (ElementOccurrenceResult) minIterator.next(), minSeverity);
			}
		}
	}

	/**
	 * @param element
	 * @param schemaElement
	 */
	private void validateMaxElementMult(Element element, ISchemaElement schemaElement) {
		// Validate max element occurence violations
		int maxSeverity = CompilerFlags.getFlag(fProject, CompilerFlags.P_UNKNOWN_ELEMENT);
		if (maxSeverity != CompilerFlags.IGNORE) {
			HashSet<?> maxElementSet = ElementOccurenceChecker.findMaxOccurenceViolations(schemaElement, element);
			Iterator<?> maxIterator = maxElementSet.iterator();
			while (maxIterator.hasNext()) {
				reportMaxOccurenceViolation((ElementOccurrenceResult) maxIterator.next(), maxSeverity);
			}
		}
	}

	private void computeAllowedElements(ISchemaType type, HashSet<String> elementSet) {
		if (type instanceof ISchemaComplexType) {
			ISchemaComplexType complexType = (ISchemaComplexType) type;
			ISchemaCompositor compositor = complexType.getCompositor();
			if (compositor != null)
				computeAllowedElements(compositor, elementSet);

			ISchemaAttribute[] attrs = complexType.getAttributes();
			for (int i = 0; i < attrs.length; i++) {
				if (attrs[i].getKind() == IMetaAttribute.JAVA)
					elementSet.add(attrs[i].getName());
			}
		}
	}

	private void computeAllowedElements(ISchemaCompositor compositor, HashSet<String> elementSet) {
		ISchemaObject[] children = compositor.getChildren();
		for (int i = 0; i < children.length; i++) {
			ISchemaObject child = children[i];
			if (child instanceof ISchemaObjectReference) {
				ISchemaObjectReference ref = (ISchemaObjectReference) child;
				ISchemaElement refElement = (ISchemaElement) ref.getReferencedObject();
				if (refElement != null)
					elementSet.add(refElement.getName());
			} else if (child instanceof ISchemaCompositor) {
				computeAllowedElements((ISchemaCompositor) child, elementSet);
			}
		}
	}

	private void validateRequiredExtensionAttributes(Element element, ISchemaElement schemaElement) {
		int severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_NO_REQUIRED_ATT);
		if (severity == CompilerFlags.IGNORE)
			return;

		ISchemaAttribute[] attInfos = schemaElement.getAttributes();
		for (int i = 0; i < attInfos.length; i++) {
			ISchemaAttribute attInfo = attInfos[i];
			if (attInfo.getUse() == ISchemaAttribute.REQUIRED) {
				boolean found = element.getAttributeNode(attInfo.getName()) != null;
				if (!found && attInfo.getKind() == IMetaAttribute.JAVA) {
					NodeList children = element.getChildNodes();
					for (int j = 0; j < children.getLength(); j++) {
						if (attInfo.getName().equals(children.item(j).getNodeName())) {
							found = true;
							break;
						}
					}
				}
				if (!found) {
					reportMissingRequiredAttribute(element, attInfo.getName(), severity);
				}
			}
		}
	}

	private void validateExistingExtensionAttributes(Element element, NamedNodeMap attrs, ISchemaElement schemaElement) {
		for (int i = 0; i < attrs.getLength(); i++) {
			Attr attr = (Attr) attrs.item(i);
			ISchemaAttribute attInfo = schemaElement.getAttribute(attr.getName());
			if (attInfo == null) {
				HashSet<String> allowedElements = new HashSet<String>();
				computeAllowedElements(schemaElement.getType(), allowedElements);
				if (allowedElements.contains(attr.getName())) {
					validateJavaAttribute(element, attr);
				} else {
					int flag = CompilerFlags.getFlag(fProject, CompilerFlags.P_UNKNOWN_ATTRIBUTE);
					if (flag != CompilerFlags.IGNORE)
						reportUnknownAttribute(element, attr.getName(), flag);
				}
			} else {
				validateExtensionAttribute(element, attr, attInfo);
			}
		}
	}

	private void validateExtensionAttribute(Element element, Attr attr, ISchemaAttribute attInfo) {
		ISchemaSimpleType type = attInfo.getType();

		int kind = attInfo.getKind();
		if (kind == IMetaAttribute.JAVA) {
			validateJavaAttribute(element, attr);
		} else if (kind == IMetaAttribute.RESOURCE) {
			validateResourceAttribute(element, attr);
		} else if (kind == IMetaAttribute.IDENTIFIER) {
			validateIdentifierAttribute(element, attr, attInfo);
		} else if (kind == IMetaAttribute.STRING) {
			ISchemaRestriction restriction = type.getRestriction();
			if (restriction != null) {
				validateRestrictionAttribute(element, attr, restriction);
			}
		} else if (type.getName().equals("boolean")) { //$NON-NLS-1$
			validateBoolean(element, attr);
		}

		validateTranslatableString(element, attr, attInfo.isTranslatable());

		if (attInfo.isDeprecated()) {
			reportDeprecatedAttribute(element, attr);
		}
	}

	protected void validateExtensionPoint(Element element) {
		if (assertAttributeDefined(element, "id", CompilerFlags.ERROR)) { //$NON-NLS-1$
			Attr idAttr = element.getAttributeNode("id"); //$NON-NLS-1$
			double schemaVersion = getSchemaVersion();
			String message = null;
			if (schemaVersion < 3.2 && !IdUtil.isValidSimpleID(idAttr.getValue()))
				message = NLS.bind(PDECoreMessages.Builders_Manifest_simpleID, idAttr.getValue());
			else if (schemaVersion >= 3.2) {
				if (!IdUtil.isValidCompositeID(idAttr.getValue())) {
					message = NLS.bind(PDECoreMessages.Builders_Manifest_compositeID, idAttr.getValue());
				}
			}

			if (message != null)
				report(message, getLine(element, idAttr.getName()), CompilerFlags.WARNING, PDEMarkerFactory.CAT_OTHER);
		}

		assertAttributeDefined(element, "name", CompilerFlags.ERROR); //$NON-NLS-1$

		int severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_UNKNOWN_ATTRIBUTE);
		NamedNodeMap attrs = element.getAttributes();
		for (int i = 0; i < attrs.getLength(); i++) {
			Attr attr = (Attr) attrs.item(i);
			String name = attr.getName();
			if ("name".equals(name)) { //$NON-NLS-1$
				validateTranslatableString(element, attr, true);
			} else if (!"id".equals(name) && !"schema".equals(name) && severity != CompilerFlags.IGNORE) { //$NON-NLS-1$ //$NON-NLS-2$
				reportUnknownAttribute(element, name, severity);
			}
		}

		severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_UNKNOWN_ELEMENT);
		if (severity != CompilerFlags.IGNORE) {
			NodeList children = element.getChildNodes();
			for (int i = 0; i < children.getLength(); i++)
				reportIllegalElement((Element) children.item(i), severity);
		}

		// Validate the "schema" attribute of the extension point
		Attr attr = element.getAttributeNode(IPluginExtensionPoint.P_SCHEMA);
		// Only validate the attribute if it was defined
		if (attr != null) {
			String schemaValue = attr.getValue();
			IResource res = getFile().getProject().findMember(schemaValue);
			String errorMessage = null;
			// Check to see if the value specified is an extension point schema and it exists
			if (!(res instanceof IFile && (res.getName().endsWith(".exsd") || //$NON-NLS-1$
			res.getName().endsWith(".mxsd")))) //$NON-NLS-1$
				errorMessage = PDECoreMessages.ExtensionsErrorReporter_InvalidSchema;
			// Report an error if one was found
			if (errorMessage != null) {
				severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_UNKNOWN_RESOURCE);
				if (severity != CompilerFlags.IGNORE)
					report(NLS.bind(errorMessage, schemaValue), getLine(element), severity, PDEMarkerFactory.CAT_OTHER);
			}
		}
	}

	protected void validateTranslatableString(Element element, Attr attr, boolean shouldTranslate) {
		if (!shouldTranslate)
			return;
		int severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_NOT_EXTERNALIZED);
		if (severity == CompilerFlags.IGNORE)
			return;
		String value = attr.getValue();
		if (!value.startsWith("%")) { //$NON-NLS-1$
			report(NLS.bind(PDECoreMessages.Builders_Manifest_non_ext_attribute, attr.getName()), getLine(element, attr.getName()), severity, PDEMarkerFactory.P_UNTRANSLATED_NODE, element, attr.getName(), PDEMarkerFactory.CAT_NLS);
		} else if (fModel instanceof AbstractNLModel) {
			NLResourceHelper helper = ((AbstractNLModel) fModel).getNLResourceHelper();
			if (helper == null || !helper.resourceExists(value)) {
				report(NLS.bind(PDECoreMessages.Builders_Manifest_key_not_found, value.substring(1), PDEManager.getBundleLocalization(fModel).concat(".properties")), getLine(element, attr.getName()), severity, PDEMarkerFactory.CAT_NLS); //$NON-NLS-1$
			}
		}
	}

	protected void validateTranslatableElementContent(Element element) {
		int severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_NOT_EXTERNALIZED);
		if (severity == CompilerFlags.IGNORE)
			return;
		String value = getTextContent(element);
		if (value == null)
			return;
		if (!value.startsWith("%")) { //$NON-NLS-1$
			report(NLS.bind(PDECoreMessages.Builders_Manifest_non_ext_element, element.getNodeName()), getLine(element), severity, PDEMarkerFactory.P_UNTRANSLATED_NODE, element, null, PDEMarkerFactory.CAT_NLS);
		} else if (fModel instanceof AbstractNLModel) {
			NLResourceHelper helper = ((AbstractNLModel) fModel).getNLResourceHelper();
			if (helper == null || !helper.resourceExists(value)) {
				report(NLS.bind(PDECoreMessages.Builders_Manifest_key_not_found, value.substring(1), PDEManager.getBundleLocalization(fModel).concat(".properties")), getLine(element), severity, PDEMarkerFactory.CAT_NLS); //$NON-NLS-1$
			}
		}
	}

	protected void validateResourceAttribute(Element element, Attr attr) {
		int severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_UNKNOWN_RESOURCE);
		if (severity != CompilerFlags.IGNORE && !resourceExists(attr.getValue())) {
			report(NLS.bind(PDECoreMessages.Builders_Manifest_resource, (new String[] {attr.getValue(), attr.getName()})), getLine(element, attr.getName()), severity, PDEMarkerFactory.CAT_OTHER);
		}
	}

	private boolean resourceExists(String location) {
		String bundleJar = null;
		IPath path = new Path(location);
		if ("platform:".equals(path.getDevice()) && path.segmentCount() > 2) { //$NON-NLS-1$
			if ("plugin".equals(path.segment(0))) { //$NON-NLS-1$
				String id = path.segment(1);
				IPluginModelBase model = PluginRegistry.findModel(id);
				if (model != null && model.isEnabled()) {
					path = path.setDevice(null).removeFirstSegments(2);
					String bundleLocation = model.getInstallLocation();
					if (new File(bundleLocation).isDirectory()) {
						path = new Path(model.getInstallLocation()).append(path);
					} else {
						bundleJar = bundleLocation;
					}
					location = path.toString();
				}
			}
		} else if (path.getDevice() == null && path.segmentCount() > 3 && "platform:".equals(path.segment(0))) { //$NON-NLS-1$
			if ("plugin".equals(path.segment(1))) { //$NON-NLS-1$
				String id = path.segment(2);
				IPluginModelBase model = PluginRegistry.findModel(id);
				if (model != null && model.isEnabled()) {
					path = path.removeFirstSegments(3);
					String bundleLocation = model.getInstallLocation();
					if (new File(bundleLocation).isDirectory()) {
						path = new Path(model.getInstallLocation()).append(path);
					} else {
						bundleJar = bundleLocation;
					}
					location = path.toString();
				}
			}
		}

		ArrayList<String> paths = new ArrayList<String>();
		if (location.indexOf("$nl$") != -1) { //$NON-NLS-1$
			StringTokenizer tokenizer = new StringTokenizer(TargetPlatform.getNL(), "_"); //$NON-NLS-1$
			String language = tokenizer.hasMoreTokens() ? tokenizer.nextToken() : null;
			String country = tokenizer.hasMoreTokens() ? tokenizer.nextToken() : null;
			if (language != null && country != null)
				paths.add(location.replaceAll("\\$nl\\$", "nl" + IPath.SEPARATOR + language + IPath.SEPARATOR + country)); //$NON-NLS-1$ //$NON-NLS-2$
			if (language != null)
				paths.add(location.replaceAll("\\$nl\\$", "nl" + IPath.SEPARATOR + language)); //$NON-NLS-1$ //$NON-NLS-2$
			paths.add(location.replaceAll("\\$nl\\$", "")); //$NON-NLS-1$ //$NON-NLS-2$
		} else {
			paths.add(location);
		}

		for (int i = 0; i < paths.size(); i++) {
			if (bundleJar == null) {
				IPath currPath = new Path(paths.get(i).toString());
				if (currPath.isAbsolute() && currPath.toFile().exists())
					return true;
				if (PDEProject.getBundleRoot(fFile.getProject()).findMember(currPath) != null)
					return true;
				if (fBuildModel != null && fBuildModel.getEntry("source." + paths.get(i)) != null) //$NON-NLS-1$
					return true;
			} else {
				if (CoreUtility.jarContainsResource(new File(bundleJar), paths.get(i).toString(), false))
					return true;
			}
		}

		return false;
	}

	protected void validateJavaAttribute(Element element, Attr attr) {
		String value = attr.getValue();
		IJavaProject javaProject = JavaCore.create(fFile.getProject());

		// be careful: people have the option to use the format:
		// fullqualifiedName:staticMethod
		int index = value.indexOf(":"); //$NON-NLS-1$
		if (index != -1)
			value = value.substring(0, index);

		// assume we're on the classpath already
		boolean onClasspath = true;
		int severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_UNKNOWN_CLASS);
		if (severity != CompilerFlags.IGNORE && javaProject.isOpen()) {
			onClasspath = PDEJavaHelper.isOnClasspath(value, javaProject);
			if (!onClasspath) {
				report(NLS.bind(PDECoreMessages.Builders_Manifest_class, (new String[] {value, attr.getName()})), getLine(element, attr.getName()), severity, PDEMarkerFactory.P_UNKNOWN_CLASS, element, attr.getName() + F_ATT_VALUE_PREFIX + attr.getValue(), PDEMarkerFactory.CAT_FATAL);
			}
		}

		severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_DISCOURAGED_CLASS);
		if (severity != CompilerFlags.IGNORE && javaProject.isOpen()) {
			BundleDescription desc = fModel.getBundleDescription();
			if (desc == null)
				return;
			// only check if we're discouraged if there is something on the classpath
			if (onClasspath && PDEJavaHelper.isDiscouraged(value, javaProject, desc)) {
				report(NLS.bind(PDECoreMessages.Builders_Manifest_discouragedClass, (new String[] {value, attr.getName()})), getLine(element, attr.getName()), severity, PDEMarkerFactory.M_DISCOURAGED_CLASS, element, attr.getName() + F_ATT_VALUE_PREFIX + attr.getValue(), PDEMarkerFactory.CAT_OTHER);
			}
		}
	}

	protected void validateRestrictionAttribute(Element element, Attr attr, ISchemaRestriction restriction) {
		Object[] children = restriction.getChildren();
		String value = attr.getValue();
		for (int i = 0; i < children.length; i++) {
			Object child = children[i];
			if (child instanceof ISchemaEnumeration) {
				ISchemaEnumeration enumeration = (ISchemaEnumeration) child;
				if (enumeration.getName().equals(value)) {
					return;
				}
			}
		}
		reportIllegalAttributeValue(element, attr);
	}

	private void validateIdentifierAttribute(Element element, Attr attr, ISchemaAttribute attInfo) {
		int severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_UNKNOWN_IDENTIFIER);
		if (severity != CompilerFlags.IGNORE) {
			String value = attr.getValue();
			String basedOn = attInfo.getBasedOn();
			// only validate if we have a valid value and basedOn value
			if (value != null && basedOn != null && value.length() > 0 && basedOn.length() > 0) {
				Map<?, ?> attributes = PDESchemaHelper.getValidAttributes(attInfo);
				if (!attributes.containsKey(value)) { // report error if we are missing something
					report(NLS.bind(PDECoreMessages.ExtensionsErrorReporter_unknownIdentifier, (new String[] {attr.getValue(), attr.getName()})), getLine(element, attr.getName()), severity, PDEMarkerFactory.CAT_OTHER);
				}
			}
		}
	}

	protected void reportUnusedAttribute(Element element, String attName, int severity) {
		String message = NLS.bind(PDECoreMessages.Builders_Manifest_unused_attribute, attName);
		report(message, getLine(element, attName), severity, PDEMarkerFactory.CAT_OTHER);
	}

	protected void reportUnusedElement(Element element, int severity) {
		Node parent = element.getParentNode();
		report(NLS.bind(PDECoreMessages.Builders_Manifest_unused_element, (new String[] {element.getNodeName(), parent.getNodeName()})), getLine(element), severity, PDEMarkerFactory.CAT_OTHER);
	}

	protected void reportDeprecatedElement(Element element) {
		int severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_DEPRECATED);
		if (severity != CompilerFlags.IGNORE) {
			report(NLS.bind(PDECoreMessages.Builders_Manifest_deprecated_element, element.getNodeName()), getLine(element), severity, PDEMarkerFactory.CAT_DEPRECATION);
		}
	}

	protected void reportDeprecatedRootElement(Element element, String suggestion) {
		int severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_DEPRECATED);
		if (severity != CompilerFlags.IGNORE) {
			String point = element.getAttribute("point"); //$NON-NLS-1$
			if (point == null)
				return; // should never come to this...
			String message;
			if (suggestion != null)
				message = NLS.bind(PDECoreMessages.Builders_Manifest_deprecated_rootElementSuggestion, point, suggestion);
			else
				message = NLS.bind(PDECoreMessages.Builders_Manifest_deprecated_rootElement, point);
			report(message, getLine(element, "point"), severity, PDEMarkerFactory.CAT_DEPRECATION); //$NON-NLS-1$
		}
	}
}

Back to the top