1 11 package org.eclipse.pde.internal.core.builders; 12 13 import java.net.MalformedURLException ; 14 import java.net.URL ; 15 import java.util.ArrayList ; 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 ; 24 import org.w3c.dom.Element ; 25 import org.w3c.dom.Node ; 26 import org.w3c.dom.NodeList ; 27 import org.w3c.dom.Text ; 28 29 30 public class ManifestErrorReporter extends XMLErrorReporter { 31 32 35 public ManifestErrorReporter(IFile file) { 36 super(file); 37 } 38 39 protected void reportIllegalElement(Element element, int severity) { 40 Node parent = element.getParentNode(); 41 if (parent == null || parent instanceof org.w3c.dom.Document ) { 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 [] { 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 element, String attName, int severity) { 54 String message = NLS.bind(PDECoreMessages.Builders_Manifest_missingRequired, (new String [] { attName, element.getNodeName() })); report(message, getLine(element), severity, PDEMarkerFactory.CAT_FATAL); 56 } 57 58 protected boolean assertAttributeDefined(Element element, String attrName, int severity) { 59 Attr 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 element, String attName, int severity) { 68 String 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 element, Attr attr) { 74 String message = NLS.bind(PDECoreMessages.Builders_Manifest_att_value, (new String [] { attr.getValue(), attr.getName() })); 75 report(message, getLine(element, attr.getName()), CompilerFlags.ERROR, PDEMarkerFactory.CAT_FATAL); 76 } 77 78 protected void validateVersionAttribute(Element element, Attr 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 element, Attr attr) { 85 String value = attr.getValue(); 86 if (!"perfect".equals(value) && !"equivalent".equals(value) && !"greaterOrEqual".equals(value) && !"compatible".equals(value)) reportIllegalAttributeValue(element, attr); 89 } 90 91 protected void validateElementWithContent(Element element, boolean hasContent) { 92 NodeList children = element.getChildNodes(); 93 boolean textFound = false; 94 for (int i = 0; i < children.getLength(); i++) { 95 Node child = children.item(i); 96 if (child instanceof Text ) { 97 textFound = ((Text )child).getNodeValue().trim().length() > 0; 98 } else if (child instanceof Element ) { 99 reportIllegalElement((Element )child, CompilerFlags.ERROR); 100 } 101 } 102 if (!textFound) 103 reportMissingElementContent(element); 104 } 105 106 private void reportMissingElementContent(Element 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 elements, int maximum) { 112 if (elements.getLength() > maximum) { 113 for (int i = maximum; i < elements.getLength(); i++) { 114 Element element = (Element ) 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 element, String attName) { 122 String value = element.getAttribute(attName); 123 try { 124 if (!value.startsWith("http:") && !value.startsWith("file:")) value = "file:" + value; new URL (value); 127 } catch (MalformedURLException e) { 128 report(NLS.bind(PDECoreMessages.Builders_Feature_badURL, attName), getLine(element, attName), CompilerFlags.ERROR, PDEMarkerFactory.CAT_FATAL); 129 } 130 } 131 132 137 protected boolean validatePluginID(Element element, Attr attr) { 138 if (!IdUtil.isValidCompositeID(attr.getValue())) { 139 String 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 element, Attr attr) { 149 String value = attr.getValue(); 150 if (!value.equalsIgnoreCase("true") && !value.equalsIgnoreCase("false")) reportIllegalAttributeValue(element, attr); 152 } 153 154 protected NodeList getChildrenByName(Element element, String name) { 155 class NodeListImpl implements NodeList { 156 ArrayList nodes = new ArrayList (); 157 158 public int getLength() { 159 return nodes.size(); 160 } 161 162 public Node item(int index) { 163 return (Node ) nodes.get(index); 164 } 165 166 protected void add(Node node) { 167 nodes.add(node); 168 } 169 } 170 NodeListImpl list = new NodeListImpl(); 171 NodeList allChildren = element.getChildNodes(); 172 for (int i = 0; i < allChildren.getLength(); i++) { 173 Node 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 element, Attr 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 |