* expose file media.types entry as {mediaType} binding

This commit is contained in:
Reinhard Pointner 2013-12-22 08:33:19 +00:00
parent 1525623ccd
commit 30d3134f81
3 changed files with 36 additions and 26 deletions

View File

@ -1,7 +1,5 @@
package net.sourceforge.filebot; package net.sourceforge.filebot;
import static java.util.Collections.*; import static java.util.Collections.*;
import static net.sourceforge.tuned.XPathUtilities.*; import static net.sourceforge.tuned.XPathUtilities.*;
@ -18,75 +16,81 @@ import net.sourceforge.tuned.FileUtilities.ExtensionFileFilter;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Node; import org.w3c.dom.Node;
public class MediaTypes { public class MediaTypes {
private static final MediaTypes defaultInstance = parseDefault(); private static final MediaTypes defaultInstance = parseDefault();
private static MediaTypes parseDefault() { private static MediaTypes parseDefault() {
try { try {
Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(MediaTypes.class.getResourceAsStream("media.types")); Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(MediaTypes.class.getResourceAsStream("media.types"));
Map<String, List<String>> types = new HashMap<String, List<String>>(); Map<String, List<String>> types = new HashMap<String, List<String>>();
for (Node it : getChildren("type", dom.getFirstChild())) { for (Node it : getChildren("type", dom.getFirstChild())) {
List<String> extensions = new ArrayList<String>(2); List<String> extensions = new ArrayList<String>(2);
for (Node ie : getChildren("extension", it)) { for (Node ie : getChildren("extension", it)) {
extensions.add(getTextContent(ie)); extensions.add(getTextContent(ie));
} }
types.put(getAttribute("name", it), extensions); types.put(getAttribute("name", it), extensions);
} }
return new MediaTypes(types); return new MediaTypes(types);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
private Map<String, List<String>> types; private Map<String, List<String>> types;
private Map<String, ExtensionFileFilter> filters = synchronizedMap(new HashMap<String, ExtensionFileFilter>()); private Map<String, ExtensionFileFilter> filters = synchronizedMap(new HashMap<String, ExtensionFileFilter>());
public MediaTypes(Map<String, List<String>> types) { public MediaTypes(Map<String, List<String>> types) {
this.types = types; this.types = types;
} }
public List<String> getExtensionList(String name) { public List<String> getExtensionList(String name) {
List<String> list = new ArrayList<String>(); List<String> list = new ArrayList<String>();
for (Entry<String, List<String>> type : types.entrySet()) { for (Entry<String, List<String>> type : types.entrySet()) {
if (type.getKey().startsWith(name)) { if (type.getKey().startsWith(name)) {
list.addAll(type.getValue()); list.addAll(type.getValue());
} }
} }
return list; return list;
} }
public ExtensionFileFilter getFilter(String name) { public ExtensionFileFilter getFilter(String name) {
ExtensionFileFilter filter = filters.get(name); ExtensionFileFilter filter = filters.get(name);
if (filter == null) { if (filter == null) {
filter = new ExtensionFileFilter(getExtensionList(name)); filter = new ExtensionFileFilter(getExtensionList(name));
filters.put(name, filter); filters.put(name, filter);
} }
return filter; return filter;
} }
public static MediaTypes getDefault() { public static MediaTypes getDefault() {
return defaultInstance; return defaultInstance;
} }
public Map<String, List<String>> getTypes() {
return types;
}
public String getMediaType(String extension) {
for (Entry<String, List<String>> it : getTypes().entrySet()) {
if (it.getValue().contains(extension)) {
return it.getKey();
}
}
return null;
}
public static ExtensionFileFilter getDefaultFilter(String name) { public static ExtensionFileFilter getDefaultFilter(String name) {
return defaultInstance.getFilter(name); return defaultInstance.getFilter(name);
} }
// some convenience filters // some convenience filters
public static final ExtensionFileFilter AUDIO_FILES = getDefaultFilter("audio"); public static final ExtensionFileFilter AUDIO_FILES = getDefaultFilter("audio");
public static final ExtensionFileFilter VIDEO_FILES = getDefaultFilter("video"); public static final ExtensionFileFilter VIDEO_FILES = getDefaultFilter("video");

View File

@ -27,6 +27,7 @@ import java.util.SortedSet;
import java.util.TreeSet; import java.util.TreeSet;
import net.sourceforge.filebot.Cache; import net.sourceforge.filebot.Cache;
import net.sourceforge.filebot.MediaTypes;
import net.sourceforge.filebot.WebServices; import net.sourceforge.filebot.WebServices;
import net.sourceforge.filebot.hash.HashType; import net.sourceforge.filebot.hash.HashType;
import net.sourceforge.filebot.media.MetaAttributes; import net.sourceforge.filebot.media.MetaAttributes;
@ -626,6 +627,11 @@ public class MediaBindingBean {
return null; return null;
} }
@Define("mediaType")
public List<String> getMediaType() throws Exception {
return asList(MediaTypes.getDefault().getMediaType(getExtension()).split("/")); // format engine does not allow / in binding value
}
@Define("file") @Define("file")
public File getMediaFile() { public File getMediaFile() {
return mediaFile; return mediaFile;

View File

@ -225,7 +225,7 @@ public class FormatDialog extends JDialog {
// initialize window properties // initialize window properties
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
setSize(620, 445); setSize(650, 450);
// initialize data // initialize data
setState(initMode, lockOnBinding != null ? lockOnBinding : restoreSample(initMode), lockOnBinding != null); setState(initMode, lockOnBinding != null ? lockOnBinding : restoreSample(initMode), lockOnBinding != null);