mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-05 02:39:23 +02:00
encoding stuff refactoring
This commit is contained in:
parent
2668a39629
commit
ee34841eeb
9 changed files with 198 additions and 79 deletions
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* 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.List;
|
||||
import java.util.Collections;
|
||||
|
||||
public final class AutoEncodingCollection extends EncodingCollection {
|
||||
private final Encoding myEncoding = new Encoding(null, "auto", "auto");
|
||||
|
||||
public List<Encoding> encodings() {
|
||||
return Collections.singletonList(myEncoding);
|
||||
}
|
||||
|
||||
public Encoding getEncoding(String alias) {
|
||||
return myEncoding;
|
||||
}
|
||||
|
||||
public Encoding getEncoding(int code) {
|
||||
return myEncoding;
|
||||
}
|
||||
}
|
42
src/org/geometerplus/fbreader/formats/Encoding.java
Normal file
42
src/org/geometerplus/fbreader/formats/Encoding.java
Normal file
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* 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;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
|
@ -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<String,String> myEncodingByAlias = new HashMap<String,String>();
|
||||
|
||||
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<Encoding> encodings();
|
||||
public abstract Encoding getEncoding(String alias);
|
||||
public abstract Encoding getEncoding(int code);
|
||||
}
|
||||
|
|
|
@ -51,4 +51,6 @@ public abstract class FormatPlugin {
|
|||
NONE
|
||||
};
|
||||
public abstract Type type();
|
||||
|
||||
public abstract EncodingCollection supportedEncodings();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
* 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.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<Encoding> myEncodings = new ArrayList<Encoding>();
|
||||
private final Map<String,Encoding> myEncodingByAlias = new HashMap<String,Encoding>();
|
||||
|
||||
public JavaEncodingCollection() {
|
||||
new EncodingCollectionReader().readQuietly(
|
||||
ZLResourceFile.createResourceFile("encodings/Encodings.xml")
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Encoding> 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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -101,4 +101,10 @@ public class NativeFormatPlugin extends FormatPlugin {
|
|||
public Type type() {
|
||||
return Type.NATIVE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EncodingCollection supportedEncodings() {
|
||||
// TODO: implement
|
||||
return new JavaEncodingCollection();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue