001/* 002 * Copyright 2002-2019 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.core.io.support; 018 019import org.springframework.core.io.ResourceLoader; 020import org.springframework.lang.Nullable; 021import org.springframework.util.ResourceUtils; 022 023/** 024 * Utility class for determining whether a given URL is a resource 025 * location that can be loaded via a {@link ResourcePatternResolver}. 026 * 027 * <p>Callers will usually assume that a location is a relative path 028 * if the {@link #isUrl(String)} method returns {@code false}. 029 * 030 * @author Juergen Hoeller 031 * @since 1.2.3 032 */ 033public abstract class ResourcePatternUtils { 034 035 /** 036 * Return whether the given resource location is a URL: either a 037 * special "classpath" or "classpath*" pseudo URL or a standard URL. 038 * @param resourceLocation the location String to check 039 * @return whether the location qualifies as a URL 040 * @see ResourcePatternResolver#CLASSPATH_ALL_URL_PREFIX 041 * @see org.springframework.util.ResourceUtils#CLASSPATH_URL_PREFIX 042 * @see org.springframework.util.ResourceUtils#isUrl(String) 043 * @see java.net.URL 044 */ 045 public static boolean isUrl(@Nullable String resourceLocation) { 046 return (resourceLocation != null && 047 (resourceLocation.startsWith(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX) || 048 ResourceUtils.isUrl(resourceLocation))); 049 } 050 051 /** 052 * Return a default {@link ResourcePatternResolver} for the given {@link ResourceLoader}. 053 * <p>This might be the {@code ResourceLoader} itself, if it implements the 054 * {@code ResourcePatternResolver} extension, or a default 055 * {@link PathMatchingResourcePatternResolver} built on the given {@code ResourceLoader}. 056 * @param resourceLoader the ResourceLoader to build a pattern resolver for 057 * (may be {@code null} to indicate a default ResourceLoader) 058 * @return the ResourcePatternResolver 059 * @see PathMatchingResourcePatternResolver 060 */ 061 public static ResourcePatternResolver getResourcePatternResolver(@Nullable ResourceLoader resourceLoader) { 062 if (resourceLoader instanceof ResourcePatternResolver) { 063 return (ResourcePatternResolver) resourceLoader; 064 } 065 else if (resourceLoader != null) { 066 return new PathMatchingResourcePatternResolver(resourceLoader); 067 } 068 else { 069 return new PathMatchingResourcePatternResolver(); 070 } 071 } 072 073}