-
Notifications
You must be signed in to change notification settings - Fork 459
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #369 from diffplug/eclipse-wtp-prevent-external-uri
WTP - Ignore external URIs by default
- Loading branch information
Showing
17 changed files
with
253 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...ain/java/com/diffplug/spotless/extra/eclipse/wtp/PreventExternalURIResolverExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2016 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.extra.eclipse.wtp; | ||
|
||
import org.eclipse.core.resources.IFile; | ||
import org.eclipse.emf.common.util.URI; | ||
import org.eclipse.wst.common.uriresolver.internal.provisional.URIResolverExtension; | ||
import org.osgi.framework.BundleActivator; | ||
import org.osgi.framework.BundleContext; | ||
|
||
/** | ||
* The URI resolver extension | ||
*/ | ||
public class PreventExternalURIResolverExtension implements URIResolverExtension, BundleActivator { | ||
|
||
private static final String REFUSE_EXTERNAL_URI = "file://refused.external.uri"; | ||
|
||
/** | ||
* @param file the in-workspace base resource, if one exists | ||
* @param baseLocation - the location of the resource that contains the uri | ||
* @param publicId - an optional public identifier (i.e. namespace name), or null if none | ||
* @param systemId - an absolute or relative URI, or null if none | ||
* | ||
* @return an absolute URI or null if this extension can not resolve this reference | ||
*/ | ||
@Override | ||
public String resolve(IFile file, String baseLocation, String publicId, String systemId) { | ||
if (null != systemId) { | ||
try { | ||
URI proposalByPreviousResolver = org.eclipse.emf.common.util.URI.createURI(systemId); | ||
String host = proposalByPreviousResolver.host(); | ||
/* | ||
* The host is empty (not null) | ||
*/ | ||
if (!(null == host || host.isEmpty())) { | ||
return REFUSE_EXTERNAL_URI; | ||
} | ||
} catch (IllegalArgumentException ignore) { | ||
//If it is no a valid URI, there is nothing to do here. | ||
} | ||
} | ||
return null; //Don't alter the proposal of previous resolver extensions by proposing something else | ||
} | ||
|
||
@Override | ||
public void start(BundleContext context) throws Exception { | ||
//Nothing to do. The bundle-activator interface only allows to load this extension as a stand-alone plugin. | ||
} | ||
|
||
@Override | ||
public void stop(BundleContext context) throws Exception { | ||
//Nothing to do. The bundle-activator interface only allows to load this extension as a stand-alone plugin. | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Manifest-Version: 1.0 | ||
Bundle-ManifestVersion: 2 | ||
Bundle-SymbolicName: com.diffplug.gradle.spotless.eclipse.wtp; singleton:=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<?eclipse version="3.0"?> | ||
<plugin> | ||
<!-- Prevents resolution of external URIs by redirecting them to invalid external URI. --> | ||
<!-- The low priority assures that the org.eclipse.wst.common.uriresolver.internal.ExtensibleURIResolver --> | ||
<!-- ask this resolver extension at the end and let it override all previous results. Hence the term --> | ||
<!-- "low priority' might be missleading. In fact the result of this extension overrules all other --> | ||
<!-- physical resolvers with higher priority. --> | ||
<extension point="org.eclipse.wst.common.uriresolver.resolverExtensions"> | ||
<resolverExtension stage="physical" priority="low" class="com.diffplug.spotless.extra.eclipse.wtp.PreventExternalURIResolverExtension"> | ||
</resolverExtension> | ||
</extension> | ||
</plugin> |
60 changes: 60 additions & 0 deletions
60
...diffplug/spotless/extra/eclipse/wtp/EclipseXmlFormatterStepImplAllowExternalURIsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2016 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.extra.eclipse.wtp; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.util.Properties; | ||
|
||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import com.diffplug.spotless.extra.eclipse.wtp.sse.SpotlessPreferences; | ||
|
||
/** Test configuration allowExternalURI=false */ | ||
public class EclipseXmlFormatterStepImplAllowExternalURIsTest { | ||
private static TestData TEST_DATA = null; | ||
|
||
@BeforeClass | ||
public static void initializeStatic() throws Exception { | ||
TEST_DATA = TestData.getTestDataOnFileSystem("xml"); | ||
} | ||
|
||
@Test | ||
public void dtdExternalPath() throws Throwable { | ||
String output = format(TEST_DATA.input("dtd_external.test")); | ||
assertEquals("External DTD not resolved. Restrictions are not applied by formatter.", | ||
TEST_DATA.expected("dtd_external.test"), output); | ||
} | ||
|
||
@Test | ||
public void xsdExternalPath() throws Throwable { | ||
String output = format(TEST_DATA.input("xsd_external.test")); | ||
assertEquals("External XSD not resolved. Restrictions are not applied by formatter.", | ||
TEST_DATA.expected("xsd_external.test"), output); | ||
} | ||
|
||
private static String format(final String[] input) throws Exception { | ||
return format(input[0], input[1]); | ||
} | ||
|
||
private static String format(final String input, final String location) throws Exception { | ||
Properties properties = new Properties(); | ||
properties.put(SpotlessPreferences.RESOLVE_EXTERNAL_URI, "TRUE"); | ||
EclipseXmlFormatterStepImpl formatter = new EclipseXmlFormatterStepImpl(properties); | ||
return formatter.format(input, location); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
_ext/eclipse-wtp/src/test/resources/xml/expected/dtd_external.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE r SYSTEM "https://raw.githubusercontent.com/diffplug/spotless/master/_ext/eclipse-wtp/src/test/resources/xml/restrictions/test.dtd"> | ||
<r> | ||
<x>indent | ||
this text</x> | ||
<y>preserve | ||
spaces</y> | ||
</r> |
7 changes: 7 additions & 0 deletions
7
_ext/eclipse-wtp/src/test/resources/xml/expected/xsd_external.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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"> | ||
<t:x>remove spaces</t:x> | ||
<t:y> preserve spaces </t:y> | ||
</t:r> |
8 changes: 8 additions & 0 deletions
8
_ext/eclipse-wtp/src/test/resources/xml/input/dtd_external.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE r SYSTEM "https://raw.githubusercontent.com/diffplug/spotless/master/_ext/eclipse-wtp/src/test/resources/xml/restrictions/test.dtd"> | ||
<r> | ||
<x>indent | ||
this text</x> | ||
<y>preserve | ||
spaces</y> | ||
</r> |
4 changes: 4 additions & 0 deletions
4
_ext/eclipse-wtp/src/test/resources/xml/input/xsd_external.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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"> | ||
<t:x> remove spaces </t:x><t:y> preserve spaces </t:y> | ||
</t:r> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters