Skip to content

Commit be60ee1

Browse files
committed
Internal refactoring
1 parent 0bd8c21 commit be60ee1

9 files changed

Lines changed: 38 additions & 45 deletions

src/main/java/org/apache/commons/configuration2/io/AbsoluteNameLocationStrategy.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,16 @@
2222
import org.apache.commons.lang3.StringUtils;
2323

2424
/**
25-
* <p>
2625
* A specialized implementation of {@code FileLocationStrategy} which checks whether the provided file name is already
2726
* an absolute file name.
28-
* </p>
2927
* <p>
3028
* This strategy ignores the URL and the base path stored in the passed in {@link FileLocator}. It is only triggered by
3129
* absolute names in the locator's {@code fileName} component.
3230
* </p>
3331
*
3432
* @since 2.0
3533
*/
36-
public class AbsoluteNameLocationStrategy implements FileLocationStrategy {
34+
public class AbsoluteNameLocationStrategy extends AbstractFileLocationStrategy {
3735

3836
/**
3937
* A singleton instance of this strategy.
@@ -59,7 +57,6 @@ public URL locate(final FileSystem fileSystem, final FileLocator locator) {
5957
return FileLocatorUtils.convertFileToURL(file);
6058
}
6159
}
62-
6360
return null;
6461
}
6562
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.configuration2.io;
18+
19+
/**
20+
* Abstracts services for {@link FileLocationStrategy} implementations.
21+
*/
22+
abstract class AbstractFileLocationStrategy implements FileLocationStrategy {
23+
24+
}

src/main/java/org/apache/commons/configuration2/io/BasePathLocationStrategy.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@
2222
import org.apache.commons.lang3.StringUtils;
2323

2424
/**
25-
* <p>
2625
* A specialized implementation of {@code FileLocationStrategy} which tries to construct a file path from the locator's
2726
* base path and file name.
28-
* </p>
2927
* <p>
3028
* This strategies ignores the URL stored in the passed in {@link FileLocator}. It generates a path by concatenating the
3129
* base path (if present) and the file name. If the resulting path points to a valid file, the corresponding URL is
@@ -34,7 +32,7 @@
3432
*
3533
* @since 2.0
3634
*/
37-
public class BasePathLocationStrategy implements FileLocationStrategy {
35+
public class BasePathLocationStrategy extends AbstractFileLocationStrategy {
3836

3937
/**
4038
* A singleton instance of this strategy.
@@ -60,7 +58,6 @@ public URL locate(final FileSystem fileSystem, final FileLocator locator) {
6058
return FileLocatorUtils.convertFileToURL(file);
6159
}
6260
}
63-
6461
return null;
6562
}
6663
}

src/main/java/org/apache/commons/configuration2/io/ClasspathLocationStrategy.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,15 @@
2121
import org.apache.commons.lang3.StringUtils;
2222

2323
/**
24-
* <p>
2524
* A specialized {@code FileLocationStrategy} implementation which searches for files on the class path.
26-
* </p>
2725
* <p>
2826
* This strategy implementation ignores the URL and the base path components of the passed in {@link FileLocator}. It
2927
* tries to look up the file name on both the class path and the system class path.
3028
* </p>
3129
*
3230
* @since 2.0
3331
*/
34-
public class ClasspathLocationStrategy implements FileLocationStrategy {
32+
public class ClasspathLocationStrategy extends AbstractFileLocationStrategy {
3533

3634
/**
3735
* A singleton instance of this strategy.

src/main/java/org/apache/commons/configuration2/io/CombinedLocationStrategy.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@
2222
import java.util.Collections;
2323

2424
/**
25-
* <p>
2625
* A specialized implementation of a {@code FileLocationStrategy} which encapsulates an arbitrary number of
2726
* {@code FileLocationStrategy} objects.
28-
* </p>
2927
* <p>
3028
* A collection with the wrapped {@code FileLocationStrategy} objects is passed at construction time. During a
3129
* [{@code locate()} operation the wrapped strategies are called one after the other until one returns a non <strong>null</strong>
@@ -42,7 +40,7 @@
4240
*
4341
* @since 2.0
4442
*/
45-
public class CombinedLocationStrategy implements FileLocationStrategy {
43+
public class CombinedLocationStrategy extends AbstractFileLocationStrategy {
4644

4745
/** A collection with all sub strategies managed by this object. */
4846
private final Collection<FileLocationStrategy> subStrategies;
@@ -84,7 +82,6 @@ public URL locate(final FileSystem fileSystem, final FileLocator locator) {
8482
return url;
8583
}
8684
}
87-
8885
return null;
8986
}
9087
}

src/main/java/org/apache/commons/configuration2/io/FileLocatorUtils.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,10 @@ public final class FileLocatorUtils {
110110
static String appendPath(final String path, final String ext) {
111111
final StringBuilder fName = new StringBuilder();
112112
fName.append(path);
113-
114113
// My best friend. Paranoia.
115114
if (!path.endsWith(File.separator)) {
116115
fName.append(File.separator);
117116
}
118-
119117
//
120118
// We have a relative path, and we have
121119
// two possible forms here. If we have the
@@ -140,14 +138,12 @@ static String appendPath(final String path, final String ext) {
140138
*/
141139
static File constructFile(final String basePath, final String fileName) {
142140
final File file;
143-
144141
final File absolute = new File(fileName);
145142
if (StringUtils.isEmpty(basePath) || absolute.isAbsolute()) {
146143
file = absolute;
147144
} else {
148145
file = new File(appendPath(basePath, fileName));
149146
}
150-
151147
return file;
152148
}
153149

@@ -281,7 +277,6 @@ public static FileLocator fullyInitializedLocator(final FileLocator locator) {
281277
// already fully initialized
282278
return locator;
283279
}
284-
285280
final URL url = locate(locator);
286281
return url != null ? createFullyInitializedLocatorFromURL(locator, url) : null;
287282
}
@@ -296,12 +291,11 @@ static String getBasePath(final URL url) {
296291
if (url == null) {
297292
return null;
298293
}
299-
300294
String s = url.toString();
301-
if (s.startsWith(FILE_SCHEME) && !s.startsWith("file://")) {
302-
s = "file://" + s.substring(FILE_SCHEME.length());
295+
final String schemeHierPrefix = FILE_SCHEME + "//";
296+
if (s.startsWith(FILE_SCHEME) && !s.startsWith(schemeHierPrefix)) {
297+
s = schemeHierPrefix + s.substring(FILE_SCHEME.length());
303298
}
304-
305299
if (s.endsWith("/") || StringUtils.isEmpty(url.getPath())) {
306300
return s;
307301
}
@@ -325,7 +319,6 @@ static URL getClasspathResource(final String resourceName) {
325319
LOG.debug("Loading configuration from the context classpath (" + resourceName + ")");
326320
}
327321
}
328-
329322
// attempt to load from the system classpath
330323
if (url == null) {
331324
url = ClassLoader.getSystemResource(resourceName);
@@ -364,7 +357,6 @@ static File getFile(final String basePath, final String fileName) {
364357
if (f.isAbsolute()) {
365358
return f;
366359
}
367-
368360
// Check if URLs are involved
369361
URL url;
370362
try {
@@ -376,11 +368,9 @@ static File getFile(final String basePath, final String fileName) {
376368
url = null;
377369
}
378370
}
379-
380371
if (url != null) {
381372
return fileFromURL(url);
382373
}
383-
384374
return constructFile(basePath, fileName);
385375
}
386376

@@ -394,9 +384,7 @@ static String getFileName(final URL url) {
394384
if (url == null) {
395385
return null;
396386
}
397-
398387
final String path = url.getPath();
399-
400388
if (path.endsWith("/") || StringUtils.isEmpty(path)) {
401389
return null;
402390
}
@@ -525,7 +513,6 @@ public static void put(final FileLocator locator, final Map<String, Object> map)
525513
if (map == null) {
526514
throw new IllegalArgumentException("Map must not be null.");
527515
}
528-
529516
if (locator != null) {
530517
map.put(PROP_BASE_PATH, locator.getBasePath());
531518
map.put(PROP_ENCODING, locator.getEncoding());

src/main/java/org/apache/commons/configuration2/io/FileSystemLocationStrategy.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@
1919
import java.net.URL;
2020

2121
/**
22-
* <p>
2322
* A specialized implementation of {@code FileLocationStrategy} which uses the passed in {@link FileSystem} to locate a
2423
* file.
25-
* </p>
2624
* <p>
2725
* This strategy implementation ignores the URL of the passed in {@link FileLocator} and operates on its base path and
2826
* file name. These properties are passed to the {@code locateFromURL()} method of {@code FileSystem}. So the burden of
@@ -31,7 +29,7 @@
3129
*
3230
* @since 2.0
3331
*/
34-
public class FileSystemLocationStrategy implements FileLocationStrategy {
32+
public class FileSystemLocationStrategy extends AbstractFileLocationStrategy {
3533

3634
/**
3735
* A singleton instance of this strategy.

src/main/java/org/apache/commons/configuration2/io/HomeDirectoryLocationStrategy.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@
2323
import org.apache.commons.lang3.SystemProperties;
2424

2525
/**
26-
* <p>
2726
* A specialized implementation of {@code FileLocationStrategy} which searches for files in the user's home directory or
2827
* another special configurable directory.
29-
* </p>
3028
* <p>
3129
* This strategy implementation ignores the URL stored in the passed in {@link FileLocator}. It constructs a file path
3230
* from the configured home directory (which is the user's home directory per default, but can be changed to another
@@ -40,7 +38,7 @@
4038
* always ignored, and only the file name is evaluated.
4139
* </p>
4240
*/
43-
public class HomeDirectoryLocationStrategy implements FileLocationStrategy {
41+
public class HomeDirectoryLocationStrategy extends AbstractFileLocationStrategy {
4442

4543
/**
4644
* Obtains the home directory to be used by a new instance. If a directory name is provided, it is used. Otherwise, the
@@ -49,7 +47,7 @@ public class HomeDirectoryLocationStrategy implements FileLocationStrategy {
4947
* @param homeDir the passed in home directory
5048
* @return the directory to be used
5149
*/
52-
private static String fetchHomeDirectory(final String homeDir) {
50+
private static String getHomeDirectory(final String homeDir) {
5351
return homeDir != null ? homeDir : SystemProperties.getUserHome();
5452
}
5553

@@ -84,7 +82,7 @@ public HomeDirectoryLocationStrategy(final boolean withBasePath) {
8482
* @param withBasePath a flag whether the base path should be evaluated
8583
*/
8684
public HomeDirectoryLocationStrategy(final String homeDir, final boolean withBasePath) {
87-
homeDirectory = fetchHomeDirectory(homeDir);
85+
homeDirectory = getHomeDirectory(homeDir);
8886
evaluateBasePath = withBasePath;
8987
}
9088

@@ -94,7 +92,7 @@ public HomeDirectoryLocationStrategy(final String homeDir, final boolean withBas
9492
* @param locator the {@code FileLocator}
9593
* @return the base path to be used
9694
*/
97-
private String fetchBasePath(final FileLocator locator) {
95+
private String getBasePath(final FileLocator locator) {
9896
if (isEvaluateBasePath() && StringUtils.isNotEmpty(locator.getBasePath())) {
9997
return FileLocatorUtils.appendPath(getHomeDirectory(), locator.getBasePath());
10098
}
@@ -127,13 +125,12 @@ public boolean isEvaluateBasePath() {
127125
@Override
128126
public URL locate(final FileSystem fileSystem, final FileLocator locator) {
129127
if (StringUtils.isNotEmpty(locator.getFileName())) {
130-
final String basePath = fetchBasePath(locator);
128+
final String basePath = getBasePath(locator);
131129
final File file = FileLocatorUtils.constructFile(basePath, locator.getFileName());
132130
if (file.isFile()) {
133131
return FileLocatorUtils.convertFileToURL(file);
134132
}
135133
}
136-
137134
return null;
138135
}
139136
}

src/main/java/org/apache/commons/configuration2/io/ProvidedURLLocationStrategy.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@
1919
import java.net.URL;
2020

2121
/**
22-
* <p>
2322
* A specialized implementation of {@code FileLocationStrategy} which checks whether a passed in {@link FileLocator}
2423
* already has a defined URL.
25-
* </p>
2624
* <p>
2725
* {@code FileLocator} objects that have a URL already reference a file in an unambiguous way. Therefore, this strategy
2826
* just returns the URL of the passed in {@code FileLocator}. It can be used as a first step of the file resolving
@@ -31,7 +29,7 @@
3129
*
3230
* @since 2.0
3331
*/
34-
public class ProvidedURLLocationStrategy implements FileLocationStrategy {
32+
public class ProvidedURLLocationStrategy extends AbstractFileLocationStrategy {
3533

3634
/**
3735
* A singleton instance of this strategy.

0 commit comments

Comments
 (0)