| 1 | package fr.sii.ogham.core.resource.resolver; | |
| 2 | ||
| 3 | import java.io.File; | |
| 4 | import java.io.IOException; | |
| 5 | import java.io.InputStream; | |
| 6 | import java.util.List; | |
| 7 | ||
| 8 | import org.slf4j.Logger; | |
| 9 | import org.slf4j.LoggerFactory; | |
| 10 | ||
| 11 | import fr.sii.ogham.core.exception.resource.ResourceResolutionException; | |
| 12 | import fr.sii.ogham.core.resource.ByteResource; | |
| 13 | import fr.sii.ogham.core.resource.Resource; | |
| 14 | import fr.sii.ogham.core.resource.path.ResolvedPath; | |
| 15 | import fr.sii.ogham.core.resource.path.ResolvedResourcePath; | |
| 16 | import fr.sii.ogham.core.resource.path.ResourcePath; | |
| 17 | ||
| 18 | /** | |
| 19 | * Resource resolver that searches for the resource into the classpath. This | |
| 20 | * implementation is able to manage path starting or not with '/'. The resource | |
| 21 | * resolution needs an absolute class path. The generated resource information | |
| 22 | * will only contain a reference to the stream of the found resource. If the | |
| 23 | * path points nowhere, an {@link ResourceResolutionException} is thrown to | |
| 24 | * indicate that the resource couldn't be found. | |
| 25 | * | |
| 26 | * @author Aurélien Baudet | |
| 27 | * @see ByteResource | |
| 28 | */ | |
| 29 | public class ClassPathResolver extends AbstractPrefixedLookupPathResolver implements RelativisableResourceResolver { | |
| 30 | ||
| 31 | private static final Logger LOG = LoggerFactory.getLogger(ClassPathResolver.class); | |
| 32 | ||
| 33 | public ClassPathResolver(List<String> lookups) { | |
| 34 | super(lookups); | |
| 35 | } | |
| 36 | ||
| 37 | public ClassPathResolver(String... lookups) { | |
| 38 | super(lookups); | |
| 39 | } | |
| 40 | ||
| 41 | @Override | |
| 42 | public Resource getResource(ResourcePath path) throws ResourceResolutionException { | |
| 43 | ResolvedPath resourcePath = resolve(path); | |
| 44 |
1
1. getResource : replaced return value with null for fr/sii/ogham/core/resource/resolver/ClassPathResolver::getResource → RUN_ERROR |
return getResource(resourcePath); |
| 45 | } | |
| 46 | ||
| 47 | @Override | |
| 48 | protected Resource getResource(ResolvedPath resourcePath) throws ResourceResolutionException { | |
| 49 | try { | |
| 50 | LOG.debug("Loading resource {} from classpath...", resourcePath); | |
| 51 | String resolvedPath = resourcePath.getResolvedPath(); | |
| 52 |
1
1. getResource : negated conditional → RUN_ERROR |
InputStream stream = getClass().getClassLoader().getResourceAsStream(resolvedPath.startsWith("/") ? resolvedPath.substring(1) : resolvedPath); |
| 53 |
1
1. getResource : negated conditional → RUN_ERROR |
if (stream == null) { |
| 54 | throw new ResourceResolutionException("Resource " + resolvedPath + " not found in the classpath", resourcePath); | |
| 55 | } | |
| 56 | LOG.debug("Resource {} available in the classpath...", resourcePath); | |
| 57 |
1
1. getResource : replaced return value with null for fr/sii/ogham/core/resource/resolver/ClassPathResolver::getResource → RUN_ERROR |
return new ByteResource(extractName(resolvedPath), stream); |
| 58 | } catch (IOException e) { | |
| 59 | throw new ResourceResolutionException("The resource " + resourcePath.getOriginalPath() + " is not readable", resourcePath, e); | |
| 60 | } | |
| 61 | } | |
| 62 | ||
| 63 | private static String extractName(String path) { | |
| 64 |
1
1. extractName : replaced return value with "" for fr/sii/ogham/core/resource/resolver/ClassPathResolver::extractName → RUN_ERROR |
return new File(path).getName(); |
| 65 | } | |
| 66 | ||
| 67 | ||
| 68 | @Override | |
| 69 | public boolean isAbsolute(ResourcePath path) { | |
| 70 | ResolvedPath resourcePath = resolve(path); | |
| 71 |
2
1. isAbsolute : replaced boolean return with true for fr/sii/ogham/core/resource/resolver/ClassPathResolver::isAbsolute → RUN_ERROR 2. isAbsolute : replaced boolean return with false for fr/sii/ogham/core/resource/resolver/ClassPathResolver::isAbsolute → RUN_ERROR |
return resourcePath.getResolvedPath().startsWith("/"); |
| 72 | } | |
| 73 | ||
| 74 | @Override | |
| 75 | public ResolvedPath resolve(ResourcePath relativePath, String prefixPath, String suffixPath) { | |
| 76 | ResolvedPath resourcePath = resolve(relativePath); | |
| 77 | String lookup = getLookup(relativePath); | |
| 78 |
1
1. resolve : replaced return value with null for fr/sii/ogham/core/resource/resolver/ClassPathResolver::resolve → RUN_ERROR |
return new ResolvedResourcePath(relativePath, lookup, prefixPath + resourcePath.getResolvedPath() + suffixPath); |
| 79 | } | |
| 80 | } | |
Mutations | ||
| 44 |
1.1 |
|
| 52 |
1.1 |
|
| 53 |
1.1 |
|
| 57 |
1.1 |
|
| 64 |
1.1 |
|
| 71 |
1.1 2.2 |
|
| 78 |
1.1 |