KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.net.MalformedURLException JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.ArrayList JavaDoc;
16
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.osgi.util.NLS;
20 import org.eclipse.pde.internal.core.PDECoreMessages;
21 import org.eclipse.pde.internal.core.util.IdUtil;
22 import org.eclipse.pde.internal.core.util.VersionUtil;
23 import org.w3c.dom.Attr JavaDoc;
24 import org.w3c.dom.Element JavaDoc;
25 import org.w3c.dom.Node JavaDoc;
26 import org.w3c.dom.NodeList JavaDoc;
27 import org.w3c.dom.Text JavaDoc;
28
29
30 public class ManifestErrorReporter extends XMLErrorReporter {
31
32     /**
33      * @param file
34      */

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

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