| 1 | package fr.sii.ogham.core.resource.resolver; | |
| 2 | ||
| 3 | import org.slf4j.Logger; | |
| 4 | import org.slf4j.LoggerFactory; | |
| 5 | ||
| 6 | import fr.sii.ogham.core.exception.resource.ResourceResolutionException; | |
| 7 | import fr.sii.ogham.core.resource.Resource; | |
| 8 | import fr.sii.ogham.core.resource.path.ResolvedPath; | |
| 9 | import fr.sii.ogham.core.resource.path.ResolvedResourcePath; | |
| 10 | import fr.sii.ogham.core.resource.path.ResourcePath; | |
| 11 | ||
| 12 | /** | |
| 13 | * <p> | |
| 14 | * Decorator resource resolver that use parent path and extension for resource | |
| 15 | * resolution. | |
| 16 | * </p> | |
| 17 | * <p> | |
| 18 | * For example, the parent path values "email/user/" and the extension is | |
| 19 | * ".html". The resource name is "hello". This resource resolver appends the | |
| 20 | * parent path, the resource name and the extension generating the path | |
| 21 | * "email/user/hello.html". | |
| 22 | * </p> | |
| 23 | * <p> | |
| 24 | * Once the path is generated, then this implementation delegates the real | |
| 25 | * resource resolution to another implementation. | |
| 26 | * </p> | |
| 27 | * | |
| 28 | * @author Aurélien Baudet | |
| 29 | * | |
| 30 | */ | |
| 31 | public class RelativeResolver implements DelegateResourceResolver { | |
| 32 | private static final Logger LOG = LoggerFactory.getLogger(RelativeResolver.class); | |
| 33 | ||
| 34 | /** | |
| 35 | * The parent path to add to the resource name (or path) | |
| 36 | */ | |
| 37 | private String parentPath; | |
| 38 | ||
| 39 | /** | |
| 40 | * The suffix to add to the resource name (or path) | |
| 41 | */ | |
| 42 | private String extension; | |
| 43 | ||
| 44 | /** | |
| 45 | * The delegate resolver that will do the real resource resolution | |
| 46 | */ | |
| 47 | private RelativisableResourceResolver delegate; | |
| 48 | ||
| 49 | /** | |
| 50 | * Initialize the resolver with the mandatory delegate and a parent path. No | |
| 51 | * extension will be appended to the resource path. | |
| 52 | * | |
| 53 | * @param delegate | |
| 54 | * the resolver that will do the real resource resolution | |
| 55 | * @param parentPath | |
| 56 | * a string to add before the resource path | |
| 57 | */ | |
| 58 | public RelativeResolver(RelativisableResourceResolver delegate, String parentPath) { | |
| 59 | this(delegate, parentPath, ""); | |
| 60 | } | |
| 61 | ||
| 62 | /** | |
| 63 | * Initialize the resolver with the mandatory delegate, a parent path and a | |
| 64 | * extension. | |
| 65 | * | |
| 66 | * @param delegate | |
| 67 | * the resolver that will do the real resource resolution | |
| 68 | * @param parentPath | |
| 69 | * a string to add before the resource path | |
| 70 | * @param extension | |
| 71 | * a string to add after the resource path | |
| 72 | */ | |
| 73 | public RelativeResolver(RelativisableResourceResolver delegate, String parentPath, String extension) { | |
| 74 | super(); | |
| 75 |
1
1. <init> : negated conditional → RUN_ERROR |
this.parentPath = parentPath == null ? "" : parentPath; |
| 76 |
1
1. <init> : negated conditional → RUN_ERROR |
this.extension = extension == null ? "" : extension; |
| 77 | this.delegate = delegate; | |
| 78 | } | |
| 79 | ||
| 80 | @Override | |
| 81 | public Resource getResource(ResourcePath path) throws ResourceResolutionException { | |
| 82 |
1
1. getResource : negated conditional → RUN_ERROR |
if (delegate.isAbsolute(path)) { |
| 83 | LOG.trace("Absolute resource path {} => do not add parentPath/extension", path); | |
| 84 |
1
1. getResource : replaced return value with null for fr/sii/ogham/core/resource/resolver/RelativeResolver::getResource → RUN_ERROR |
return delegate.getResource(path); |
| 85 | } | |
| 86 | // special case when extension is already set | |
| 87 |
2
1. getResource : negated conditional → NO_COVERAGE 2. getResource : negated conditional → RUN_ERROR |
if (!extension.isEmpty() && path.getOriginalPath().endsWith(extension)) { |
| 88 | LOG.debug("Adding parentPath '{}' and skipping extension '{}' to the resource path {}", parentPath, extension, path); | |
| 89 |
1
1. getResource : replaced return value with null for fr/sii/ogham/core/resource/resolver/RelativeResolver::getResource → NO_COVERAGE |
return delegate.getResource(delegate.resolve(path, parentPath, "")); |
| 90 | } | |
| 91 | LOG.debug("Adding parentPath '{}' and extension '{}' to the resource path {}", parentPath, extension, path); | |
| 92 |
1
1. getResource : replaced return value with null for fr/sii/ogham/core/resource/resolver/RelativeResolver::getResource → RUN_ERROR |
return delegate.getResource(delegate.resolve(path, parentPath, extension)); |
| 93 | } | |
| 94 | ||
| 95 | @Override | |
| 96 | public boolean supports(ResourcePath path) { | |
| 97 |
2
1. supports : replaced boolean return with false for fr/sii/ogham/core/resource/resolver/RelativeResolver::supports → NO_COVERAGE 2. supports : replaced boolean return with true for fr/sii/ogham/core/resource/resolver/RelativeResolver::supports → NO_COVERAGE |
return delegate.supports(path); |
| 98 | } | |
| 99 | ||
| 100 | @Override | |
| 101 | public ResolvedPath resolve(ResourcePath path) { | |
| 102 | ResolvedPath resourcePath = delegate.resolve(path); | |
| 103 |
1
1. resolve : negated conditional → NO_COVERAGE |
if (delegate.isAbsolute(resourcePath)) { |
| 104 | LOG.trace("Absolute resource path {} => do not add parentPath/extension", path); | |
| 105 |
1
1. resolve : replaced return value with null for fr/sii/ogham/core/resource/resolver/RelativeResolver::resolve → NO_COVERAGE |
return resourcePath; |
| 106 | } | |
| 107 | // special case when extension is already set | |
| 108 |
2
1. resolve : negated conditional → NO_COVERAGE 2. resolve : negated conditional → NO_COVERAGE |
if (!extension.isEmpty() && resourcePath.getResolvedPath().endsWith(extension)) { |
| 109 | LOG.debug("Adding parentPath ({}) and skipping extension ({}) to the resource path {}", parentPath, extension, path); | |
| 110 |
1
1. resolve : replaced return value with null for fr/sii/ogham/core/resource/resolver/RelativeResolver::resolve → NO_COVERAGE |
return new ResolvedResourcePath(path, resourcePath.getLookup(), parentPath + resourcePath.getResolvedPath()); |
| 111 | } | |
| 112 | LOG.debug("Adding parentPath ({}) and extension ({}) to the resource path {}", parentPath, extension, path); | |
| 113 |
1
1. resolve : replaced return value with null for fr/sii/ogham/core/resource/resolver/RelativeResolver::resolve → NO_COVERAGE |
return new ResolvedResourcePath(path, resourcePath.getLookup(), parentPath + resourcePath.getResolvedPath() + extension); |
| 114 | } | |
| 115 | ||
| 116 | @Override | |
| 117 | public ResourceResolver getActualResourceResolver() { | |
| 118 |
2
1. getActualResourceResolver : negated conditional → NO_COVERAGE 2. getActualResourceResolver : replaced return value with null for fr/sii/ogham/core/resource/resolver/RelativeResolver::getActualResourceResolver → NO_COVERAGE |
return delegate instanceof DelegateResourceResolver ? ((RelativeResolver) delegate).getActualResourceResolver() : delegate; |
| 119 | } | |
| 120 | ||
| 121 | @Override | |
| 122 | public String toString() { | |
| 123 |
1
1. toString : replaced return value with "" for fr/sii/ogham/core/resource/resolver/RelativeResolver::toString → NO_COVERAGE |
return "RelativeResolver [parentPath=" + parentPath + ", extension=" + extension + ", delegate=" + delegate + "]"; |
| 124 | } | |
| 125 | } | |
Mutations | ||
| 75 |
1.1 |
|
| 76 |
1.1 |
|
| 82 |
1.1 |
|
| 84 |
1.1 |
|
| 87 |
1.1 2.2 |
|
| 89 |
1.1 |
|
| 92 |
1.1 |
|
| 97 |
1.1 2.2 |
|
| 103 |
1.1 |
|
| 105 |
1.1 |
|
| 108 |
1.1 2.2 |
|
| 110 |
1.1 |
|
| 113 |
1.1 |
|
| 118 |
1.1 2.2 |
|
| 123 |
1.1 |