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.util.ResourceUtils; 021 022/** 023 * Utility class for determining whether a given URL is a resource 024 * location that can be loaded via a {@link ResourcePatternResolver}. 025 * 026 * <p>Callers will usually assume that a location is a relative path 027 * if the {@link #isUrl(String)} method returns {@code false}. 028 * 029 * @author Juergen Hoeller 030 * @since 1.2.3 031 */ 032public abstract class ResourcePatternUtils { 033 034 /** 035 * Return whether the given resource location is a URL: either a 036 * special "classpath" or "classpath*" pseudo URL or a standard URL. 037 * @param resourceLocation the location String to check 038 * @return whether the location qualifies as a URL 039 * @see ResourcePatternResolver#CLASSPATH_ALL_URL_PREFIX 040 * @see org.springframework.util.ResourceUtils#CLASSPATH_URL_PREFIX 041 * @see org.springframework.util.ResourceUtils#isUrl(String) 042 * @see java.net.URL 043 */ 044 public static boolean isUrl(String resourceLocation) { 045 return (resourceLocation != null && 046 (resourceLocation.startsWith(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX) || 047 ResourceUtils.isUrl(resourceLocation))); 048 } 049 050 /** 051 * Return a default {@link ResourcePatternResolver} for the given {@link ResourceLoader}. 052 * <p>This might be the {@code ResourceLoader} itself, if it implements the 053 * {@code ResourcePatternResolver} extension, or a default 054 * {@link PathMatchingResourcePatternResolver} built on the given {@code ResourceLoader}. 055 * @param resourceLoader the ResourceLoader to build a pattern resolver for 056 * (may be {@code null} to indicate a default ResourceLoader) 057 * @return the ResourcePatternResolver 058 * @see PathMatchingResourcePatternResolver 059 */ 060 public static ResourcePatternResolver getResourcePatternResolver(ResourceLoader resourceLoader) { 061 if (resourceLoader instanceof ResourcePatternResolver) { 062 return (ResourcePatternResolver) resourceLoader; 063 } 064 else if (resourceLoader != null) { 065 return new PathMatchingResourcePatternResolver(resourceLoader); 066 } 067 else { 068 return new PathMatchingResourcePatternResolver(); 069 } 070 } 071 072}