com.devonfw.tools.ide.configurator.merge.PropertiesMerger Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of devon-ide-configurator Show documentation
Show all versions of devon-ide-configurator Show documentation
Code for configurator the creates or updates configuration of IDEs (Eclipse, etc.).
package com.devonfw.tools.ide.configurator.merge;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
import java.util.Set;
import com.devonfw.tools.ide.configurator.SortedProperties;
import com.devonfw.tools.ide.configurator.resolve.VariableResolver;
import com.devonfw.tools.ide.logging.Log;
/**
* Implementation of {@link FileTypeMerger} for {@link Properties} files.
*
* @since 3.0.0
*/
public class PropertiesMerger extends FileTypeMerger {
@Override
public void merge(File setupFile, File updateFile, VariableResolver resolver, File workspaceFile) {
SortedProperties properties = new SortedProperties();
boolean updateFileExists = updateFile.exists();
if (workspaceFile.exists()) {
if (!updateFileExists) {
Log.trace("Nothing to do as update file does not exist: " + updateFile);
return; // nothing to do ...
}
load(properties, workspaceFile);
} else if (setupFile.exists()) {
load(properties, setupFile);
}
if (updateFileExists) {
load(properties, updateFile);
}
resolve(properties, resolver);
save(properties, workspaceFile);
Log.trace("Saved merged properties to: " + workspaceFile);
}
/**
* @param file the {@link File} to load.
* @return the loaded {@link Properties}.
*/
public static Properties load(File file) {
Properties properties = new Properties();
load(properties, file);
return properties;
}
/**
* @param file the {@link File} to load.
* @return the loaded {@link Properties}.
*/
public static Properties loadIfExists(File file) {
Properties properties = new Properties();
if (file != null) {
if (file.exists()) {
load(properties, file);
} else {
Log.trace("Properties file does not exist: " + file);
}
}
return properties;
}
/**
* @param properties the existing {@link Properties} instance.
* @param file the properties {@link File} to load.
*/
public static void load(Properties properties, File file) {
Log.trace("Loading properties file " + file);
try (InputStream in = new FileInputStream(file);
Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8)) {
properties.load(reader);
} catch (IOException e) {
throw new IllegalStateException("Could not load properties from file: " + file, e);
}
}
private void resolve(Properties properties, VariableResolver resolver) {
Set