|
| 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 | +} |
0 commit comments