diff --git a/source/net/sourceforge/filebot/Main.java b/source/net/sourceforge/filebot/Main.java index 473cafcb..20e4d31d 100644 --- a/source/net/sourceforge/filebot/Main.java +++ b/source/net/sourceforge/filebot/Main.java @@ -92,6 +92,9 @@ public class Main { startUserInterface(args); } }); + + // pre-load media.types (when loaded during DnD it will freeze the UI for a few hundred milliseconds) + MediaTypes.getDefault(); } catch (CmdLineException e) { // illegal arguments => just print CLI error message and stop System.err.println(e.getMessage()); @@ -99,7 +102,7 @@ public class Main { } } - + private static void startUserInterface(ArgumentBean args) { JFrame frame; @@ -126,7 +129,7 @@ public class Main { frame.setVisible(true); } - + private static void restoreWindowBounds(final JFrame window, final Settings settings) { // store bounds on close window.addWindowListener(new WindowAdapter() { @@ -151,7 +154,7 @@ public class Main { window.setBounds(x, y, width, height); } - + /** * Shutdown ehcache properly, so that disk-persistent stores can actually be saved to disk */ @@ -165,7 +168,7 @@ public class Main { }); } - + /** * Initialize default SecurityManager and grant all permissions via security policy. * Initialization is required in order to run {@link ExpressionFormat} in a secure sandbox. @@ -182,7 +185,7 @@ public class Main { return true; } - + @Override public PermissionCollection getPermissions(CodeSource codesource) { // VisualVM can't connect if this method does return diff --git a/source/net/sourceforge/filebot/MediaTypes.java b/source/net/sourceforge/filebot/MediaTypes.java index 5b8b6f3e..743b6e21 100644 --- a/source/net/sourceforge/filebot/MediaTypes.java +++ b/source/net/sourceforge/filebot/MediaTypes.java @@ -3,70 +3,71 @@ package net.sourceforge.filebot; import static java.util.Collections.*; +import static net.sourceforge.tuned.XPathUtilities.*; -import java.io.FileFilter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Map.Entry; -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Unmarshaller; -import javax.xml.bind.annotation.XmlAttribute; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.w3c.dom.Document; +import org.w3c.dom.Node; import net.sourceforge.tuned.FileUtilities.ExtensionFileFilter; -@XmlRootElement(name = "media-types") public class MediaTypes { - @XmlElement(name = "type") - private Type[] types; + private static final MediaTypes defaultInstance = parseDefault(); - - private static class Type { - - @XmlAttribute(name = "name") - private String name; - - @XmlElement(name = "extension") - private String[] extensions; - } - - private static final MediaTypes defaultInstance = unmarshal(); - - - private static MediaTypes unmarshal() { + private static MediaTypes parseDefault() { try { - Unmarshaller unmarshaller = JAXBContext.newInstance(MediaTypes.class).createUnmarshaller(); - return (MediaTypes) unmarshaller.unmarshal(MediaTypes.class.getResource("media.types")); - } catch (JAXBException e) { + Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(MediaTypes.class.getResourceAsStream("media.types")); + Map> types = new HashMap>(); + + for (Node it : getChildren("type", dom.getFirstChild())) { + List extensions = new ArrayList(2); + for (Node ie : getChildren("extension", it)) { + extensions.add(getTextContent(ie)); + } + + types.put(getAttribute("name", it), extensions); + } + + return new MediaTypes(types); + } catch (Exception e) { throw new RuntimeException(e); } } - + + private Map> types; private Map filters = synchronizedMap(new HashMap()); - + + public MediaTypes(Map> types) { + this.types = types; + } + + public List getExtensionList(String name) { List list = new ArrayList(); - for (Type type : defaultInstance.types) { - if (type.name.startsWith(name)) { - addAll(list, type.extensions); + for (Entry> type : types.entrySet()) { + if (type.getKey().startsWith(name)) { + list.addAll(type.getValue()); } } return list; } - - public FileFilter getFilter(String name) { + + public ExtensionFileFilter getFilter(String name) { ExtensionFileFilter filter = filters.get(name); if (filter == null) { @@ -77,19 +78,19 @@ public class MediaTypes { return filter; } - + public static MediaTypes getDefault() { return defaultInstance; } - + public static ExtensionFileFilter getDefaultFilter(String name) { - return new ExtensionFileFilter(getDefault().getExtensionList(name)); + return defaultInstance.getFilter(name); } - + // some convenience filters - public static final ExtensionFileFilter AUDIO_FILES = MediaTypes.getDefaultFilter("audio"); - public static final ExtensionFileFilter VIDEO_FILES = MediaTypes.getDefaultFilter("video"); - public static final ExtensionFileFilter SUBTITLE_FILES = MediaTypes.getDefaultFilter("subtitle"); + public static final ExtensionFileFilter AUDIO_FILES = getDefaultFilter("audio"); + public static final ExtensionFileFilter VIDEO_FILES = getDefaultFilter("video"); + public static final ExtensionFileFilter SUBTITLE_FILES = getDefaultFilter("subtitle"); }