remove legacy io strategies

This commit is contained in:
marhali 2022-02-03 16:44:15 +01:00
parent 53ea963db8
commit 6587f7b163
5 changed files with 0 additions and 590 deletions

View File

@ -1,63 +0,0 @@
package de.marhali.easyi18n.io;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import de.marhali.easyi18n.model.SettingsState;
import de.marhali.easyi18n.model.TranslationData;
import org.apache.commons.io.FilenameUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.function.Consumer;
/**
* Primary interface for the exchange of translation data with the underlying IO system.
* The selection of the right IO strategy is done by the @canUse method (first match).
* Every strategy needs to be registered inside {@link de.marhali.easyi18n.DataStore}
*
* @author marhali
*/
public interface IOStrategy {
/**
* Decides whether this strategy should be applied or not. First matching one will be used.
* @param project IntelliJ project context
* @param localesPath Root directory which leads to all i18n files
* @param state Plugin configuration
* @return true if strategy is responsible for the found structure
*/
boolean canUse(@NotNull Project project, @NotNull String localesPath, @NotNull SettingsState state);
/**
* Loads the translation files and passes them in the result consumer.
* Result payload might be null if operation failed.
* @param project IntelliJ project context
* @param localesPath Root directory which leads to all i18n files
* @param state Plugin configuration
* @param result Passes loaded data
*/
void read(@NotNull Project project, @NotNull String localesPath, @NotNull SettingsState state,
@NotNull Consumer<@Nullable TranslationData> result);
/**
* Writes the provided translation data to the IO system.
* @param project InteliJ project context
* @param localesPath Root directory which leads to all i18n files
* @param state Plugin configuration
* @param data Translations to save
* @param result Indicates whether the operation was successful
*/
void write(@NotNull Project project, @NotNull String localesPath, @NotNull SettingsState state,
@NotNull TranslationData data, @NotNull Consumer<Boolean> result);
/**
* Checks if the provided file should be processed for translation data
* @param state Plugin configuration
* @param file File to check
* @return true if file matches pattern
*/
default boolean isFileRelevant(@NotNull SettingsState state, @NotNull VirtualFile file) {
return FilenameUtils.wildcardMatch(file.getName(), state.getFilePattern());
}
}

View File

@ -1,125 +0,0 @@
package de.marhali.easyi18n.io.json;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import de.marhali.easyi18n.io.IOStrategy;
import de.marhali.easyi18n.ionext.parser.json.JsonMapper;
import de.marhali.easyi18n.model.SettingsState;
import de.marhali.easyi18n.model.TranslationData;
import de.marhali.easyi18n.util.NotificationHelper;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.function.Consumer;
/**
* Strategy for simple json locale files. Each locale has its own file.
* For example localesPath/en.json, localesPath/de.json.
* @author marhali
*/
public class JsonIOStrategy implements IOStrategy {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
private final String FILE_EXTENSION;
public JsonIOStrategy(@NotNull String fileExtension) {
this.FILE_EXTENSION = fileExtension;
}
@Override
public boolean canUse(@NotNull Project project, @NotNull String localesPath, @NotNull SettingsState state) {
VirtualFile directory = LocalFileSystem.getInstance().findFileByIoFile(new File(localesPath));
if(directory == null || directory.getChildren() == null) {
return false;
}
for(VirtualFile children : directory.getChildren()) {
if(!children.isDirectory() && isFileRelevant(state, children)) {
if(children.getExtension().equalsIgnoreCase(FILE_EXTENSION)) {
return true;
}
}
}
return false;
}
@Override
public void read(@NotNull Project project, @NotNull String localesPath,
@NotNull SettingsState state, @NotNull Consumer<@Nullable TranslationData> result) {
ApplicationManager.getApplication().saveAll(); // Save opened files (required if new locales were added)
ApplicationManager.getApplication().runReadAction(() -> {
VirtualFile directory = LocalFileSystem.getInstance().findFileByIoFile(new File(localesPath));
if(directory == null || directory.getChildren() == null) {
throw new IllegalArgumentException("Specified folder is invalid (" + localesPath + ")");
}
TranslationData data = new TranslationData(state.isSortKeys());
for(VirtualFile file : directory.getChildren()) {
if(file.isDirectory() || !isFileRelevant(state, file)) {
continue;
}
String locale = file.getNameWithoutExtension();
data.addLocale(locale);
try {
JsonObject tree = GSON.fromJson(new InputStreamReader(file.getInputStream(), file.getCharset()),
JsonObject.class);
JsonMapper.read(locale, tree, data.getRootNode());
} catch (Exception e) {
NotificationHelper.createIOError(file.getName(), this.getClass(), e);
result.accept(null);
return;
}
}
result.accept(data);
});
}
@Override
public void write(@NotNull Project project, @NotNull String localesPath,
@NotNull SettingsState state, @NotNull TranslationData data, @NotNull Consumer<Boolean> result) {
ApplicationManager.getApplication().runWriteAction(() -> {
try {
for(String locale : data.getLocales()) {
JsonObject content = new JsonObject();
JsonMapper.write(locale, content, data.getRootNode());
File file = new File(localesPath + "/" + locale + "." + FILE_EXTENSION);
boolean exists = file.createNewFile();
VirtualFile vf = exists
? LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file)
: LocalFileSystem.getInstance().findFileByIoFile(file);
vf.setBinaryContent(GSON.toJson(content).getBytes(vf.getCharset()));
}
result.accept(true);
} catch(IOException e) {
e.printStackTrace();
result.accept(false);
}
});
}
}

View File

@ -1,158 +0,0 @@
package de.marhali.easyi18n.io.json;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import de.marhali.easyi18n.io.IOStrategy;
import de.marhali.easyi18n.ionext.parser.json.JsonMapper;
import de.marhali.easyi18n.model.KeyPath;
import de.marhali.easyi18n.model.SettingsState;
import de.marhali.easyi18n.model.TranslationData;
import de.marhali.easyi18n.model.TranslationNode;
import de.marhali.easyi18n.util.NotificationHelper;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Consumer;
/**
* Strategy for distributed json files per locale. Each locale can have multiple modules. The file name
* of each module will be used as the key for the underlying translations. <br/>
* Full key example: <moduleFileName>.<username>.<title>
*
* @author marhali
*/
public class ModularizedJsonIOStrategy implements IOStrategy {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
private final String FILE_EXTENSION;
public ModularizedJsonIOStrategy(@NotNull String fileExtension) {
this.FILE_EXTENSION = fileExtension;
}
@Override
public boolean canUse(@NotNull Project project, @NotNull String localesPath, @NotNull SettingsState state) {
VirtualFile directory = LocalFileSystem.getInstance().findFileByIoFile(new File(localesPath));
if(directory == null || directory.getChildren() == null) {
return false;
}
// We expect something like this:
// <localesPath>/<localeDir>/<moduleFile>
for(VirtualFile children : directory.getChildren()) {
if(children.isDirectory()) { // Contains module folders
for(VirtualFile moduleFile : children.getChildren()) {
if(!moduleFile.isDirectory() && isFileRelevant(state, moduleFile)) {
if(moduleFile.getExtension().equalsIgnoreCase(FILE_EXTENSION)) {
return true;
}
}
}
}
}
return false;
}
@Override
public void read(@NotNull Project project, @NotNull String localesPath,
@NotNull SettingsState state, @NotNull Consumer<@Nullable TranslationData> result) {
ApplicationManager.getApplication().saveAll(); // Save opened files (required if new locales were added)
ApplicationManager.getApplication().runReadAction(() -> {
VirtualFile directory = LocalFileSystem.getInstance().findFileByIoFile(new File(localesPath));
if(directory == null || directory.getChildren() == null) {
throw new IllegalArgumentException("Specified folder is invalid (" + localesPath + ")");
}
TranslationData data = new TranslationData(state.isSortKeys());
VirtualFile[] localeDirectories = directory.getChildren();
for(VirtualFile localeDir : localeDirectories) {
String locale = localeDir.getNameWithoutExtension();
data.addLocale(locale);
// Read all underlying module files
for(VirtualFile module : localeDir.getChildren()) {
if(module.isDirectory() || !isFileRelevant(state, module)) {
continue;
}
String moduleName = module.getNameWithoutExtension();
TranslationNode moduleNode = data.getNode(KeyPath.of(moduleName)) != null
? data.getNode(KeyPath.of(moduleName))
: new TranslationNode(state.isSortKeys() ? new TreeMap<>() : new LinkedHashMap<>());
try {
JsonObject tree = GSON.fromJson(new InputStreamReader(module.getInputStream(),
module.getCharset()), JsonObject.class);
JsonMapper.read(locale, tree, moduleNode);
data.getRootNode().setChildren(moduleName, moduleNode);
} catch (Exception e) {
NotificationHelper.createIOError(locale + " / " + moduleName, this.getClass(), e);
result.accept(null);
return;
}
}
}
result.accept(data);
});
}
// TODO: there will be problems when adding translations via TranslationData with non-nested key mode
@Override
public void write(@NotNull Project project, @NotNull String localesPath,
@NotNull SettingsState state, @NotNull TranslationData data, @NotNull Consumer<Boolean> result) {
ApplicationManager.getApplication().runWriteAction(() -> {
try {
for(String locale : data.getLocales()) {
for(Map.Entry<String, TranslationNode> entry : data.getRootNode().getChildren().entrySet()) {
String module = entry.getKey();
JsonObject content = new JsonObject();
JsonMapper.write(locale, content, entry.getValue());
String fullPath = localesPath + "/" + locale + "/" + module + "." + FILE_EXTENSION;
File file = new File(fullPath);
boolean exists = file.createNewFile();
VirtualFile vf = exists
? LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file)
: LocalFileSystem.getInstance().findFileByIoFile(file);
vf.setBinaryContent(GSON.toJson(content).getBytes(vf.getCharset()));
}
}
result.accept(true);
} catch(IOException e) {
e.printStackTrace();
result.accept(false);
}
});
}
}

View File

@ -1,120 +0,0 @@
package de.marhali.easyi18n.io.properties;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import de.marhali.easyi18n.io.IOStrategy;
import de.marhali.easyi18n.ionext.parser.properties.PropertiesMapper;
import de.marhali.easyi18n.ionext.parser.properties.SortableProperties;
import de.marhali.easyi18n.model.SettingsState;
import de.marhali.easyi18n.model.TranslationData;
import de.marhali.easyi18n.util.NotificationHelper;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.function.Consumer;
/**
* Strategy for simple 'properties' locale files. Each locale has its own file.
* For example localesPath/en.properties, localesPath/de.properties.
* @author marhali
*/
public class PropertiesIOStrategy implements IOStrategy {
private static final String FILE_EXTENSION = "properties";
@Override
public boolean canUse(@NotNull Project project, @NotNull String localesPath, @NotNull SettingsState state) {
VirtualFile directory = LocalFileSystem.getInstance().findFileByIoFile(new File(localesPath));
if(directory == null || directory.getChildren() == null) {
return false;
}
for(VirtualFile children : directory.getChildren()) {
if(!children.isDirectory() && isFileRelevant(state, children)) {
if(children.getExtension().equalsIgnoreCase(FILE_EXTENSION)) {
return true;
}
}
}
return false;
}
@Override
public void read(@NotNull Project project, @NotNull String localesPath,
@NotNull SettingsState state, @NotNull Consumer<@Nullable TranslationData> result) {
ApplicationManager.getApplication().saveAll(); // Save opened files (required if new locales were added)
ApplicationManager.getApplication().runReadAction(() -> {
VirtualFile directory = LocalFileSystem.getInstance().findFileByIoFile(new File(localesPath));
if(directory == null || directory.getChildren() == null) {
throw new IllegalArgumentException("Specified folder is invalid (" + localesPath + ")");
}
TranslationData data = new TranslationData(state.isSortKeys());
for(VirtualFile file : directory.getChildren()) {
if(file.isDirectory() || !isFileRelevant(state, file)) {
continue;
}
String locale = file.getNameWithoutExtension();
data.addLocale(locale);
try {
SortableProperties properties = new SortableProperties(state.isSortKeys());
properties.load(new InputStreamReader(file.getInputStream(), file.getCharset()));
PropertiesMapper.read(locale, properties, data);
} catch (Exception e) {
NotificationHelper.createIOError(file.getName(), this.getClass(), e);
result.accept(null);
return;
}
}
result.accept(data);
});
}
@Override
public void write(@NotNull Project project, @NotNull String localesPath,
@NotNull SettingsState state, @NotNull TranslationData data, @NotNull Consumer<Boolean> result) {
ApplicationManager.getApplication().runWriteAction(() -> {
try {
for(String locale : data.getLocales()) {
SortableProperties properties = new SortableProperties(state.isSortKeys());
PropertiesMapper.write(locale, properties, data);
File file = new File(localesPath + "/" + locale + "." + FILE_EXTENSION);
boolean exists = file.createNewFile();
VirtualFile vf = exists
? LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file)
: LocalFileSystem.getInstance().findFileByIoFile(file);
StringWriter writer = new StringWriter();
properties.store(writer, null);
vf.setBinaryContent(writer.toString().getBytes(vf.getCharset()));
}
result.accept(true);
} catch(IOException e) {
e.printStackTrace();
result.accept(false);
}
});
}
}

View File

@ -1,124 +0,0 @@
package de.marhali.easyi18n.io.yaml;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import de.marhali.easyi18n.io.IOStrategy;
import de.marhali.easyi18n.ionext.parser.yaml.YamlMapper;
import de.marhali.easyi18n.model.SettingsState;
import de.marhali.easyi18n.model.TranslationData;
import de.marhali.easyi18n.util.NotificationHelper;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import thito.nodeflow.config.MapSection;
import thito.nodeflow.config.Section;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.function.Consumer;
/**
* Strategy for simple yaml locale files. Each locale has its own file.
* For example localesPath/en.y(a)ml, localesPath/de.y(a)ml
* @author marhali
*/
public class YamlIOStrategy implements IOStrategy {
private final String FILE_EXTENSION;
public YamlIOStrategy(@NotNull String fileExtension) {
this.FILE_EXTENSION = fileExtension;
}
@Override
public boolean canUse(@NotNull Project project, @NotNull String localesPath, @NotNull SettingsState state) {
VirtualFile directory = LocalFileSystem.getInstance().findFileByIoFile(new File(localesPath));
if(directory == null || directory.getChildren() == null) {
return false;
}
for(VirtualFile children : directory.getChildren()) {
if(!children.isDirectory() && isFileRelevant(state, children)) {
if(children.getExtension().equalsIgnoreCase(FILE_EXTENSION)) {
return true;
}
}
}
return false;
}
@Override
public void read(@NotNull Project project, @NotNull String localesPath,
@NotNull SettingsState state, @NotNull Consumer<@Nullable TranslationData> result) {
ApplicationManager.getApplication().saveAll(); // Save opened files (required if new locales were added)
ApplicationManager.getApplication().runReadAction(() -> {
VirtualFile directory = LocalFileSystem.getInstance().findFileByIoFile(new File(localesPath));
if(directory == null || directory.getChildren() == null) {
throw new IllegalArgumentException("Specified folder is invalid (" + localesPath + ")");
}
TranslationData data = new TranslationData(state.isSortKeys());
for(VirtualFile file : directory.getChildren()) {
if(file.isDirectory() || !isFileRelevant(state, file)) {
continue;
}
String locale = file.getNameWithoutExtension();
data.addLocale(locale);
try {
try(Reader reader = new InputStreamReader(file.getInputStream(), file.getCharset())) {
Section section = Section.parseToMap(reader);
YamlMapper.read(locale, section, data.getRootNode());
}
} catch (Exception e) {
NotificationHelper.createIOError(file.getName(), this.getClass(), e);
result.accept(null);
return;
}
}
result.accept(data);
});
}
@Override
public void write(@NotNull Project project, @NotNull String localesPath,
@NotNull SettingsState state, @NotNull TranslationData data, @NotNull Consumer<Boolean> result) {
ApplicationManager.getApplication().runWriteAction(() -> {
try {
for(String locale : data.getLocales()) {
Section section = new MapSection();
YamlMapper.write(locale, section, data.getRootNode());
File file = new File(localesPath + "/" + locale + "." + FILE_EXTENSION);
boolean exists = file.createNewFile();
VirtualFile vf = exists
? LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file)
: LocalFileSystem.getInstance().findFileByIoFile(file);
vf.setBinaryContent(Section.toString(section).getBytes(vf.getCharset()));
}
result.accept(true);
} catch(IOException e) {
e.printStackTrace();
result.accept(false);
}
});
}
}