Skip to content

Commit e3f97f5

Browse files
authored
Merge pull request #369 from diffplug/eclipse-wtp-prevent-external-uri
WTP - Ignore external URIs by default
2 parents 9f346e9 + 035afba commit e3f97f5

File tree

17 files changed

+253
-17
lines changed

17 files changed

+253
-17
lines changed

Diff for: _ext/eclipse-wtp/CHANGES.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# spotless-eclipse-wtp
22

3-
### Versioni 3.10.0 - TBD
3+
### Version 3.9.8 - TBD
4+
5+
* XML formatter ignores external URIs per default. ([#369](https://github.com/diffplug/spotless/issues/369)). Add `resolveExternalURI=true` property to switch to previous behavior.
46

57
### Version 3.9.7 - February 25th 2018 ([artifact]([jcenter](https://bintray.com/diffplug/opensource/spotless-eclipse-wtp)))
68

Diff for: _ext/eclipse-wtp/build.gradle

+6
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ dependencies {
7474
compile "org.eclipse.xsd:org.eclipse.xsd:${VER_ECLISPE_XSD}"
7575
}
7676

77+
jar {
78+
manifest {
79+
from 'src/main/resources/META-INF/MANIFEST.MF'
80+
}
81+
}
82+
7783
//////////
7884
// Test //
7985
//////////

Diff for: _ext/eclipse-wtp/gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Versions correspond to the Eclipse-WTP version used for the fat-JAR.
22
# See https://www.eclipse.org/webtools/ for further information about Eclipse-WTP versions.
33
# Patch version can be incremented independently for backward compatible patches of this library.
4-
ext_version=3.9.7
4+
ext_version=3.9.8
55
ext_artifactId=spotless-eclipse-wtp
66
ext_description=Eclipse's WTP formatters bundled for Spotless
77

Diff for: _ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseHtmlFormatterStepImpl.java

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public EclipseHtmlFormatterStepImpl(Properties properties) throws Exception {
6767
additionalPlugins.add(new org.eclipse.core.internal.filesystem.Activator());
6868
additionalPlugins.add(new JavaScriptCore());
6969
additionalPlugins.add(new HTMLCorePlugin());
70+
//The HTML formatter only uses the DOCTYPE/SCHEMA for content model selection.
71+
additionalPlugins.add(new PreventExternalURIResolverExtension());
7072
});
7173
/*
7274
* The cleanup processor tries to load DTDs into the cache (which we have not setup).

Diff for: _ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseXmlFormatterStepImpl.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ public class EclipseXmlFormatterStepImpl {
5858
private final INodeAdapterFactory xmlAdapterFactory;
5959

6060
public EclipseXmlFormatterStepImpl(Properties properties) throws Exception {
61-
setupFramework();
61+
setupFramework(SpotlessPreferences.doResolveExternalURI(properties));
6262
preferences = PREFERENCE_FACTORY.create(properties);
6363
formatter = new DefaultXMLPartitionFormatter();
6464
//The adapter factory maintains the common CMDocumentCache
6565
xmlAdapterFactory = new ModelQueryAdapterFactoryForXML();
6666
}
6767

68-
private static void setupFramework() throws BundleException {
68+
private static void setupFramework(boolean resolveExternalURI) throws BundleException {
6969
if (SpotlessEclipseFramework.setup(
7070
plugins -> {
7171
plugins.applyDefault();
@@ -77,6 +77,9 @@ private static void setupFramework() throws BundleException {
7777
plugins.add(new DTDCorePlugin());
7878
//Support formatting based on XSD restrictions
7979
plugins.add(new XSDCorePlugin());
80+
if (!resolveExternalURI) {
81+
plugins.add(new PreventExternalURIResolverExtension());
82+
}
8083
})) {
8184
PREFERENCE_FACTORY = new XmlFormattingPreferencesFactory();
8285
//Register required EMF factories
@@ -104,7 +107,6 @@ public String format(String raw, String baseLocation) throws Exception {
104107
}
105108

106109
private static class XmlFormattingPreferencesFactory {
107-
108110
private final static Set<String> SUPPORTED_XML_FORMAT_PREFS = new HashSet<String>(Arrays.asList(
109111
FORMAT_COMMENT_TEXT,
110112
FORMAT_COMMENT_JOIN_LINES,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2016 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.extra.eclipse.wtp;
17+
18+
import org.eclipse.core.resources.IFile;
19+
import org.eclipse.emf.common.util.URI;
20+
import org.eclipse.wst.common.uriresolver.internal.provisional.URIResolverExtension;
21+
import org.osgi.framework.BundleActivator;
22+
import org.osgi.framework.BundleContext;
23+
24+
/**
25+
* The URI resolver extension
26+
*/
27+
public class PreventExternalURIResolverExtension implements URIResolverExtension, BundleActivator {
28+
29+
private static final String REFUSE_EXTERNAL_URI = "file://refused.external.uri";
30+
31+
/**
32+
* @param file the in-workspace base resource, if one exists
33+
* @param baseLocation - the location of the resource that contains the uri
34+
* @param publicId - an optional public identifier (i.e. namespace name), or null if none
35+
* @param systemId - an absolute or relative URI, or null if none
36+
*
37+
* @return an absolute URI or null if this extension can not resolve this reference
38+
*/
39+
@Override
40+
public String resolve(IFile file, String baseLocation, String publicId, String systemId) {
41+
if (null != systemId) {
42+
try {
43+
URI proposalByPreviousResolver = org.eclipse.emf.common.util.URI.createURI(systemId);
44+
String host = proposalByPreviousResolver.host();
45+
/*
46+
* The host is empty (not null)
47+
*/
48+
if (!(null == host || host.isEmpty())) {
49+
return REFUSE_EXTERNAL_URI;
50+
}
51+
} catch (IllegalArgumentException ignore) {
52+
//If it is no a valid URI, there is nothing to do here.
53+
}
54+
}
55+
return null; //Don't alter the proposal of previous resolver extensions by proposing something else
56+
}
57+
58+
@Override
59+
public void start(BundleContext context) throws Exception {
60+
//Nothing to do. The bundle-activator interface only allows to load this extension as a stand-alone plugin.
61+
}
62+
63+
@Override
64+
public void stop(BundleContext context) throws Exception {
65+
//Nothing to do. The bundle-activator interface only allows to load this extension as a stand-alone plugin.
66+
}
67+
68+
}

Diff for: _ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/sse/SpotlessPreferences.java

+23
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ public class SpotlessPreferences {
4141
*/
4242
public static final String USER_CATALOG = "userCatalog";
4343

44+
/**
45+
* Indicates if external URIs (location hints) should be resolved
46+
* and the referenced DTD/XSD shall be applied. Per default
47+
* external URIs are ignored.
48+
* <p>
49+
* Value is of type <code>Boolean</code>.
50+
* </p>
51+
*/
52+
public static final String RESOLVE_EXTERNAL_URI = "resolveExternalURI";
53+
4454
/** Configures the Eclipse properties for a Plugin and returns its previous values. */
4555
public static Properties configurePluginPreferences(Plugin plugin, Properties newValues) {
4656
IEclipsePreferences globalPreferences = DefaultScope.INSTANCE.getNode(plugin.getBundle().getSymbolicName());
@@ -55,6 +65,19 @@ public static Properties configurePluginPreferences(Plugin plugin, Properties ne
5565
return oldValues;
5666
}
5767

68+
public static boolean doResolveExternalURI(Properties properties) {
69+
Object obj = properties.get(RESOLVE_EXTERNAL_URI);
70+
if (null != obj) {
71+
if (obj instanceof Boolean) {
72+
return (Boolean) obj;
73+
}
74+
if (obj instanceof String) {
75+
return ((String) obj).equalsIgnoreCase("true");
76+
}
77+
}
78+
return false;
79+
}
80+
5881
public static void configureCatalog(final Properties config) {
5982
Optional<File> catalog = getCatalogConfig(config);
6083
Catalog defaultCatalog = getDefaultCatalog();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Bundle-ManifestVersion: 2
3+
Bundle-SymbolicName: com.diffplug.gradle.spotless.eclipse.wtp; singleton:=true

Diff for: _ext/eclipse-wtp/src/main/resources/plugin.xml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<?eclipse version="3.0"?>
3+
<plugin>
4+
<!-- Prevents resolution of external URIs by redirecting them to invalid external URI. -->
5+
<!-- The low priority assures that the org.eclipse.wst.common.uriresolver.internal.ExtensibleURIResolver -->
6+
<!-- ask this resolver extension at the end and let it override all previous results. Hence the term -->
7+
<!-- "low priority' might be missleading. In fact the result of this extension overrules all other -->
8+
<!-- physical resolvers with higher priority. -->
9+
<extension point="org.eclipse.wst.common.uriresolver.resolverExtensions">
10+
<resolverExtension stage="physical" priority="low" class="com.diffplug.spotless.extra.eclipse.wtp.PreventExternalURIResolverExtension">
11+
</resolverExtension>
12+
</extension>
13+
</plugin>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2016 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.extra.eclipse.wtp;
17+
18+
import static org.junit.Assert.*;
19+
20+
import java.util.Properties;
21+
22+
import org.junit.BeforeClass;
23+
import org.junit.Test;
24+
25+
import com.diffplug.spotless.extra.eclipse.wtp.sse.SpotlessPreferences;
26+
27+
/** Test configuration allowExternalURI=false */
28+
public class EclipseXmlFormatterStepImplAllowExternalURIsTest {
29+
private static TestData TEST_DATA = null;
30+
31+
@BeforeClass
32+
public static void initializeStatic() throws Exception {
33+
TEST_DATA = TestData.getTestDataOnFileSystem("xml");
34+
}
35+
36+
@Test
37+
public void dtdExternalPath() throws Throwable {
38+
String output = format(TEST_DATA.input("dtd_external.test"));
39+
assertEquals("External DTD not resolved. Restrictions are not applied by formatter.",
40+
TEST_DATA.expected("dtd_external.test"), output);
41+
}
42+
43+
@Test
44+
public void xsdExternalPath() throws Throwable {
45+
String output = format(TEST_DATA.input("xsd_external.test"));
46+
assertEquals("External XSD not resolved. Restrictions are not applied by formatter.",
47+
TEST_DATA.expected("xsd_external.test"), output);
48+
}
49+
50+
private static String format(final String[] input) throws Exception {
51+
return format(input[0], input[1]);
52+
}
53+
54+
private static String format(final String input, final String location) throws Exception {
55+
Properties properties = new Properties();
56+
properties.put(SpotlessPreferences.RESOLVE_EXTERNAL_URI, "TRUE");
57+
EclipseXmlFormatterStepImpl formatter = new EclipseXmlFormatterStepImpl(properties);
58+
return formatter.format(input, location);
59+
}
60+
}

Diff for: _ext/eclipse-wtp/src/test/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseXmlFormatterStepImplTest.java

+14
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,27 @@ public void dtdRelativePath() throws Throwable {
100100
TEST_DATA.expected("dtd_relative.test"), output);
101101
}
102102

103+
@Test
104+
public void dtdExternalPath() throws Throwable {
105+
String output = format(TEST_DATA.input("dtd_external.test"), config -> {});
106+
assertNotEquals("External DTD resolved by default. Restrictions are applied by formatter.",
107+
TEST_DATA.expected("dtd_external.test"), output);
108+
}
109+
103110
@Test
104111
public void xsdRelativePath() throws Throwable {
105112
String output = format(TEST_DATA.input("xsd_relative.test"), config -> {});
106113
assertEquals("Relative XSD not resolved. Restrictions are not applied by formatter.",
107114
TEST_DATA.expected("xsd_relative.test"), output);
108115
}
109116

117+
@Test
118+
public void xsdExternalPath() throws Throwable {
119+
String output = format(TEST_DATA.input("xsd_external.test"), config -> {});
120+
assertNotEquals("External XSD resolved per default. Restrictions are applied by formatter.",
121+
TEST_DATA.expected("xsd_external.test"), output);
122+
}
123+
110124
@Test
111125
public void xsdNotFound() throws Throwable {
112126
String output = format(TEST_DATA.input("xsd_not_found.test"), config -> {});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE r SYSTEM "https://raw.githubusercontent.com/diffplug/spotless/master/_ext/eclipse-wtp/src/test/resources/xml/restrictions/test.dtd">
3+
<r>
4+
<x>indent
5+
this text</x>
6+
<y>preserve
7+
spaces</y>
8+
</r>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<t:r xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns:t="http://foo.bar/test"
4+
xsi:schemaLocation="http://foo.bar/test https://raw.githubusercontent.com/diffplug/spotless/master/_ext/eclipse-wtp/src/test/resources/xml/restrictions/test.xsd">
5+
<t:x>remove spaces</t:x>
6+
<t:y> preserve spaces </t:y>
7+
</t:r>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE r SYSTEM "https://raw.githubusercontent.com/diffplug/spotless/master/_ext/eclipse-wtp/src/test/resources/xml/restrictions/test.dtd">
3+
<r>
4+
<x>indent
5+
this text</x>
6+
<y>preserve
7+
spaces</y>
8+
</r>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<t:r xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:t="http://foo.bar/test" xsi:schemaLocation="http://foo.bar/test https://raw.githubusercontent.com/diffplug/spotless/master/_ext/eclipse-wtp/src/test/resources/xml/restrictions/test.xsd">
3+
<t:x> remove spaces </t:x><t:y> preserve spaces </t:y>
4+
</t:r>

Diff for: plugin-gradle/README.md

+14-6
Original file line numberDiff line numberDiff line change
@@ -495,18 +495,26 @@ The WTP formatter accept multiple configuration files. All Eclipse configuration
495495

496496
| Type | Configuration | File location
497497
| ---- | ------------------- | -------------
498-
| CSS | editor preferences | .metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.css.core.prefs
499-
| | cleanup preferences | .metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.css.core.prefs
500-
| HTML | editor preferences | .metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.html.core.prefs
501-
| | cleanup preferences | .metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.html.core.prefs
502-
| | embedded CSS | .metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.css.core.prefs
498+
| CSS | editor preferences | .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.wst.css.core.prefs
499+
| | cleanup preferences | .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.wst.css.core.prefs
500+
| HTML | editor preferences | .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.wst.html.core.prefs
501+
| | cleanup preferences | .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.wst.html.core.prefs
502+
| | embedded CSS | .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.wst.css.core.prefs
503503
| | embedded JS | Use the export in the Eclipse editor configuration dialog
504504
| JS | editor preferences | Use the export in the Eclipse editor configuration dialog
505505
| JSON | editor preferences | .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.wst.json.core.prefs
506-
| XML | editor preferences | .metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.xml.core.prefs
506+
| XML | editor preferences | .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.wst.xml.core.prefs
507507

508508
Note that `HTML` should be used for `X-HTML` sources instead of `XML`.
509509

510+
The Eclipse XML catalog cannot be configured for the Spotless WTP formatter, instead a
511+
user defined catalog file can be specified using the property `userCatalog`. Catalog versions
512+
1.0 and 1.1 are supported by Spotless.
513+
514+
Unlike Eclipse, Spotless WTP ignores per default external URIs in schema location hints and
515+
external entities. To allow the access of external URIs, set the property `resolveExternalURI`
516+
to true.
517+
510518
<a name="license-header"></a>
511519

512520
## License header options

Diff for: plugin-maven/README.md

+14-6
Original file line numberDiff line numberDiff line change
@@ -287,18 +287,26 @@ The WTP formatter accept multiple configuration files. All Eclipse configuration
287287

288288
| Type | Configuration | File location
289289
| ---- | ------------------- | -------------
290-
| CSS | editor preferences | .metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.css.core.prefs
291-
| | cleanup preferences | .metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.css.core.prefs
292-
| HTML | editor preferences | .metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.html.core.prefs
293-
| | cleanup preferences | .metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.html.core.prefs
294-
| | embedded CSS | .metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.css.core.prefs
290+
| CSS | editor preferences | .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.wst.css.core.prefs
291+
| | cleanup preferences | .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.wst.css.core.prefs
292+
| HTML | editor preferences | .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.wst.html.core.prefs
293+
| | cleanup preferences | .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.wst.html.core.prefs
294+
| | embedded CSS | .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.wst.css.core.prefs
295295
| | embedded JS | Use the export in the Eclipse editor configuration dialog
296296
| JS | editor preferences | Use the export in the Eclipse editor configuration dialog
297297
| JSON | editor preferences | .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.wst.json.core.prefs
298-
| XML | editor preferences | .metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.xml.core.prefs
298+
| XML | editor preferences | .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.wst.xml.core.prefs
299299

300300
Note that `HTML` should be used for `X-HTML` sources instead of `XML`.
301301

302+
The Eclipse XML catalog cannot be configured for the Spotless WTP formatter, instead a
303+
user defined catalog file can be specified using the property `userCatalog`. Catalog versions
304+
1.0 and 1.1 are supported by Spotless.
305+
306+
Unlike Eclipse, Spotless WTP ignores per default external URIs in schema location hints and
307+
external entities. To allow the access of external URIs, set the property `resolveExternalURI`
308+
to true.
309+
302310
<a name="invisible"></a>
303311

304312
## Line endings and encodings (invisible stuff)

0 commit comments

Comments
 (0)