From 32f323668f263fad0bc38a43c6d4e6a1f4579c8a Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Tue, 22 Sep 2009 15:48:46 +0000 Subject: [PATCH] URLProvider interface git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@817702 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 5 +++ .../ant/taskdefs/optional/TraXLiaison.java | 8 ++--- .../ant/types/resources/URLProvider.java | 35 +++++++++++++++++++ .../ant/types/resources/URLResource.java | 10 +++++- 4 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 src/main/org/apache/tools/ant/types/resources/URLProvider.java diff --git a/WHATSNEW b/WHATSNEW index c760f2f4f..a317a66cb 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -513,6 +513,11 @@ Other changes: are instances or subclasses of FileResource. Bugzilla Report 43348 + * There is now a URLProvider interface for resources that act as a + source of URLs. This should be used by tasks that require resources + to provide URLs, rather than require that all resources are + instances or subclasses of URLResource. + * Fixcrlf now gives better error messages on bad directory attributes. Bugzilla Report 43936 diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java b/src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java index a22fee083..48f3ecd2e 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java @@ -54,7 +54,7 @@ import org.apache.tools.ant.types.XMLCatalog; import org.apache.tools.ant.types.Resource; import org.apache.tools.ant.types.resources.FileProvider; import org.apache.tools.ant.types.resources.FileResource; -import org.apache.tools.ant.types.resources.URLResource; +import org.apache.tools.ant.types.resources.URLProvider; import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.util.JAXPUtils; import org.xml.sax.EntityResolver; @@ -266,13 +266,13 @@ public class TraXLiaison implements XSLTLiaison3, ErrorListener, XSLTLoggerAware } private String resourceToURI(Resource resource) { - // TODO turn URLResource into Provider FileProvider fp = (FileProvider) resource.as(FileProvider.class); if (fp != null) { return FILE_UTILS.toURI(fp.getFile().getAbsolutePath()); } - if (resource instanceof URLResource) { - URL u = ((URLResource) resource).getURL(); + URLProvider up = (URLProvider) resource.as(URLProvider.class); + if (up != null) { + URL u = up.getURL(); return String.valueOf(u); } else { return resource.getName(); diff --git a/src/main/org/apache/tools/ant/types/resources/URLProvider.java b/src/main/org/apache/tools/ant/types/resources/URLProvider.java new file mode 100644 index 000000000..81bb66979 --- /dev/null +++ b/src/main/org/apache/tools/ant/types/resources/URLProvider.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.tools.ant.types.resources; + +import java.net.URL; + +/** + * This is an interface that resources that can provide an URL should implement. + * This is a refactoring of {@link URLResource}, to allow other resources + * to act as sources of URLs. + * @since Ant 1.8 + */ +public interface URLProvider { + /** + * Get the URL represented by this Resource. + * @return the file. + */ + URL getURL(); +} diff --git a/src/main/org/apache/tools/ant/types/resources/URLResource.java b/src/main/org/apache/tools/ant/types/resources/URLResource.java index a9fd4d32d..fc4c05d20 100644 --- a/src/main/org/apache/tools/ant/types/resources/URLResource.java +++ b/src/main/org/apache/tools/ant/types/resources/URLResource.java @@ -39,7 +39,7 @@ import org.apache.tools.ant.util.FileUtils; * Exposes a URL as a Resource. * @since Ant 1.7 */ -public class URLResource extends Resource { +public class URLResource extends Resource implements URLProvider { private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); private static final int NULL_URL = Resource.getMagicNumber("null URL".getBytes()); @@ -61,6 +61,14 @@ public class URLResource extends Resource { setURL(u); } + /** + * Convenience constructor. + * @param u holds the URL to expose. + */ + public URLResource(URLProvider u) { + setURL(u.getURL()); + } + /** * Convenience constructor. * @param f the File to set as a URL.