Skip to content

Commit

Permalink
Support option XMLResource.OPTION_RESOLVE_ENTITIES
Browse files Browse the repository at this point in the history
This option is false by default and will prevent
XMLHandler.resolveEntity(String, String) from loading external entities.

#10
  • Loading branch information
merks committed Aug 1, 2023
1 parent 53bef72 commit c30b1f2
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 19 deletions.
2 changes: 1 addition & 1 deletion features/org.eclipse.emf.ecore-feature/feature.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<feature
id="org.eclipse.emf.ecore"
label="%featureName"
version="2.33.0.qualifier"
version="2.35.0.qualifier"
provider-name="%providerName"
license-feature="org.eclipse.emf.license"
license-feature-version="2.11.0">
Expand Down
2 changes: 1 addition & 1 deletion features/org.eclipse.emf.ecore-feature/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>org.eclipse.emf.features</groupId>
<artifactId>org.eclipse.emf.ecore</artifactId>
<version>2.33.0-SNAPSHOT</version>
<version>2.35.0-SNAPSHOT</version>
<packaging>eclipse-feature</packaging>

</project>
2 changes: 1 addition & 1 deletion plugins/org.eclipse.emf.codegen.ecore/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.9.0,4.0.0)",
org.eclipse.jdt.launching;bundle-version="[3.5.0,4.0.0)",
org.eclipse.emf.ecore;bundle-version="[2.35.0,3.0.0)";visibility:=reexport,
org.eclipse.emf.codegen;bundle-version="[2.23.0,3.0.0)";visibility:=reexport,
org.eclipse.emf.ecore.xmi;bundle-version="[2.18.0,3.0.0)",
org.eclipse.emf.ecore.xmi;bundle-version="[2.35.0,3.0.0)",
org.eclipse.text;bundle-version="[3.5.0,4.0.0)"
Provide-Capability: org.eclipse.emf.ecore.generated_package;uri="http://www.eclipse.org/emf/2002/GenModel";class=org.eclipse.emf.codegen.ecore.genmodel.GenModelPackage;genModel="model/GenModel.genmodel"
Bundle-ActivationPolicy: lazy
Expand Down
10 changes: 5 additions & 5 deletions plugins/org.eclipse.emf.ecore.xmi/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.emf.ecore.xmi; singleton:=true
Bundle-Version: 2.18.0.qualifier
Bundle-Version: 2.35.0.qualifier
Bundle-ClassPath: .
Bundle-Activator: org.eclipse.emf.ecore.xmi.XMIPlugin$Implementation$Activator
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Bundle-RequiredExecutionEnvironment: J2SE-1.5
Export-Package: org.eclipse.emf.ecore.xmi;version="2.18.0",
org.eclipse.emf.ecore.xmi.impl;version="2.18.0",
org.eclipse.emf.ecore.xmi.util;version="2.18.0"
Export-Package: org.eclipse.emf.ecore.xmi;version="2.35.0",
org.eclipse.emf.ecore.xmi.impl;version="2.35.0",
org.eclipse.emf.ecore.xmi.util;version="2.35.0"
Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.9.0,4.0.0)";resolution:=optional;x-installation:=greedy,
org.eclipse.emf.ecore;bundle-version="[2.33.0,3.0.0)";visibility:=reexport
org.eclipse.emf.ecore;bundle-version="[2.35.0,3.0.0)";visibility:=reexport
Import-Package: javax.xml.namespace,
javax.xml.parsers,
org.osgi.framework;version="[1.5.0,2.0.0)",
Expand Down
2 changes: 1 addition & 1 deletion plugins/org.eclipse.emf.ecore.xmi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>org.eclipse.emf</groupId>
<artifactId>org.eclipse.emf.ecore.xmi</artifactId>
<version>2.18.0-SNAPSHOT</version>
<version>2.35.0-SNAPSHOT</version>
<packaging>eclipse-plugin</packaging>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,13 @@ interface MissingPackageHandler
*/
String OPTION_MISSING_PACKAGE_HANDLER = "MISSING_PACKAGE_HANDLER";

/**
* A load option that specifies whether external entities should be resolved during XML loading.
* The default value is false.
* @since 2.35
*/
String OPTION_RESOLVE_ENTITIES = "RESOLVED_ENTITIES";

String HREF = "href";
String NIL = "nil";
String TYPE = "type";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,11 @@ public int getColumnNumber()
*/
protected XMLResource.MissingPackageHandler missingPackageHandler;

/**
* @since 2.35
*/
protected boolean resolveEntities;

/**
*/
public XMLHandler(XMLResource xmlResource, XMLHelper helper, Map<?, ?> options)
Expand Down Expand Up @@ -509,6 +514,7 @@ public XMLHandler(XMLResource xmlResource, XMLHelper helper, Map<?, ?> options)

usePackageNsURIAsLocation = !Boolean.FALSE.equals(options.get(XMLResource.OPTION_USE_PACKAGE_NS_URI_AS_LOCATION));
missingPackageHandler = (XMLResource.MissingPackageHandler)options.get(XMLResource.OPTION_MISSING_PACKAGE_HANDLER);
resolveEntities = Boolean.TRUE.equals(options.get(XMLResource.OPTION_RESOLVE_ENTITIES));
}

protected void setExtendedMetaDataOption(Object extendedMetaDataOption)
Expand Down Expand Up @@ -804,6 +810,11 @@ public void notationDecl(String name, String publicId, String systemId) throws S
@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException
{
if (!resolveEntities)
{
return null;
}

try
{
Map<Object, Object> options = new HashMap<Object, Object>();
Expand Down
18 changes: 9 additions & 9 deletions tests/org.eclipse.emf.test.xml/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.emf.test.xml;singleton:=true
Bundle-Version: 2.15.0.qualifier
Bundle-Version: 2.16.0.qualifier
Bundle-ClassPath: .
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Bundle-RequiredExecutionEnvironment: J2SE-1.5
Export-Package: org.eclipse.emf.test.xml;version="2.15.0",
org.eclipse.emf.test.xml.encoding;version="2.15.0",
org.eclipse.emf.test.xml.rss;version="2.15.0",
org.eclipse.emf.test.xml.xmi;version="2.15.0",
org.eclipse.emf.test.xml.xsd;version="2.15.0",
org.eclipse.emf.test.xml.xsdecore;version="2.15.0"
Export-Package: org.eclipse.emf.test.xml;version="2.16.0",
org.eclipse.emf.test.xml.encoding;version="2.16.0",
org.eclipse.emf.test.xml.rss;version="2.16.0",
org.eclipse.emf.test.xml.xmi;version="2.16.0",
org.eclipse.emf.test.xml.xsd;version="2.16.0",
org.eclipse.emf.test.xml.xsdecore;version="2.16.0"
Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.9.0,4.0.0)",
org.eclipse.core.resources;bundle-version="[3.8.0,4.0.0)",
org.eclipse.emf.ecore;bundle-version="[2.33.0,3.0.0)",
org.eclipse.emf.ecore.xmi;bundle-version="[2.18.0,3.0.0)",
org.eclipse.emf.ecore;bundle-version="[2.35.0,3.0.0)",
org.eclipse.emf.ecore.xmi;bundle-version="[2.35.0,3.0.0)",
org.eclipse.xsd;bundle-version="[2.19.0,3.0.0)",
org.junit;bundle-version="[4.12.0,5.0.0)",
org.eclipse.emf.test.common;bundle-version="[1.11.0,2.0.0)"
Expand Down
1 change: 1 addition & 0 deletions tests/org.eclipse.emf.test.xml/data/xmi/root.dtd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!ELEMENT root (#PCDATA)>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root SYSTEM "root.dtd">
<root/>
2 changes: 1 addition & 1 deletion tests/org.eclipse.emf.test.xml/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>org.eclipse.emf</groupId>
<artifactId>org.eclipse.emf.test.xml</artifactId>
<version>2.15.0-SNAPSHOT</version>
<version>2.16.0-SNAPSHOT</version>
<packaging>eclipse-test-plugin</packaging>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.FileNotFoundException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Collections;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
Expand Down Expand Up @@ -49,6 +53,7 @@

public class URIHandlerTest
{
final static String BASE_XMI_URI = TestUtil.getPluginDirectory(AllSuites.PLUGIN_ID) + "/data/xmi/";
final static String BASE_XML_URI = TestUtil.getPluginDirectory(AllSuites.PLUGIN_ID) + "/data/xml/";
final static String LF = System.getProperty("line.separator");

Expand Down Expand Up @@ -169,4 +174,32 @@ public void testEntityHandler() throws Exception
Book reloadedFirstBook = reloadedFirstAuthor.getBooks().get(0);
assertEquals(firstLibraryResource.getURI(), reloadedFirstBook.eResource().getURI());
}

@Test
public void testEntityResolver() throws Exception
{
// Expecting external entities not to be resolved by default.
// So the redirection of that external reference to a non-existent location should not result in a file not found exception.
//
resourceSet.getURIConverter().getURIMap().put(URI.createFileURI(BASE_XMI_URI + "/root.dtd"), URI.createFileURI(BASE_XMI_URI + "/not-found.dtd"));
Resource resource = resourceSet.getResource(URI.createFileURI(BASE_XMI_URI + "/test-with-external-entity.xmi"), true);

try
{
// Load it again, but resolving external entities.
//
resource.unload();
resource.load(Collections.singletonMap(XMLResource.OPTION_RESOLVE_ENTITIES, Boolean.TRUE));

// We should not get here, we should get an exception.
//
fail("Expecting the URI to be loaded to the redirected bogus location, causing a FileNotFoundException");
}
catch (FileNotFoundException ex)
{
// The exception should refer to the redirected URI.
//
assertTrue("Expecting the entity reference to be loaded to the redirected bogus location", ex.toString().contains("not-found.dtd"));
}
}
}

0 comments on commit c30b1f2

Please sign in to comment.