|
| 1 | +package com.ss.editor.file.converter.impl; |
| 2 | + |
| 3 | +import com.ss.editor.Editor; |
| 4 | +import com.ss.editor.file.converter.FileConverter; |
| 5 | +import com.ss.editor.ui.event.FXEventManager; |
| 6 | +import com.ss.editor.ui.event.impl.CreatedFileEvent; |
| 7 | +import com.ss.editor.ui.event.impl.FileChangedEvent; |
| 8 | + |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Path; |
| 11 | + |
| 12 | +import rlib.logging.Logger; |
| 13 | +import rlib.logging.LoggerManager; |
| 14 | +import rlib.util.FileUtils; |
| 15 | +import rlib.util.array.Array; |
| 16 | +import rlib.util.array.ArrayFactory; |
| 17 | + |
| 18 | +import static rlib.util.FileUtils.containsExtensions; |
| 19 | + |
| 20 | +/** |
| 21 | + * Базовая реализация конвертера файлов. |
| 22 | + * |
| 23 | + * @author Ronn |
| 24 | + */ |
| 25 | +public abstract class AbstractFileConverter implements FileConverter { |
| 26 | + |
| 27 | + protected static final Logger LOGGER = LoggerManager.getLogger(FileConverter.class); |
| 28 | + |
| 29 | + private static final Array<String> EMPTY_ARRAY = ArrayFactory.newArray(String.class); |
| 30 | + |
| 31 | + protected static final FXEventManager FX_EVENT_MANAGER = FXEventManager.getInstance(); |
| 32 | + protected static final Editor EDITOR = Editor.getInstance(); |
| 33 | + |
| 34 | + @Override |
| 35 | + public void convert(final Path source) { |
| 36 | + |
| 37 | + final String targetFileName = FileUtils.getNameWithoutExtension(source) + "." + getTargetExtension(); |
| 38 | + |
| 39 | + final Path parent = source.getParent(); |
| 40 | + final Path targetFile = parent.resolve(targetFileName); |
| 41 | + |
| 42 | + convert(source, targetFile); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void convert(final Path source, final Path destination) { |
| 47 | + |
| 48 | + if (source == null || destination == null) { |
| 49 | + throw new IllegalArgumentException("source or destination is null."); |
| 50 | + } else if (Files.isDirectory(source) || Files.isDirectory(destination)) { |
| 51 | + throw new IllegalArgumentException("source or destination is folder."); |
| 52 | + } |
| 53 | + |
| 54 | + final Array<String> extensions = getAvailableExtensions(); |
| 55 | + extensions.trimToSize(); |
| 56 | + |
| 57 | + if (!extensions.isEmpty() && !containsExtensions(extensions.array(), source)) { |
| 58 | + throw new IllegalArgumentException("incorrect extension of file " + source); |
| 59 | + } |
| 60 | + |
| 61 | + convertImpl(source, destination, Files.exists(destination)); |
| 62 | + } |
| 63 | + |
| 64 | + protected void convertImpl(final Path source, final Path destination, final boolean overwrite) { |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @return список доступных расширений для конвертации. |
| 69 | + */ |
| 70 | + protected Array<String> getAvailableExtensions() { |
| 71 | + return EMPTY_ARRAY; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public String getTargetExtension() { |
| 76 | + return ""; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Уведомление всех об изменении файла. |
| 81 | + * |
| 82 | + * @param file изменяемый файл. |
| 83 | + */ |
| 84 | + protected void notifyFileChanged(final Path file) { |
| 85 | + |
| 86 | + final FileChangedEvent event = new FileChangedEvent(); |
| 87 | + event.setFile(file); |
| 88 | + |
| 89 | + FX_EVENT_MANAGER.notify(event); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Уведомление всех об создании файла. |
| 94 | + * |
| 95 | + * @param file созданный файл. |
| 96 | + */ |
| 97 | + protected void notifyFileCreated(final Path file) { |
| 98 | + |
| 99 | + final CreatedFileEvent event = new CreatedFileEvent(); |
| 100 | + event.setFile(file); |
| 101 | + |
| 102 | + FX_EVENT_MANAGER.notify(event); |
| 103 | + } |
| 104 | +} |
0 commit comments