Skip to content

Commit eece780

Browse files
committed
FileUtils: refactor version pattern matching
Hopefully it is a little less arcane now, if more verbose.
1 parent 7ec75c5 commit eece780

File tree

1 file changed

+44
-9
lines changed

1 file changed

+44
-9
lines changed

src/main/java/org/scijava/util/FileUtils.java

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ public final class FileUtils {
7070
public static final String SHORTENER_SLASH = "/";
7171
public static final String SHORTENER_ELLIPSE = "...";
7272

73+
/** A regular expression to match filenames containing version information. */
74+
private static final Pattern VERSION_PATTERN = buildVersionPattern();
75+
7376
private FileUtils() {
7477
// prevent instantiation of utility class
7578
}
@@ -170,13 +173,8 @@ public static void writeFile(final File file, final byte[] bytes)
170173
}
171174
}
172175

173-
/** A regular expression to match filenames containing version information. */
174-
private final static Pattern versionPattern =
175-
Pattern
176-
.compile("(.+?)(-\\d+(\\.\\d+|\\d{7})+[a-z]?\\d?(-[A-Za-z0-9.]+?|\\.GA)*?)?((-(swing|swt|shaded|sources|javadoc|native|linux-x86|linux-x86_64|macosx-x86_64|windows-x86|windows-x86_64|android-arm|android-x86))?(\\.jar(-[a-z]*)?))");
177-
178176
public static String stripFilenameVersion(final String filename) {
179-
final Matcher matcher = versionPattern.matcher(filename);
177+
final Matcher matcher = VERSION_PATTERN.matcher(filename);
180178
if (!matcher.matches()) return filename;
181179
return matcher.group(1) + matcher.group(5);
182180
}
@@ -191,7 +189,7 @@ public static String stripFilenameVersion(final String filename) {
191189
public static File[] getAllVersions(final File directory,
192190
final String filename)
193191
{
194-
final Matcher matcher = versionPattern.matcher(filename);
192+
final Matcher matcher = VERSION_PATTERN.matcher(filename);
195193
if (!matcher.matches()) {
196194
final File file = new File(directory, filename);
197195
return file.exists() ? new File[] { file } : null;
@@ -203,7 +201,7 @@ public static File[] getAllVersions(final File directory,
203201
@Override
204202
public boolean accept(final File dir, final String name) {
205203
if (!name.startsWith(baseName)) return false;
206-
final Matcher matcher2 = versionPattern.matcher(name);
204+
final Matcher matcher2 = VERSION_PATTERN.matcher(name);
207205
return matcher2.matches() && baseName.equals(matcher2.group(1)) &&
208206
equals(classifier, matcher2.group(6));
209207
}
@@ -609,6 +607,43 @@ else if (protocol.equals("jar")) {
609607
return result;
610608
}
611609

610+
// -- Helper methods --
611+
612+
/** Builds the {@link #VERSION_PATTERN} constant. */
613+
private static Pattern buildVersionPattern() {
614+
final String version =
615+
"\\d+(\\.\\d+|\\d{7})+[a-z]?\\d?(-[A-Za-z0-9.]+?|\\.GA)*?";
616+
final String suffix = "\\.jar(-[a-z]*)?";
617+
return Pattern.compile("(.+?)(-" + version + ")?((-(" + classifiers() +
618+
"))?(" + suffix + "))");
619+
}
620+
621+
/** Helper method of {@link #buildVersionPattern()}. */
622+
private static String classifiers() {
623+
final String[] classifiers = {
624+
"swing",
625+
"swt",
626+
"shaded",
627+
"sources",
628+
"javadoc",
629+
"native",
630+
"linux-x86",
631+
"linux-x86_64",
632+
"macosx-x86_64",
633+
"windows-x86",
634+
"windows-x86_64",
635+
"android-arm",
636+
"android-x86",
637+
};
638+
final StringBuilder sb = new StringBuilder("(");
639+
for (final String classifier : classifiers) {
640+
if (sb.length() > 1) sb.append("|");
641+
sb.append(classifier);
642+
}
643+
sb.append(")");
644+
return sb.toString();
645+
}
646+
612647
// -- Deprecated methods --
613648

614649
/**
@@ -620,7 +655,7 @@ else if (protocol.equals("jar")) {
620655
*/
621656
@Deprecated
622657
public static Matcher matchVersionedFilename(final String filename) {
623-
return versionPattern.matcher(filename);
658+
return VERSION_PATTERN.matcher(filename);
624659
}
625660

626661
}

0 commit comments

Comments
 (0)