Skip to content

Commit

Permalink
Refactor XML processing
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Aug 1, 2023
1 parent 3bd3dbd commit cfed903
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,15 @@ public long getContributionsTimestamp() {
* @see org.eclipse.core.runtime.IExtensionRegistry#addContribution(java.io.InputStream, IContributor, boolean, String, ResourceBundle, Object)
*/
public SAXParserFactory getXMLParser() {
if (theXMLParserFactory == null)
if (theXMLParserFactory == null) {
theXMLParserFactory = SAXParserFactory.newInstance();
try {
// force org.xml.sax.SAXParseException for any DOCTYPE:
theXMLParserFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); //$NON-NLS-1$
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return theXMLParserFactory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.xml.XMLConstants;
import javax.xml.transform.*;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
Expand Down Expand Up @@ -113,17 +114,14 @@ public XSLTStreamTransformer(ServiceTracker<FrameworkLog, FrameworkLog> logTrack
public InputStream getInputStream(InputStream inputStream, URL transformerUrl) throws IOException {
Templates template = getTemplate(transformerUrl);
if (template != null) {

Transformer transformer = null;
try {
transformer = template.newTransformer();
Transformer transformer = template.newTransformer();
XSLTPipe pipe = new XSLTPipe(inputStream, transformer);
return pipe.getPipedInputStream();
} catch (TransformerConfigurationException e) {
log(FrameworkEvent.ERROR, "Could not perform transform.", e); //$NON-NLS-1$
}
}

return null;
}

Expand All @@ -148,6 +146,8 @@ private synchronized Templates getTemplate(URL transformerURL) {
TransformerFactory tFactory = null;
try {
tFactory = TransformerFactory.newInstance();
tFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); //$NON-NLS-1$
tFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, ""); //$NON-NLS-1$

InputSource inputSource = new InputSource(xsltStream);
XMLReader reader = XMLReaderFactory.createXMLReader();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
package org.eclipse.osgi.internal.framework;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.osgi.framework.*;
import org.osgi.framework.Bundle;
import org.osgi.framework.ServiceFactory;
import org.osgi.framework.ServiceRegistration;
import org.osgi.framework.wiring.BundleWiring;

class XMLParsingServiceFactory implements ServiceFactory<Object> {
Expand Down Expand Up @@ -55,9 +58,27 @@ public Object getService(Bundle bundle, ServiceRegistration<Object> registration
}

private Object createService() {
if (isSax)
return SAXParserFactory.newInstance();
return DocumentBuilderFactory.newInstance();
if (isSax) {
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
// ignore DOCTYPE:
factory.setFeature("http://xml.org/sax/features/external-general-entities", false); //$NON-NLS-1$
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); //$NON-NLS-1$
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); //$NON-NLS-1$
} catch (Exception e) {
throw new IllegalStateException(e);
}
return factory;
}
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
// completely disable external entities declarations:
factory.setFeature("http://xml.org/sax/features/external-general-entities", false); //$NON-NLS-1$
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); //$NON-NLS-1$
} catch (ParserConfigurationException e) {
throw new IllegalStateException(e.getMessage(), e);
}
return factory;
}

@Override
Expand Down

0 comments on commit cfed903

Please sign in to comment.