Skip to content

Commit

Permalink
Merge pull request #399 from PabloOQ/regex-array
Browse files Browse the repository at this point in the history
Allow an array or a value in automations regex field
  • Loading branch information
TrianguloY authored Nov 24, 2024
2 parents cb82e98 + aa3f3a8 commit c891f85
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.trianguloy.urlchecker.utilities.methods.AndroidUtils;
import com.trianguloy.urlchecker.utilities.methods.JavaUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

Expand Down Expand Up @@ -78,11 +79,17 @@ public List<String> check(UrlData urlData) {
var automation = catalog.getJSONObject(key);
if (!automation.optBoolean("enabled", true)) continue;

if (urlData.url.matches(automation.getString("regex"))) {
matches.add(automation.getString("action"));
for (String pattern : JavaUtils.getArrayOrElement(automation.get("regex"), String.class)){
if (urlData.url.matches(pattern)) {
matches.add(automation.getString("action"));
break;
}
}

} catch (JSONException e) {
AndroidUtils.assertError("Invalid automation rule", e);
} catch (ClassCastException e) {
AndroidUtils.assertError("Invalid automation regex", e);
}
}
return matches;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.trianguloy.urlchecker.utilities.methods;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

Expand Down Expand Up @@ -46,6 +47,44 @@ static JSONObject toJson(String content) {
}
}

/**
* Given an {@link Object} gotten from {@link JSONObject#get(String)} and a {@link Class},
* it will check if the object is either a value or an array, of said class and return it
* as a list.
*
* @throws ClassCastException if the values are not from type {@code T}
*/
static <T> List<T> getArrayOrElement(Object object, Class<T> clazz) throws ClassCastException, JSONException {
List<T> result = new ArrayList<T>();

if (object instanceof JSONArray array) {
// List
for (int i = 0; i < array.length(); i++) {
result.add(getAsOrCrash(array.get(i), clazz));
}
} else {
// Value
result.add(getAsOrCrash(object, clazz));
}

return result;
}

// I don't like it but it wasn't throwing an exception when wrongly casting,
// probably due to type casting
/**
* Returns the value as type {@code T} if possible, if not, it throws an exception
*
* @throws ClassCastException if the values are not from type {@code T}
*/
static <T> T getAsOrCrash(Object object, Class<T> clazz) throws ClassCastException{
if (clazz.isInstance(object)) {
return (T) object;
} else {
throw new ClassCastException("Not of class " + clazz.getName());
}
}

/**
* Clamps a value between two other values.
*/
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ Hope you find the app useful! And don't hesitate to suggest features, report bug
<string name="auto_error_toast">Show toast if automation fails</string>
<string name="auto_notFound">"No automation with key '%s' is available. Make sure it is valid and the required module is active."</string>
<string name="auto_editor">"Automations configuration. Format: list of objects where the key is a unique automation name and the content includes the following values:
- 'regex': string, required: a valid java regex that the url must match for the automation to run.
- 'regex': string|list, required: a valid java regex that the url must match for the automation to run. If list, at least one regex must match.
- 'action': string, required: the key of the automation to run. Check below for a list of all the available keys.
- 'enabled': boolean, optional: if false this automation will be skipped. True by default."</string>
<string name="auto_available_prefix">Available automation actions (keys):</string>
Expand Down

0 comments on commit c891f85

Please sign in to comment.