Skip to content
This repository has been archived by the owner on Aug 3, 2024. It is now read-only.

Commit

Permalink
fix handling of json values for workspace/project settings
Browse files Browse the repository at this point in the history
fixes issues with back slashes in particular

fixes #612
  • Loading branch information
ervandew committed Mar 13, 2021
1 parent 4298ba3 commit f112e3f
Show file tree
Hide file tree
Showing 14 changed files with 191 additions and 88 deletions.
2 changes: 1 addition & 1 deletion .settings/org.eclim.prefs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ eclipse.preferences.version=1
org.eclim.java.checkstyle.config=ant/checkstyle.xml
org.eclim.java.doc.dest=doc/api
org.eclim.java.doc.packagenames=org.eclim
org.eclim.java.import.exclude=["^com.sun..*", "^sunw?..*", "^antlr..*"]
org.eclim.java.import.exclude=["^com\\\\.sun\\\\..*","^sunw?\\\\..*","^antlr\\\\..*"]
org.eclim.java.import.package_separation_level=-1
org.eclim.java.junit.output_dir=build/test/junit/results
org.eclim.java.logging.impl=custom
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2005 - 2020 Eric Van Dewoestine
* Copyright (C) 2005 - 2021 Eric Van Dewoestine
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -17,10 +17,8 @@
package org.eclim.plugin.core.command.admin;

import java.io.File;
import java.io.FileReader;

import java.util.ArrayList;
import java.util.Map;
import java.util.List;

import org.eclim.Services;

Expand All @@ -36,12 +34,6 @@

import org.eclim.plugin.core.preference.Preferences;

import org.eclim.util.IOUtils;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonStreamParser;

/**
* Command to update global settings.
*
Expand All @@ -60,26 +52,12 @@ public Object execute(CommandLine commandLine)
{
String settings = commandLine.getValue(Options.SETTINGS_OPTION);

FileReader in = null;
Preferences preferences = getPreferences();
File file = new File(settings);
ArrayList<Error> errors = new ArrayList<Error>();
List<Error> errors;
try{
in = new FileReader(file);
JsonStreamParser parser = new JsonStreamParser(in);
JsonObject obj = (JsonObject)parser.next();

Preferences preferences = getPreferences();
for (Map.Entry<String, JsonElement> entry : obj.entrySet()){
String name = entry.getKey();
String value = entry.getValue().getAsString();
try{
preferences.setValue(name, value);
}catch(IllegalArgumentException iae){
errors.add(new Error(iae.getMessage(), null, 0, 0));
}
}
errors = preferences.setValues(file);
}finally{
IOUtils.closeQuietly(in);
try{
file.delete();
}catch(Exception e){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2005 - 2012 Eric Van Dewoestine
* Copyright (C) 2005 - 2021 Eric Van Dewoestine
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -36,7 +36,7 @@
*
* @author Eric Van Dewoestine
*/
@Command(name = "project_settings", options = "OPTIONAL p project ARG")
@Command(name = "project_settings", options = "REQUIRED p project ARG")
public class ProjectSettingsCommand
extends AbstractCommand
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2005 - 2020 Eric Van Dewoestine
* Copyright (C) 2005 - 2021 Eric Van Dewoestine
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -17,15 +17,8 @@
package org.eclim.plugin.core.command.project;

import java.io.File;
import java.io.FileReader;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.eclim.Services;

Expand All @@ -45,14 +38,8 @@

import org.eclim.plugin.core.util.ProjectUtils;

import org.eclim.util.IOUtils;

import org.eclipse.core.resources.IProject;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

/**
* Command to update a project.
*
Expand Down Expand Up @@ -100,35 +87,20 @@ public Object execute(CommandLine commandLine)
*
* @param project The project.
* @param settings The temp settings file.
*
* @return List of errors or an empty List if none.
*/
private List<Error> updateSettings(IProject project, String settings)
throws Exception
{
FileReader in = null;
Path file = Paths.get(settings);
ArrayList<Error> errors = new ArrayList<Error>();
Preferences preferences = getPreferences();
File file = new File(settings);
List<Error> errors;
try{
String content = Files.readString(file);
// somewhere between gson 1.7.1 and 2.8.6 it became stupidily strict on
// back slashes in values.
content = content.replace("\\", "\\\\");

JsonObject obj = (JsonObject)JsonParser.parseString(content);
Preferences preferences = getPreferences();
for (Map.Entry<String, JsonElement> entry : obj.entrySet()){
String name = entry.getKey();
String value = entry.getValue().getAsString();
try{
preferences.setValue(project, name, value);
}catch(IllegalArgumentException iae){
errors.add(new Error(iae.getMessage(), null, 0, 0));
}
}
errors = preferences.setValues(project, file);
}finally{
IOUtils.closeQuietly(in);
try{
file.toFile().delete();
file.delete();
}catch(Exception e){
logger.warn("Error deleting project settings temp file: " + file, e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (C) 2021 Eric Van Dewoestine
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.eclim.plugin.core.preference;

/**
* Abstract base class for Validator implementations.
*/
public abstract class AbstractValidator
implements Validator
{
@Override
public OptionInstance optionInstance(Option option, String value){
return new OptionInstance(option, value);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2011 - 2012 Eric Van Dewoestine
* Copyright (C) 2011 - 2021 Eric Van Dewoestine
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -16,8 +16,11 @@
*/
package org.eclim.plugin.core.preference;

import org.eclim.logging.Logger;

import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSyntaxException;

/**
* Option validator that validates that the option value is valid json.
Expand All @@ -27,6 +30,8 @@
public class JsonValidator
implements Validator
{
private static final Logger logger = Logger.getLogger(JsonValidator.class);

private static final Gson GSON = new Gson();

private Class<?> type;
Expand Down Expand Up @@ -83,4 +88,16 @@ public String getMessage(String name, Object value)
}
return null;
}

@Override
public OptionInstance optionInstance(Option option, String value)
{
try{
Object result = GSON.fromJson(value, type);
return new OptionInstance(option, result);
}catch(JsonSyntaxException jse){
logger.error("Malformed json for option: " + option.getName(), jse);
return new OptionInstance(option, value);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2005 - 2017 Eric Van Dewoestine
* Copyright (C) 2005 - 2021 Eric Van Dewoestine
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -24,15 +24,15 @@
public class OptionInstance
extends Option
{
private String value;
private Object value;

/**
* Constructs a new instance.
*
* @param option The option.
* @param value The option value.
*/
public OptionInstance (Option option, String value)
public OptionInstance(Option option, Object value)
{
this.value = value;
setPath(option.getPath());
Expand All @@ -49,9 +49,9 @@ public void setName(String name)
/**
* Get value.
*
* @return value as String.
* @return the option value.
*/
public String getValue()
public Object getValue()
{
return this.value;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2005 - 2020 Eric Van Dewoestine
* Copyright (C) 2005 - 2021 Eric Van Dewoestine
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -90,7 +90,7 @@ public static Preferences addPreferences(
preference.setNature(nature);
preference.setPath(attrs[0]);
preference.setName(attrs[1]);
preference.setDefaultValue(attrs[2]);
String defaultValue = attrs[2];
if (attrs[3] != null && !attrs[3].trim().equals(StringUtils.EMPTY)){
Matcher jsonArrayMatcher = JSON_ARRAY.matcher(attrs[3]);
Matcher jsonObjectMatcher = JSON_OBJECT.matcher(attrs[3]);
Expand All @@ -99,13 +99,18 @@ public static Preferences addPreferences(
preference.setValidator(new JsonValidator(
String[].class,
pattern.length() != 0 ? new RegexValidator(pattern) : null));

// escape chars in json require additional escaping at the storage
// level, but don't force plugin writers to deal with this burden
defaultValue = defaultValue.replace("\\", "\\\\");
}else if (jsonObjectMatcher.matches()){
Map<String, String> map = new HashMap<String, String>();
preference.setValidator(new JsonValidator(map.getClass(), null));
}else{
preference.setValidator(new RegexValidator(attrs[3]));
}
}
preference.setDefaultValue(defaultValue);

preferences.addPreference(preference);
}
Expand Down
Loading

0 comments on commit f112e3f

Please sign in to comment.