/*
* Copyright (C) 2022 Daniel Dietsch (dietsch@informatik.uni-freiburg.de)
* Copyright (C) 2022 University of Freiburg
*
* This file is part of the ULTIMATE Core.
*
* The ULTIMATE Core is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ULTIMATE Core 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the ULTIMATE Core. If not, see .
*
* Additional permission under GNU GPL version 3 section 7:
* If you modify the ULTIMATE Core, or any covered work, by linking
* or combining it with Eclipse RCP (or a modified version of Eclipse RCP),
* containing parts covered by the terms of the Eclipse Public License, the
* licensors of the ULTIMATE Core grant you additional permission
* to convey the resulting work.
*/
package de.uni_freiburg.informatik.ultimate.preferencejson;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.stream.Collectors;
import com.google.gson.Gson;
import de.uni_freiburg.informatik.ultimate.core.model.ICore;
import de.uni_freiburg.informatik.ultimate.core.model.IUltimatePlugin;
import de.uni_freiburg.informatik.ultimate.core.model.preferences.BaseUltimatePreferenceItem;
import de.uni_freiburg.informatik.ultimate.core.model.preferences.IPreferenceProvider;
import de.uni_freiburg.informatik.ultimate.core.model.preferences.PreferenceType;
import de.uni_freiburg.informatik.ultimate.core.model.preferences.UltimatePreferenceItem;
import de.uni_freiburg.informatik.ultimate.core.model.services.ILogger;
import de.uni_freiburg.informatik.ultimate.core.model.services.IUltimateServiceProvider;
/**
*
* @author Daniel Dietsch (dietsch@informatik.uni-freiburg.de)
*
*/
public final class PreferenceUtil {
private static final String PLUGIN_ID_CORE = "de.uni_freiburg.informatik.ultimate.core";
private PreferenceUtil() {
// do not instantiate utility class
}
/**
* Convert the label of an {@link UltimatePreferenceItem} to a unique string that contains the plugin id, no spaces,
* and no special symbols.
*/
public static String convertLabelToLongName(final String pluginId, final UltimatePreferenceItem> item) {
return convertLabelToLongName(pluginId, item.getLabel());
}
/**
* Convert the label of an {@link UltimatePreferenceItem} to a unique string that contains the plugin id, no spaces,
* and no special symbols.
*/
public static String convertLabelToLongName(final String pluginId, final String label) {
final int lastIdx = pluginId.lastIndexOf('.');
final String prefix = lastIdx > 0 ? pluginId.substring(lastIdx + 1) : pluginId;
final String unprocessedName = prefix + " " + label;
final String replaced =
unprocessedName.replace(":", "").replaceAll("[ ()\"'.]+", ".").toLowerCase(Locale.ENGLISH);
if (replaced.endsWith(".")) {
// prevent label ending with '.'
return replaced.substring(0, replaced.length() - 1);
}
return replaced;
}
/**
* Generate a JSON representation of all default settings provided by Ultimate that can be used by the web frontend
* as user settings.
*/
public static String generateFrontendSettingsJsonFromDefaultSettings(final ICore> core) {
return generateFrontendSettingsJson(core, getDefaultSettings(core));
}
/**
* Generate a JSON representation of all settings provided by Ultimate that differ from their defaults. The JSON
* representation can be used by the web frontend as user settings.
*/
public static String generateFrontendSettingsJsonFromDeltaSettings(final IUltimateServiceProvider services,
final ICore> core) {
return generateFrontendSettingsJson(core, getDeltaSettings(services, core));
}
/**
* Generate a JSON representation of all default settings provided by Ultimate that can be used by the web backend
* as whitelist.
*/
public static String generateBackendWhitelistJsonFromDefaultSettings(final ICore> core) {
return generateBackendWhitelistJson(getDefaultSettings(core));
}
/**
* Generate a JSON representation of all settings provided by Ultimate that differ from their defaults. The JSON
* representation can be used by the web backend as whitelist.
*/
public static String generateBackendWhitelistJsonFromDeltaSettings(final IUltimateServiceProvider services,
final ICore> core) {
return generateBackendWhitelistJson(getDeltaSettings(services, core));
}
private static String generateFrontendSettingsJson(final ICore> core,
final Map> settings) {
final Map>> jsonAsPoJ = new LinkedHashMap<>(1);
final List