1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-04 02:09:35 +02:00
FBReaderJ/src/org/geometerplus/fbreader/formats/PluginCollection.java
2012-02-23 22:51:55 +01:00

118 lines
3.5 KiB
Java

/*
* Copyright (C) 2007-2012 Geometer Plus <contact@geometerplus.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
package org.geometerplus.fbreader.formats;
import java.util.*;
import org.geometerplus.zlibrary.core.options.*;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.fbreader.formats.fb2.FB2Plugin;
import org.geometerplus.fbreader.formats.oeb.OEBPlugin;
import org.geometerplus.fbreader.formats.pdb.MobipocketPlugin;
import org.geometerplus.fbreader.filetype.*;
public class PluginCollection {
static {
System.loadLibrary("NativeFormats-v1");
}
private static PluginCollection ourInstance;
private final Map<FormatPlugin.Type,List<FormatPlugin>> myPlugins =
new HashMap<FormatPlugin.Type,List<FormatPlugin>>();
public ZLStringOption DefaultLanguageOption;
public ZLStringOption DefaultEncodingOption;
public ZLBooleanOption LanguageAutoDetectOption;
public static PluginCollection Instance() {
if (ourInstance == null) {
ourInstance = new PluginCollection();
// This code can not be moved to constructor because nativePlugins() is a native method
for (NativeFormatPlugin p : ourInstance.nativePlugins()) {
ourInstance.addPlugin(p);
}
}
return ourInstance;
}
public static void deleteInstance() {
if (ourInstance != null) {
ourInstance = null;
}
}
private PluginCollection() {
LanguageAutoDetectOption = new ZLBooleanOption("Format", "AutoDetect", true);
DefaultLanguageOption = new ZLStringOption("Format", "DefaultLanguage", "en");
DefaultEncodingOption = new ZLStringOption("Format", "DefaultEncoding", "windows-1252");
addPlugin(new FB2Plugin());
addPlugin(new MobipocketPlugin());
addPlugin(new OEBPlugin());
}
private void addPlugin(FormatPlugin plugin) {
final FormatPlugin.Type type = plugin.type();
List<FormatPlugin> list = myPlugins.get(type);
if (list == null) {
list = new ArrayList<FormatPlugin>();
myPlugins.put(type, list);
}
list.add(plugin);
}
public FormatPlugin getPlugin(ZLFile file) {
return getPlugin(file, FormatPlugin.Type.ANY);
}
public FormatPlugin getPlugin(ZLFile file, FormatPlugin.Type formatType) {
final FileType fileType = FileTypeCollection.Instance.typeForFile(file);
return getPlugin(fileType, formatType);
}
public FormatPlugin getPlugin(FileType fileType, FormatPlugin.Type formatType) {
if (fileType == null) {
return null;
}
if (formatType == FormatPlugin.Type.ANY) {
FormatPlugin p = getPlugin(fileType, FormatPlugin.Type.JAVA);
if (p == null) {
p = getPlugin(fileType, FormatPlugin.Type.NATIVE);
}
return p;
} else {
final List<FormatPlugin> list = myPlugins.get(formatType);
if (list == null) {
return null;
}
for (FormatPlugin p : list) {
if (fileType.Id.equalsIgnoreCase(p.supportedFileType())) {
return p;
}
}
return null;
}
}
private native NativeFormatPlugin[] nativePlugins();
}