1 11 package org.eclipse.pde.internal.builders; 12 13 import java.net.*; 14 import java.util.ArrayList ; 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 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 ) { 36 report(PDEMessages.Builders_Manifest_illegalRoot, getLine(element), severity); } else { 38 report(NLS.bind(PDEMessages.Builders_Manifest_child, (new String [] { element.getNodeName(), parent.getNodeName() })), 40 getLine(element), severity); 41 } 42 } 43 44 protected void reportMissingRequiredAttribute(Element element, String attName, int severity) { 45 String message = NLS.bind(PDEMessages.Builders_Manifest_missingRequired, (new String [] { attName, element.getNodeName() })); report(message, getLine(element), severity); 47 } 48 49 protected boolean assertAttributeDefined(Element element, String 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 attName, int severity) { 59 String 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 message = NLS.bind(PDEMessages.Builders_Manifest_att_value, (new String [] { 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 value = attr.getValue(); 76 if (!"perfect".equals(value) && !"equivalent".equals(value) && !"greaterOrEqual".equals(value) && !"compatible".equals(value)) 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 .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 attName) { 112 String value = element.getAttribute(attName); 113 try { 114 if (!value.startsWith("http:") && !value.startsWith("file:")) value = "file:" + value; new URL(value); 117 } catch (MalformedURLException e) { 118 report(NLS.bind(PDEMessages.Builders_Feature_badURL, attName), getLine(element, attName), CompilerFlags.ERROR); } 120 } 121 122 127 protected boolean validatePluginID(Element element, Attr attr) { 128 if (!IdUtil.isValidPluginId(attr.getValue())) { 129 String 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 value = attr.getValue(); 139 if (!value.equalsIgnoreCase("true") && !value.equalsIgnoreCase("false")) reportIllegalAttributeValue(element, attr); 141 } 142 143 protected NodeList getChildrenByName(Element element, String name) { 144 class NodeListImpl implements NodeList { 145 ArrayList nodes = new ArrayList (); 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 |