From 7b86df0b12fbf72832cfdd796d2e8409437a71f1 Mon Sep 17 00:00:00 2001 From: Hannes Wellmann Date: Thu, 20 Jul 2023 19:58:54 +0200 Subject: [PATCH] Unify XMLErrorReporter/DefaultSAXParser classes and show errors --- .../internal/ds/core/builders/DSBuilder.java | 4 +- .../ds/core/builders/DSErrorReporter.java | 44 ++- .../ds/core/builders/DSMarkerFactory.java | 45 --- .../ds/core/builders/DefaultSAXParser.java | 41 -- .../ds/core/builders/XMLErrorReporter.java | 365 ------------------ .../builders/ExtensionPointSchemaBuilder.java | 2 +- .../core/builders/UpdateSiteBuilder.java | 2 +- .../core/builders/XMLErrorReporter.java | 49 +-- 8 files changed, 38 insertions(+), 514 deletions(-) delete mode 100644 ds/org.eclipse.pde.ds.core/src/org/eclipse/pde/internal/ds/core/builders/DSMarkerFactory.java delete mode 100644 ds/org.eclipse.pde.ds.core/src/org/eclipse/pde/internal/ds/core/builders/DefaultSAXParser.java delete mode 100644 ds/org.eclipse.pde.ds.core/src/org/eclipse/pde/internal/ds/core/builders/XMLErrorReporter.java diff --git a/ds/org.eclipse.pde.ds.core/src/org/eclipse/pde/internal/ds/core/builders/DSBuilder.java b/ds/org.eclipse.pde.ds.core/src/org/eclipse/pde/internal/ds/core/builders/DSBuilder.java index 2d69d1cb48..eb18e51471 100644 --- a/ds/org.eclipse.pde.ds.core/src/org/eclipse/pde/internal/ds/core/builders/DSBuilder.java +++ b/ds/org.eclipse.pde.ds.core/src/org/eclipse/pde/internal/ds/core/builders/DSBuilder.java @@ -33,7 +33,9 @@ import org.eclipse.core.runtime.jobs.ISchedulingRule; import org.eclipse.core.runtime.jobs.MultiRule; import org.eclipse.osgi.util.NLS; +import org.eclipse.pde.internal.core.builders.DefaultSAXParser; import org.eclipse.pde.internal.core.builders.PDEBuilderHelper; +import org.eclipse.pde.internal.core.builders.PDEMarkerFactory; import org.eclipse.pde.internal.ds.core.Activator; import org.eclipse.pde.internal.ds.core.Messages; @@ -130,7 +132,7 @@ protected IProject[] build(int kind, Map args, IProgressMonitor @Override protected void clean(IProgressMonitor monitor) throws CoreException { // bug 426874 - delete markers set and files created - getProject().deleteMarkers(DSMarkerFactory.MARKER_ID, true, IResource.DEPTH_INFINITE); + getProject().deleteMarkers(PDEMarkerFactory.MARKER_ID, true, IResource.DEPTH_INFINITE); } diff --git a/ds/org.eclipse.pde.ds.core/src/org/eclipse/pde/internal/ds/core/builders/DSErrorReporter.java b/ds/org.eclipse.pde.ds.core/src/org/eclipse/pde/internal/ds/core/builders/DSErrorReporter.java index c9b781141b..a56ffafedc 100644 --- a/ds/org.eclipse.pde.ds.core/src/org/eclipse/pde/internal/ds/core/builders/DSErrorReporter.java +++ b/ds/org.eclipse.pde.ds.core/src/org/eclipse/pde/internal/ds/core/builders/DSErrorReporter.java @@ -27,6 +27,8 @@ import org.eclipse.jdt.core.JavaCore; import org.eclipse.jface.text.Document; import org.eclipse.osgi.util.NLS; +import org.eclipse.pde.internal.core.builders.PDEMarkerFactory; +import org.eclipse.pde.internal.core.builders.XMLErrorReporter; import org.eclipse.pde.internal.core.text.IDocumentElementNode; import org.eclipse.pde.internal.core.util.CoreUtility; import org.eclipse.pde.internal.ds.core.Activator; @@ -56,7 +58,7 @@ public DSErrorReporter(IFile file) { } @Override - public void validateContent(IProgressMonitor monitor) { + public void validate(IProgressMonitor monitor) { try { Document textDocument = CoreUtility.getTextDocument(fFile @@ -93,7 +95,7 @@ private void reportIllegalAttributeValue(Element element, Attr attr) { String message = NLS.bind(Messages.DSErrorReporter_attrValue, attr .getValue(), attr.getName()); report(message, getLine(element, attr.getName()), ERROR, - DSMarkerFactory.CAT_OTHER); + PDEMarkerFactory.CAT_OTHER); } private void validateReferenceElements(IDSReference[] references) { @@ -147,7 +149,7 @@ private void reportInvalidTarget(Element element, String target) { .getAttribute(IDSConstants.ATTRIBUTE_REFERENCE_NAME); String message = NLS.bind(Messages.DSErrorReporter_invalidTarget, name, target); - report(message, getLine(element), ERROR, DSMarkerFactory.CAT_OTHER); + report(message, getLine(element), ERROR, PDEMarkerFactory.CAT_OTHER); } private void validateReferenceElementNames(Hashtable referencedNames, @@ -171,7 +173,7 @@ private void reportDuplicateReferenceElementName(Element element, Messages.DSErrorReporter_duplicateReferenceName, name); report(message, getLine(element, IDSConstants.ATTRIBUTE_REFERENCE_NAME), ERROR, - DSMarkerFactory.CAT_OTHER); + PDEMarkerFactory.CAT_OTHER); } private void validateReferencePolicy(Element element) { @@ -203,7 +205,7 @@ private void reportIllegalPolicy(Element element, String policy) { Attr attr = element .getAttributeNode(IDSConstants.ATTRIBUTE_REFERENCE_POLICY); report(message, getLine(element, attr.getName()), ERROR, - DSMarkerFactory.CAT_OTHER); + PDEMarkerFactory.CAT_OTHER); } private void validateReferenceCardinality(Element element) { @@ -242,7 +244,7 @@ private void reportIllegalCardinality(Element element, String cardinality) { if (attr == null || attr.getValue() == null || attr.getName() == null) return; report(message, getLine(element, attr.getName()), ERROR, - DSMarkerFactory.CAT_OTHER); + PDEMarkerFactory.CAT_OTHER); } private void validatePropertiesElements(IDSProperties[] propertiesElements) { @@ -261,7 +263,7 @@ private void validatePropertiesElements(IDSProperties[] propertiesElements) { .bind( Messages.DSErrorReporter_cannotFindProperties, properties.getEntry()), getLine(element), WARNING, - DSMarkerFactory.CAT_OTHER); + PDEMarkerFactory.CAT_OTHER); } } @@ -368,7 +370,7 @@ private void reportPropertyTypeCastException(Element element, String value, String message = NLS.bind( Messages.DSErrorReporter_propertyTypeCastException, new String[] { value, type }); - report(message, getLine(element), WARNING, DSMarkerFactory.CAT_OTHER); + report(message, getLine(element), WARNING, PDEMarkerFactory.CAT_OTHER); } /** @@ -409,7 +411,7 @@ private void validatePropertyAttrValueAndBody(Element element, private void reportEmptyPropertyValue(Element element, String propertyName) { String message = NLS.bind(Messages.DSErrorReporter_emptyPropertyValue, propertyName); - report(message, getLine(element), WARNING, DSMarkerFactory.CAT_OTHER); + report(message, getLine(element), WARNING, PDEMarkerFactory.CAT_OTHER); } @@ -418,7 +420,7 @@ private void reportSingleAndMultiplePropertyValues(Element element, String message = NLS.bind( Messages.DSErrorReporter_singleAndMultipleAttrValue, propertyName, value); - report(message, getLine(element), WARNING, DSMarkerFactory.CAT_OTHER); + report(message, getLine(element), WARNING, PDEMarkerFactory.CAT_OTHER); } private void validatePropertyTypes(Element element) { @@ -485,7 +487,7 @@ private void reportMissingRequiredAttribute(Element element, String attName, int severity) { String message = NLS.bind(Messages.DSErrorReporter_requiredAttribute, attName, element.getNodeName()); - report(message, getLine(element), severity, DSMarkerFactory.CAT_OTHER); + report(message, getLine(element), severity, PDEMarkerFactory.CAT_OTHER); } /** @@ -507,7 +509,7 @@ private void reportJavaTypeNotFound(String elementConstant, Element element = (Element) elementsByTagName.item(index); report(NLS.bind(Messages.DSErrorReporter_cannotFindJavaType, resource, attributeConstant), getLine(element), WARNING, - DSMarkerFactory.CAT_OTHER); + PDEMarkerFactory.CAT_OTHER); } private void validateComponentElement(IDSComponent component) { @@ -518,7 +520,7 @@ private void validateComponentElement(IDSComponent component) { report(NLS.bind(Messages.DSErrorReporter_requiredElement, IDSConstants.ELEMENT_IMPLEMENTATION), getLine(getDocumentRoot()), ERROR, - DSMarkerFactory.CAT_OTHER); + PDEMarkerFactory.CAT_OTHER); } // validate boolean values @@ -556,7 +558,7 @@ private void validateConfigurationPolicyAttribute(Element element, report( Messages.DSErrorReporter_invalidConfigurationPolicyValue, getLine(element), WARNING, - DSMarkerFactory.CAT_OTHER); + PDEMarkerFactory.CAT_OTHER); } } } @@ -589,13 +591,13 @@ private void validateImmediateAttribute(Element element, private void reportInvalidImmediateFactory(Element element) { report(Messages.DSErrorReporter_invalidImmediateValueFactory, - getLine(element), WARNING, DSMarkerFactory.CAT_OTHER); + getLine(element), WARNING, PDEMarkerFactory.CAT_OTHER); } private void reportInvalidImmediate(Element element) { report(Messages.DSErrorReporter_invalidImmediateValue, - getLine(element), WARNING, DSMarkerFactory.CAT_OTHER); + getLine(element), WARNING, PDEMarkerFactory.CAT_OTHER); } private void validateEmpty(Element element, Attr attr) { @@ -612,7 +614,7 @@ private void reportIllegalEmptyAttributeValue(Element element, Attr attr) { String message = NLS.bind(Messages.DSErrorReporter_emptyAttrValue, attr .getName()); report(message, getLine(element, attr.getName()), ERROR, - DSMarkerFactory.CAT_OTHER); + PDEMarkerFactory.CAT_OTHER); } private void validateServiceElement(IDSService service) { @@ -635,7 +637,7 @@ private void validateServiceElement(IDSService service) { private void reportEmptyService(Element element) { report(Messages.DSErrorReporter_illegalEmptyService, getLine(element), - ERROR, DSMarkerFactory.CAT_OTHER); + ERROR, PDEMarkerFactory.CAT_OTHER); } private void validateServiceFactory(Element element, IDSService service) { @@ -658,14 +660,14 @@ private void validateServiceFactory(Element element, IDSService service) { private void reportIllegalServiceFactory_Immediate(Element element) { report(Messages.DSErrorReporter_illegalServiceFactory_Immediate, - getLine(element), ERROR, DSMarkerFactory.CAT_OTHER); + getLine(element), ERROR, PDEMarkerFactory.CAT_OTHER); } private void reportIllegalServiceFactory(Element element) { report(Messages.DSErrorReporter_illegalServiceFactory, - getLine(element), ERROR, DSMarkerFactory.CAT_OTHER); + getLine(element), ERROR, PDEMarkerFactory.CAT_OTHER); } private void validateProvideElement(IDSProvide[] providedServices) { @@ -702,7 +704,7 @@ private void validateDuplicateInterface(Hashtable providedInterf String message = NLS.bind( Messages.DSErrorReporter_duplicatedInterface, interface1); report(message, getLine(element), WARNING, - DSMarkerFactory.CAT_OTHER); + PDEMarkerFactory.CAT_OTHER); } else { providedInterfaces.put(interface1, interface1); } diff --git a/ds/org.eclipse.pde.ds.core/src/org/eclipse/pde/internal/ds/core/builders/DSMarkerFactory.java b/ds/org.eclipse.pde.ds.core/src/org/eclipse/pde/internal/ds/core/builders/DSMarkerFactory.java deleted file mode 100644 index 16919d6d75..0000000000 --- a/ds/org.eclipse.pde.ds.core/src/org/eclipse/pde/internal/ds/core/builders/DSMarkerFactory.java +++ /dev/null @@ -1,45 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2008 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Rafael Oliveira Nóbrega - bug 230232 - *******************************************************************************/ -package org.eclipse.pde.internal.ds.core.builders; - -import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.IMarker; -import org.eclipse.core.runtime.CoreException; - -public class DSMarkerFactory { - - public static final String MARKER_ID = "org.eclipse.pde.ds.core.problem"; //$NON-NLS-1$ - public static final int NO_RESOLUTION = -1; - - public static final String CAT_OTHER = ""; //$NON-NLS-1$ - - /** - * @param file - * @return - * @throws CoreException - */ - public IMarker createMarker(IFile file) throws CoreException { - return createMarker(file, NO_RESOLUTION, ""); //$NON-NLS-1$ - } - - public IMarker createMarker(IFile file, int id, String category) - throws CoreException { - IMarker marker = file.createMarker(MARKER_ID); - marker.setAttribute("id", id); //$NON-NLS-1$ - marker.setAttribute("categoryId", category); //$NON-NLS-1$ - return marker; - } - -} diff --git a/ds/org.eclipse.pde.ds.core/src/org/eclipse/pde/internal/ds/core/builders/DefaultSAXParser.java b/ds/org.eclipse.pde.ds.core/src/org/eclipse/pde/internal/ds/core/builders/DefaultSAXParser.java deleted file mode 100644 index 7732776cae..0000000000 --- a/ds/org.eclipse.pde.ds.core/src/org/eclipse/pde/internal/ds/core/builders/DefaultSAXParser.java +++ /dev/null @@ -1,41 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2008 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Rafael Oliveira N√≥brega - bug 230232 - *******************************************************************************/ - -package org.eclipse.pde.internal.ds.core.builders; - -import java.io.BufferedInputStream; -import java.io.IOException; -import java.io.InputStream; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.parsers.SAXParser; - -import org.eclipse.core.resources.IFile; -import org.eclipse.core.runtime.CoreException; -import org.xml.sax.SAXException; - -public class DefaultSAXParser { - - public static void parse(IFile file, XMLErrorReporter reporter) { - try (InputStream stream = new BufferedInputStream(file.getContents())) { - @SuppressWarnings("restriction") - SAXParser parser = org.eclipse.core.internal.runtime.XmlProcessorFactory - .createSAXParserWithErrorOnDOCTYPE(); - parser.parse(stream, - reporter); - } catch (CoreException | SAXException | IOException | ParserConfigurationException e) { - } - } -} diff --git a/ds/org.eclipse.pde.ds.core/src/org/eclipse/pde/internal/ds/core/builders/XMLErrorReporter.java b/ds/org.eclipse.pde.ds.core/src/org/eclipse/pde/internal/ds/core/builders/XMLErrorReporter.java deleted file mode 100644 index cb464d8ebe..0000000000 --- a/ds/org.eclipse.pde.ds.core/src/org/eclipse/pde/internal/ds/core/builders/XMLErrorReporter.java +++ /dev/null @@ -1,365 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2008, 2015 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package org.eclipse.pde.internal.ds.core.builders; - -import java.io.StringReader; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Stack; - -import javax.xml.parsers.ParserConfigurationException; - -import org.eclipse.core.filebuffers.FileBuffers; -import org.eclipse.core.filebuffers.ITextFileBufferManager; -import org.eclipse.core.filebuffers.LocationKind; -import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.IMarker; -import org.eclipse.core.resources.IProject; -import org.eclipse.core.resources.IResource; -import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.jface.text.BadLocationException; -import org.eclipse.jface.text.FindReplaceDocumentAdapter; -import org.eclipse.jface.text.IDocument; -import org.eclipse.jface.text.IRegion; -import org.eclipse.jface.text.Position; -import org.eclipse.pde.internal.core.builders.CompilerFlags; -import org.w3c.dom.Element; -import org.w3c.dom.Text; -import org.xml.sax.Attributes; -import org.xml.sax.InputSource; -import org.xml.sax.Locator; -import org.xml.sax.SAXException; -import org.xml.sax.SAXParseException; -import org.xml.sax.helpers.DefaultHandler; - -public class XMLErrorReporter extends DefaultHandler { - - public static final char F_ATT_PREFIX = '@'; - public static final char F_ATT_VALUE_PREFIX = '!'; - public static final char F_CHILD_SEP = '>'; - - static class ElementData { - int offset; - boolean fErrorNode; - - public ElementData(int offset) { - this.offset = offset; - } - } - - protected IFile fFile; - protected IProject fProject; - private int fErrorCount; - private DSMarkerFactory fMarkerFactory; - private org.w3c.dom.Document fXMLDocument; - private IDocument fTextDocument; - private Stack fElementStack; - private Element fRootElement; - private Locator fLocator; - private int fHighestOffset; - private HashMap fOffsetTable; - private FindReplaceDocumentAdapter fFindReplaceAdapter; - - public XMLErrorReporter(IFile file) { - ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager(); - try { - fFile = file; - fProject = file.getProject(); - manager.connect(file.getFullPath(), LocationKind.NORMALIZE, null); - fTextDocument = manager.getTextFileBuffer(file.getFullPath(), LocationKind.NORMALIZE).getDocument(); - manager.disconnect(file.getFullPath(), LocationKind.NORMALIZE, null); - fFindReplaceAdapter = new FindReplaceDocumentAdapter(fTextDocument); - fOffsetTable = new HashMap<>(); - fElementStack = new Stack<>(); - removeFileMarkers(); - } catch (CoreException e) { - // TODO log message - } - } - - public IFile getFile() { - return fFile; - } - - private IMarker addMarker(String message, int lineNumber, int severity, int fixId, String category) { - try { - IMarker marker = getMarkerFactory().createMarker(fFile, fixId, category); - marker.setAttribute(IMarker.MESSAGE, message); - marker.setAttribute(IMarker.SEVERITY, severity); - if (lineNumber == -1) - lineNumber = 1; - marker.setAttribute(IMarker.LINE_NUMBER, lineNumber); - if (severity == IMarker.SEVERITY_ERROR) - fErrorCount += 1; - return marker; - } catch (CoreException e) { - // TODO log something - } - return null; - } - - private DSMarkerFactory getMarkerFactory() { - if (fMarkerFactory == null) - fMarkerFactory = new DSMarkerFactory(); - return fMarkerFactory; - } - - private void addMarker(SAXParseException e, int severity) { - addMarker(e.getMessage(), e.getLineNumber(), severity, - DSMarkerFactory.NO_RESOLUTION, DSMarkerFactory.CAT_OTHER); - } - - @Override - public void error(SAXParseException exception) throws SAXException { - addMarker(exception, IMarker.SEVERITY_ERROR); - generateErrorElementHierarchy(); - } - - @Override - public void fatalError(SAXParseException exception) throws SAXException { - addMarker(exception, IMarker.SEVERITY_ERROR); - generateErrorElementHierarchy(); - } - - public int getErrorCount() { - return fErrorCount; - } - - private void removeFileMarkers() { - try { - fFile.deleteMarkers(IMarker.PROBLEM, false, IResource.DEPTH_ZERO); - fFile.deleteMarkers(DSMarkerFactory.MARKER_ID, false, IResource.DEPTH_ZERO); - } catch (CoreException e) { - // TODO log exception - } - } - - - public IMarker report(String message, int line, int severity, int fixId, String category) { - if (severity == CompilerFlags.ERROR) - return addMarker(message, line, IMarker.SEVERITY_ERROR, fixId, category); - if (severity == CompilerFlags.WARNING) - return addMarker(message, line, IMarker.SEVERITY_WARNING, fixId, category); - return null; - } - - public IMarker report(String message, int line, int severity, String category) { - return report(message, line, severity, DSMarkerFactory.NO_RESOLUTION, - category); - } - - @Override - public void warning(SAXParseException exception) throws SAXException { - addMarker(exception, IMarker.SEVERITY_WARNING); - } - - @Override - @SuppressWarnings("restriction") - public void startDocument() throws SAXException { - try { - // TODO we should be using a dom level 2 impl - fXMLDocument = org.eclipse.core.internal.runtime.XmlProcessorFactory - .createDocumentBuilderWithErrorOnDOCTYPE().newDocument(); - } catch (ParserConfigurationException e) { - } - } - - @Override - public void endDocument() throws SAXException { - fXMLDocument.appendChild(fRootElement); - } - - @Override - public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { - Element element = fXMLDocument.createElement(qName); - for (int i = 0; i < attributes.getLength(); i++) { - element.setAttribute(attributes.getQName(i), attributes.getValue(i)); - } - - if (fRootElement == null) - fRootElement = element; - else - fElementStack.peek().appendChild(element); - fElementStack.push(element); - try { - if (fTextDocument != null) - fOffsetTable.put(element, new ElementData(getStartOffset(qName))); - } catch (BadLocationException e) { - } - } - - @Override - public void endElement(String uri, String localName, String qName) throws SAXException { - fElementStack.pop(); - } - - private void generateErrorElementHierarchy() { - while (!fElementStack.isEmpty()) { - ElementData data = fOffsetTable.get(fElementStack.pop()); - if (data != null) - data.fErrorNode = true; - } - } - - @Override - public void characters(char[] characters, int start, int length) throws SAXException { - StringBuilder buff = new StringBuilder(); - for (int i = 0; i < length; i++) { - buff.append(characters[start + i]); - } - Text text = fXMLDocument.createTextNode(buff.toString()); - if (fRootElement == null) - fXMLDocument.appendChild(text); - else - fElementStack.peek().appendChild(text); - } - - @Override - public void setDocumentLocator(Locator locator) { - fLocator = locator; - } - - private int getStartOffset(String elementName) throws BadLocationException { - int line = fLocator.getLineNumber(); - int col = fLocator.getColumnNumber(); - if (col < 0) - col = fTextDocument.getLineLength(line); - String text = fTextDocument.get(fHighestOffset + 1, fTextDocument.getLineOffset(line) - fHighestOffset - 1); - - ArrayList commentPositions = new ArrayList<>(); - for (int idx = 0; idx < text.length();) { - idx = text.indexOf("", idx); //$NON-NLS-1$ - if (end == -1) - break; - - commentPositions.add(new Position(idx, end - idx)); - idx = end + 1; - } - - int idx = 0; - for (; idx < text.length(); idx += 1) { - idx = text.indexOf("<" + elementName, idx); //$NON-NLS-1$ - if (idx == -1) - break; - boolean valid = true; - for (int i = 0; i < commentPositions.size(); i++) { - Position pos = commentPositions.get(i); - if (pos.includes(idx)) { - valid = false; - break; - } - } - if (valid) - break; - } - if (idx > -1) - fHighestOffset += idx + 1; - return fHighestOffset; - } - - private int getAttributeOffset(String name, String value, int offset) throws BadLocationException { - IRegion nameRegion = fFindReplaceAdapter.find(offset, name + "=\"" + getWritableString(value), true, false, false, false); //$NON-NLS-1$ - if (nameRegion != null) { - return nameRegion.getOffset(); - } - return -1; - } - - private String getWritableString(String source) { - StringBuilder buf = new StringBuilder(); - for (int i = 0; i < source.length(); i++) { - char c = source.charAt(i); - switch (c) { - case '&' : - buf.append("&"); //$NON-NLS-1$ - break; - case '<' : - buf.append("<"); //$NON-NLS-1$ - break; - case '>' : - buf.append(">"); //$NON-NLS-1$ - break; - case '\'' : - buf.append("'"); //$NON-NLS-1$ - break; - case '\"' : - buf.append("""); //$NON-NLS-1$ - break; - default : - buf.append(c); - break; - } - } - return buf.toString(); - } - - protected String getTextContent(Element element) { - ElementData data = fOffsetTable.get(element); - try { - IRegion nameRegion = fFindReplaceAdapter.find(data.offset, "", true, true, false, false); //$NON-NLS-1$ //$NON-NLS-2$ - int offset = data.offset + element.getNodeName().length() + 2; - if (nameRegion != null) - return fTextDocument.get(offset, nameRegion.getOffset() - offset).trim(); - } catch (BadLocationException e) { - } - return null; - } - - protected int getLine(Element element) { - ElementData data = fOffsetTable.get(element); - try { - return (data == null) ? 1 : fTextDocument.getLineOfOffset(data.offset) + 1; - } catch (Exception e) { - return 1; - } - } - - protected int getLine(Element element, String attName) { - ElementData data = fOffsetTable.get(element); - try { - int offset = getAttributeOffset(attName, element.getAttribute(attName), data.offset); - if (offset != -1) - return fTextDocument.getLineOfOffset(offset) + 1; - } catch (BadLocationException e) { - } - return getLine(element); - } - - public void validateContent(IProgressMonitor monitor) { - - } - - public Element getDocumentRoot() { - if (fRootElement != null) - fRootElement.normalize(); - return fRootElement; - } - - @Override - public InputSource resolveEntity(String publicId, String systemId) throws SAXException { - int x = fTextDocument.get().indexOf("!DOCTYPE"); //$NON-NLS-1$ - if (x > 0) { - // do something? - } - // Prevent the resolution of external entities in order to - // prevent the parser from accessing the Internet - // This will prevent huge workbench performance degradations and hangs - return new InputSource(new StringReader("")); //$NON-NLS-1$ - } - -} diff --git a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/builders/ExtensionPointSchemaBuilder.java b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/builders/ExtensionPointSchemaBuilder.java index a381b6a098..92c27ebaa2 100644 --- a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/builders/ExtensionPointSchemaBuilder.java +++ b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/builders/ExtensionPointSchemaBuilder.java @@ -138,7 +138,7 @@ private void compileFile(IFile file, IProgressMonitor monitor) { SchemaErrorReporter reporter = new SchemaErrorReporter(file); DefaultSAXParser.parse(file, reporter); - reporter.validate(monitor); + reporter.validateContent(monitor); try (StringWriter swriter = new StringWriter(); PrintWriter writer = new PrintWriter(swriter)) { boolean generateDoc = CompilerFlags.getBoolean(file.getProject(), CompilerFlags.S_CREATE_DOCS); diff --git a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/builders/UpdateSiteBuilder.java b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/builders/UpdateSiteBuilder.java index bd0be468d8..e607025c68 100644 --- a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/builders/UpdateSiteBuilder.java +++ b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/builders/UpdateSiteBuilder.java @@ -97,7 +97,7 @@ private void checkFile(IFile file, IProgressMonitor monitor) { UpdateSiteErrorReporter reporter = new UpdateSiteErrorReporter(file); DefaultSAXParser.parse(file, reporter); if (reporter.getErrorCount() == 0) { - reporter.validate(monitor); + reporter.validateContent(monitor); } monitor.subTask(PDECoreMessages.Builders_updating); monitor.done(); diff --git a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/builders/XMLErrorReporter.java b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/builders/XMLErrorReporter.java index 842371ed69..a56694e727 100644 --- a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/builders/XMLErrorReporter.java +++ b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/builders/XMLErrorReporter.java @@ -15,9 +15,11 @@ package org.eclipse.pde.internal.core.builders; import java.io.StringReader; +import java.util.ArrayDeque; import java.util.ArrayList; +import java.util.Deque; import java.util.HashMap; -import java.util.Stack; +import java.util.List; import javax.xml.parsers.ParserConfigurationException; @@ -38,6 +40,7 @@ import org.eclipse.pde.internal.core.PDECoreMessages; import org.eclipse.pde.internal.core.TargetPlatformHelper; import org.eclipse.pde.internal.core.builders.IncrementalErrorReporter.VirtualMarker; +import org.eclipse.pde.internal.core.util.PDEXMLHelper; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -74,7 +77,7 @@ public ElementData(int offset) { private IDocument fTextDocument; - private Stack fElementStack; + private Deque fElementStack; private Element fRootElement; @@ -100,7 +103,7 @@ public XMLErrorReporter(IFile file) { manager.disconnect(file.getFullPath(), LocationKind.NORMALIZE, null); fFindReplaceAdapter = new FindReplaceDocumentAdapter(fTextDocument); fOffsetTable = new HashMap<>(); - fElementStack = new Stack<>(); + fElementStack = new ArrayDeque<>(); } catch (CoreException e) { PDECore.log(e); } @@ -268,13 +271,9 @@ public void setDocumentLocator(Locator locator) { private int getStartOffset(String elementName) throws BadLocationException { int line = fLocator.getLineNumber(); - int col = fLocator.getColumnNumber(); - if (col < 0) { - col = fTextDocument.getLineLength(line); - } String text = fTextDocument.get(fHighestOffset + 1, fTextDocument.getLineOffset(line) - fHighestOffset - 1); - ArrayList commentPositions = new ArrayList<>(); + List commentPositions = new ArrayList<>(); for (int idx = 0; idx < text.length();) { idx = text.indexOf("