From f360c4f99a6dbefd6e26fb0202607c24da35e431 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Wed, 16 Apr 2014 17:23:25 +0000 Subject: [PATCH] * continue porting --- .../net/sourceforge/filebot/MediaTypes.java | 17 ++-- .../filebot/cli/ScriptShellBaseClass.java | 78 +++++++------------ .../filebot/util/FileUtilities.java | 24 ++++++ 3 files changed, 65 insertions(+), 54 deletions(-) diff --git a/source/net/sourceforge/filebot/MediaTypes.java b/source/net/sourceforge/filebot/MediaTypes.java index 5854c35f..c1bfff72 100644 --- a/source/net/sourceforge/filebot/MediaTypes.java +++ b/source/net/sourceforge/filebot/MediaTypes.java @@ -18,7 +18,14 @@ import org.w3c.dom.Node; public class MediaTypes { - private static final MediaTypes defaultInstance = parseDefault(); + private static MediaTypes defaultInstance; + + public static synchronized MediaTypes getDefault() { + if (defaultInstance == null) { + defaultInstance = parseDefault(); + } + return defaultInstance; + } private static MediaTypes parseDefault() { try { @@ -70,10 +77,6 @@ public class MediaTypes { return filter; } - public static MediaTypes getDefault() { - return defaultInstance; - } - public Map> getTypes() { return types; } @@ -88,13 +91,15 @@ public class MediaTypes { } public static ExtensionFileFilter getDefaultFilter(String name) { - return defaultInstance.getFilter(name); + return getDefault().getFilter(name); } // some convenience filters public static final ExtensionFileFilter AUDIO_FILES = getDefaultFilter("audio"); public static final ExtensionFileFilter VIDEO_FILES = getDefaultFilter("video"); public static final ExtensionFileFilter SUBTITLE_FILES = getDefaultFilter("subtitle"); + public static final ExtensionFileFilter ARCHIVE_FILES = getDefaultFilter("archive"); + public static final ExtensionFileFilter VERIFICATION_FILES = getDefaultFilter("verification"); public static final ExtensionFileFilter NFO_FILES = getDefaultFilter("application/nfo"); public static final ExtensionFileFilter LIST_FILES = getDefaultFilter("application/list"); } diff --git a/source/net/sourceforge/filebot/cli/ScriptShellBaseClass.java b/source/net/sourceforge/filebot/cli/ScriptShellBaseClass.java index b9e70643..2e9d5b53 100644 --- a/source/net/sourceforge/filebot/cli/ScriptShellBaseClass.java +++ b/source/net/sourceforge/filebot/cli/ScriptShellBaseClass.java @@ -1,6 +1,5 @@ package net.sourceforge.filebot.cli; -import static java.util.Collections.*; import static net.sourceforge.filebot.Settings.*; import static net.sourceforge.filebot.cli.CLILogging.*; import groovy.lang.Closure; @@ -8,10 +7,6 @@ import groovy.lang.MissingPropertyException; import groovy.lang.Script; import java.io.Console; -import java.io.File; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.List; import java.util.Map; import java.util.logging.Logger; @@ -20,6 +15,7 @@ import javax.script.SimpleBindings; import net.sourceforge.filebot.MediaTypes; import net.sourceforge.filebot.format.AssociativeScriptObject; +import net.sourceforge.filebot.util.FileUtilities; public abstract class ScriptShellBaseClass extends Script { @@ -27,6 +23,31 @@ public abstract class ScriptShellBaseClass extends Script { System.out.println(this); } + private Map defaultValues; + + public void setDefaultValues(Map values) { + this.defaultValues = values; + } + + public Map getDefaultValues() { + return defaultValues; + } + + @Override + public Object getProperty(String property) { + try { + return super.getProperty(property); + } catch (MissingPropertyException e) { + // try user-defined default values + if (defaultValues != null && defaultValues.containsKey(property)) { + return defaultValues.get(property); + } + + // can't use default value, rethrow exception + throw e; + } + } + public void include(String input) throws Throwable { try { executeScript(input, null); @@ -47,38 +68,13 @@ public abstract class ScriptShellBaseClass extends Script { if (bindings != null) { parameters.putAll(bindings); } - parameters.put(ScriptShell.ARGV_BINDING_NAME, asFileList(args)); + parameters.put(ScriptShell.ARGV_BINDING_NAME, FileUtilities.asFileList(args)); // run given script ScriptShell shell = (ScriptShell) getBinding().getVariable(ScriptShell.SHELL_BINDING_NAME); return shell.runScript(input, parameters); } - private Map defaultValues; - - public void setDefaultValues(Map values) { - this.defaultValues = values; - } - - public Map getDefaultValues() { - return defaultValues == null ? null : unmodifiableMap(defaultValues); - } - - @Override - public Object getProperty(String property) { - try { - return super.getProperty(property); - } catch (MissingPropertyException e) { - // try user-defined default values - if (defaultValues != null && defaultValues.containsKey(property)) { - return defaultValues.get(property); - } - - // can't use default value, rethrow exception - throw e; - } - } - public Object tryQuietly(Closure c) { try { return c.call(); @@ -104,11 +100,6 @@ public abstract class ScriptShellBaseClass extends Script { throw new Exception(message); } - @Override - public Object run() { - return null; - } - // define global variable: _args public ArgumentBean get_args() { return getApplicationArguments(); @@ -144,18 +135,9 @@ public abstract class ScriptShellBaseClass extends Script { return System.console(); } - public static List asFileList(Object... paths) { - List files = new ArrayList(); - for (Object it : paths) { - if (it instanceof CharSequence) { - files.add(new File(it.toString())); - } else if (it instanceof File) { - files.add((File) it); - } else if (it instanceof Path) { - files.add(((Path) it).toFile()); - } - } - return files; + @Override + public Object run() { + return null; } } diff --git a/source/net/sourceforge/filebot/util/FileUtilities.java b/source/net/sourceforge/filebot/util/FileUtilities.java index eb8602e4..97c9df7c 100644 --- a/source/net/sourceforge/filebot/util/FileUtilities.java +++ b/source/net/sourceforge/filebot/util/FileUtilities.java @@ -15,8 +15,10 @@ import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.charset.Charset; import java.nio.file.AtomicMoveNotSupportedException; +import java.nio.file.Path; import java.nio.file.StandardCopyOption; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Comparator; import java.util.HashMap; @@ -395,6 +397,14 @@ public final class FileUtilities { return f; } + public static List listFiles(File... folders) { + return listFiles(Arrays.asList(folders)); + } + + public static List listFiles(Iterable folders) { + return listFiles(folders, 255, false); + } + public static List listFiles(Iterable folders, int maxDepth, boolean listHiddenFiles) { List files = new ArrayList(); @@ -539,6 +549,20 @@ public final class FileUtilities { return Pattern.compile("\\s*[\\\\/]+\\s*").matcher(path).replaceAll(replacement); } + public static List asFileList(Object... paths) { + List files = new ArrayList(paths.length); + for (Object it : paths) { + if (it instanceof CharSequence) { + files.add(new File(it.toString())); + } else if (it instanceof File) { + files.add((File) it); + } else if (it instanceof Path) { + files.add(((Path) it).toFile()); + } + } + return files; + } + public static String getXmlString(Document dom) throws TransformerException { Transformer tr = TransformerFactory.newInstance().newTransformer(); tr.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");