diff --git a/src/org/geometerplus/fbreader/formats/AutoEncodingCollection.java b/src/org/geometerplus/fbreader/formats/AutoEncodingCollection.java new file mode 100644 index 000000000..f1035dce0 --- /dev/null +++ b/src/org/geometerplus/fbreader/formats/AutoEncodingCollection.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2007-2012 Geometer Plus + * + * 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.List; +import java.util.Collections; + +public final class AutoEncodingCollection extends EncodingCollection { + private final Encoding myEncoding = new Encoding(null, "auto", "auto"); + + public List encodings() { + return Collections.singletonList(myEncoding); + } + + public Encoding getEncoding(String alias) { + return myEncoding; + } + + public Encoding getEncoding(int code) { + return myEncoding; + } +} diff --git a/src/org/geometerplus/fbreader/formats/Encoding.java b/src/org/geometerplus/fbreader/formats/Encoding.java new file mode 100644 index 000000000..b4076f4e1 --- /dev/null +++ b/src/org/geometerplus/fbreader/formats/Encoding.java @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2007-2012 Geometer Plus + * + * 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; + +public final class Encoding { + public final String Family; + public final String Name; + public final String DisplayName; + + Encoding(String family, String name, String displayName) { + Family = family; + Name = name; + DisplayName = displayName; + } + + @Override + public boolean equals(Object other) { + return other instanceof Encoding && Name.equals(((Encoding)other).Name); + } + + @Override + public int hashCode() { + return Name.hashCode(); + } +} diff --git a/src/org/geometerplus/fbreader/formats/EncodingCollection.java b/src/org/geometerplus/fbreader/formats/EncodingCollection.java index 8f2be250c..b369af704 100644 --- a/src/org/geometerplus/fbreader/formats/EncodingCollection.java +++ b/src/org/geometerplus/fbreader/formats/EncodingCollection.java @@ -19,74 +19,10 @@ package org.geometerplus.fbreader.formats; -import java.util.HashMap; +import java.util.List; -import org.geometerplus.zlibrary.core.filesystem.ZLResourceFile; -import org.geometerplus.zlibrary.core.xml.ZLStringMap; -import org.geometerplus.zlibrary.core.xml.ZLXMLReaderAdapter; - -public final class EncodingCollection { - private static EncodingCollection ourInstance; - - public static EncodingCollection Instance() { - if (ourInstance == null) { - ourInstance = new EncodingCollection(); - } - return ourInstance; - } - - private final HashMap myEncodingByAlias = new HashMap(); - - private EncodingCollection() { - new EncodingCollectionReader().readQuietly( - ZLResourceFile.createResourceFile("encodings/Encodings.xml") - ); - } - - public String getEncodingName(String alias) { - final String name = myEncodingByAlias.get(alias); - return (name != null) ? name : alias; - } - - public String getEncodingName(int code) { - return myEncodingByAlias.get("" + code); - } - - private class EncodingCollectionReader extends ZLXMLReaderAdapter { - private String myCurrentEncodingName; - - public boolean dontCacheAttributeValues() { - return true; - } - - private static final String ENCODING = "encoding"; - private static final String NAME = "name"; - private static final String ALIAS = "alias"; - private static final String CODE = "code"; - private static final String NUMBER = "number"; - - public boolean startElementHandler(String tag, ZLStringMap attributes) { - if (ENCODING == tag) { - myCurrentEncodingName = attributes.getValue(NAME); - } else if (myCurrentEncodingName != null) { - String alias = null; - if (ALIAS == tag) { - alias = attributes.getValue(NAME); - } else if (CODE == tag) { - alias = attributes.getValue(NUMBER); - } - if (alias != null) { - myEncodingByAlias.put(alias, myCurrentEncodingName); - } - } - return false; - } - - public boolean endElementHandler(String tag) { - if (ENCODING == tag) { - myCurrentEncodingName = null; - } - return false; - } - } +public abstract class EncodingCollection { + public abstract List encodings(); + public abstract Encoding getEncoding(String alias); + public abstract Encoding getEncoding(int code); } diff --git a/src/org/geometerplus/fbreader/formats/FormatPlugin.java b/src/org/geometerplus/fbreader/formats/FormatPlugin.java index 606da6d7f..33b431912 100644 --- a/src/org/geometerplus/fbreader/formats/FormatPlugin.java +++ b/src/org/geometerplus/fbreader/formats/FormatPlugin.java @@ -51,4 +51,6 @@ public abstract class FormatPlugin { NONE }; public abstract Type type(); + + public abstract EncodingCollection supportedEncodings(); } diff --git a/src/org/geometerplus/fbreader/formats/JavaEncodingCollection.java b/src/org/geometerplus/fbreader/formats/JavaEncodingCollection.java new file mode 100644 index 000000000..812d5fc3f --- /dev/null +++ b/src/org/geometerplus/fbreader/formats/JavaEncodingCollection.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2007-2012 Geometer Plus + * + * 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.filesystem.ZLResourceFile; +import org.geometerplus.zlibrary.core.xml.ZLStringMap; +import org.geometerplus.zlibrary.core.xml.ZLXMLReaderAdapter; + +public final class JavaEncodingCollection extends EncodingCollection { + private final List myEncodings = new ArrayList(); + private final Map myEncodingByAlias = new HashMap(); + + public JavaEncodingCollection() { + new EncodingCollectionReader().readQuietly( + ZLResourceFile.createResourceFile("encodings/Encodings.xml") + ); + } + + @Override + public List encodings() { + return Collections.unmodifiableList(myEncodings); + } + + @Override + public Encoding getEncoding(String alias) { + final Encoding e = myEncodingByAlias.get(alias); + return e != null ? e : new Encoding(null, alias, alias); + } + + @Override + public Encoding getEncoding(int code) { + return getEncoding(String.valueOf(code)); + } + + private class EncodingCollectionReader extends ZLXMLReaderAdapter { + private String myCurrentFamilyName; + private Encoding myCurrentEncoding; + + public boolean dontCacheAttributeValues() { + return true; + } + + public boolean startElementHandler(String tag, ZLStringMap attributes) { + if ("group".equals(tag)) { + myCurrentFamilyName = attributes.getValue("name"); + } else if ("encoding".equals(tag)) { + final String name = attributes.getValue("name"); + final String region = attributes.getValue("region"); + myCurrentEncoding = new Encoding( + myCurrentFamilyName, name, name + " (" + region + ")" + ); + myEncodings.add(myCurrentEncoding); + myEncodingByAlias.put(name, myCurrentEncoding); + } else if ("code".equals(tag)) { + myEncodingByAlias.put(attributes.getValue("number"), myCurrentEncoding); + } else if ("alias".equals(tag)) { + myEncodingByAlias.put(attributes.getValue("name"), myCurrentEncoding); + } + return false; + } + } +} diff --git a/src/org/geometerplus/fbreader/formats/NativeFormatPlugin.java b/src/org/geometerplus/fbreader/formats/NativeFormatPlugin.java index 8cfbc2452..a56c82ba5 100644 --- a/src/org/geometerplus/fbreader/formats/NativeFormatPlugin.java +++ b/src/org/geometerplus/fbreader/formats/NativeFormatPlugin.java @@ -101,4 +101,10 @@ public class NativeFormatPlugin extends FormatPlugin { public Type type() { return Type.NATIVE; } + + @Override + public EncodingCollection supportedEncodings() { + // TODO: implement + return new JavaEncodingCollection(); + } } diff --git a/src/org/geometerplus/fbreader/formats/fb2/FB2Plugin.java b/src/org/geometerplus/fbreader/formats/fb2/FB2Plugin.java index e65868385..c4076a559 100644 --- a/src/org/geometerplus/fbreader/formats/fb2/FB2Plugin.java +++ b/src/org/geometerplus/fbreader/formats/fb2/FB2Plugin.java @@ -19,12 +19,13 @@ package org.geometerplus.fbreader.formats.fb2; +import org.geometerplus.zlibrary.core.filesystem.ZLFile; +import org.geometerplus.zlibrary.core.image.ZLImage; + import org.geometerplus.fbreader.bookmodel.BookModel; import org.geometerplus.fbreader.bookmodel.BookReadingException; import org.geometerplus.fbreader.library.Book; -import org.geometerplus.fbreader.formats.JavaFormatPlugin; -import org.geometerplus.zlibrary.core.filesystem.ZLFile; -import org.geometerplus.zlibrary.core.image.ZLImage; +import org.geometerplus.fbreader.formats.*; public class FB2Plugin extends JavaFormatPlugin { public FB2Plugin() { @@ -50,4 +51,9 @@ public class FB2Plugin extends JavaFormatPlugin { public String readAnnotation(ZLFile file) { return new FB2AnnotationReader().readAnnotation(file); } + + @Override + public EncodingCollection supportedEncodings() { + return new AutoEncodingCollection(); + } } diff --git a/src/org/geometerplus/fbreader/formats/oeb/OEBPlugin.java b/src/org/geometerplus/fbreader/formats/oeb/OEBPlugin.java index 548010d33..3fb62ff58 100644 --- a/src/org/geometerplus/fbreader/formats/oeb/OEBPlugin.java +++ b/src/org/geometerplus/fbreader/formats/oeb/OEBPlugin.java @@ -25,7 +25,7 @@ import org.geometerplus.zlibrary.core.image.ZLImage; import org.geometerplus.fbreader.bookmodel.BookModel; import org.geometerplus.fbreader.bookmodel.BookReadingException; import org.geometerplus.fbreader.library.Book; -import org.geometerplus.fbreader.formats.JavaFormatPlugin; +import org.geometerplus.fbreader.formats.*; public class OEBPlugin extends JavaFormatPlugin { public OEBPlugin() { @@ -83,4 +83,9 @@ public class OEBPlugin extends JavaFormatPlugin { return null; } } + + @Override + public EncodingCollection supportedEncodings() { + return new AutoEncodingCollection(); + } } diff --git a/src/org/geometerplus/fbreader/formats/pdb/MobipocketPlugin.java b/src/org/geometerplus/fbreader/formats/pdb/MobipocketPlugin.java index 456278503..be3ce945c 100644 --- a/src/org/geometerplus/fbreader/formats/pdb/MobipocketPlugin.java +++ b/src/org/geometerplus/fbreader/formats/pdb/MobipocketPlugin.java @@ -30,8 +30,7 @@ import org.geometerplus.zlibrary.core.util.MimeType; import org.geometerplus.fbreader.library.Book; import org.geometerplus.fbreader.bookmodel.BookModel; import org.geometerplus.fbreader.bookmodel.BookReadingException; -import org.geometerplus.fbreader.formats.JavaFormatPlugin; -import org.geometerplus.fbreader.formats.EncodingCollection; +import org.geometerplus.fbreader.formats.*; public class MobipocketPlugin extends JavaFormatPlugin { public MobipocketPlugin() { @@ -51,10 +50,8 @@ public class MobipocketPlugin extends JavaFormatPlugin { final int length = (int)PdbUtil.readInt(stream); PdbUtil.skip(stream, 4); final int encodingCode = (int)PdbUtil.readInt(stream); - String encodingName = EncodingCollection.Instance().getEncodingName(encodingCode); - if (encodingName == null) { - encodingName = "utf-8"; - } + final Encoding encoding = supportedEncodings().getEncoding(encodingCode); + final String encodingName = encoding != null ? encoding.Name : "utf-8"; book.setEncoding(encodingName); PdbUtil.skip(stream, 52); final int fullNameOffset = (int)PdbUtil.readInt(stream); @@ -228,4 +225,9 @@ public class MobipocketPlugin extends JavaFormatPlugin { public String readAnnotation(ZLFile file) { return null; } + + @Override + public EncodingCollection supportedEncodings() { + return new JavaEncodingCollection(); + } }