task: create property expansion framework
This framework is only used for the existing ${_name} property, but
moving this code to its own file, and using a more advanced replacement
method which can match anything inside of ${...} paves the way for the
upcoming custom property feature.
Change-Id: Ica9ecd59d4383e5072d2ccaaef53ce79f0bea9c8
diff --git a/src/main/java/com/googlesource/gerrit/plugins/task/Properties.java b/src/main/java/com/googlesource/gerrit/plugins/task/Properties.java
new file mode 100644
index 0000000..67a4be9
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/task/Properties.java
@@ -0,0 +1,76 @@
+// Copyright (C) 2019 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.task;
+
+import com.googlesource.gerrit.plugins.task.TaskConfig.Task;
+import java.lang.reflect.Field;
+import java.util.HashMap;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/** Use to expand properties like ${_name} for a Task Definition. */
+public class Properties {
+ // "${_name}" -> group(1) = "_name"
+ protected static final Pattern PATTERN = Pattern.compile("\\$\\{([^}]+)\\}");
+
+ protected Map<String, String> expanded = new HashMap<>();
+
+ public Properties(Task definition) {
+ expanded.put("_name", definition.name);
+
+ for (Field field : Task.class.getFields()) {
+ try {
+ field.setAccessible(true);
+ Object o = field.get(definition);
+ if (o instanceof String) {
+ field.set(definition, expandLiteral((String) o));
+ } else if (o instanceof List) {
+ expandInPlace((List<String>) o);
+ }
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ }
+
+ protected void expandInPlace(List<String> list) {
+ if (list != null) {
+ for (ListIterator<String> it = list.listIterator(); it.hasNext(); ) {
+ it.set(expandLiteral(it.next()));
+ }
+ }
+ }
+
+ protected String expandLiteral(String literal) {
+ if (literal == null) {
+ return null;
+ }
+ StringBuffer out = new StringBuffer();
+ Matcher m = PATTERN.matcher(literal);
+ while (m.find()) {
+ m.appendReplacement(out, Matcher.quoteReplacement(getExpandedValue(m.group(1))));
+ }
+ m.appendTail(out);
+ return out.toString();
+ }
+
+ protected String getExpandedValue(String property) {
+ String value = expanded.get(property);
+ return value == null ? "" : value;
+ }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/task/TaskTree.java b/src/main/java/com/googlesource/gerrit/plugins/task/TaskTree.java
index 56e7f03..0b37f28 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/task/TaskTree.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/task/TaskTree.java
@@ -27,7 +27,6 @@
import com.googlesource.gerrit.plugins.task.TaskConfig.Task;
import com.googlesource.gerrit.plugins.task.cli.PatchSetArgument;
import java.io.IOException;
-import java.lang.reflect.Field;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
@@ -98,7 +97,7 @@
this.definition = definition;
this.path.addAll(path);
this.path.add(definition);
- expandProperties(definition);
+ new Properties(definition);
}
public List<Node> getSubNodes() throws OrmException {
@@ -183,36 +182,4 @@
return new Branch.NameKey(allUsers.get(), RefNames.refsUsers(acct.getId()));
}
}
-
- protected static void expandProperties(Task definition) {
- for (Field field : Task.class.getFields()) {
- try {
- field.setAccessible(true);
- Object o = field.get(definition);
- if (o instanceof String) {
- o = expandProperties((String) o, definition);
- } else if (o instanceof List) {
- o = expandProperties((List<String>) o, definition);
- }
- field.set(definition, o);
- } catch (IllegalAccessException e) {
- throw new RuntimeException(e);
- }
- }
- }
-
- protected static List<String> expandProperties(List<String> strings, Task definition) {
- if (strings == null) {
- return null;
- }
- List<String> expanded = new ArrayList<>(strings.size());
- for (String string : strings) {
- expanded.add(expandProperties(string, definition));
- }
- return expanded;
- }
-
- protected static String expandProperties(String string, Task definition) {
- return string == null ? null : string.replaceAll("\\$\\{_name\\}", definition.name);
- }
}