finalize folder strategies
This commit is contained in:
parent
8b752480db
commit
4c0a1fa24a
@ -1,14 +1,20 @@
|
||||
package de.marhali.easyi18n.ionext.folder;
|
||||
|
||||
import com.intellij.openapi.vfs.LocalFileSystem;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
|
||||
import de.marhali.easyi18n.model.ParserStrategyType;
|
||||
import de.marhali.easyi18n.model.SettingsState;
|
||||
import de.marhali.easyi18n.model.TranslationData;
|
||||
import de.marhali.easyi18n.model.TranslationFile;
|
||||
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Represents a specific translation file directory structure.
|
||||
@ -28,7 +34,16 @@ public abstract class FolderStrategy {
|
||||
* @param localesDirectory Configured translation file directory
|
||||
* @return translation files which matches the strategy
|
||||
*/
|
||||
public abstract List<TranslationFile> findFiles(@NotNull VirtualFile localesDirectory);
|
||||
public abstract @NotNull List<TranslationFile> analyzeFolderStructure(@NotNull VirtualFile localesDirectory);
|
||||
|
||||
/**
|
||||
* Analyzes the provided translation data and returns the directory structure based on the implementing strategy
|
||||
* @param localesPath Configured locales path
|
||||
* @param data Translation data to use for write action
|
||||
* @return translation file structure
|
||||
*/
|
||||
public abstract @NotNull List<TranslationFile> constructFolderStructure(@NotNull String localesPath,
|
||||
@NotNull ParserStrategyType type, @NotNull TranslationData data) throws IOException;
|
||||
|
||||
/**
|
||||
* Checks if the provided file is not a directory and matches the configured file pattern
|
||||
@ -38,4 +53,22 @@ public abstract class FolderStrategy {
|
||||
protected boolean isFileRelevant(@NotNull VirtualFile file) {
|
||||
return !file.isDirectory() && FilenameUtils.wildcardMatch(file.getName(), this.settings.getFilePattern());
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param parent Directory path
|
||||
* @param child File name with extension
|
||||
* @return IntelliJ {@link VirtualFile}
|
||||
* @throws IOException Could not access file
|
||||
*/
|
||||
protected @NotNull VirtualFile constructFile(@NotNull String parent, @NotNull String child) throws IOException {
|
||||
File file = new File(parent, child);
|
||||
boolean exists = file.createNewFile();
|
||||
|
||||
VirtualFile vf = exists
|
||||
? LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file)
|
||||
: LocalFileSystem.getInstance().findFileByIoFile(file);
|
||||
|
||||
return Objects.requireNonNull(vf);
|
||||
}
|
||||
}
|
||||
|
@ -2,11 +2,14 @@ package de.marhali.easyi18n.ionext.folder;
|
||||
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
|
||||
import de.marhali.easyi18n.model.ParserStrategyType;
|
||||
import de.marhali.easyi18n.model.SettingsState;
|
||||
import de.marhali.easyi18n.model.TranslationData;
|
||||
import de.marhali.easyi18n.model.TranslationFile;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@ -22,7 +25,7 @@ public class ModularLocaleFolderStrategy extends FolderStrategy {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TranslationFile> findFiles(@NotNull VirtualFile localesDirectory) {
|
||||
public @NotNull List<TranslationFile> analyzeFolderStructure(@NotNull VirtualFile localesDirectory) {
|
||||
List<TranslationFile> files = new ArrayList<>();
|
||||
|
||||
for(VirtualFile localeModuleDir : localesDirectory.getChildren()) {
|
||||
@ -39,4 +42,23 @@ public class ModularLocaleFolderStrategy extends FolderStrategy {
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull List<TranslationFile> constructFolderStructure(
|
||||
@NotNull String localesPath, @NotNull ParserStrategyType type,
|
||||
@NotNull TranslationData data) throws IOException {
|
||||
|
||||
List<TranslationFile> files = new ArrayList<>();
|
||||
|
||||
for(String locale : data.getLocales()) {
|
||||
for(String namespace : data.getRootNode().getChildren().keySet()) {
|
||||
VirtualFile vf = super.constructFile(localesPath + "/" + locale,
|
||||
namespace + "." + type.getFileExtension());
|
||||
|
||||
files.add(new TranslationFile(vf, locale, namespace));
|
||||
}
|
||||
}
|
||||
|
||||
return files;
|
||||
}
|
||||
}
|
||||
|
@ -2,11 +2,14 @@ package de.marhali.easyi18n.ionext.folder;
|
||||
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
|
||||
import de.marhali.easyi18n.model.ParserStrategyType;
|
||||
import de.marhali.easyi18n.model.SettingsState;
|
||||
import de.marhali.easyi18n.model.TranslationData;
|
||||
import de.marhali.easyi18n.model.TranslationFile;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@ -22,7 +25,7 @@ public class ModularNamespaceFolderStrategy extends FolderStrategy {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TranslationFile> findFiles(@NotNull VirtualFile localesDirectory) {
|
||||
public @NotNull List<TranslationFile> analyzeFolderStructure(@NotNull VirtualFile localesDirectory) {
|
||||
List<TranslationFile> files = new ArrayList<>();
|
||||
|
||||
for(VirtualFile namespaceModuleDir : localesDirectory.getChildren()) {
|
||||
@ -39,4 +42,23 @@ public class ModularNamespaceFolderStrategy extends FolderStrategy {
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull List<TranslationFile> constructFolderStructure(
|
||||
@NotNull String localesPath, @NotNull ParserStrategyType type,
|
||||
@NotNull TranslationData data) throws IOException {
|
||||
|
||||
List<TranslationFile> files = new ArrayList<>();
|
||||
|
||||
for(String namespace : data.getRootNode().getChildren().keySet()) {
|
||||
for(String locale : data.getLocales()) {
|
||||
VirtualFile vf = super.constructFile(localesPath + "/" + namespace,
|
||||
locale + "." + type.getFileExtension());
|
||||
|
||||
files.add(new TranslationFile(vf, locale, namespace));
|
||||
}
|
||||
}
|
||||
|
||||
return files;
|
||||
}
|
||||
}
|
||||
|
@ -2,11 +2,14 @@ package de.marhali.easyi18n.ionext.folder;
|
||||
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
|
||||
import de.marhali.easyi18n.model.ParserStrategyType;
|
||||
import de.marhali.easyi18n.model.SettingsState;
|
||||
import de.marhali.easyi18n.model.TranslationData;
|
||||
import de.marhali.easyi18n.model.TranslationFile;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@ -14,6 +17,7 @@ import java.util.List;
|
||||
* Single directory translation folder strategy.
|
||||
* Every child is recognized as a file for a specific language.
|
||||
* Directory => en.file, de.file, fr.file
|
||||
*
|
||||
* @author marhali
|
||||
*/
|
||||
public class SingleFolderStrategy extends FolderStrategy {
|
||||
@ -23,15 +27,32 @@ public class SingleFolderStrategy extends FolderStrategy {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TranslationFile> findFiles(@NotNull VirtualFile localesDirectory) {
|
||||
public @NotNull List<TranslationFile> analyzeFolderStructure(@NotNull VirtualFile localesDirectory) {
|
||||
List<TranslationFile> files = new ArrayList<>();
|
||||
|
||||
for(VirtualFile file : localesDirectory.getChildren()) {
|
||||
if(super.isFileRelevant(file)) {
|
||||
for (VirtualFile file : localesDirectory.getChildren()) {
|
||||
if (super.isFileRelevant(file)) {
|
||||
files.add(new TranslationFile(file, file.getNameWithoutExtension(), null));
|
||||
}
|
||||
}
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull List<TranslationFile> constructFolderStructure(
|
||||
@NotNull String localesPath, @NotNull ParserStrategyType type,
|
||||
@NotNull TranslationData data) throws IOException {
|
||||
|
||||
List<TranslationFile> files = new ArrayList<>();
|
||||
|
||||
for (String locale : data.getLocales()) {
|
||||
VirtualFile vf = super.constructFile(localesPath,
|
||||
locale + "." + type.getFileExtension());
|
||||
|
||||
files.add(new TranslationFile(vf, locale, null));
|
||||
}
|
||||
|
||||
return files;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user