KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.*;
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.core.resources.*;
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.osgi.util.NLS;
19 import org.eclipse.pde.internal.*;
20 import org.eclipse.pde.internal.core.util.IdUtil;
21 import org.w3c.dom.*;
22
23
24 public class ManifestErrorReporter extends XMLErrorReporter {
25
26     /**
27      * @param file
28      */

29     public ManifestErrorReporter(IFile file) {
30         super(file);
31     }
32     
33     protected void reportIllegalElement(Element element, int severity) {
34         Node parent = element.getParentNode();
35         if (parent == null || parent instanceof org.w3c.dom.Document JavaDoc) {
36             report(PDEMessages.Builders_Manifest_illegalRoot, getLine(element), severity); //$NON-NLS-1$
37
} else {
38             report(NLS.bind(PDEMessages.Builders_Manifest_child, (new String JavaDoc[] { //$NON-NLS-1$
39
element.getNodeName(), parent.getNodeName() })),
40                     getLine(element), severity);
41         }
42     }
43     
44     protected void reportMissingRequiredAttribute(Element element, String JavaDoc attName, int severity) {
45         String JavaDoc message = NLS.bind(PDEMessages.Builders_Manifest_missingRequired, (new String JavaDoc[] { attName, element.getNodeName() })); //$NON-NLS-1$
46
report(message, getLine(element), severity);
47     }
48
49     protected boolean assertAttributeDefined(Element element, String JavaDoc attrName, int severity) {
50         Attr attr = element.getAttributeNode(attrName);
51         if (attr == null) {
52             reportMissingRequiredAttribute(element, attrName, severity);
53             return false;
54         }
55         return true;
56     }
57
58     protected void reportUnknownAttribute(Element element, String JavaDoc attName, int severity) {
59         String JavaDoc message = NLS.bind(PDEMessages.Builders_Manifest_attribute, attName);
60         report(message, getLine(element, attName), severity);
61     }
62     
63     protected void reportIllegalAttributeValue(Element element, Attr attr) {
64         String JavaDoc message = NLS.bind(PDEMessages.Builders_Manifest_att_value, (new String JavaDoc[] { attr.getValue(), attr.getName() }));
65         report(message, getLine(element, attr.getName()), CompilerFlags.ERROR);
66     }
67     
68     protected void validateVersionAttribute(Element element, Attr attr) {
69         IStatus status = PluginVersionIdentifier.validateVersion(attr.getValue());
70         if (status.getSeverity() != IStatus.OK)
71             report(status.getMessage(), getLine(element, attr.getName()), CompilerFlags.ERROR);
72     }
73     
74     protected void validateMatch(Element element, Attr attr) {
75         String JavaDoc value = attr.getValue();
76         if (!"perfect".equals(value) && !"equivalent".equals(value) //$NON-NLS-1$ //$NON-NLS-2$
77
&& !"greaterOrEqual".equals(value) && !"compatible".equals(value)) //$NON-NLS-1$ //$NON-NLS-2$
78
reportIllegalAttributeValue(element, attr);
79     }
80
81     protected void validateElementWithContent(Element element, boolean hasContent) {
82         NodeList children = element.getChildNodes();
83         boolean textFound = false;
84         for (int i = 0; i < children.getLength(); i++) {
85             Node child = children.item(i);
86             if (child instanceof Text) {
87                 textFound = ((Text)child).getNodeValue().trim().length() > 0;
88             } else if (child instanceof Element) {
89                 reportIllegalElement((Element)child, CompilerFlags.ERROR);
90             }
91         }
92         if (!textFound)
93             reportMissingElementContent(element);
94     }
95
96     private void reportMissingElementContent(Element element) {
97         report(NLS.bind(PDEMessages.Builders_Feature_empty, element //$NON-NLS-1$
98
.getNodeName()), getLine(element), CompilerFlags.ERROR);
99     }
100     
101     protected void reportExtraneousElements(NodeList elements, int maximum) {
102         if (elements.getLength() > maximum) {
103             for (int i = maximum; i < elements.getLength(); i++) {
104                 Element element = (Element) elements.item(i);
105                 report(NLS.bind(PDEMessages.Builders_Feature_multiplicity, element.getNodeName()), getLine(element),
106                         CompilerFlags.ERROR);
107             }
108         }
109     }
110
111     protected void validateURL(Element element, String JavaDoc attName) {
112         String JavaDoc value = element.getAttribute(attName);
113         try {
114             if (!value.startsWith("http:") && !value.startsWith("file:")) //$NON-NLS-1$ //$NON-NLS-2$
115
value = "file:" + value; //$NON-NLS-1$
116
new URL(value);
117         } catch (MalformedURLException e) {
118             report(NLS.bind(PDEMessages.Builders_Feature_badURL, attName), getLine(element, attName), CompilerFlags.ERROR); //$NON-NLS-1$
119
}
120     }
121     
122     /**
123      * @param element
124      * @param attr
125      * @return false if failed
126      */

127     protected boolean validatePluginID(Element element, Attr attr) {
128         if (!IdUtil.isValidPluginId(attr.getValue())) {
129             String JavaDoc message = NLS.bind(PDEMessages.Builders_Manifest_pluginId_value, attr.getValue(), attr.getName());
130             report(message, getLine(element, attr.getName()),
131                     CompilerFlags.WARNING);
132             return false;
133         }
134         return true;
135     }
136
137     protected void validateBoolean(Element element, Attr attr) {
138         String JavaDoc value = attr.getValue();
139         if (!value.equalsIgnoreCase("true") && !value.equalsIgnoreCase("false")) //$NON-NLS-1$ //$NON-NLS-2$
140
reportIllegalAttributeValue(element, attr);
141     }
142
143     protected NodeList getChildrenByName(Element element, String JavaDoc name) {
144         class NodeListImpl implements NodeList {
145             ArrayList JavaDoc nodes = new ArrayList JavaDoc();
146
147             public int getLength() {
148                 return nodes.size();
149             }
150
151             public Node item(int index) {
152                 return (Node) nodes.get(index);
153             }
154
155             protected void add(Node node) {
156                 nodes.add(node);
157             }
158         }
159         NodeListImpl list = new NodeListImpl();
160         NodeList allChildren = element.getChildNodes();
161         for (int i = 0; i < allChildren.getLength(); i++) {
162             Node node = allChildren.item(i);
163             if (name.equals(node.getNodeName())) {
164                 list.add(node);
165             }
166         }
167         return list;
168     }
169
170     protected void reportDeprecatedAttribute(Element element, Attr attr) {
171         int severity = CompilerFlags.getFlag(fProject, CompilerFlags.P_DEPRECATED);
172         if (severity != CompilerFlags.IGNORE) {
173             report(NLS.bind(PDEMessages.Builders_Manifest_deprecated_attribute, attr.getName()), getLine(element, attr.getName()), severity);
174         }
175     }
176 }
177
Popular Tags