01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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:
17: package org.springframework.core.io.support;
18:
19: import org.springframework.core.io.ResourceLoader;
20: import org.springframework.util.Assert;
21: import org.springframework.util.ResourceUtils;
22:
23: /**
24: * Utility class for determining whether a given URL is a resource
25: * location that can be loaded via a ResourcePatternResolver.
26: *
27: * <p>Callers will usually assume that a location is a relative path
28: * if the {@link #isUrl(String)} method returns <code>false</code>.
29: *
30: * @author Juergen Hoeller
31: * @since 1.2.3
32: */
33: public abstract class ResourcePatternUtils {
34:
35: /**
36: * Return whether the given resource location is a URL: either a
37: * special "classpath" or "classpath*" pseudo URL or a standard URL.
38: * @param resourceLocation the location String to check
39: * @return whether the location qualifies as a URL
40: * @see ResourcePatternResolver#CLASSPATH_ALL_URL_PREFIX
41: * @see org.springframework.util.ResourceUtils#CLASSPATH_URL_PREFIX
42: * @see org.springframework.util.ResourceUtils#isUrl(String)
43: * @see java.net.URL
44: */
45: public static boolean isUrl(String resourceLocation) {
46: return (resourceLocation != null && (resourceLocation
47: .startsWith(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX) || ResourceUtils
48: .isUrl(resourceLocation)));
49: }
50:
51: /**
52: * Return a default ResourcePatternResolver for the given ResourceLoader.
53: * <p>This might be the ResourceLoader itself, if it implements the
54: * ResourcePatternResolver extension, or a PathMatchingResourcePatternResolver
55: * built on the given ResourceLoader.
56: * @param resourceLoader the ResourceLoader to build a pattern resolver for
57: * (not <code>null</code>)
58: * @return the ResourcePatternResolver
59: * @see PathMatchingResourcePatternResolver
60: */
61: public static ResourcePatternResolver getResourcePatternResolver(
62: ResourceLoader resourceLoader) {
63: Assert.notNull(resourceLoader,
64: "ResourceLoader must not be null");
65: if (resourceLoader instanceof ResourcePatternResolver) {
66: return (ResourcePatternResolver) resourceLoader;
67: } else {
68: return new PathMatchingResourcePatternResolver(
69: resourceLoader);
70: }
71: }
72:
73: }
|