Skip to content

Commit

Permalink
requested changes done
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKruse committed Apr 26, 2024
1 parent 7594a69 commit 4c04fb6
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@
/**
* Class to configure rule option by option panel
* @author Fred Kruse
* @since 4.2
* @since 6.5
*/
public class RuleOption {
public final static String DEFAULT_VALUE = "defaultValue";
public final static String DEFAULT_TYPE = "defaultType";
public final static String MIN_CONF_VALUE = "minConfigurableValue";
public final static String MAX_CONF_VALUE = "maxConfigurableValue";
public final static String CONF_TEXT = "configureText";
private final Object defaultValue;
private final String configureText;
private Object minConfigurableValue;
Expand Down Expand Up @@ -74,6 +79,87 @@ public Object getMaxConfigurableValue() {
return maxConfigurableValue;
}

/**
* Gives back a special String representation of an Object for save or communication
* add a character to characterize the type of object before its String value
* (i for Integer, b for Boolean, etc.
* Can be decoded by StringToObject
*/
public static String ObjectToString(Object o) {

This comment has been minimized.

Copy link
@danielnaber

danielnaber Apr 26, 2024

Member

by convention, ObjectToString should be objectToString (same below)

char c;
if (o instanceof Integer) {
c = 'i';
} else if (o instanceof Character) {
c = 'c';
} else if (o instanceof Boolean) {
c = 'b';
} else if (o instanceof Float) {
c = 'f';
} else if (o instanceof Double) {
c = 'd';
} else {
c = 's';
}
return c + o.toString();
}

/**
* Gives back a special String representation of an array of Objects for save or communication
* use ObjectToString for encoding the single objects
* separate the objects by ';'
* Can be decoded by StringToObjects
*/
public static String ObjectsToString(Object[] o) {
String s = "";
for (int i = 0; i < o.length; i++) {
if (i > 0) {
s += ";";
}
s += ObjectToString(o[i]);
}
return s;
}

/**
* decodes an String to an object encoded by ObjectToString
* Note: if there is no special encoding character at the beginning of the string
* an integer is assumed for compatibility with older versions of LT
*/
public static Object StringToObject(String s) {
Object o;
char c = s.charAt(0);
String str = s.substring(1);
if (c == 's') {
o = str;
} else if (c == 'b') {
o = Boolean.parseBoolean(str);
} else if (c == 'f') {
o = Float.parseFloat(str);
} else if (c == 'd') {
o = Double.parseDouble(str);
} else if (c == 'c') {
o = str.charAt(0);
} else if (c == 'i') {
o = Integer.parseInt(str);
} else { // compatible to old version
o = Integer.parseInt(s);
}
return o;
}

/**
* decodes an String to an array of object encoded by ObjectsToString
*/
public static Object[] StringToObjects(String s) {
String[] strs = s.split(";");
Object[] o = new Object[strs.length];
for (int i = 0; i < strs.length; i++) {
String str = strs[i].trim();
o[i] = StringToObject(str);
}
return o;
}




Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.languagetool.Languages;
import org.languagetool.rules.ITSIssueType;
import org.languagetool.rules.Rule;
import org.languagetool.rules.RuleOption;

/**
* Configuration like list of disabled rule IDs, server mode etc.
Expand Down Expand Up @@ -1474,28 +1475,7 @@ private void parseConfigurableRuleValues(String rulesValueString) {
if (ruleAndValue.length != 2) {
throw new RuntimeException("Could not parse rule and value, colon expected: '" + ruleToValue + "'");
}
String[] values = ruleAndValue[1].split(";");
Object[] objects = new Object[values.length];
for (int i = 0; i < values.length; i++) {
String str = values[i].trim();
char c = values[i].charAt(0);
str = str.substring(1);
if (c == 's') {
objects[i] = str;
} else if (c == 'b') {
objects[i] = Boolean.parseBoolean(str);
} else if (c == 'f') {
objects[i] = Float.parseFloat(str);
} else if (c == 'd') {
objects[i] = Double.parseDouble(str);
} else if (c == 'c') {
objects[i] = str.charAt(0);
} else if (c == 'i') {
objects[i] = Integer.parseInt(str);
} else { // compatible to old version
objects[i] = Integer.parseInt(values[i]);
}
}
Object[] objects = RuleOption.StringToObjects(ruleAndValue[1]);
configurableRuleValues.put(ruleAndValue[0].trim(), objects);
}
}
Expand Down Expand Up @@ -1796,30 +1776,15 @@ private void saveConfigForCurrentProfile(Properties props, String prefix, String
}
if (!configurableRuleValues.isEmpty()) {
StringBuilder sbRV = new StringBuilder();
int i = 0;
for (Map.Entry<String, Object[]> entry : configurableRuleValues.entrySet()) {
Object[] obs = entry.getValue();
if (obs != null && obs.length > 0) {
StringBuilder sbOV = new StringBuilder();
char c;
for (int i = 0; i < obs.length; i++) {
Object o = entry.getValue()[i];
if (o instanceof Integer) {
c = 'i';
} else if (o instanceof Boolean) {
c = 'b';
} else if (o instanceof Float) {
c = 'f';
} else if (o instanceof Double) {
c = 'd';
} else {
c = 's';
}
if (i > 0) {
sbOV.append(";");
}
sbOV.append(c).append(o);
if (i > 0) {
sbRV.append(",");
}
sbRV.append(entry.getKey()).append(':').append(sbOV.toString()).append(", ");
sbRV.append(entry.getKey()).append(':').append(RuleOption.ObjectsToString(obs));
i++;
}
}
props.setProperty(prefix + CONFIGURABLE_RULE_VALUES_KEY + qualifier, sbRV.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,29 +316,11 @@ private void setRuleValues(Map<String, Object[]> configurableValues) {
for (String rule : rules) {
Object[] obs = configurableValues.get(rule);
if (obs != null && obs.length > 0) {
StringBuilder sbOV = new StringBuilder();
char c;
for (int i = 0; i < obs.length; i++) {
Object o = obs[i];
if (o instanceof Integer) {
c = 'i';
} else if (o instanceof Character) {
c = 'c';
} else if (o instanceof Boolean) {
c = 'b';
} else if (o instanceof Float) {
c = 'f';
} else if (o instanceof Double) {
c = 'd';
} else {
c = 's';
}
if (i > 0) {
sbOV.append(";");
}
sbOV.append(c).append(o);
String rOptions = RuleOption.ObjectsToString(obs);
if (rOptions.charAt(0) == 'i') { // for compatibility with older server versions
rOptions = rOptions.substring(1);
}
String ruleValueString = rule + ":" + sbOV.toString();
String ruleValueString = rule + ":" + rOptions;
ruleValues.add(ruleValueString);
}
}
Expand Down Expand Up @@ -476,6 +458,51 @@ private void storeAllRules(List<Map<String,String>> listRuleMaps) {
allRules.add(rule);
}
}

static RuleOption[] getRuleOptionsFromMap(Map<String,String> ruleMap) {
if (ruleMap.containsKey("ruleOptions")) {
Object o = ruleMap.get("ruleOptions");
@SuppressWarnings("unchecked")
List<Map<String,Object>> rOptions = (List<Map<String, Object>>) o;
RuleOption[] ruleOptions = new RuleOption[rOptions.size()];
for (int i = 0; i < rOptions.size(); i++) {
String defaultType = (String) rOptions.get(i).get(RuleOption.DEFAULT_TYPE);
String configureText = (String) rOptions.get(i).get(RuleOption.CONF_TEXT);
Object defaultValue;
Object minConfigurableValue;
Object maxConfigurableValue;
if (defaultType.equals("Integer")) {
defaultValue = (int) rOptions.get(i).get(RuleOption.DEFAULT_VALUE);
minConfigurableValue = (int) rOptions.get(i).get(RuleOption.MIN_CONF_VALUE);
maxConfigurableValue = (int) rOptions.get(i).get(RuleOption.MAX_CONF_VALUE);
} else if (defaultType.equals("Character")) {
defaultValue = rOptions.get(i).get(RuleOption.DEFAULT_VALUE).toString().charAt(0);
minConfigurableValue = rOptions.get(i).get(RuleOption.MIN_CONF_VALUE).toString().charAt(0);
maxConfigurableValue = rOptions.get(i).get(RuleOption.MAX_CONF_VALUE).toString().charAt(0);
} else if (defaultType.equals("Boolean")) {
defaultValue = (boolean) rOptions.get(i).get(RuleOption.DEFAULT_VALUE);
minConfigurableValue = (int) rOptions.get(i).get(RuleOption.MIN_CONF_VALUE);
maxConfigurableValue = (int) rOptions.get(i).get(RuleOption.MAX_CONF_VALUE);
} else if (defaultType.equals("Float")) {
defaultValue = (float) rOptions.get(i).get(RuleOption.DEFAULT_VALUE);
minConfigurableValue = (float) rOptions.get(i).get(RuleOption.MIN_CONF_VALUE);
maxConfigurableValue = (float) rOptions.get(i).get(RuleOption.MAX_CONF_VALUE);
} else if (defaultType.equals("Double")) {
defaultValue = (double) rOptions.get(i).get(RuleOption.DEFAULT_VALUE);
minConfigurableValue = (double) rOptions.get(i).get(RuleOption.MIN_CONF_VALUE);
maxConfigurableValue = (double) rOptions.get(i).get(RuleOption.MAX_CONF_VALUE);
} else {
defaultValue = rOptions.get(i).get(RuleOption.DEFAULT_VALUE).toString();
minConfigurableValue = rOptions.get(i).get(RuleOption.MIN_CONF_VALUE).toString();
maxConfigurableValue = rOptions.get(i).get(RuleOption.MAX_CONF_VALUE).toString();
}
ruleOptions[i] = new RuleOption(defaultValue, configureText, minConfigurableValue, maxConfigurableValue);
}
return ruleOptions;
} else {
return null;
}
}

/**
* Class to define remote (sentence level) rules
Expand Down Expand Up @@ -505,54 +532,13 @@ static class RemoteRule extends Rule {
setOfficeDefaultOff();
}
isDictionaryBasedSpellingRule = ruleMap.containsKey("isDictionaryBasedSpellingRule");
if (ruleMap.containsKey("ruleOptions")) {
Object o = ruleMap.get("ruleOptions");
@SuppressWarnings("unchecked")
List<Map<String,Object>> rOptions = (List<Map<String, Object>>) o;
ruleOptions = new RuleOption[rOptions.size()];
for (int i = 0; i < rOptions.size(); i++) {
String defaultType = (String) rOptions.get(i).get("defaultType");
String configureText = (String) rOptions.get(i).get("configureText");
Object defaultValue;
Object minConfigurableValue;
Object maxConfigurableValue;
if (defaultType.equals("Integer")) {
defaultValue = (int) rOptions.get(i).get("defaultValue");
minConfigurableValue = (int) rOptions.get(i).get("minConfigurableValue");
maxConfigurableValue = (int) rOptions.get(i).get("maxConfigurableValue");
} else if (defaultType.equals("Character")) {
defaultValue = rOptions.get(i).get("defaultValue").toString().charAt(0);
minConfigurableValue = rOptions.get(i).get("minConfigurableValue").toString().charAt(0);
maxConfigurableValue = rOptions.get(i).get("maxConfigurableValue").toString().charAt(0);
} else if (defaultType.equals("Boolean")) {
defaultValue = (boolean) rOptions.get(i).get("defaultValue");
minConfigurableValue = (int) rOptions.get(i).get("minConfigurableValue");
maxConfigurableValue = (int) rOptions.get(i).get("maxConfigurableValue");
} else if (defaultType.equals("Float")) {
defaultValue = (float) rOptions.get(i).get("defaultValue");
minConfigurableValue = (float) rOptions.get(i).get("minConfigurableValue");
maxConfigurableValue = (float) rOptions.get(i).get("maxConfigurableValue");
} else if (defaultType.equals("Double")) {
defaultValue = (double) rOptions.get(i).get("defaultValue");
minConfigurableValue = (double) rOptions.get(i).get("minConfigurableValue");
maxConfigurableValue = (double) rOptions.get(i).get("maxConfigurableValue");
} else {
defaultValue = rOptions.get(i).get("defaultValue").toString();
minConfigurableValue = rOptions.get(i).get("minConfigurableValue").toString();
maxConfigurableValue = rOptions.get(i).get("maxConfigurableValue").toString();
}
ruleOptions[i] = new RuleOption(defaultValue, configureText, minConfigurableValue, maxConfigurableValue);
}
// MessageHandler.showMessage("ruleId: " + ruleId + "\n" + "ruleOptions:\n" + ruleMap.get("ruleOptions"));
} else {
ruleOptions = null;
}
ruleOptions = getRuleOptionsFromMap(ruleMap);
if (ruleMap.containsKey("hasConfigurableValue")) {
hasConfigurableValue = true;
defaultValue = Integer.parseInt(ruleMap.get("defaultValue"));
minConfigurableValue = Integer.parseInt(ruleMap.get("minConfigurableValue"));
maxConfigurableValue = Integer.parseInt(ruleMap.get("maxConfigurableValue"));
configureText = ruleMap.get("configureText");
defaultValue = Integer.parseInt(ruleMap.get(RuleOption.DEFAULT_VALUE));
minConfigurableValue = Integer.parseInt(ruleMap.get(RuleOption.MIN_CONF_VALUE));
maxConfigurableValue = Integer.parseInt(ruleMap.get(RuleOption.MAX_CONF_VALUE));
configureText = ruleMap.get(RuleOption.CONF_TEXT);
} else {
hasConfigurableValue = false;
defaultValue = 0;
Expand Down Expand Up @@ -648,53 +634,13 @@ static class RemoteTextLevelRule extends TextLevelRule {
setOfficeDefaultOff();
}
isDictionaryBasedSpellingRule = ruleMap.containsKey("isDictionaryBasedSpellingRule");
if (ruleMap.containsKey("ruleOptions")) {
Object o = ruleMap.get("ruleOptions");
@SuppressWarnings("unchecked")
List<Map<String,Object>> rOptions = (List<Map<String, Object>>) o;
ruleOptions = new RuleOption[rOptions.size()];
for (int i = 0; i < rOptions.size(); i++) {
String defaultType = (String) rOptions.get(i).get("defaultType");
String configureText = (String) rOptions.get(i).get("configureText");
Object defaultValue;
Object minConfigurableValue;
Object maxConfigurableValue;
if (defaultType.equals("Integer")) {
defaultValue = (int) rOptions.get(i).get("defaultValue");
minConfigurableValue = (int) rOptions.get(i).get("minConfigurableValue");
maxConfigurableValue = (int) rOptions.get(i).get("maxConfigurableValue");
} else if (defaultType.equals("Character")) {
defaultValue = rOptions.get(i).get("defaultValue").toString().charAt(0);
minConfigurableValue = rOptions.get(i).get("minConfigurableValue").toString().charAt(0);
maxConfigurableValue = rOptions.get(i).get("maxConfigurableValue").toString().charAt(0);
} else if (defaultType.equals("Boolean")) {
defaultValue = (boolean) rOptions.get(i).get("defaultValue");
minConfigurableValue = (int) rOptions.get(i).get("minConfigurableValue");
maxConfigurableValue = (int) rOptions.get(i).get("maxConfigurableValue");
} else if (defaultType.equals("Float")) {
defaultValue = (float) rOptions.get(i).get("defaultValue");
minConfigurableValue = (float) rOptions.get(i).get("minConfigurableValue");
maxConfigurableValue = (float) rOptions.get(i).get("maxConfigurableValue");
} else if (defaultType.equals("Double")) {
defaultValue = (double) rOptions.get(i).get("defaultValue");
minConfigurableValue = (double) rOptions.get(i).get("minConfigurableValue");
maxConfigurableValue = (double) rOptions.get(i).get("maxConfigurableValue");
} else {
defaultValue = rOptions.get(i).get("defaultValue").toString();
minConfigurableValue = rOptions.get(i).get("minConfigurableValue").toString();
maxConfigurableValue = rOptions.get(i).get("maxConfigurableValue").toString();
}
ruleOptions[i] = new RuleOption(defaultValue, configureText, minConfigurableValue, maxConfigurableValue);
}
} else {
ruleOptions = null;
}
ruleOptions = getRuleOptionsFromMap(ruleMap);
if (ruleMap.containsKey("hasConfigurableValue")) {
hasConfigurableValue = true;
defaultValue = Integer.parseInt(ruleMap.get("defaultValue"));
minConfigurableValue = Integer.parseInt(ruleMap.get("minConfigurableValue"));
maxConfigurableValue = Integer.parseInt(ruleMap.get("maxConfigurableValue"));
configureText = ruleMap.get("configureText");
defaultValue = Integer.parseInt(ruleMap.get(RuleOption.DEFAULT_VALUE));
minConfigurableValue = Integer.parseInt(ruleMap.get(RuleOption.MIN_CONF_VALUE));
maxConfigurableValue = Integer.parseInt(ruleMap.get(RuleOption.MAX_CONF_VALUE));
configureText = ruleMap.get(RuleOption.CONF_TEXT);
} else {
hasConfigurableValue = false;
defaultValue = 0;
Expand Down
Loading

0 comments on commit 4c04fb6

Please sign in to comment.