KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005 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.builders;
12
13 import java.util.*;
14
15 import org.eclipse.core.resources.*;
16 import org.eclipse.core.runtime.*;
17 import org.eclipse.osgi.util.NLS;
18 import org.eclipse.pde.core.plugin.*;
19 import org.eclipse.pde.internal.*;
20 import org.eclipse.pde.internal.core.*;
21 import org.eclipse.pde.internal.core.ifeature.*;
22 import org.eclipse.pde.internal.core.util.CoreUtility;
23 import org.w3c.dom.*;
24
25
26 public class FeatureErrorReporter extends ManifestErrorReporter {
27     
28     static HashSet attrs = new HashSet();
29
30     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$
31
"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$
32
"plugin", "application" }; //$NON-NLS-1$ //$NON-NLS-2$
33

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

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

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