This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Simplify regex and add comments

  • Loading branch information...
seanf committed Apr 15, 2016
1 parent 1be9682 commit 32d02142f11dcb776b45f4f487f35bc711cd06b6
@@ -33,7 +33,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
@@ -87,8 +86,12 @@
private static final Logger log = LoggerFactory
.getLogger(PushCommand.class);
private static final Pattern fileNameExtensionsPattern = Pattern.compile(
"(?:([^\\[]*)?(?:\\[(.*?)\\])?)");
private static final Pattern docTypeMappingPattern = Pattern.compile("" +
"(?x) # enable regex comments\n" +
"([^\\[]*) # capture (1) everything before EOL or brackets (DocumentType)\n" +
"(?: # optional non-capture group for any bracketed text\n" +
"\\[(.*)\\] # capture (2) any filename extensions inside brackets (semicolon-separated)\n" +
")?");
private final ConsoleInteractor consoleInteractor;
@@ -100,6 +103,7 @@ public RawPushCommand(PushOptions opts) {
consoleInteractor = new ConsoleInteractorImpl(opts);
}
@VisibleForTesting
public RawPushCommand(PushOptions opts, RestClientFactory clientFactory) {
super(opts, clientFactory);
client = getClientFactory().getFileResourceClient();
@@ -117,7 +121,7 @@ public RawPushCommand(PushOptions opts, RestClientFactory clientFactory,
* Extract extensions from input string
*/
public List<String> extractExtensions(String typeWithExtension) {
Matcher matcher = fileNameExtensionsPattern.matcher(typeWithExtension);
Matcher matcher = docTypeMappingPattern.matcher(typeWithExtension);
if (matcher.find()) {
String rawExtensions = matcher.group(2);
if (!StringUtils.isEmpty(rawExtensions)) {
@@ -135,7 +139,7 @@ public RawPushCommand(PushOptions opts, RestClientFactory clientFactory,
* @return result[0] - type, result[1] - extensions. e.g [txt,html]
*/
public @Nullable String extractType(String typeWithExtension) {
Matcher matcher = fileNameExtensionsPattern.matcher(typeWithExtension);
Matcher matcher = docTypeMappingPattern.matcher(typeWithExtension);
if (matcher.find()) {
return matcher.group(1);
}

0 comments on commit 32d0214

Please sign in to comment.