KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.core.resources.IFile;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.osgi.util.NLS;
16 import org.eclipse.pde.core.plugin.IPluginModelBase;
17 import org.eclipse.pde.core.plugin.PluginRegistry;
18 import org.eclipse.pde.internal.core.PDECoreMessages;
19 import org.w3c.dom.Attr JavaDoc;
20 import org.w3c.dom.Element JavaDoc;
21 import org.w3c.dom.NodeList JavaDoc;
22
23
24 public abstract class PluginBaseErrorReporter extends ExtensionsErrorReporter {
25
26     public PluginBaseErrorReporter(IFile file) {
27         super(file);
28     }
29     
30     /* (non-Javadoc)
31      * @see org.eclipse.pde.internal.builders.ExtensionsErrorReporter#validateContent(org.eclipse.core.runtime.IProgressMonitor)
32      */

33     public void validateContent(IProgressMonitor monitor) {
34         Element JavaDoc element = getDocumentRoot();
35         if (element == null)
36             return;
37         String JavaDoc elementName = element.getNodeName();
38         if (!getRootElementName().equals(elementName)) {
39             reportIllegalElement(element, CompilerFlags.ERROR);
40         } else {
41             validateTopLevelAttributes(element);
42             NodeList JavaDoc children = element.getChildNodes();
43             for (int i = 0; i < children.getLength(); i++) {
44                 if (monitor.isCanceled())
45                     break;
46                 Element JavaDoc child = (Element JavaDoc)children.item(i);
47                 String JavaDoc name = child.getNodeName();
48                 if (name.equals("extension")) { //$NON-NLS-1$
49
validateExtension(child);
50                 } else if (name.equals("extension-point")) { //$NON-NLS-1$
51
validateExtensionPoint(child);
52                 } else if (name.equals("runtime")){ //$NON-NLS-1$
53
validateRuntime(child);
54                 } else if (name.equals("requires")) { //$NON-NLS-1$
55
validateRequires(child);
56                 } else {
57                     int severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_UNKNOWN_ELEMENT);
58                     if (severity != CompilerFlags.IGNORE)
59                         reportIllegalElement(element, severity);
60                 }
61             }
62         }
63     }
64     
65     protected void validateTopLevelAttributes(Element JavaDoc element) {
66         if (assertAttributeDefined(element, "id", CompilerFlags.ERROR)) { //$NON-NLS-1$
67
validatePluginID(element, element.getAttributeNode("id")); //$NON-NLS-1$
68
}
69         if (assertAttributeDefined(element, "version", CompilerFlags.ERROR)) { //$NON-NLS-1$
70
validateVersionAttribute(element, element.getAttributeNode("version")); //$NON-NLS-1$
71
}
72         if (assertAttributeDefined(element, "name", CompilerFlags.ERROR)) { //$NON-NLS-1$
73
validateTranslatableString(element, element.getAttributeNode("name"), true); //$NON-NLS-1$
74
}
75         Attr JavaDoc attr = element.getAttributeNode("provider-name"); //$NON-NLS-1$
76
if (attr != null)
77             validateTranslatableString(element, attr, true);
78     }
79         
80     protected abstract String JavaDoc getRootElementName();
81
82     protected void validateRequires(Element JavaDoc element) {
83         int severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_UNKNOWN_ELEMENT);
84         NodeList JavaDoc children = element.getChildNodes();
85         for (int i = 0; i < children.getLength(); i++) {
86             Element JavaDoc child = (Element JavaDoc)children.item(i);
87             if (child.getNodeName().equals("import")) { //$NON-NLS-1$
88
validateImport(child);
89             } else if (severity != CompilerFlags.IGNORE) {
90                     reportIllegalElement(child, severity);
91             }
92         }
93     }
94     
95     protected void validateImport(Element JavaDoc element) {
96         if (assertAttributeDefined(element, "plugin", CompilerFlags.ERROR)) { //$NON-NLS-1$
97
validatePluginIDRef(element, element.getAttributeNode("plugin")); //$NON-NLS-1$
98
}
99         Attr JavaDoc attr = element.getAttributeNode("version"); //$NON-NLS-1$
100
if (attr != null)
101             validateVersionAttribute(element, attr);
102         
103         attr = element.getAttributeNode("match"); //$NON-NLS-1$
104
if (attr != null)
105             validateMatch(element, attr);
106         
107         attr = element.getAttributeNode("export"); //$NON-NLS-1$
108
if (attr != null)
109             validateBoolean(element, attr);
110         
111         attr = element.getAttributeNode("optional"); //$NON-NLS-1$
112
if (attr != null)
113             validateBoolean(element, attr);
114     }
115     
116     protected void validateRuntime(Element JavaDoc element) {
117         int severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_UNKNOWN_ELEMENT);
118         NodeList JavaDoc children = element.getChildNodes();
119         for (int i = 0; i < children.getLength(); i++) {
120             Element JavaDoc child = (Element JavaDoc)children.item(i);
121             if (child.getNodeName().equals("library")) { //$NON-NLS-1$
122
validateLibrary(child);
123             } else if (severity != CompilerFlags.IGNORE) {
124                 reportIllegalElement(child, severity);
125             }
126         }
127         
128     }
129     
130     protected void validateLibrary(Element JavaDoc element) {
131         assertAttributeDefined(element, "name", CompilerFlags.ERROR); //$NON-NLS-1$
132

133         int unknownSev = CompilerFlags.getFlag(fProject, CompilerFlags.P_UNKNOWN_ELEMENT);
134         int deprecatedSev = CompilerFlags.getFlag(fProject, CompilerFlags.P_DEPRECATED);
135         NodeList JavaDoc children = element.getChildNodes();
136         for (int i = 0; i < children.getLength(); i++) {
137             Element JavaDoc child = (Element JavaDoc)children.item(i);
138             String JavaDoc elementName = child.getNodeName();
139             if ("export".equals(elementName)) { //$NON-NLS-1$
140
assertAttributeDefined(child, "name", CompilerFlags.ERROR); //$NON-NLS-1$
141
} else if ("packages".equals(elementName)) { //$NON-NLS-1$
142
if (deprecatedSev != CompilerFlags.IGNORE) {
143                     reportDeprecatedElement(child, deprecatedSev);
144                 }
145             } else if (unknownSev != CompilerFlags.IGNORE) {
146                 reportIllegalElement(child, unknownSev);
147             }
148         }
149     }
150     
151     protected void validatePluginIDRef(Element JavaDoc element, Attr JavaDoc attr) {
152         if(!validatePluginID(element, attr)){
153             return;
154         }
155         int severity = CompilerFlags
156                 .getFlag(fProject, CompilerFlags.P_UNRESOLVED_IMPORTS);
157         if ("true".equals(element.getAttribute("optional")) && severity == CompilerFlags.ERROR) //$NON-NLS-1$ //$NON-NLS-2$
158
severity = CompilerFlags.WARNING;
159         if (severity != CompilerFlags.IGNORE) {
160             IPluginModelBase model = PluginRegistry.findModel(attr.getValue());
161             if (model == null || !model.isEnabled()) {
162                 report(NLS.bind(PDECoreMessages.Builders_Manifest_dependency, attr.getValue()),
163                         getLine(element, attr.getName()),
164                         severity,
165                         PDEMarkerFactory.CAT_FATAL);
166             }
167         }
168     }
169     
170     private void reportDeprecatedElement(Element JavaDoc element, int severity) {
171         report(NLS.bind(PDECoreMessages.Builders_Manifest_deprecated_3_0, element.getNodeName()), getLine(element), severity, PDEMarkerFactory.CAT_DEPRECATION);
172     }
173
174 }
175
Popular Tags