1 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 [] attrNames = { "id", "version", "label", "provider-name", "image", "os", "ws", "arch", "nl", "colocation-affinity", "primary", "exclusive", "plugin", "application" }; 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 45 public void validateContent(IProgressMonitor monitor) { 46 fMonitor = monitor; 47 Element element = getDocumentRoot(); 48 if (element == null) 49 return; 50 String elementName = element.getNodeName(); 51 if (!"feature".equals(elementName)) { 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"); 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); NamedNodeMap attributes = data.getAttributes(); 75 for (int j = 0; j < attributes.getLength(); j++) { 76 Attr attr = (Attr)attributes.item(j); 77 String name = attr.getName(); 78 if (!name.equals("id") && !name.equals("os") && !name.equals("ws") && !name.equals("nl") && !name.equals("arch") && !name.equals("download-size") && !name.equals("install-size")) { reportUnknownAttribute(data, name, CompilerFlags.ERROR); 82 } 83 } 84 } 85 } 86 87 90 private void validatePlugins(Element parent) { 91 NodeList list = getChildrenByName(parent, "plugin"); 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); assertAttributeDefined(plugin, "version", CompilerFlags.ERROR); NamedNodeMap attributes = plugin.getAttributes(); 99 boolean isFragment = plugin.getAttribute("fragment").equals("true"); for (int j = 0; j < attributes.getLength(); j++) { 101 Attr attr = (Attr)attributes.item(j); 102 String name = attr.getName(); 103 if (name.equals("id")) { validatePluginID(plugin, attr, isFragment); 105 } else if (name.equals("version")) { validateVersionAttribute(plugin, attr); 107 } else if (name.equals("fragment") || name.equals("unpack")) { validateBoolean(plugin, attr); 109 } else if (!name.equals("os") && !name.equals("ws") && !name.equals("nl") && !name.equals("arch") && !name.equals("download-size") && !name.equals("install-size")){ 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"); 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"); 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"); Attr feature = element.getAttributeNode("feature"); if (plugin == null && feature == null) { 136 assertAttributeDefined(element, "plugin", CompilerFlags.ERROR); } else if (plugin != null && feature != null){ 138 reportExclusiveAttributes(element, "plugin", "feature", CompilerFlags.ERROR); } 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 name = attr.getName(); 148 if (name.equals("version")) { validateVersionAttribute(element, attr); 150 } else if (name.equals("match")) { if (element.getAttributeNode("patch") != null) { report( 153 NLS.bind(PDEMessages.Builders_Feature_patchedMatch, attr.getValue()), getLine(element, attr.getValue()), CompilerFlags.ERROR); 156 } else { 157 validateMatch(element, attr); 158 } 159 } else if (name.equals("patch")) { if ("true".equalsIgnoreCase(attr.getValue()) && feature == null) { report( 162 NLS.bind(PDEMessages.Builders_Feature_patchPlugin, attr.getValue()), getLine(element, attr.getValue()), CompilerFlags.ERROR); 165 } else if ("true".equalsIgnoreCase(attr.getValue()) && element.getAttributeNode("version") == null) { report( 167 NLS.bind(PDEMessages.Builders_Feature_patchedVersion, attr.getValue()), getLine(element, attr.getValue()), CompilerFlags.ERROR); 170 } else { 171 validateBoolean(element, attr); 172 } 173 } else if (!name.equals("plugin") && !name.equals("feature")) { reportUnknownAttribute(element, name, CompilerFlags.ERROR); 175 } 176 } 177 178 } 179 180 } 181 182 private void validateIncludes(Element parent) { 183 NodeList list = getChildrenByName(parent, "includes"); 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) && assertAttributeDefined(include, "version", CompilerFlags.ERROR)) { 191 192 validateFeatureID(include, include.getAttributeNode("id")); } 194 NamedNodeMap attributes = include.getAttributes(); 195 for (int j = 0; j < attributes.getLength(); j++) { 196 Attr attr = (Attr)attributes.item(j); 197 String name = attr.getName(); 198 if (name.equals("version")) { validateVersionAttribute(include, attr); 200 } else if (name.equals("optional")) { validateBoolean(include, attr); 202 } else if (name.equals("search-location")) { String value = include.getAttribute("search-location"); if (!value.equals("root") && !value.equals("self") && !value.equals("both")) { reportIllegalAttributeValue(include, attr); 206 } 207 } else if (!name.equals("id") && !name.equals("name") && !name.equals("os") && !name.equals("ws") && !name.equals("nl") && !name.equals("arch")) { reportUnknownAttribute(include, name, CompilerFlags.ERROR); 210 } 211 } 212 } 213 } 214 215 private void validateURLElement(Element parent) { 216 NodeList list = getChildrenByName(parent, "url"); 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"); if (list.getLength() > 0) { 228 if (fMonitor.isCanceled()) 229 return; 230 Element update = (Element)list.item(0); 231 assertAttributeDefined(update, "url", CompilerFlags.ERROR); NamedNodeMap attributes = update.getAttributes(); 233 for (int i = 0; i < attributes.getLength(); i++) { 234 String name = attributes.item(i).getNodeName(); 235 if (name.equals("url")) { validateURL(update, "url"); } else if (!name.equals("label")) { 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"); if (list.getLength() > 0) { 248 if (fMonitor.isCanceled()) 249 return; 250 Element discovery = (Element)list.item(0); 251 assertAttributeDefined(discovery, "url", CompilerFlags.ERROR); NamedNodeMap attributes = discovery.getAttributes(); 253 for (int i = 0; i < attributes.getLength(); i++) { 254 String name = attributes.item(i).getNodeName(); 255 if (name.equals("url")) { validateURL(discovery, "url"); } else if (name.equals("type")) { String value = discovery.getAttribute("type"); if (!value.equals("web") && !value.equals("update")) { reportIllegalAttributeValue(discovery, (Attr)attributes.item(i)); 261 } 262 reportDeprecatedAttribute(discovery, discovery.getAttributeNode("type")); } else if (!name.equals("label")) { reportUnknownAttribute(discovery, name, CompilerFlags.ERROR); 265 } 266 } 267 } 268 } 269 270 private void validateCopyright(Element parent) { 271 NodeList list = getChildrenByName(parent, "copyright"); 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 name = attr.getName(); 281 if (name.equals("url")) { 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"); 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 name = attr.getName(); 302 if (name.equals("url")) { 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"); 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 name = attr.getName(); 323 if (name.equals("url")) { 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"); 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 name = attributes.item(i).getNodeName(); 343 if (!name.equals("library") && !name.equals("handler")) 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); assertAttributeDefined(element, "version", CompilerFlags.ERROR); NamedNodeMap attributes = element.getAttributes(); 356 for (int i = 0; i < attributes.getLength(); i++) { 357 String name = attributes.item(i).getNodeName(); 358 if (!attrs.contains(name)) { 359 reportUnknownAttribute(element, name, CompilerFlags.ERROR); 360 } else if (name.equals("id")){ validatePluginID(element, (Attr)attributes.item(i)); } else if (name.equals("primary") || name.equals("exclusive")){ validateBoolean(element, (Attr)attributes.item(i)); 364 } else if (name.equals("version")) { validateVersionAttribute(element, (Attr)attributes.item(i)); 366 } 367 if (name.equals("primary")){ reportDeprecatedAttribute(element, (Attr)attributes.item(i)); 369 } else if (name.equals("plugin")){ validatePluginID(element, (Attr)attributes.item(i), false); 371 } 372 } 373 } 374 375 private void validatePluginID(Element element, Attr attr, boolean isFragment) { 376 String 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), 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()), getLine(element, attr.getName()), 401 severity); 402 } 403 } 404 } 405 protected void reportExclusiveAttributes(Element element, String attName1, String attName2, int severity) { 406 String message = NLS.bind(PDEMessages.Builders_Feature_exclusiveAttributes, (new String [] {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 severity = CompilerFlags.WARNING; 419 } 420 String unpack = parent.getAttribute("unpack"); if ("false".equals(unpack)) return; 423 IPluginModel pModel = PDECore.getDefault().getModelManager() 424 .findPluginModel(parent.getAttribute("id")); if (pModel == null) { 426 return; 427 } 428 if(!CoreUtility.guessUnpack(pModel.getBundleDescription())){ 429 String message = NLS 430 .bind( 431 PDEMessages.Builders_Feature_missingUnpackFalse, 432 (new String [] { 433 parent.getAttribute("id"), "unpack=\"false\"" })); report(message, getLine(parent), severity); 435 } 436 } 437 438 } 439 | Popular Tags |