Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factory for SAXParser parsing XML without DOCTYPE #632

Merged
merged 1 commit into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.JavaCore;
Expand All @@ -48,6 +46,7 @@
import org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeContainer;
import org.eclipse.pde.api.tools.internal.provisional.scanner.TagScanner;
import org.eclipse.pde.api.tools.internal.util.Util;
import org.eclipse.pde.internal.core.util.XmlParserFactory;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.xml.sax.Attributes;
Expand Down Expand Up @@ -459,15 +458,9 @@ private boolean isAPIToolsNature(File dotProjectFile) {
* @return true if it contains a source extension point, false otherwise
*/
private boolean containsAPIToolsNature(String pluginXMLContents) {
SAXParserFactory factory = null;
try {
factory = SAXParserFactory.newInstance();
} catch (FactoryConfigurationError e) {
return false;
}
SAXParser saxParser = null;
try {
saxParser = factory.newSAXParser();
saxParser = XmlParserFactory.createSAXParserIgnoringDOCTYPE();
} catch (ParserConfigurationException | SAXException e) {
// ignore
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
Expand Down Expand Up @@ -79,6 +77,7 @@
import org.eclipse.pde.api.tools.internal.util.Util;
import org.eclipse.pde.internal.core.TargetWeaver;
import org.eclipse.pde.internal.core.util.ManifestUtils;
import org.eclipse.pde.internal.core.util.XmlParserFactory;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.Version;
Expand Down Expand Up @@ -1068,15 +1067,9 @@ private static boolean isSourceComponent(Map<String, String> manifest, File loca
* @return true if it contains a source extension point, false otherwise
*/
private static boolean containsSourceExtensionPoint(String pluginXMLContents) {
SAXParserFactory factory = null;
try {
factory = SAXParserFactory.newInstance();
} catch (FactoryConfigurationError e) {
return false;
}
SAXParser saxParser = null;
try {
saxParser = factory.newSAXParser();
saxParser = XmlParserFactory.createSAXParserWithErrorOnDOCTYPE();
} catch (ParserConfigurationException | SAXException e) {
// ignore
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
Expand Down Expand Up @@ -66,6 +65,7 @@
import org.eclipse.pde.api.tools.internal.provisional.search.IMetadata;
import org.eclipse.pde.api.tools.internal.util.Signatures;
import org.eclipse.pde.api.tools.internal.util.Util;
import org.eclipse.pde.internal.core.util.XmlParserFactory;
import org.eclipse.pde.internal.core.util.XmlTransformerFactory;
import org.osgi.framework.Version;
import org.w3c.dom.Element;
Expand Down Expand Up @@ -741,9 +741,8 @@ protected List<?> parse(IProgressMonitor monitor) throws Exception {
*/
SAXParser getParser() throws Exception {
if (this.parser == null) {
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
this.parser = factory.newSAXParser();
this.parser = XmlParserFactory.createSAXParserWithErrorOnDOCTYPE();
} catch (ParserConfigurationException pce) {
throw new Exception(SearchMessages.UseReportConverter_pce_error_getting_parser, pce);
} catch (SAXException se) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;
Expand All @@ -36,6 +35,7 @@
import org.eclipse.pde.api.tools.internal.provisional.descriptors.IComponentDescriptor;
import org.eclipse.pde.api.tools.internal.provisional.descriptors.IMemberDescriptor;
import org.eclipse.pde.api.tools.internal.util.Util;
import org.eclipse.pde.internal.core.util.XmlParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
Expand Down Expand Up @@ -307,9 +307,9 @@ public void parse(String xmlLocation, IProgressMonitor monitor, UseScanVisitor u
* builds
*/
SAXParser getParser() throws Exception {
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
return factory.newSAXParser();
return XmlParserFactory.createSAXParserWithErrorOnDOCTYPE();

} catch (ParserConfigurationException pce) {
throw new Exception(SearchMessages.UseReportConverter_pce_error_getting_parser, pce);
} catch (SAXException se) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
Expand All @@ -34,6 +33,7 @@
import org.eclipse.pde.api.tools.internal.IApiXmlConstants;
import org.eclipse.pde.api.tools.internal.provisional.comparator.IDelta;
import org.eclipse.pde.api.tools.internal.util.Util;
import org.eclipse.pde.internal.core.util.XmlParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
Expand Down Expand Up @@ -311,10 +311,9 @@ public void execute() throws BuildException {
System.out.println("xmlFileLocation : " + this.xmlFileLocation); //$NON-NLS-1$
System.out.println("htmlFileLocation : " + this.htmlFileLocation); //$NON-NLS-1$
}
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = null;
try {
parser = factory.newSAXParser();
parser = XmlParserFactory.createSAXParserWithErrorOnDOCTYPE();
} catch (ParserConfigurationException | SAXException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
Expand All @@ -34,6 +33,7 @@
import org.eclipse.pde.api.tools.internal.IApiXmlConstants;
import org.eclipse.pde.api.tools.internal.provisional.comparator.IDelta;
import org.eclipse.pde.api.tools.internal.util.Util;
import org.eclipse.pde.internal.core.util.XmlParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
Expand Down Expand Up @@ -491,10 +491,9 @@ public void execute() throws BuildException {
}
}
}
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = null;
try {
parser = factory.newSAXParser();
parser = XmlParserFactory.createSAXParserWithErrorOnDOCTYPE();
} catch (ParserConfigurationException | SAXException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.eclipse.osgi.util.NLS;
import org.eclipse.pde.api.tools.internal.IApiXmlConstants;
import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin;
import org.eclipse.pde.api.tools.internal.util.Util;
import org.eclipse.pde.internal.core.util.XmlParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
Expand Down Expand Up @@ -429,10 +429,9 @@ public void execute() throws BuildException {
if (!this.reportsRoot.exists() || !this.reportsRoot.isDirectory()) {
throw new BuildException(NLS.bind(Messages.invalid_directory_name, this.xmlReportsLocation));
}
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = null;
try {
parser = factory.newSAXParser();
parser = XmlParserFactory.createSAXParserWithErrorOnDOCTYPE();
} catch (ParserConfigurationException | SAXException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
Expand Down Expand Up @@ -66,7 +65,6 @@ public class JNLPGenerator extends DefaultHandler {
* feature.includes = extension
* feature.plugin = jar
*/
private final static SAXParserFactory parserFactory = SAXParserFactory.newInstance();
private PrintWriter out;
private String destination;
private String provider;
Expand Down Expand Up @@ -109,8 +107,7 @@ public JNLPGenerator(String feature, String destination, String codebase, String
this.locale = locale;
this.generateOfflineAllowed = generateOfflineAllowed;
try {
parserFactory.setNamespaceAware(true);
parser = parserFactory.newSAXParser();
parser = XmlParserFactory.createNsAwareSAXParserWithErrorOnDOCTYPE();
} catch (ParserConfigurationException | SAXException e) {
System.out.println(e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*******************************************************************************
* Copyright (c) 2023 Joerg Kubitz 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
*******************************************************************************/
package org.eclipse.pde.internal.build.tasks;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

public class XmlParserFactory {
private XmlParserFactory() {
// static Utility only
}

private static final SAXParserFactory SAX_FACTORY_ERROR_ON_DOCTYPE_NS = createSAXFactoryWithErrorOnDOCTYPE();

private static synchronized SAXParserFactory createSAXFactoryWithErrorOnDOCTYPE() {
SAXParserFactory f = SAXParserFactory.newInstance();
f.setNamespaceAware(true);
try {
// force org.xml.sax.SAXParseException for any DOCTYPE:
f.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); //$NON-NLS-1$
} catch (Exception e) {
throw new RuntimeException(e);
}
return f;
}

public static SAXParser createNsAwareSAXParserWithErrorOnDOCTYPE() throws ParserConfigurationException, SAXException {
return SAX_FACTORY_ERROR_ON_DOCTYPE_NS.newSAXParser();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.eclipse.core.filebuffers.FileBuffers;
import org.eclipse.core.filebuffers.ITextFileBuffer;
Expand All @@ -41,6 +40,7 @@
import org.eclipse.pde.core.IModelChangedEvent;
import org.eclipse.pde.core.IModelChangedListener;
import org.eclipse.pde.core.ModelChangedEvent;
import org.eclipse.pde.internal.core.util.XmlParserFactory;
import org.xml.sax.SAXException;

public abstract class AbstractModel extends PlatformObject implements IModel, IModelChangeProviderExtension, Serializable {
Expand Down Expand Up @@ -237,7 +237,7 @@ public void throwParseErrorsException(Throwable e) throws CoreException {
}

protected SAXParser getSaxParser() throws ParserConfigurationException, SAXException, FactoryConfigurationError {
return SAXParserFactory.newInstance().newSAXParser();
return XmlParserFactory.createSAXParserWithErrorOnDOCTYPE();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
Expand All @@ -36,6 +35,7 @@
import org.eclipse.pde.core.plugin.ISharedPluginModel;
import org.eclipse.pde.internal.core.PDECore;
import org.eclipse.pde.internal.core.PDECoreMessages;
import org.eclipse.pde.internal.core.util.XmlParserFactory;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

Expand Down Expand Up @@ -199,7 +199,7 @@ public String getSchemaVersion() {
try {
InputStream stream = new BufferedInputStream(((IFile) res).getContents(true));
PluginHandler handler = new PluginHandler(true);
SAXParserFactory.newInstance().newSAXParser().parse(stream, handler);
XmlParserFactory.createSAXParserWithErrorOnDOCTYPE().parse(stream, handler);
return handler.getSchemaVersion();
} catch (CoreException | SAXException | IOException | ParserConfigurationException e) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.osgi.service.resolver.BundleDescription;
Expand All @@ -36,6 +35,7 @@
import org.eclipse.pde.internal.core.PDECoreMessages;
import org.eclipse.pde.internal.core.PDEState;
import org.eclipse.pde.internal.core.bundle.BundlePluginBase;
import org.eclipse.pde.internal.core.util.XmlParserFactory;
import org.osgi.framework.Version;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
Expand Down Expand Up @@ -404,7 +404,8 @@ protected boolean hasRequiredAttributes() {
}

protected SAXParser getSaxParser() throws ParserConfigurationException, SAXException, FactoryConfigurationError {
return SAXParserFactory.newInstance().newSAXParser();
return XmlParserFactory.createSAXParserWithErrorOnDOCTYPE();

}

public static int getMatchRule(VersionRange versionRange) {
Expand Down Expand Up @@ -447,6 +448,7 @@ public String getBundleSourceEntry() {
return fBundleSourceEntry;
}

@Override
public boolean exportsExternalAnnotations() {
return fExportsExternalAnnotations;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.eclipse.core.runtime.URIUtil;
import org.eclipse.osgi.util.NLS;
import org.eclipse.pde.internal.core.util.XmlParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
Expand All @@ -43,7 +43,6 @@ class ConfigurationParser extends DefaultHandler implements IConfigurationConsta

private static final String URL_PROPERTY = "org.eclipse.update.resolution_url"; //$NON-NLS-1$
private static final String EMPTY_STRING = ""; //$NON-NLS-1$
private final static SAXParserFactory parserFactory = SAXParserFactory.newInstance();
private SAXParser parser;

private URL currentSiteURL;
Expand All @@ -58,8 +57,7 @@ class ConfigurationParser extends DefaultHandler implements IConfigurationConsta
public ConfigurationParser() throws InvocationTargetException {

try {
parserFactory.setNamespaceAware(true);
this.parser = parserFactory.newSAXParser();
this.parser = XmlParserFactory.createSAXParserWithErrorOnDOCTYPE(true);
} catch (ParserConfigurationException | SAXException e) {
Utils.log(Utils.newStatus("ConfigurationParser", e)); //$NON-NLS-1$
throw new InvocationTargetException(e);
Expand Down
Loading