KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > builders > FeatureErrorReporter


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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.core.builders;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.HashSet JavaDoc;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.osgi.util.NLS;
19 import org.eclipse.pde.core.plugin.IPluginModelBase;
20 import org.eclipse.pde.core.plugin.PluginRegistry;
21 import org.eclipse.pde.internal.core.PDECore;
22 import org.eclipse.pde.internal.core.PDECoreMessages;
23 import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
24 import org.eclipse.pde.internal.core.util.CoreUtility;
25 import org.w3c.dom.Attr JavaDoc;
26 import org.w3c.dom.Element JavaDoc;
27 import org.w3c.dom.NamedNodeMap JavaDoc;
28 import org.w3c.dom.NodeList JavaDoc;
29
30
31 public class FeatureErrorReporter extends ManifestErrorReporter {
32     
33     static HashSet JavaDoc attrs = new HashSet JavaDoc();
34
35     static String JavaDoc[] attrNames = { "id", "version", "label", "provider-name", "image", "os", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
36
"ws", "arch", "nl", "colocation-affinity", "primary", "exclusive", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
37
"plugin", "application" }; //$NON-NLS-1$ //$NON-NLS-2$
38

39     private IProgressMonitor fMonitor;
40     
41     public FeatureErrorReporter(IFile file) {
42         super(file);
43         if (attrs.isEmpty())
44             attrs.addAll(Arrays.asList(attrNames));
45     }
46     
47     /* (non-Javadoc)
48      * @see org.eclipse.pde.internal.builders.XMLErrorReporter#validateContent(org.eclipse.core.runtime.IProgressMonitor)
49      */

50     public void validateContent(IProgressMonitor monitor) {
51         fMonitor = monitor;
52         Element JavaDoc element = getDocumentRoot();
53         if (element == null)
54             return;
55         String JavaDoc elementName = element.getNodeName();
56         if (!"feature".equals(elementName)) { //$NON-NLS-1$
57
reportIllegalElement(element, CompilerFlags.ERROR);
58         } else {
59             validateFeatureAttributes(element);
60             validateInstallHandler(element);
61             validateDescription(element);
62             validateLicense(element);
63             validateCopyright(element);
64             validateURLElement(element);
65             validateIncludes(element);
66             validateRequires(element);
67             validatePlugins(element);
68             validateData(element);
69         }
70     }
71
72     private void validateData(Element JavaDoc parent) {
73         NodeList JavaDoc list = getChildrenByName(parent, "data"); //$NON-NLS-1$
74
for (int i = 0; i < list.getLength(); i++) {
75             if (fMonitor.isCanceled())
76                 return;
77             Element JavaDoc data = (Element JavaDoc)list.item(i);
78             assertAttributeDefined(data, "id", CompilerFlags.ERROR); //$NON-NLS-1$
79
NamedNodeMap JavaDoc attributes = data.getAttributes();
80             for (int j = 0; j < attributes.getLength(); j++) {
81                 Attr JavaDoc attr = (Attr JavaDoc)attributes.item(j);
82                 String JavaDoc name = attr.getName();
83                 if (!name.equals("id") && !name.equals("os") && !name.equals("ws") //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
84
&& !name.equals("nl") && !name.equals("arch") //$NON-NLS-1$ //$NON-NLS-2$
85
&& !name.equals("download-size") && !name.equals("install-size")) { //$NON-NLS-1$ //$NON-NLS-2$
86
reportUnknownAttribute(data, name, CompilerFlags.ERROR);
87                 }
88             }
89         }
90     }
91
92     /**
93      * @param element
94      */

95     private void validatePlugins(Element JavaDoc parent) {
96         NodeList JavaDoc list = getChildrenByName(parent, "plugin"); //$NON-NLS-1$
97
for (int i = 0; i < list.getLength(); i++) {
98             if (fMonitor.isCanceled())
99                 return;
100             Element JavaDoc plugin = (Element JavaDoc)list.item(i);
101             assertAttributeDefined(plugin, "id", CompilerFlags.ERROR); //$NON-NLS-1$
102
assertAttributeDefined(plugin, "version", CompilerFlags.ERROR); //$NON-NLS-1$
103
NamedNodeMap JavaDoc attributes = plugin.getAttributes();
104             boolean isFragment = plugin.getAttribute("fragment").equals("true"); //$NON-NLS-1$ //$NON-NLS-2$
105
for (int j = 0; j < attributes.getLength(); j++) {
106                 Attr JavaDoc attr = (Attr JavaDoc)attributes.item(j);
107                 String JavaDoc name = attr.getName();
108                 if (name.equals("id")) { //$NON-NLS-1$
109
validatePluginID(plugin, attr, isFragment);
110                 } else if (name.equals("version")) { //$NON-NLS-1$
111
validateVersionAttribute(plugin, attr);
112                 } else if (name.equals("fragment") || name.equals("unpack")) { //$NON-NLS-1$ //$NON-NLS-2$
113
validateBoolean(plugin, attr);
114                 } else if (!name.equals("os") && !name.equals("ws") && !name.equals("nl") //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
115
&& !name.equals("arch") && !name.equals("download-size") //$NON-NLS-1$ //$NON-NLS-2$
116
&& !name.equals("install-size")){ //$NON-NLS-1$
117
reportUnknownAttribute(plugin, name, CompilerFlags.ERROR);
118                 }
119             }
120             validateUnpack(plugin);
121         }
122     }
123
124     private void validateRequires(Element JavaDoc parent) {
125         NodeList JavaDoc list = getChildrenByName(parent, "requires"); //$NON-NLS-1$
126
if (list.getLength() > 0) {
127             validateImports((Element JavaDoc)list.item(0));
128             reportExtraneousElements(list, 1);
129         }
130     }
131     
132     private void validateImports(Element JavaDoc parent) {
133         NodeList JavaDoc list = getChildrenByName(parent, "import"); //$NON-NLS-1$
134
for (int i = 0; i < list.getLength(); i++) {
135             if (fMonitor.isCanceled())
136                 return;
137             Element JavaDoc element = (Element JavaDoc)list.item(i);
138             Attr JavaDoc plugin = element.getAttributeNode("plugin"); //$NON-NLS-1$
139
Attr JavaDoc feature = element.getAttributeNode("feature"); //$NON-NLS-1$
140
if (plugin == null && feature == null) {
141                 assertAttributeDefined(element, "plugin", CompilerFlags.ERROR); //$NON-NLS-1$
142
} else if (plugin != null && feature != null){
143                 reportExclusiveAttributes(element, "plugin", "feature", CompilerFlags.ERROR); //$NON-NLS-1$//$NON-NLS-2$
144
} else if (plugin != null) {
145                 validatePluginID(element, plugin, false);
146             } else if (feature != null) {
147                 validateFeatureID(element, feature);
148             }
149             NamedNodeMap JavaDoc attributes = element.getAttributes();
150             for (int j = 0; j < attributes.getLength(); j++) {
151                 Attr JavaDoc attr = (Attr JavaDoc) attributes.item(j);
152                 String JavaDoc name = attr.getName();
153                 if (name.equals("version")) { //$NON-NLS-1$
154
validateVersionAttribute(element, attr);
155                 } else if (name.equals("match")) { //$NON-NLS-1$
156
if (element.getAttributeNode("patch") != null) { //$NON-NLS-1$
157
report(
158                                 NLS.bind(PDECoreMessages.Builders_Feature_patchedMatch, attr.getValue()),
159                                 getLine(element, attr.getValue()),
160                                 CompilerFlags.ERROR,
161                                 PDEMarkerFactory.CAT_FATAL);
162                     } else {
163                         validateMatch(element, attr);
164                     }
165                 } else if (name.equals("patch")) { //$NON-NLS-1$
166
if ("true".equalsIgnoreCase(attr.getValue()) && feature == null) { //$NON-NLS-1$
167
report(
168                                 NLS.bind(PDECoreMessages.Builders_Feature_patchPlugin, attr.getValue()),
169                                 getLine(element, attr.getValue()),
170                                 CompilerFlags.ERROR,
171                                 PDEMarkerFactory.CAT_FATAL);
172                     } else if ("true".equalsIgnoreCase(attr.getValue()) && element.getAttributeNode("version") == null) { //$NON-NLS-1$ //$NON-NLS-2$
173
report(
174                                 NLS.bind(PDECoreMessages.Builders_Feature_patchedVersion, attr.getValue()),
175                                 getLine(element, attr.getValue()),
176                                 CompilerFlags.ERROR,
177                                 PDEMarkerFactory.CAT_FATAL);
178                     } else {
179                         validateBoolean(element, attr);
180                     }
181                 } else if (!name.equals("plugin") && !name.equals("feature")) { //$NON-NLS-1$ //$NON-NLS-2$
182
reportUnknownAttribute(element, name, CompilerFlags.ERROR);
183                 }
184             }
185             
186         }
187         
188     }
189
190     private void validateIncludes(Element JavaDoc parent) {
191         NodeList JavaDoc list = getChildrenByName(parent, "includes"); //$NON-NLS-1$
192
for (int i = 0; i < list.getLength(); i++) {
193             if (fMonitor.isCanceled())
194                 return;
195             Element JavaDoc include = (Element JavaDoc)list.item(i);
196             if (assertAttributeDefined(include, "id", CompilerFlags.ERROR) //$NON-NLS-1$
197
&& assertAttributeDefined(include, "version", //$NON-NLS-1$
198
CompilerFlags.ERROR)) {
199
200                 validateFeatureID(include, include.getAttributeNode("id")); //$NON-NLS-1$
201
}
202             NamedNodeMap JavaDoc attributes = include.getAttributes();
203             for (int j = 0; j < attributes.getLength(); j++) {
204                 Attr JavaDoc attr = (Attr JavaDoc)attributes.item(j);
205                 String JavaDoc name = attr.getName();
206                 if (name.equals("version")) { //$NON-NLS-1$
207
validateVersionAttribute(include, attr);
208                 } else if (name.equals("optional")) { //$NON-NLS-1$
209
validateBoolean(include, attr);
210                 } else if (name.equals("search-location")) { //$NON-NLS-1$
211
String JavaDoc value = include.getAttribute("search-location"); //$NON-NLS-1$
212
if (!value.equals("root") && !value.equals("self") && !value.equals("both")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
213
reportIllegalAttributeValue(include, attr);
214                     }
215                 } else if (!name.equals("id") && !name.equals("name") && !name.equals("os") && !name.equals("ws") //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
216
&& !name.equals("nl") && !name.equals("arch")) { //$NON-NLS-1$ //$NON-NLS-2$
217
reportUnknownAttribute(include, name, CompilerFlags.ERROR);
218                 }
219             }
220         }
221     }
222
223     private void validateURLElement(Element JavaDoc parent) {
224         NodeList JavaDoc list = getChildrenByName(parent, "url"); //$NON-NLS-1$
225
if (list.getLength() > 0) {
226             Element JavaDoc url = (Element JavaDoc)list.item(0);
227             validateUpdateURL(url);
228             validateDiscoveryURL(url);
229             reportExtraneousElements(list, 1);
230         }
231     }
232     
233     private void validateUpdateURL(Element JavaDoc parent) {
234         NodeList JavaDoc list = getChildrenByName(parent, "update"); //$NON-NLS-1$
235
if (list.getLength() > 0) {
236             if (fMonitor.isCanceled())
237                 return;
238             Element JavaDoc update = (Element JavaDoc)list.item(0);
239             assertAttributeDefined(update, "url", CompilerFlags.ERROR); //$NON-NLS-1$
240
NamedNodeMap JavaDoc attributes = update.getAttributes();
241             for (int i = 0; i < attributes.getLength(); i++) {
242                 String JavaDoc name = attributes.item(i).getNodeName();
243                 if (name.equals("url")) { //$NON-NLS-1$
244
validateURL(update, "url"); //$NON-NLS-1$
245
} else if (!name.equals("label")) { //$NON-NLS-1$
246
reportUnknownAttribute(update, name, CompilerFlags.ERROR);
247                 }
248             }
249             reportExtraneousElements(list, 1);
250         }
251     }
252     
253     private void validateDiscoveryURL(Element JavaDoc parent) {
254         NodeList JavaDoc list = getChildrenByName(parent, "discovery"); //$NON-NLS-1$
255
if (list.getLength() > 0) {
256             if (fMonitor.isCanceled())
257                 return;
258             Element JavaDoc discovery = (Element JavaDoc)list.item(0);
259             assertAttributeDefined(discovery, "url", CompilerFlags.ERROR); //$NON-NLS-1$
260
NamedNodeMap JavaDoc attributes = discovery.getAttributes();
261             for (int i = 0; i < attributes.getLength(); i++) {
262                 String JavaDoc name = attributes.item(i).getNodeName();
263                 if (name.equals("url")) { //$NON-NLS-1$
264
validateURL(discovery, "url"); //$NON-NLS-1$
265
} else if (name.equals("type")) { //$NON-NLS-1$
266
String JavaDoc value = discovery.getAttribute("type"); //$NON-NLS-1$
267
if (!value.equals("web") && !value.equals("update")) { //$NON-NLS-1$ //$NON-NLS-2$
268
reportIllegalAttributeValue(discovery, (Attr JavaDoc)attributes.item(i));
269                     }
270                     reportDeprecatedAttribute(discovery, discovery.getAttributeNode("type")); //$NON-NLS-1$
271
} else if (!name.equals("label")) { //$NON-NLS-1$
272
reportUnknownAttribute(discovery, name, CompilerFlags.ERROR);
273                 }
274             }
275         }
276     }
277     
278     private void validateCopyright(Element JavaDoc parent) {
279         NodeList JavaDoc list = getChildrenByName(parent, "copyright"); //$NON-NLS-1$
280
if (list.getLength() > 0) {
281             if (fMonitor.isCanceled())
282                 return;
283             Element JavaDoc element = (Element JavaDoc)list.item(0);
284             validateElementWithContent((Element JavaDoc)list.item(0), true);
285             NamedNodeMap JavaDoc attributes = element.getAttributes();
286             for (int i = 0; i < attributes.getLength(); i++) {
287                 Attr JavaDoc attr = (Attr JavaDoc)attributes.item(i);
288                 String JavaDoc name = attr.getName();
289                 if (name.equals("url")) { //$NON-NLS-1$
290
validateURL(element, name);
291                 } else {
292                     reportUnknownAttribute(element, name, CompilerFlags.ERROR);
293                 }
294             }
295             reportExtraneousElements(list, 1);
296         }
297     }
298
299     private void validateLicense(Element JavaDoc parent) {
300         NodeList JavaDoc list = getChildrenByName(parent, "license"); //$NON-NLS-1$
301
if (list.getLength() > 0) {
302             if (fMonitor.isCanceled())
303                 return;
304             Element JavaDoc element = (Element JavaDoc)list.item(0);
305             validateElementWithContent((Element JavaDoc)list.item(0), true);
306             NamedNodeMap JavaDoc attributes = element.getAttributes();
307             for (int i = 0; i < attributes.getLength(); i++) {
308                 Attr JavaDoc attr = (Attr JavaDoc)attributes.item(i);
309                 String JavaDoc name = attr.getName();
310                 if (name.equals("url")) { //$NON-NLS-1$
311
validateURL(element, name);
312                 } else {
313                     reportUnknownAttribute(element, name, CompilerFlags.ERROR);
314                 }
315             }
316             reportExtraneousElements(list, 1);
317         }
318     }
319     
320     private void validateDescription(Element JavaDoc parent) {
321         NodeList JavaDoc list = getChildrenByName(parent, "description"); //$NON-NLS-1$
322
if (list.getLength() > 0) {
323             if (fMonitor.isCanceled())
324                 return;
325             Element JavaDoc element = (Element JavaDoc)list.item(0);
326             validateElementWithContent((Element JavaDoc)list.item(0), true);
327             NamedNodeMap JavaDoc attributes = element.getAttributes();
328             for (int i = 0; i < attributes.getLength(); i++) {
329                 Attr JavaDoc attr = (Attr JavaDoc)attributes.item(i);
330                 String JavaDoc name = attr.getName();
331                 if (name.equals("url")) { //$NON-NLS-1$
332
validateURL(element, name);
333                 } else {
334                     reportUnknownAttribute(element, name, CompilerFlags.ERROR);
335                 }
336             }
337             reportExtraneousElements(list, 1);
338         }
339     }
340
341
342     private void validateInstallHandler(Element JavaDoc element) {
343         NodeList JavaDoc elements = getChildrenByName(element, "install-handler"); //$NON-NLS-1$
344
if (elements.getLength() > 0) {
345             if (fMonitor.isCanceled())
346                 return;
347             Element JavaDoc handler = (Element JavaDoc)elements.item(0);
348             NamedNodeMap JavaDoc attributes = handler.getAttributes();
349             for (int i = 0; i < attributes.getLength(); i++) {
350                 String JavaDoc name = attributes.item(i).getNodeName();
351                 if (!name.equals("library") && !name.equals("handler")) //$NON-NLS-1$ //$NON-NLS-2$
352
reportUnknownAttribute(handler, name, CompilerFlags.ERROR);
353             }
354             reportExtraneousElements(elements, 1);
355         }
356     }
357     
358     private void validateFeatureAttributes(Element JavaDoc element) {
359         if (fMonitor.isCanceled())
360             return;
361         assertAttributeDefined(element, "id", CompilerFlags.ERROR); //$NON-NLS-1$
362
assertAttributeDefined(element, "version", CompilerFlags.ERROR); //$NON-NLS-1$
363
NamedNodeMap JavaDoc attributes = element.getAttributes();
364         for (int i = 0; i < attributes.getLength(); i++) {
365             String JavaDoc name = attributes.item(i).getNodeName();
366             if (!attrs.contains(name)) {
367                 reportUnknownAttribute(element, name, CompilerFlags.ERROR);
368             } else if (name.equals("id")){ //$NON-NLS-1$
369
validatePluginID(element, (Attr JavaDoc)attributes.item(i));
370             } else if (name.equals("primary") || name.equals("exclusive")){ //$NON-NLS-1$ //$NON-NLS-2$
371
validateBoolean(element, (Attr JavaDoc)attributes.item(i));
372             } else if (name.equals("version")) { //$NON-NLS-1$
373
validateVersionAttribute(element, (Attr JavaDoc)attributes.item(i));
374             }
375             if (name.equals("primary")){ //$NON-NLS-1$
376
reportDeprecatedAttribute(element, (Attr JavaDoc)attributes.item(i));
377             } else if (name.equals("plugin")){ //$NON-NLS-1$
378
validatePluginID(element, (Attr JavaDoc)attributes.item(i), false);
379             }
380         }
381     }
382     
383     private void validatePluginID(Element JavaDoc element, Attr JavaDoc attr, boolean isFragment) {
384         String JavaDoc id = attr.getValue();
385         if(!validatePluginID(element, attr)){
386             return;
387         }
388         int severity = CompilerFlags.getFlag(fProject, CompilerFlags.F_UNRESOLVED_PLUGINS);
389         if (severity != CompilerFlags.IGNORE) {
390             IPluginModelBase model = PluginRegistry.findModel(id);
391             if (model == null
392                     || !model.isEnabled()
393                     || (isFragment && !model.isFragmentModel())
394                     || (!isFragment && model.isFragmentModel())) {
395                 report(NLS.bind(PDECoreMessages.Builders_Feature_reference, id),
396                         getLine(element, attr.getName()),
397                         severity,
398                         PDEMarkerFactory.CAT_OTHER);
399             }
400         }
401     }
402
403     private void validateFeatureID(Element JavaDoc element, Attr JavaDoc attr) {
404         int severity = CompilerFlags.getFlag(fProject, CompilerFlags.F_UNRESOLVED_FEATURES);
405         if (severity != CompilerFlags.IGNORE) {
406             IFeatureModel[] models = PDECore.getDefault().getFeatureModelManager().findFeatureModels(attr.getValue());
407             if (models.length == 0) {
408                 report(NLS.bind(PDECoreMessages.Builders_Feature_freference, attr.getValue()),
409                         getLine(element, attr.getName()),
410                         severity,
411                         PDEMarkerFactory.CAT_OTHER);
412             }
413         }
414     }
415     protected void reportExclusiveAttributes(Element JavaDoc element, String JavaDoc attName1, String JavaDoc attName2, int severity) {
416         String JavaDoc message = NLS.bind(PDECoreMessages.Builders_Feature_exclusiveAttributes, (new String JavaDoc[] {attName1, attName2}));
417         report(message, getLine(element, attName2), severity, PDEMarkerFactory.CAT_OTHER);
418     }
419
420     private void validateUnpack(Element JavaDoc parent) {
421         int severity = CompilerFlags.getFlag(fProject,
422                 CompilerFlags.F_UNRESOLVED_PLUGINS);
423         if (severity == CompilerFlags.IGNORE) {
424             return;
425         }
426         if( severity == CompilerFlags.ERROR){
427             // this might not be an error, so max the flag at WARNING level.
428
severity = CompilerFlags.WARNING;
429         }
430         String JavaDoc unpack = parent.getAttribute("unpack"); //$NON-NLS-1$
431
if ("false".equals(unpack)) //$NON-NLS-1$
432
return;
433         IPluginModelBase pModel = PluginRegistry.findModel(parent.getAttribute("id")); //$NON-NLS-1$
434
if (pModel == null) {
435             return;
436         }
437         if(!CoreUtility.guessUnpack(pModel.getBundleDescription())){
438                         String JavaDoc message = NLS
439                         .bind(
440                                 PDECoreMessages.Builders_Feature_missingUnpackFalse,
441                                 (new String JavaDoc[] {
442                                         parent.getAttribute("id"), "unpack=\"false\"" })); //$NON-NLS-1$ //$NON-NLS-2$
443
report(message, getLine(parent), severity, PDEMarkerFactory.CAT_OTHER);
444         }
445     }
446
447 }
448
Popular Tags