AbstractSingleResolutionBuilder.java

1
package fr.sii.ogham.core.builder.resolution;
2
3
import static java.util.Arrays.asList;
4
5
import java.util.ArrayList;
6
import java.util.List;
7
8
import org.slf4j.Logger;
9
import org.slf4j.LoggerFactory;
10
11
import fr.sii.ogham.core.builder.Builder;
12
import fr.sii.ogham.core.builder.configuration.ConfigurationValueBuilderHelper;
13
import fr.sii.ogham.core.builder.context.BuildContext;
14
import fr.sii.ogham.core.fluent.AbstractParent;
15
import fr.sii.ogham.core.resource.resolver.RelativeResolver;
16
import fr.sii.ogham.core.resource.resolver.RelativisableResourceResolver;
17
import fr.sii.ogham.core.resource.resolver.ResourceResolver;
18
19
/**
20
 * Base implementation to handle lookup prefix configuration. Path prefix/suffix
21
 * configuration is also partially managed. As path prefix/suffix for string
22
 * resolution has no sense, this base class doesn't implement
23
 * {@link PrefixSuffixBuilder}.
24
 * 
25
 * @author Aurélien Baudet
26
 *
27
 * @param <MYSELF>
28
 *            The type of this instance. This is needed to have the right return
29
 *            type for fluent chaining with inheritance
30
 * @param <P>
31
 *            the type of the parent builder (when calling {@link #and()}
32
 *            method)
33
 */
34
@SuppressWarnings("squid:S00119")
35
public abstract class AbstractSingleResolutionBuilder<MYSELF extends AbstractSingleResolutionBuilder<MYSELF, P>, P> extends AbstractParent<P> implements Builder<ResourceResolver> {
36
	private static final Logger LOG = LoggerFactory.getLogger(AbstractSingleResolutionBuilder.class);
37
38
	protected final BuildContext buildContext;
39
	protected final List<String> lookups;
40
	protected final ConfigurationValueBuilderHelper<MYSELF, String> pathPrefixValueBuilder;
41
	protected final ConfigurationValueBuilderHelper<MYSELF, String> pathSuffixValueBuilder;
42
	protected final MYSELF myself;
43
44
	@SuppressWarnings("unchecked")
45
	protected AbstractSingleResolutionBuilder(Class<?> selfType, P parent, BuildContext buildContext) {
46
		super(parent);
47
		myself = (MYSELF) selfType.cast(this);
48
		this.buildContext = buildContext;
49
		lookups = new ArrayList<>();
50
		pathPrefixValueBuilder = buildContext.newConfigurationValueBuilder(myself, String.class);
51
		pathSuffixValueBuilder = buildContext.newConfigurationValueBuilder(myself, String.class);
52
	}
53
54
	/**
55
	 * Configure lookup prefix. For example:
56
	 * 
57
	 * <pre>
58
	 * .classpath().lookup("classpath:");
59
	 * 
60
	 * // path prefixed by classpath: matches 
61
	 * // then classpath resolver is used
62
	 * resourceResolver.getResource("classpath:foo/bar.html");
63
	 * // path is not prefixed (or using another prefix) doesn't match 
64
	 * // then classpath resolver is not used
65
	 * resourceResolver.getResource("foo/bar.html");
66
	 * </pre>
67
	 * 
68
	 * <p>
69
	 * Several lookups can be provided:
70
	 * 
71
	 * <pre>
72
	 * .classpath().lookup("classpath:", "cp:");
73
	 * </pre>
74
	 * 
75
	 * If a path starts with one of the prefix ("classpath:" or "cp:"), the
76
	 * corresponding resolver (classpath resolver in this example) is used to
77
	 * resolve the file.
78
	 * 
79
	 * <p>
80
	 * Lookup may be empty meaning that if the path has no prefix, the
81
	 * corresponding resolver is use as default. For example:
82
	 * 
83
	 * <pre>
84
	 * .classpath().lookup("");
85
	 * </pre>
86
	 * 
87
	 * If the path is "foo/bar.html" is provided then the classpath resolver is
88
	 * used.
89
	 * 
90
	 * @param prefix
91
	 *            one or several prefixes that indicates which resolver to use
92
	 *            according to path
93
	 * @return this instance for fluent chaining
94
	 */
95
	public MYSELF lookup(String... prefix) {
96
		this.lookups.addAll(asList(prefix));
97 1 1. lookup : replaced return value with null for fr/sii/ogham/core/builder/resolution/AbstractSingleResolutionBuilder::lookup → NO_COVERAGE
		return myself;
98
	}
99
100
	/**
101
	 * Configure lookup prefix. For example:
102
	 * 
103
	 * <pre>
104
	 * .classpath().lookup("classpath:");
105
	 * 
106
	 * // path prefixed by classpath: matches 
107
	 * // then classpath resolver is used
108
	 * resourceResolver.getResource("classpath:foo/bar.html");
109
	 * // path is not prefixed (or using another prefix) doesn't match 
110
	 * // then classpath resolver is not used
111
	 * resourceResolver.getResource("foo/bar.html");
112
	 * </pre>
113
	 * 
114
	 * <p>
115
	 * Several lookups can be provided:
116
	 * 
117
	 * <pre>
118
	 * .classpath().lookup("classpath:", "cp:");
119
	 * </pre>
120
	 * 
121
	 * If a path starts with one of the prefix ("classpath:" or "cp:"), the
122
	 * corresponding resolver (classpath resolver in this example) is used to
123
	 * resolve the file.
124
	 * 
125
	 * <p>
126
	 * Lookup may be empty meaning that if the path has no prefix, the
127
	 * corresponding resolver is use as default. For example:
128
	 * 
129
	 * <pre>
130
	 * .classpath().lookup("");
131
	 * </pre>
132
	 * 
133
	 * If the path is "foo/bar.html" is provided then the classpath resolver is
134
	 * used.
135
	 * 
136
	 * @param prefix
137
	 *            one or several prefixes that indicates which resolver to use
138
	 *            according to path
139
	 * @return this instance for fluent chaining
140
	 */
141
	public MYSELF lookup(List<String> prefix) {
142
		this.lookups.addAll(prefix);
143 1 1. lookup : replaced return value with null for fr/sii/ogham/core/builder/resolution/AbstractSingleResolutionBuilder::lookup → RUN_ERROR
		return myself;
144
	}
145
146
	@Override
147
	public ResourceResolver build() {
148
		ResourceResolver resolver = buildContext.register(createResolver());
149 1 1. build : negated conditional → RUN_ERROR
		if (!(resolver instanceof RelativisableResourceResolver)) {
150 1 1. build : replaced return value with null for fr/sii/ogham/core/builder/resolution/AbstractSingleResolutionBuilder::build → RUN_ERROR
			return resolver;
151
		}
152
		String resolvedPathPrefix = pathPrefixValueBuilder.getValue("");
153
		String resolvedPathSuffix = pathSuffixValueBuilder.getValue("");
154 2 1. build : negated conditional → RUN_ERROR
2. build : negated conditional → RUN_ERROR
		if (!resolvedPathPrefix.isEmpty() || !resolvedPathSuffix.isEmpty()) {
155
			LOG.debug("Using parentPath {} and extension {} for resource resolution", resolvedPathPrefix, resolvedPathSuffix);
156
			resolver = buildContext.register(new RelativeResolver((RelativisableResourceResolver) resolver, resolvedPathPrefix, resolvedPathSuffix));
157
		}
158 1 1. build : replaced return value with null for fr/sii/ogham/core/builder/resolution/AbstractSingleResolutionBuilder::build → RUN_ERROR
		return resolver;
159
	}
160
161
	protected abstract ResourceResolver createResolver();
162
163
	/**
164
	 * Provide the list of registered lookups
165
	 * 
166
	 * @return the list of lookups
167
	 */
168
	public List<String> getLookups() {
169 1 1. getLookups : replaced return value with Collections.emptyList for fr/sii/ogham/core/builder/resolution/AbstractSingleResolutionBuilder::getLookups → RUN_ERROR
		return lookups;
170
	}
171
}

Mutations

97

1.1
Location : lookup
Killed by :
replaced return value with null for fr/sii/ogham/core/builder/resolution/AbstractSingleResolutionBuilder::lookup → NO_COVERAGE

143

1.1
Location : lookup
Killed by :
replaced return value with null for fr/sii/ogham/core/builder/resolution/AbstractSingleResolutionBuilder::lookup → RUN_ERROR

149

1.1
Location : build
Killed by :
negated conditional → RUN_ERROR

150

1.1
Location : build
Killed by :
replaced return value with null for fr/sii/ogham/core/builder/resolution/AbstractSingleResolutionBuilder::build → RUN_ERROR

154

1.1
Location : build
Killed by :
negated conditional → RUN_ERROR

2.2
Location : build
Killed by :
negated conditional → RUN_ERROR

158

1.1
Location : build
Killed by :
replaced return value with null for fr/sii/ogham/core/builder/resolution/AbstractSingleResolutionBuilder::build → RUN_ERROR

169

1.1
Location : getLookups
Killed by :
replaced return value with Collections.emptyList for fr/sii/ogham/core/builder/resolution/AbstractSingleResolutionBuilder::getLookups → RUN_ERROR

Active mutators

Tests examined


Report generated by PIT 1.13.1