Skip to content

Commit

Permalink
Various XML processing simplifications
Browse files Browse the repository at this point in the history
- Inline SAXParserWrapper
- Remove unused code from PDEXMLHelper
- Use XMLProcessorFactory from eclipse.platform in favor of a own one
  • Loading branch information
HannesWell committed Aug 13, 2023
1 parent e621405 commit daecf47
Show file tree
Hide file tree
Showing 59 changed files with 244 additions and 807 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;

import org.eclipse.core.internal.runtime.XmlProcessorFactory;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.pde.internal.core.util.PDEXmlProcessorFactory;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
Expand Down Expand Up @@ -63,7 +63,7 @@ private void runTaskAndVerify(String resourceName) throws Exception, CoreExcepti
IFile reportFile = buildFolder.getFile("report.xml"); //$NON-NLS-1$
assertTrue("report.xml must exist", reportFile.exists()); //$NON-NLS-1$
InputSource is = new InputSource(reportFile.getContents());
DocumentBuilder db = PDEXmlProcessorFactory.createDocumentBuilderWithErrorOnDOCTYPE();
DocumentBuilder db = XmlProcessorFactory.createDocumentBuilderWithErrorOnDOCTYPE();
Document doc = db.parse(is);
NodeList elems = doc.getElementsByTagName("delta"); //$NON-NLS-1$
boolean found = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;

import org.eclipse.core.internal.runtime.XmlProcessorFactory;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.pde.internal.core.util.PDEXmlProcessorFactory;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
Expand Down Expand Up @@ -66,7 +66,7 @@ private IFolder runTaskAndVerify(String resourceName) throws Exception, CoreExce
public void test1() throws Exception {
IFolder reportFolder = runTaskAndVerify("test1"); //$NON-NLS-1$
InputSource is = new InputSource(reportFolder.getFile("not_searched.xml").getContents()); //$NON-NLS-1$
DocumentBuilder db = PDEXmlProcessorFactory.createDocumentBuilderWithErrorOnDOCTYPE();
DocumentBuilder db = XmlProcessorFactory.createDocumentBuilderWithErrorOnDOCTYPE();
Document doc = db.parse(is);

NodeList elems = doc.getElementsByTagName("component"); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;

import org.eclipse.core.internal.runtime.XmlProcessorFactory;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.pde.internal.core.util.PDEXmlProcessorFactory;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
Expand Down Expand Up @@ -60,7 +60,7 @@ private void runTaskAndVerify(String resourceName) throws Exception, CoreExcepti
assertTrue("report folder must exist", folder.exists()); //$NON-NLS-1$
assertTrue("report xml must exist", folder.getFile("compare.xml").exists()); //$NON-NLS-1$ //$NON-NLS-2$
InputSource is = new InputSource(folder.getFile("compare.xml").getContents()); //$NON-NLS-1$
DocumentBuilder db = PDEXmlProcessorFactory.createDocumentBuilderWithErrorOnDOCTYPE();
DocumentBuilder db = XmlProcessorFactory.createDocumentBuilderWithErrorOnDOCTYPE();
Document doc = db.parse(is);
NodeList elems = doc.getElementsByTagName("delta"); //$NON-NLS-1$
boolean found = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.eclipse.core.internal.runtime.XmlProcessorFactory;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
Expand All @@ -45,7 +46,6 @@
import org.eclipse.pde.api.tools.ui.internal.ApiUIPlugin;
import org.eclipse.pde.api.tools.ui.internal.IApiToolsConstants;
import org.eclipse.pde.api.tools.ui.internal.views.APIToolingView;
import org.eclipse.pde.internal.core.util.PDEXmlProcessorFactory;

/**
* Drop-down action to select the active session.
Expand Down Expand Up @@ -155,7 +155,7 @@ protected IStatus run(IProgressMonitor monitor) {
}
writer = new BufferedWriter(new FileWriter(reportFile));
Result result = new StreamResult(writer);
TransformerFactory f = PDEXmlProcessorFactory.createTransformerFactoryWithErrorOnDOCTYPE();
TransformerFactory f = XmlProcessorFactory.createTransformerFactoryWithErrorOnDOCTYPE();
Transformer trans = f.newTransformer(xsltSource);
trans.transform(xmlSource, result);
} catch (TransformerException | IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package org.eclipse.pde.api.tools.internal;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
Expand Down Expand Up @@ -46,7 +45,6 @@
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.PDEXmlProcessorFactory;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.xml.sax.Attributes;
Expand Down Expand Up @@ -458,27 +456,16 @@ private boolean isAPIToolsNature(File dotProjectFile) {
* @return true if it contains a source extension point, false otherwise
*/
private boolean containsAPIToolsNature(String pluginXMLContents) {
SAXParser saxParser = null;
try {
saxParser = PDEXmlProcessorFactory.createSAXParserIgnoringDOCTYPE();
} catch (ParserConfigurationException | SAXException e) {
// ignore
}

if (saxParser == null) {
return false;
}

// Parse
InputSource inputSource = new InputSource(new BufferedReader(new StringReader(pluginXMLContents)));
try {
@SuppressWarnings("restriction")
SAXParser saxParser = org.eclipse.core.internal.runtime.XmlProcessorFactory
.createSAXParserIgnoringDOCTYPE();
APIToolsNatureDefaultHandler defaultHandler = new APIToolsNatureDefaultHandler();
saxParser.parse(inputSource, defaultHandler);
saxParser.parse(new InputSource(new StringReader(pluginXMLContents)), defaultHandler);
return defaultHandler.isAPIToolsNature();
} catch (SAXException | IOException e) {
// ignore
} catch (SAXException | IOException | ParserConfigurationException e) {
return false;
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
import org.eclipse.pde.core.plugin.ModelEntry;
import org.eclipse.pde.core.plugin.PluginRegistry;
import org.eclipse.pde.internal.core.DependencyManager;
import org.eclipse.pde.internal.core.util.PDEXmlProcessorFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
Expand Down Expand Up @@ -373,7 +372,7 @@ private void writeBaselineDescription(IApiBaseline baseline, OutputStream stream
try {
stream.write(xml.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
abort("Error writing pofile descrition", e); //$NON-NLS-1$
throw new CoreException(Status.error("Error writing pofile descrition", e)); //$NON-NLS-1$
}
}

Expand Down Expand Up @@ -422,18 +421,6 @@ private String getProfileXML(IApiBaseline baseline) throws CoreException {
return Util.serializeDocument(document);
}

/**
* Throws a core exception with the given message and underlying exception,
* if any.
*
* @param message error message
* @param e underlying exception or <code>null</code>
* @throws CoreException
*/
private static void abort(String message, Throwable e) throws CoreException {
throw new CoreException(Status.error(message, e));
}

/**
* Restore a baseline from the given input stream (persisted baseline).
*
Expand All @@ -444,14 +431,8 @@ private static void abort(String message, Throwable e) throws CoreException {
*/
public IApiComponent[] readBaselineComponents(ApiBaseline baseline, InputStream stream) throws CoreException {
long start = System.currentTimeMillis();
DocumentBuilder parser = null;
DocumentBuilder parser = getConfiguredParser();
IApiComponent[] restored = null;
try {
parser = PDEXmlProcessorFactory.createDocumentBuilderWithErrorOnDOCTYPE();
parser.setErrorHandler(new DefaultHandler());
} catch (ParserConfigurationException | FactoryConfigurationError e) {
abort("Error restoring API baseline", e); //$NON-NLS-1$
}
try {
Document document = parser.parse(stream);
Element root = document.getDocumentElement();
Expand Down Expand Up @@ -505,14 +486,26 @@ public IApiComponent[] readBaselineComponents(ApiBaseline baseline, InputStream
restored = components.toArray(new IApiComponent[components.size()]);
}
} catch (IOException | SAXException e) {
abort("Error restoring API baseline", e); //$NON-NLS-1$
throw new CoreException(Status.error("Error restoring API baseline", e)); //$NON-NLS-1$
}
if (ApiPlugin.DEBUG_BASELINE_MANAGER) {
System.out.println("Time to restore a persisted baseline : " + (System.currentTimeMillis() - start) + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
}
return restored;
}

private static DocumentBuilder getConfiguredParser() throws CoreException {
try {
@SuppressWarnings("restriction")
DocumentBuilder parser = org.eclipse.core.internal.runtime.XmlProcessorFactory
.createDocumentBuilderWithErrorOnDOCTYPE();
parser.setErrorHandler(new DefaultHandler());
return parser;
} catch (ParserConfigurationException | FactoryConfigurationError e) {
throw new CoreException(Status.error("Error restoring API baseline", e)); //$NON-NLS-1$
}
}

@Override
public void saving(ISaveContext context) throws CoreException {
if (!fNeedsSaving) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*******************************************************************************/
package org.eclipse.pde.api.tools.internal.model;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -77,7 +76,6 @@
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.PDEXmlProcessorFactory;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.Version;
Expand Down Expand Up @@ -1066,27 +1064,16 @@ 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) {
SAXParser saxParser = null;
try {
saxParser = PDEXmlProcessorFactory.createSAXParserWithErrorOnDOCTYPE();
} catch (ParserConfigurationException | SAXException e) {
// ignore
}

if (saxParser == null) {
return false;
}

// Parse
InputSource inputSource = new InputSource(new BufferedReader(new StringReader(pluginXMLContents)));
try {
@SuppressWarnings("restriction")
SAXParser saxParser = org.eclipse.core.internal.runtime.XmlProcessorFactory
.createSAXParserWithErrorOnDOCTYPE();
SourceDefaultHandler defaultHandler = new SourceDefaultHandler();
saxParser.parse(inputSource, defaultHandler);
saxParser.parse(new InputSource(new StringReader(pluginXMLContents)), defaultHandler);
return defaultHandler.isSource();
} catch (SAXException | IOException e) {
// ignore
} catch (SAXException | IOException | ParserConfigurationException e) {
return false;
}
return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ public void parse(String xmlLocation, MissingRefVisitor visitor) throws Exceptio
components = sort(components);

visitor.visitScan();
SAXParser parser = getParser();
@SuppressWarnings("restriction")
SAXParser parser = org.eclipse.core.internal.runtime.XmlProcessorFactory.createSAXParserWithErrorOnDOCTYPE();
// Treat each top level directory as a producer component
for (File component : components) {
if (component.isDirectory()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ public class SearchMessages extends NLS {
public static String UseReportConverter_scan_date;
public static String UseReportConverter_scan_details;
public static String UseReportConverter_scope_pattern;
public static String UseReportConverter_se_error_parser_handle;
public static String UseReportConverter_pce_error_getting_parser;
public static String UseReportConverter_preparing_html_root;
public static String UseReportConverter_preparing_report_metadata;
public static String UseReportConverter_preparing_xml_root;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

Expand All @@ -65,7 +64,6 @@
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.PDEXmlProcessorFactory;
import org.osgi.framework.Version;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
Expand Down Expand Up @@ -738,18 +736,10 @@ protected List<?> parse(IProgressMonitor monitor) throws Exception {
* @throws Exception forwarded general exception that can be trapped in Ant
* builds
*/
SAXParser getParser() throws Exception {
@SuppressWarnings("restriction")
private SAXParser getParser() throws ParserConfigurationException, SAXException {
if (this.parser == null) {
try {
this.parser = PDEXmlProcessorFactory.createSAXParserWithErrorOnDOCTYPE();
} catch (ParserConfigurationException pce) {
throw new Exception(SearchMessages.UseReportConverter_pce_error_getting_parser, pce);
} catch (SAXException se) {
throw new Exception(SearchMessages.UseReportConverter_se_error_parser_handle, se);
}
if (this.parser == null) {
throw new Exception(SearchMessages.could_not_create_sax_parser);
}
this.parser = org.eclipse.core.internal.runtime.XmlProcessorFactory.createSAXParserWithErrorOnDOCTYPE();
}
return this.parser;
}
Expand Down Expand Up @@ -819,8 +809,9 @@ protected void applyXSLT(File xsltFile, File xmlfile, File htmloutput) throws Tr
protected void applyXSLT(Source xslt, File xmlfile, File htmlfile) throws TransformerException {
Source xml = new StreamSource(xmlfile);
Result html = new StreamResult(htmlfile);
TransformerFactory factory = PDEXmlProcessorFactory.createTransformerFactoryWithErrorOnDOCTYPE();
Transformer former = factory.newTransformer(xslt);
@SuppressWarnings("restriction")
Transformer former = org.eclipse.core.internal.runtime.XmlProcessorFactory
.createTransformerFactoryWithErrorOnDOCTYPE().newTransformer(xslt);
former.transform(xml, html);
}

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

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

import org.eclipse.core.runtime.IProgressMonitor;
Expand All @@ -35,7 +34,6 @@
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.PDEXmlProcessorFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
Expand Down Expand Up @@ -244,7 +242,9 @@ public void parse(String xmlLocation, IProgressMonitor monitor, UseScanVisitor u
localmonitor.setWorkRemaining(referees.length);
visitor.visitScan();
try {
SAXParser parser = getParser();
@SuppressWarnings("restriction")
SAXParser parser = org.eclipse.core.internal.runtime.XmlProcessorFactory
.createSAXParserWithErrorOnDOCTYPE();
// Treat each top level directory as a producer component
for (File referee : referees) {
if (referee.isDirectory()) {
Expand Down Expand Up @@ -299,24 +299,6 @@ public void parse(String xmlLocation, IProgressMonitor monitor, UseScanVisitor u
}
}

/**
* Returns a parser
*
* @return default parser
* @throws Exception forwarded general exception that can be trapped in Ant
* builds
*/
SAXParser getParser() throws Exception {
try {
return PDEXmlProcessorFactory.createSAXParserWithErrorOnDOCTYPE();

} catch (ParserConfigurationException pce) {
throw new Exception(SearchMessages.UseReportConverter_pce_error_getting_parser, pce);
} catch (SAXException se) {
throw new Exception(SearchMessages.UseReportConverter_se_error_parser_handle, se);
}
}

/**
* @return the referencingComponent or <code>null</code>
*/
Expand Down
Loading

0 comments on commit daecf47

Please sign in to comment.