1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-05 10:49:24 +02:00

file system refactoring

git-svn-id: https://only.mawhrin.net/repos/FBReaderJ/trunk@945 6a642e6f-84f6-412e-ac94-c4a38d5a04b0
This commit is contained in:
Nikolay Pultsin 2009-04-21 14:53:28 +00:00
parent a67ea1b688
commit f6dbbef90f
53 changed files with 855 additions and 724 deletions

View file

@ -75,7 +75,7 @@ final class SQLiteBooksDatabase extends BooksDatabase {
final Cursor cursor = myDatabase.query(
BOOKS_TABLE,
BOOKS_COLUMNS,
FILE_NAME_CONDITION, new String[] { description.FileName },
FILE_NAME_CONDITION, new String[] { description.File.getPath() },
null, null, null, null
);
long id = -1;

View file

@ -20,6 +20,7 @@
package org.geometerplus.zlibrary.core.sqliteconfig;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
@ -43,6 +44,14 @@ public final class ZLSQLiteConfig extends ZLConfig {
mySetValueStatement = myDatabase.compileStatement("INSERT OR REPLACE INTO config (groupName, name, value) VALUES (?, ?, ?)");
myUnsetValueStatement = myDatabase.compileStatement("DELETE FROM config WHERE groupName = ? AND name = ?");
myDeleteGroupStatement = myDatabase.compileStatement("DELETE FROM config WHERE groupName = ?");
/*
final Cursor cursor = myDatabase.rawQuery("SELECT groupName,value FROM config WHERE name = ? AND groupName LIKE ?", new String[] { "Size", "/%" });
while (cursor.moveToNext()) {
System.err.println(cursor.getString(0) + ": " + cursor.getString(1));
}
cursor.close();
*/
}
synchronized public void executeAsATransaction(Runnable actions) {

View file

@ -27,6 +27,7 @@ import android.view.*;
import org.geometerplus.zlibrary.core.application.ZLApplication;
import org.geometerplus.zlibrary.core.config.ZLConfig;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.ui.android.R;
import org.geometerplus.zlibrary.ui.android.application.ZLAndroidApplicationWindow;
@ -50,7 +51,7 @@ public abstract class ZLAndroidActivity extends Activity {
((ZLAndroidApplication)getApplication()).myMainWindow = new ZLAndroidApplicationWindow(application);
application.initWindow();
} else if (fileToOpen != null) {
ZLApplication.Instance().openFile(fileToOpen);
ZLApplication.Instance().openFile(ZLFile.createFile(fileToOpen));
}
ZLApplication.Instance().refreshWindow();
}

View file

@ -27,6 +27,7 @@ import android.content.Intent;
import android.net.Uri;
import org.geometerplus.zlibrary.core.library.ZLibrary;
import org.geometerplus.zlibrary.core.filesystem.ZLResourceFile;
import org.geometerplus.zlibrary.core.application.ZLApplication;
import org.geometerplus.zlibrary.ui.android.R;
@ -66,27 +67,6 @@ public final class ZLAndroidLibrary extends ZLibrary {
return myWidget;
}
protected InputStream getFileInputStream(String fileName) {
try {
return new FileInputStream(fileName);
} catch (FileNotFoundException e) {
return null;
}
}
protected InputStream getResourceInputStream(String fileName) {
final String fieldName = fileName.replace("/", "__").replace(".", "_").replace("-", "_").toLowerCase();
int resourceId;
try {
resourceId = R.raw.class.getField(fieldName).getInt(null);
} catch (NoSuchFieldException e) {
return null;
} catch (IllegalAccessException e) {
return null;
}
return myApplication.getResources().openRawResource(resourceId);
}
public void openInBrowser(String reference) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(reference));
@ -96,4 +76,33 @@ public final class ZLAndroidLibrary extends ZLibrary {
//public String getVersionName() {
// return myApplication.getResources().getString(android.R.attr.versionName);
//}
public ZLResourceFile createResourceFile(String path) {
return new AndroidResourceFile(path);
}
private final class AndroidResourceFile extends ZLResourceFile {
private boolean myExists;
private int myResourceId;
AndroidResourceFile(String path) {
super(path);
final String fieldName =
path.replace("/", "__").replace(".", "_").replace("-", "_").toLowerCase();
try {
myResourceId = R.raw.class.getField(fieldName).getInt(null);
myExists = true;
} catch (NoSuchFieldException e) {
} catch (IllegalAccessException e) {
}
}
public boolean exists() {
return myExists;
}
public InputStream getInputStream() {
return myExists ? myApplication.getResources().openRawResource(myResourceId) : null;
}
}
}

View file

@ -57,8 +57,7 @@ public final class BookModel {
myBookTextModels = new ArrayList();
myBookTextModels.add(BookTextModel);
Description = description;
ZLFile file = new ZLFile(description.FileName);
FormatPlugin plugin = PluginCollection.instance().getPlugin(file);
FormatPlugin plugin = PluginCollection.instance().getPlugin(description.File);
if (plugin != null) {
plugin.readModel(description, this);
}

View file

@ -22,10 +22,10 @@ package org.geometerplus.fbreader.collection;
import java.util.*;
import java.io.File;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.filesystem.*;
import org.geometerplus.zlibrary.core.library.ZLibrary;
import org.geometerplus.fbreader.formats.PluginCollection;
import org.geometerplus.fbreader.formats.*;
public class BookCollection {
private static BookCollection ourInstance;
@ -52,23 +52,39 @@ public class BookCollection {
myDoRebuild = true;
}
public String getHelpFileName() {
final String fileName = ZLibrary.JAR_DATA_PREFIX + "data/help/MiniHelp." + Locale.getDefault().getLanguage() + ".fb2";
if (new ZLFile(fileName).exists()) {
return fileName;
public ZLResourceFile getHelpFile() {
final ZLResourceFile file = ZLResourceFile.createResourceFile(
"data/help/MiniHelp." + Locale.getDefault().getLanguage() + ".fb2"
);
if (file.exists()) {
return file;
}
return ZLibrary.JAR_DATA_PREFIX + "data/help/MiniHelp.en.fb2";
return ZLResourceFile.createResourceFile("data/help/MiniHelp.en.fb2");
}
private void addDescription(LinkedList<BookDescription> list,
ZLFile physicalFile,
String fileName,
ZLFile bookFile,
Map<String,BookDescription> saved) {
BookDescription description = BookDescription.getDescription(fileName, physicalFile, saved.get(fileName), false);
if (description != null) {
list.add(description);
BookDescription description = saved.get(bookFile.getPath());
boolean doReadMetaInfo = false;
if (description == null) {
doReadMetaInfo = true;
description = new BookDescription(bookFile, false);
}
if (doReadMetaInfo) {
final FormatPlugin plugin = PluginCollection.instance().getPlugin(bookFile);
if ((plugin == null) || !plugin.readDescription(bookFile, description)) {
return;
}
String title = description.getTitle();
if ((title == null) || (title.length() == 0)) {
description.setTitle(bookFile.getName(true));
}
}
list.add(description);
}
private List<BookDescription> collectBookDescriptions() {
@ -77,7 +93,7 @@ public class BookCollection {
final Map<String,BookDescription> savedDescriptions = BooksDatabase.Instance().listBooks();
final LinkedList<BookDescription> bookDescriptions = new LinkedList<BookDescription>();
addDescription(bookDescriptions, null, getHelpFileName(), savedDescriptions);
addDescription(bookDescriptions, getHelpFile(), savedDescriptions);
dirNameQueue.offer(BookDirectory);
while (!dirNameQueue.isEmpty()) {
@ -98,16 +114,17 @@ public class BookCollection {
if (i.isDirectory()) {
dirNameQueue.add(fileName);
} else {
final ZLFile file = new ZLFile(i);
final ZLPhysicalFile file = new ZLPhysicalFile(i);
if (PluginCollection.instance().getPlugin(file) != null) {
addDescription(bookDescriptions, null, fileName, savedDescriptions);
addDescription(bookDescriptions, file, savedDescriptions);
} else if (file.isArchive()) {
if (!BookDescriptionUtil.checkInfo(file)) {
BookDescriptionUtil.resetZipInfo(file);
BookDescriptionUtil.saveInfo(file);
// TODO: reset book information for all entries
}
for (String entryName : BookDescriptionUtil.listZipEntries(file)) {
addDescription(bookDescriptions, file, entryName, savedDescriptions);
addDescription(bookDescriptions, ZLFile.createFile(entryName), savedDescriptions);
}
}
}

View file

@ -22,47 +22,42 @@ package org.geometerplus.fbreader.collection;
import java.util.*;
import org.geometerplus.zlibrary.core.util.ZLMiscUtil;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.filesystem.*;
import org.geometerplus.fbreader.formats.*;
public class BookDescription {
public static BookDescription getDescription(String fileName) {
return getDescription(fileName, null, null, true);
}
static BookDescription getDescription(String fileName, ZLFile file, BookDescription description, boolean readFromDB) {
if (fileName == null) {
return null;
}
final ZLFile bookFile = new ZLFile(fileName);
String physicalFileName;
if (file == null) {
physicalFileName = bookFile.getPhysicalFilePath();
file = new ZLFile(physicalFileName);
if (!file.exists()) {
return null;
}
} else {
physicalFileName = file.getPath();
return getDescription(ZLFile.createFile(fileName));
}
public static BookDescription getDescription(ZLFile bookFile) {
if (bookFile == null) {
return null;
}
if (description == null) {
description = new BookDescription(fileName, readFromDB);
final ZLPhysicalFile physicalFile = bookFile.getPhysicalFile();
if ((physicalFile != null) && !physicalFile.exists()) {
return null;
}
if (BookDescriptionUtil.checkInfo(file) && description.myIsSaved) {
final BookDescription description = new BookDescription(bookFile, true);
if (BookDescriptionUtil.checkInfo(physicalFile) && description.myIsSaved) {
return description;
}
if (physicalFileName != fileName) {
BookDescriptionUtil.resetZipInfo(file);
if ((physicalFile != null) && (physicalFile != bookFile)) {
BookDescriptionUtil.resetZipInfo(physicalFile);
BookDescriptionUtil.saveInfo(physicalFile);
}
BookDescriptionUtil.saveInfo(file);
final FormatPlugin plugin = PluginCollection.instance().getPlugin(bookFile);
if ((plugin == null) || !plugin.readDescription(fileName, description)) {
if ((plugin == null) || !plugin.readDescription(bookFile, description)) {
return null;
}
@ -73,7 +68,7 @@ public class BookDescription {
return description;
}
public final String FileName;
public final ZLFile File;
private long myBookId;
@ -86,17 +81,17 @@ public class BookDescription {
private boolean myIsSaved;
BookDescription(long bookId, String fileName, String title, String encoding, String language) {
BookDescription(long bookId, ZLFile file, String title, String encoding, String language) {
myBookId = bookId;
FileName = fileName;
File = file;
myTitle = title;
myEncoding = encoding;
myLanguage = language;
myIsSaved = true;
}
private BookDescription(String fileName, boolean createFromDatabase) {
FileName = fileName;
BookDescription(ZLFile file, boolean createFromDatabase) {
File = file;
if (createFromDatabase) {
final BooksDatabase database = BooksDatabase.Instance();
myBookId = database.loadBook(this);
@ -262,7 +257,7 @@ public class BookDescription {
if (myBookId >= 0) {
database.updateBookInfo(myBookId, myEncoding, myLanguage, myTitle);
} else {
myBookId = database.insertBookInfo(FileName, myEncoding, myLanguage, myTitle);
myBookId = database.insertBookInfo(File.getPath(), myEncoding, myLanguage, myTitle);
}
long index = 0;

View file

@ -22,8 +22,7 @@ package org.geometerplus.fbreader.collection;
import java.util.*;
import org.geometerplus.zlibrary.core.util.*;
import org.geometerplus.zlibrary.core.filesystem.ZLDir;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.filesystem.*;
import org.geometerplus.zlibrary.core.options.ZLIntegerOption;
import org.geometerplus.zlibrary.core.options.ZLStringOption;
@ -34,13 +33,16 @@ class BookDescriptionUtil {
private static final String ENTRY = "Entry";
private static final String ENTRIES_NUMBER = "EntriesNumber";
public static boolean checkInfo(ZLFile file) {
ZLIntegerOption op = new ZLIntegerOption(file.getPath(), SIZE, -1);
return op.getValue() == (int)file.size();
public static boolean checkInfo(ZLPhysicalFile file) {
return
(file == null) ||
(new ZLIntegerOption(file.getPath(), SIZE, -1).getValue() == (int)file.size());
}
public static void saveInfo(ZLFile file) {
new ZLIntegerOption(file.getPath(), SIZE, -1).setValue((int)file.size());
public static void saveInfo(ZLPhysicalFile file) {
if (file != null) {
new ZLIntegerOption(file.getPath(), SIZE, -1).setValue((int)file.size());
}
}
private static final ArrayList<String> EMPTY = new ArrayList<String>();
@ -64,7 +66,7 @@ class BookDescriptionUtil {
public static void resetZipInfo(ZLFile zipFile) {
//ZLOption.clearGroup(zipFile.path());
ZLDir zipDir = zipFile.getDirectory();
ZLDir zipDir = zipFile.getDirectory(false);
if (zipDir != null) {
final String zipPrefix = zipFile.getPath() + ':';
int counter = 0;
@ -74,7 +76,7 @@ class BookDescriptionUtil {
String entry = (String)entries.get(i);
final ZLStringOption entryOption =
new ZLStringOption(zipFile.getPath(), "", "");
if (PluginCollection.instance().getPlugin(new ZLFile(entry)) != null) {
if (PluginCollection.instance().getPlugin(ZLFile.createFile(entry)) != null) {
final String fullName = zipPrefix + entry;
entryOption.changeName(ENTRY + counter);
entryOption.setValue(fullName);

View file

@ -22,6 +22,8 @@ package org.geometerplus.fbreader.collection;
import java.util.Map;
import java.util.ArrayList;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
public abstract class BooksDatabase {
private static BooksDatabase ourInstance;
@ -33,8 +35,8 @@ public abstract class BooksDatabase {
ourInstance = this;
}
protected BookDescription createDescription(long bookId, String fileName, String title, String encoding, String language) {
return new BookDescription(bookId, fileName, title, encoding, language);
protected BookDescription createDescription(long bookId, String filePath, String title, String encoding, String language) {
return new BookDescription(bookId, ZLFile.createFile(filePath), title, encoding, language);
}
protected void addAuthor(BookDescription description, Author author) {

View file

@ -22,9 +22,9 @@ package org.geometerplus.fbreader.encoding;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import org.geometerplus.zlibrary.core.util.*;
import org.geometerplus.zlibrary.core.config.ZLConfig;
import org.geometerplus.zlibrary.core.filesystem.ZLResourceFile;
import org.geometerplus.zlibrary.core.library.ZLibrary;
import org.geometerplus.zlibrary.core.options.ZLBooleanOption;
import org.geometerplus.zlibrary.core.xml.ZLStringMap;
@ -48,8 +48,9 @@ public class ZLEncodingCollection {
}
return ourInstance;
}
public static String encodingDescriptionPath() {
return ZLibrary.JAR_DATA_PREFIX + "data/encodings/Encodings.xml";
public static ZLResourceFile encodingDescriptionFile() {
return ZLResourceFile.createResourceFile("data/encodings/Encodings.xml");
}
public ArrayList<ZLEncodingSet> sets() {
@ -87,7 +88,7 @@ public class ZLEncodingCollection {
if (mySets.isEmpty()) {
// String prefix = encodingDescriptionPath() + File.separator;
// System.out.println("trying to read " + prefix + "Encodings.xml");
new ZLEncodingCollectionReader(this).read(encodingDescriptionPath());
new ZLEncodingCollectionReader(this).read(encodingDescriptionFile());
}
}

View file

@ -123,8 +123,9 @@ public final class FBReader extends ZLApplication {
} catch (IOException e) {
}
}
if (!openFile(fileName) && !openFile(myBookNameOption.getValue())) {
openFile(BookCollection.Instance().getHelpFileName());
if (!openFile(fileName) &&
!openFile(myBookNameOption.getValue())) {
openFile(BookCollection.Instance().getHelpFile());
}
}
@ -217,7 +218,7 @@ public final class FBReader extends ZLApplication {
Model = null;
Model = new BookModel(description);
final String fileName = description.FileName;
final String fileName = description.File.getPath();
myBookNameOption.setValue(fileName);
ZLTextHyphenator.Instance().load(description.getLanguage());
BookTextView.setModels(Model.getBookTextModels(), fileName);
@ -246,16 +247,19 @@ public final class FBReader extends ZLApplication {
}
}
private boolean openFile(String path) {
return openFile(ZLFile.createFile(path));
}
@Override
public boolean openFile(String fileName) {
if (fileName == null) {
public boolean openFile(ZLFile file) {
if (file == null) {
return false;
}
BookDescription description = BookDescription.getDescription(fileName);
BookDescription description = BookDescription.getDescription(file);
if (description == null) {
final ZLFile file = new ZLFile(fileName);
if (file.isArchive()) {
final ZLDir directory = file.getDirectory();
final ZLDir directory = file.getDirectory(false);
if (directory != null) {
final ArrayList items = directory.collectFiles();
final int size = items.size();

View file

@ -30,14 +30,10 @@ import org.geometerplus.zlibrary.core.language.ZLLanguageDetector;
public abstract class FormatPlugin {
public abstract boolean acceptsFile(ZLFile file);
public abstract boolean readDescription(String path, BookDescription description);
public abstract boolean readDescription(ZLFile file, BookDescription description);
public abstract boolean readModel(BookDescription description, BookModel model);
public FormatInfoPage createInfoPage(ZLOptionsDialog d, String str) {
return null;
}
/*
public static void detectEncodingAndLanguage(BookDescription description, InputStream stream) throws IOException {
String language = description.getLanguage();
String encoding = description.getEncoding();
@ -70,7 +66,7 @@ public abstract class FormatPlugin {
}
}
//Last working version
/*public static void detectEncodingAndLanguage(BookDescription description, InputStream stream) {
public static void detectEncodingAndLanguage(BookDescription description, InputStream stream) {
String encoding = description.getEncoding();
if (encoding.length() == 0) {
encoding = EncodingDetector.detect(stream, PluginCollection.instance().DefaultLanguageOption.getValue());
@ -99,8 +95,4 @@ public abstract class FormatPlugin {
/*}
}*/
static class FormatInfoPage {
protected FormatInfoPage() {}
}
}

View file

@ -21,6 +21,7 @@ package org.geometerplus.fbreader.formats.fb2;
import java.util.*;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.xml.*;
import org.geometerplus.fbreader.collection.BookDescription;
@ -53,13 +54,13 @@ public class FB2DescriptionReader extends ZLXMLReaderAdapter {
return true;
}
public boolean readDescription(String fileName) {
public boolean readDescription(ZLFile file) {
myReadState = READ_NOTHING;
myAuthorNames[0] = "";
myAuthorNames[1] = "";
myAuthorNames[2] = "";
myBuffer.delete(0, myBuffer.length());
return readDocument(fileName);
return readDocument(file);
}
public boolean startElementHandler(String tagName, ZLStringMap attributes) {
@ -215,9 +216,9 @@ public class FB2DescriptionReader extends ZLXMLReaderAdapter {
}
}
public boolean readDocument(String fileName) {
public boolean readDocument(ZLFile file) {
final ZLXMLProcessor processor = ZLXMLProcessorFactory.getInstance().createXMLProcessor();
processor.setBufferSize(512);
return processor.read(this, fileName);
return processor.read(this, file);
}
}

View file

@ -29,11 +29,11 @@ public class FB2Plugin extends FormatPlugin {
return "fb2".equals(file.getExtension());
}
public boolean readDescription(String path, BookDescription description) {
return new FB2DescriptionReader(description).readDescription(path);
public boolean readDescription(ZLFile file, BookDescription description) {
return new FB2DescriptionReader(description).readDescription(file);
}
public boolean readModel(BookDescription description, BookModel model) {
return new FB2Reader(model).readBook(description.FileName);
return new FB2Reader(model).readBook(description.File);
}
}

View file

@ -20,10 +20,12 @@
package org.geometerplus.fbreader.formats.fb2;
import java.util.*;
import org.geometerplus.zlibrary.core.library.ZLibrary;
import org.geometerplus.zlibrary.core.util.*;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.fbreader.bookmodel.*;
import org.geometerplus.zlibrary.core.xml.*;
import org.geometerplus.zlibrary.core.util.*;
import org.geometerplus.zlibrary.text.model.ZLTextParagraph;
public final class FB2Reader extends BookReader implements ZLXMLReader {
@ -51,10 +53,10 @@ public final class FB2Reader extends BookReader implements ZLXMLReader {
super(model);
}
boolean readBook(String fileName) {
boolean readBook(ZLFile file) {
Base64EncodedImage.resetCounter();
final ZLXMLProcessor processor = ZLXMLProcessorFactory.getInstance().createXMLProcessor();
return processor.read(this, fileName);
return processor.read(this, file);
}
public void startDocumentHandler() {
@ -397,7 +399,7 @@ public final class FB2Reader extends BookReader implements ZLXMLReader {
public ArrayList externalDTDs() {
if (ourExternalDTDs.isEmpty()) {
ourExternalDTDs.add(ZLibrary.JAR_DATA_PREFIX + "data/formats/fb2/FBReaderVersion.ent");
ourExternalDTDs.add("data/formats/fb2/FBReaderVersion.ent");
}
return ourExternalDTDs;
}

View file

@ -23,6 +23,7 @@ import java.util.*;
import org.geometerplus.zlibrary.core.util.*;
import org.geometerplus.zlibrary.core.library.ZLibrary;
import org.geometerplus.zlibrary.core.filesystem.ZLResourceFile;
import org.geometerplus.zlibrary.core.xml.*;
import org.geometerplus.fbreader.collection.Tag;
@ -33,7 +34,7 @@ abstract class FB2TagManager {
static ArrayList<Tag> humanReadableTags(String id) {
if (ourMap.isEmpty()) {
new FB2TagInfoReader().read(
ZLibrary.JAR_DATA_PREFIX + "data/formats/fb2/fb2genres.xml"
ZLResourceFile.createResourceFile("data/formats/fb2/fb2genres.xml")
);
}
return ourMap.get(id);

View file

@ -19,11 +19,10 @@
package org.geometerplus.fbreader.formats.html;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.xml.*;
import org.geometerplus.fbreader.collection.BookDescription;
import org.geometerplus.zlibrary.core.xml.ZLStringMap;
import org.geometerplus.zlibrary.core.xml.ZLXMLProcessor;
import org.geometerplus.zlibrary.core.xml.ZLXMLProcessorFactory;
import org.geometerplus.zlibrary.core.xml.ZLXMLReaderAdapter;
public class HtmlDescriptionReader extends ZLXMLReaderAdapter {
private final BookDescription myDescription;
@ -39,9 +38,9 @@ public class HtmlDescriptionReader extends ZLXMLReaderAdapter {
return true;
}
public boolean readDescription(String fileName) {
public boolean readDescription(ZLFile file) {
myReadTitle = false;
return readDocument(fileName);
return readDocument(file);
}
public boolean startElementHandler(String tagName, ZLStringMap attributes) {
@ -74,10 +73,10 @@ public class HtmlDescriptionReader extends ZLXMLReaderAdapter {
}
}
public boolean readDocument(String fileName) {
public boolean readDocument(ZLFile file) {
final ZLXMLProcessor processor = ZLXMLProcessorFactory.getInstance()
.createXMLProcessor();
return processor.read(this, fileName);
return processor.read(this, file);
}
}

View file

@ -37,10 +37,8 @@ public class HtmlPlugin extends FormatPlugin {
}
@Override
public boolean readDescription(String path, BookDescription description) {
return new HtmlDescriptionReader(description).readDescription(path);
// always true =)
//return true;
public boolean readDescription(ZLFile file, BookDescription description) {
return new HtmlDescriptionReader(description).readDescription(file);
}
@Override
@ -49,6 +47,6 @@ public class HtmlPlugin extends FormatPlugin {
if (!description.getEncoding().equals(AUTO)) {
//new BookDescription.BookInfo(description.FileName).EncodingOption.setValue(AUTO);
}
return new HtmlReader(model).readBook(description.FileName);
return new HtmlReader(model).readBook(description.File);
}
}

View file

@ -23,6 +23,7 @@ import org.geometerplus.fbreader.bookmodel.BookModel;
import org.geometerplus.fbreader.bookmodel.BookReader;
import org.geometerplus.fbreader.bookmodel.FBTextKind;
import org.geometerplus.fbreader.formats.html.HtmlTag;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.xml.ZLStringMap;
import org.geometerplus.zlibrary.core.xml.ZLXMLProcessor;
import org.geometerplus.zlibrary.core.xml.ZLXMLProcessorFactory;
@ -58,13 +59,13 @@ public class HtmlReader extends BookReader implements ZLHtmlReader {
public boolean read() {
final ZLHtmlProcessor processor = ZLHtmlProcessorFactory.getInstance()
.createHtmlProcessor();
return processor.read(this, Model.Description.FileName);
return processor.read(this, Model.Description.File);
}
boolean readBook(String fileName) {
boolean readBook(ZLFile file) {
final ZLHtmlProcessor processor = ZLHtmlProcessorFactory.getInstance()
.createHtmlProcessor();
return processor.read(this, fileName);
return processor.read(this, file);
// return readDocument(fileName);
}
@ -328,7 +329,7 @@ public class HtmlReader extends BookReader implements ZLHtmlReader {
if (":\\".equals(ref.substring(1, 3))) {
addImage(ref, new ZLFileImage("image/auto", ref));
} else {
String fileName = Model.Description.FileName;
String fileName = Model.Description.File.getPath();
addImage(ref, new ZLFileImage("image/auto",
fileName.substring(0, fileName.lastIndexOf('\\') + 1) + ref));
}

View file

@ -67,7 +67,7 @@ class OEBBookReader extends ZLXMLReaderAdapter implements XMLNamespace {
myGuideTOC.clear();
myState = READ_NONE;
if (!read(fileName)) {
if (!read(ZLFile.createFile(fileName))) {
return false;
}
@ -75,7 +75,7 @@ class OEBBookReader extends ZLXMLReaderAdapter implements XMLNamespace {
myModelReader.pushKind(FBTextKind.REGULAR);
for (String name : myHtmlFileNames) {
new XHTMLReader(myModelReader).readFile(myFilePrefix + name, name);
new XHTMLReader(myModelReader).readFile(ZLFile.createFile(myFilePrefix + name), name);
}
generateTOC();
@ -86,7 +86,7 @@ class OEBBookReader extends ZLXMLReaderAdapter implements XMLNamespace {
private void generateTOC() {
if (myNCXTOCFileName != null) {
NCXReader ncxReader = new NCXReader(myModelReader);
if (ncxReader.read(myFilePrefix + myNCXTOCFileName)) {
if (ncxReader.read(ZLFile.createFile(myFilePrefix + myNCXTOCFileName))) {
final Map<Integer,NCXReader.NavPoint> navigationMap = ncxReader.navigationMap();
if (!navigationMap.isEmpty()) {
int level = 0;

View file

@ -21,7 +21,9 @@ package org.geometerplus.fbreader.formats.oeb;
import java.util.*;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.xml.*;
import org.geometerplus.fbreader.collection.BookDescription;
import org.geometerplus.fbreader.constants.XMLNamespace;
@ -45,13 +47,13 @@ class OEBDescriptionReader extends ZLXMLReaderAdapter implements XMLNamespace {
myDescription.setLanguage(null);
}
boolean readDescription(String fileName) {
boolean readDescription(ZLFile file) {
myReadMetaData = false;
myReadState = READ_NONE;
final ZLXMLProcessor processor = ZLXMLProcessorFactory.getInstance().createXMLProcessor();
processor.setBufferSize(512);
if (!processor.read(this, fileName)) {
if (!processor.read(this, file)) {
return false;
}

View file

@ -32,13 +32,11 @@ public class OEBPlugin extends FormatPlugin {
return (extension == "opf") || (extension == "oebzip") || (extension == "epub");
}
private String getOpfFileName(String oebFileName) {
final ZLFile oebFile = new ZLFile(oebFileName);
private String getOpfFileName(ZLFile oebFile) {
if (oebFile.getExtension().equals("opf")) {
return oebFileName;
return oebFile.getPath();
}
oebFile.forceArchiveType(ZLFile.ArchiveType.ZIP);
final ZLDir zipDir = oebFile.getDirectory(false);
if (zipDir == null) {
return null;
@ -55,16 +53,16 @@ public class OEBPlugin extends FormatPlugin {
return null;
}
public boolean readDescription(String path, BookDescription description) {
path = getOpfFileName(path);
public boolean readDescription(ZLFile file, BookDescription description) {
final String path = getOpfFileName(file);
if (path == null) {
return false;
}
return new OEBDescriptionReader(description).readDescription(path);
return new OEBDescriptionReader(description).readDescription(ZLFile.createFile(path));
}
public boolean readModel(BookDescription description, BookModel model) {
final String path = getOpfFileName(description.FileName);
final String path = getOpfFileName(description.File);
if (path == null) {
return false;
}

View file

@ -16,67 +16,67 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
package org.geometerplus.fbreader.formats.pdb;
import java.io.IOException;
import java.io.InputStream;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
public class PdbInputStream extends InputStream {
private final InputStream myBase;
private int myOffset = 0;
private final int mySize;
public PdbInputStream(ZLFile file) throws IOException {
mySize = (int)file.size();
myBase = file.getInputStream();
}
public int read() throws IOException {
int result = myBase.read();
if (result != -1) {
myOffset ++;
}
return result;
}
public int available() throws IOException {
return super.available();
}
public void close() throws IOException {
myOffset = 0;
super.close();
}
public synchronized void mark(int readlimit) {
super.mark(readlimit);
}
public boolean markSupported() {
return super.markSupported();
}
public int read(byte[] b, int off, int len) throws IOException {
return super.read(b, off, len);
}
public int read(byte[] b) throws IOException {
return super.read(b);
}
public synchronized void reset() throws IOException {
// myOffset = 0;
super.reset();
}
public int offset() {
return myOffset;
}
public int sizeOfOpened() {
return mySize - myOffset;
}
}
package org.geometerplus.fbreader.formats.pdb;
import java.io.IOException;
import java.io.InputStream;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
public class PdbInputStream extends InputStream {
private final InputStream myBase;
private int myOffset = 0;
private final int mySize;
public PdbInputStream(ZLFile file) throws IOException {
mySize = 0;//(int)file.size();
myBase = file.getInputStream();
}
public int read() throws IOException {
int result = myBase.read();
if (result != -1) {
myOffset ++;
}
return result;
}
public int available() throws IOException {
return super.available();
}
public void close() throws IOException {
myOffset = 0;
super.close();
}
public synchronized void mark(int readlimit) {
super.mark(readlimit);
}
public boolean markSupported() {
return super.markSupported();
}
public int read(byte[] b, int off, int len) throws IOException {
return super.read(b, off, len);
}
public int read(byte[] b) throws IOException {
return super.read(b);
}
public synchronized void reset() throws IOException {
// myOffset = 0;
super.reset();
}
public int offset() {
return myOffset;
}
public int sizeOfOpened() {
return mySize - myOffset;
}
}

View file

@ -37,7 +37,7 @@ public abstract class PdbPlugin extends FormatPlugin {
String fileName = file.getPath();
int index = fileName.indexOf(':');
ZLFile baseFile = (index == -1) ? file : new ZLFile(fileName.substring(0, index));
ZLFile baseFile = (index == -1) ? file : ZLFile.createFile(fileName.substring(0, index));
boolean upToDate = true;//BookDescriptionUtil.checkInfo(baseFile);
ZLStringOption palmTypeOption = new ZLStringOption(file.getPath(), "PalmType", "");

View file

@ -16,47 +16,47 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
package org.geometerplus.fbreader.formats.plucker;
import java.io.IOException;
import java.io.InputStream;
import org.geometerplus.fbreader.formats.pdb.DocDecompressor;
import org.geometerplus.zlibrary.core.image.ZLSingleImage;
import org.geometerplus.zlibrary.core.library.ZLibrary;
public class DocCompressedFileImage extends ZLSingleImage {
private final String myPath;
private final int myOffset;
private final int myCompressedSize;
public DocCompressedFileImage(String mimeType, final String path, final int offset, final int compressedSize) {
super(mimeType);
myPath = path;
myOffset = offset;
myCompressedSize = compressedSize;
}
public byte[] byteData() {
final InputStream stream = ZLibrary.Instance().getInputStream(myPath);
if (stream == null) {
return new byte[0];
}
try {
stream.skip(myOffset);
byte [] targetBuffer = new byte[65535];
final int size = DocDecompressor.decompress(stream, targetBuffer, myCompressedSize);
if (size > 0 && size != 65535) {
byte [] buffer = new byte[size];
System.arraycopy(targetBuffer, 0, buffer, 0, size);
return buffer;
}
return targetBuffer;
} catch (IOException e) {}
return new byte[0];
}
}
package org.geometerplus.fbreader.formats.plucker;
import java.io.IOException;
import java.io.InputStream;
import org.geometerplus.fbreader.formats.pdb.DocDecompressor;
import org.geometerplus.zlibrary.core.image.ZLSingleImage;
import org.geometerplus.zlibrary.core.library.ZLibrary;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
public class DocCompressedFileImage extends ZLSingleImage {
private final ZLFile myFile;
private final int myOffset;
private final int myCompressedSize;
public DocCompressedFileImage(String mimeType, final ZLFile file, final int offset, final int compressedSize) {
super(mimeType);
myFile = file;
myOffset = offset;
myCompressedSize = compressedSize;
}
public byte[] byteData() {
try {
final InputStream stream = myFile.getInputStream();
if (stream == null) {
return new byte[0];
}
stream.skip(myOffset);
byte [] targetBuffer = new byte[65535];
final int size = DocDecompressor.decompress(stream, targetBuffer, myCompressedSize);
if (size > 0 && size != 65535) {
byte [] buffer = new byte[size];
System.arraycopy(targetBuffer, 0, buffer, 0, size);
return buffer;
}
return targetBuffer;
} catch (IOException e) {}
return new byte[0];
}
}

View file

@ -33,7 +33,7 @@ import org.geometerplus.fbreader.formats.EncodedTextReader;
import org.geometerplus.fbreader.formats.pdb.*;
public class PluckerBookReader extends BookReader {
private final String myFilePath;
private final ZLFile myFile;
private PdbInputStream myStream;
private int myFont;
private char[] myCharBuffer;
@ -53,18 +53,18 @@ public class PluckerBookReader extends BookReader {
private final ZLEncodingConverter myConverter;
public PluckerBookReader(String filePath, BookModel model, String encoding){
public PluckerBookReader(ZLFile file, BookModel model, String encoding){
super(model);
myConverter = new EncodedTextReader(encoding).getConverter();
myFilePath = filePath;
System.out.println(filePath + " " + encoding);
myFile = file;
//System.out.println(filePath + " " + encoding);
myFont = FontType.FT_REGULAR;
myCharBuffer = new char[65535];
myForcedEntry = null;
}
public boolean readDocument() throws IOException {
myStream = new PdbInputStream(new ZLFile(myFilePath));
myStream = new PdbInputStream(myFile);
PdbHeader header = new PdbHeader();
if (!header.read(myStream)) {
@ -206,13 +206,13 @@ public class PluckerBookReader extends BookReader {
ZLImage image = null;
if (type == 2) {
System.out.println("non-compressed image");
image = new PluckerFileImage(mime, myFilePath, myStream.offset(), recordSize - 8);
image = new PluckerFileImage(mime, myFile, myStream.offset(), recordSize - 8);
} else if (myCompressionVersion == 1) {
System.out.println("DocCompressedImage");
image = new DocCompressedFileImage(mime, myFilePath, myStream.offset(), recordSize - 8);
image = new DocCompressedFileImage(mime, myFile, myStream.offset(), recordSize - 8);
} else if (myCompressionVersion == 2) {
System.out.println("ZCompressedImage");
image = new ZCompressedFileImage(mime, myFilePath, myStream.offset() + 2, recordSize - 10);
image = new ZCompressedFileImage(mime, myFile, myStream.offset() + 2, recordSize - 10);
}
if (image != null) {
addImage(fromNumber(uid), image);

View file

@ -16,41 +16,40 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
package org.geometerplus.fbreader.formats.plucker;
import java.io.IOException;
import java.io.InputStream;
import org.geometerplus.zlibrary.core.image.ZLSingleImage;
import org.geometerplus.zlibrary.core.library.ZLibrary;
public class PluckerFileImage extends ZLSingleImage {
private final String myPath;
private final int myOffset;
private final int mySize;
public PluckerFileImage(String mimeType, final String path, final int offset, final int size) {
super(mimeType);
myPath = path;
myOffset = offset;
mySize = size;
}
public byte[] byteData() {
final InputStream stream = ZLibrary.Instance().getInputStream(myPath);
if (stream == null) {
return new byte[0];
}
try {
stream.skip(myOffset);
byte [] buffer = new byte[mySize];
stream.read(buffer, 0, mySize);
return buffer;
} catch (IOException e) {}
return new byte[0];
}
}
package org.geometerplus.fbreader.formats.plucker;
import java.io.IOException;
import java.io.InputStream;
import org.geometerplus.zlibrary.core.image.ZLSingleImage;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
public class PluckerFileImage extends ZLSingleImage {
private final ZLFile myFile;
private final int myOffset;
private final int mySize;
public PluckerFileImage(String mimeType, final ZLFile file, final int offset, final int size) {
super(mimeType);
myFile = file;
myOffset = offset;
mySize = size;
}
public byte[] byteData() {
try {
final InputStream stream = myFile.getInputStream();
if (stream == null) {
return new byte[0];
}
stream.skip(myOffset);
byte [] buffer = new byte[mySize];
stream.read(buffer, 0, mySize);
return buffer;
} catch (IOException e) {}
return new byte[0];
}
}

View file

@ -32,13 +32,11 @@ public class PluckerPlugin extends PdbPlugin {
return "DataPlkr".equals(fileType(file));
}
public boolean readDescription(String path, BookDescription description) {
ZLFile file = new ZLFile(path);
public boolean readDescription(ZLFile file, BookDescription description) {
try {
PdbStream stream = new PluckerTextStream(file);
if (stream.open()) {
detectEncodingAndLanguage(description, stream);
//detectEncodingAndLanguage(description, stream);
stream.close();
}
} catch (IOException e) {
@ -53,7 +51,7 @@ public class PluckerPlugin extends PdbPlugin {
public boolean readModel(BookDescription description, BookModel model) {
try {
return new PluckerBookReader(description.FileName, model, description.getEncoding()).readDocument();
return new PluckerBookReader(description.File, model, description.getEncoding()).readDocument();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

View file

@ -16,64 +16,63 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
package org.geometerplus.fbreader.formats.plucker;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.zip.DataFormatException;
import java.util.zip.Inflater;
import org.geometerplus.zlibrary.core.image.ZLSingleImage;
import org.geometerplus.zlibrary.core.library.ZLibrary;
public class ZCompressedFileImage extends ZLSingleImage {
private final String myPath;
private final int myOffset;
private final int myCompressedSize;
public ZCompressedFileImage(String mimeType, final String path, final int offset, final int compressedSize) {
super(mimeType);
myPath = path;
myOffset = offset;
myCompressedSize = compressedSize;
}
public byte[] byteData() {
final InputStream stream = ZLibrary.Instance().getInputStream(myPath);
if (stream == null) {
return new byte[0];
}
final ArrayList data = new ArrayList();
byte[] buffer;
int sizeOfBufferData;
try {
stream.skip(myOffset);
byte [] targetBuffer = new byte[myCompressedSize];
stream.read(targetBuffer, 0, myCompressedSize);
Inflater decompressor = new Inflater();
decompressor.setInput(targetBuffer, 0, myCompressedSize);
do {
buffer = new byte[4096];
sizeOfBufferData = decompressor.inflate(buffer);
data.add(buffer);
} while (sizeOfBufferData == 4096);
decompressor.end();
final int dataSizeMinusOne = data.size() - 1;
buffer = new byte[dataSizeMinusOne * 4096 + sizeOfBufferData];
for (int i = 0; i < dataSizeMinusOne; ++i) {
System.arraycopy(data.get(i), 0, buffer, i * 4096, 4096);
}
System.arraycopy(data.get(dataSizeMinusOne), 0, buffer, dataSizeMinusOne * 4096, sizeOfBufferData);
return buffer;
} catch (IOException e) {
} catch (DataFormatException e) {
}
return new byte[0];
}
}
package org.geometerplus.fbreader.formats.plucker;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.zip.DataFormatException;
import java.util.zip.Inflater;
import org.geometerplus.zlibrary.core.image.ZLSingleImage;
import org.geometerplus.zlibrary.core.library.ZLibrary;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
public class ZCompressedFileImage extends ZLSingleImage {
private final ZLFile myFile;
private final int myOffset;
private final int myCompressedSize;
public ZCompressedFileImage(String mimeType, final ZLFile file, final int offset, final int compressedSize) {
super(mimeType);
myFile = file;
myOffset = offset;
myCompressedSize = compressedSize;
}
public byte[] byteData() {
try {
final InputStream stream = myFile.getInputStream();
if (stream == null) {
return new byte[0];
}
final ArrayList data = new ArrayList();
byte[] buffer;
int sizeOfBufferData;
stream.skip(myOffset);
byte [] targetBuffer = new byte[myCompressedSize];
stream.read(targetBuffer, 0, myCompressedSize);
Inflater decompressor = new Inflater();
decompressor.setInput(targetBuffer, 0, myCompressedSize);
do {
buffer = new byte[4096];
sizeOfBufferData = decompressor.inflate(buffer);
data.add(buffer);
} while (sizeOfBufferData == 4096);
decompressor.end();
final int dataSizeMinus1 = data.size() - 1;
buffer = new byte[dataSizeMinus1 * 4096 + sizeOfBufferData];
for (int i = 0; i < dataSizeMinus1; ++i) {
System.arraycopy(data.get(i), 0, buffer, i * 4096, 4096);
}
System.arraycopy(data.get(dataSizeMinus1), 0, buffer, dataSizeMinus1 * 4096, sizeOfBufferData);
return buffer;
} catch (IOException e) {
} catch (DataFormatException e) {
}
return new byte[0];
}
}

View file

@ -23,7 +23,7 @@ import org.geometerplus.zlibrary.core.filesystem.ZLFile;
public class MiscUtil {
public static String htmlDirectoryPrefix(String fileName) {
ZLFile file = new ZLFile(fileName);
ZLFile file = ZLFile.createFile(fileName);
String shortName = file.getName(false);
String path = file.getPath();
int index = -1;

View file

@ -24,6 +24,7 @@ import java.io.InputStream;
import org.geometerplus.zlibrary.core.library.ZLibrary;
import org.geometerplus.zlibrary.core.xml.*;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.fbreader.bookmodel.*;
import org.geometerplus.fbreader.formats.util.MiscUtil;
@ -133,18 +134,18 @@ public class XHTMLReader extends ZLXMLReaderAdapter {
return myPathPrefix;
}
public boolean readFile(String filePath, String referenceName) {
public boolean readFile(ZLFile file, String referenceName) {
myModelReader.addHyperlinkLabel(referenceName);
fillTagTable();
myPathPrefix = MiscUtil.htmlDirectoryPrefix(filePath);
myPathPrefix = MiscUtil.htmlDirectoryPrefix(file.getPath());
myReferenceName = referenceName;
myPreformatted = false;
myInsideBody = false;
return read(filePath);
return read(file);
}
/*
@ -225,9 +226,9 @@ cycle:
public ArrayList externalDTDs() {
if (ourExternalDTDs.isEmpty()) {
ourExternalDTDs.add(ZLibrary.JAR_DATA_PREFIX + "data/formats/xhtml/xhtml-lat1.ent");
ourExternalDTDs.add(ZLibrary.JAR_DATA_PREFIX + "data/formats/xhtml/xhtml-special.ent");
ourExternalDTDs.add(ZLibrary.JAR_DATA_PREFIX + "data/formats/xhtml/xhtml-symbol.ent");
ourExternalDTDs.add("data/formats/xhtml/xhtml-lat1.ent");
ourExternalDTDs.add("data/formats/xhtml/xhtml-special.ent");
ourExternalDTDs.add("data/formats/xhtml/xhtml-symbol.ent");
}
return ourExternalDTDs;
}

View file

@ -21,6 +21,7 @@ package org.geometerplus.fbreader.formats.xhtml;
import org.geometerplus.zlibrary.core.xml.ZLStringMap;
import org.geometerplus.zlibrary.core.image.ZLFileImage;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.fbreader.bookmodel.BookReader;
@ -44,7 +45,7 @@ class XHTMLTagImageAction extends XHTMLTagAction {
}
final String fullfileName = reader.getPathPrefix() + fileName;
modelReader.addImageReference(fullfileName, (short)0);
modelReader.addImage(fullfileName, new ZLFileImage("image/auto", fullfileName));
modelReader.addImage(fullfileName, new ZLFileImage("image/auto", ZLFile.createFile(fullfileName)));
if (flag) {
modelReader.beginParagraph();
}

View file

@ -22,6 +22,7 @@ package org.geometerplus.zlibrary.core.application;
import java.util.*;
import org.geometerplus.zlibrary.core.util.*;
import org.geometerplus.zlibrary.core.filesystem.*;
import org.geometerplus.zlibrary.core.library.ZLibrary;
import org.geometerplus.zlibrary.core.options.ZLBooleanOption;
import org.geometerplus.zlibrary.core.options.ZLIntegerOption;
@ -90,8 +91,8 @@ public abstract class ZLApplication {
//myPresentWindowHandler = new PresentWindowHandler(this);
//ZLCommunicationManager.instance().registerHandler("present", myPresentWindowHandler);
new ToolbarCreator().read(ZLibrary.JAR_DATA_PREFIX + "data/default/toolbar.xml");
new MenubarCreator().read(ZLibrary.JAR_DATA_PREFIX + "data/default/menubar.xml");
new ToolbarCreator().read(ZLResourceFile.createResourceFile("data/default/toolbar.xml"));
new MenubarCreator().read(ZLResourceFile.createResourceFile("data/default/menubar.xml"));
}
final Toolbar getToolbar() {
@ -205,7 +206,7 @@ public abstract class ZLApplication {
public void onWindowClosing() {
}
public abstract boolean openFile(String fileName);
public abstract boolean openFile(ZLFile file);
public final void presentWindow() {
if (myWindow != null) {

View file

@ -20,8 +20,7 @@
package org.geometerplus.zlibrary.core.application;
import java.util.*;
import org.geometerplus.zlibrary.core.util.*;
import org.geometerplus.zlibrary.core.filesystem.ZLResourceFile;
import org.geometerplus.zlibrary.core.library.ZLibrary;
import org.geometerplus.zlibrary.core.xml.ZLStringMap;
import org.geometerplus.zlibrary.core.xml.ZLXMLReaderAdapter;
@ -49,6 +48,6 @@ class ZLKeyBindingsReader extends ZLXMLReaderAdapter {
}
public void readBindings() {
read(ZLibrary.JAR_DATA_PREFIX + "data/default/keymap.xml");
read(ZLResourceFile.createResourceFile("data/default/keymap.xml"));
}
}

View file

@ -0,0 +1,112 @@
/*
* Copyright (C) 2007-2009 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.zlibrary.core.filesystem;
import java.util.HashMap;
import java.io.*;
import org.amse.ys.zip.*;
import org.geometerplus.zlibrary.core.library.ZLibrary;
public final class ZLArchiveEntryFile extends ZLFile {
private static HashMap<String,ZipFile> ourZipFileMap = new HashMap<String,ZipFile>();
static ZipFile getZipFile(String fileName) throws IOException {
synchronized (ourZipFileMap) {
ZipFile zf = ourZipFileMap.get(fileName);
if (zf == null) {
zf = new ZipFile(fileName);
ourZipFileMap.put(fileName, zf);
}
return zf;
}
}
private final ZLFile myParent;
private final String myName;
private final String myShortName;
public ZLArchiveEntryFile(ZLFile parent, String name) {
myParent = parent;
myName = name;
myShortName = name.substring(name.lastIndexOf('/') + 1);
init();
}
public boolean exists() {
return myParent.exists();
}
public boolean isDirectory() {
return false;
}
public String getPath() {
return myParent.getPath() + ":" + myName;
}
public String getNameWithExtension() {
return myShortName;
}
public ZLFile getParent() {
return myParent;
}
public ZLPhysicalFile getPhysicalFile() {
ZLFile ancestor = myParent;
while ((ancestor != null) && !(ancestor instanceof ZLPhysicalFile)) {
ancestor = ancestor.getParent();
}
return (ZLPhysicalFile)ancestor;
}
public InputStream getInputStream() throws IOException {
if (0 != (myParent.myArchiveType & ArchiveType.ZIP)) {
ZipFile zf = getZipFile(myParent.getPath());
/*
ZipEntry entry = zf.getEntry(myPath.substring(index+1));
stream = zf.getInputStream(entry);
*/
return zf.getInputStream(myName);
/*
while (true) {
ZipEntry entry = zipStream.getNextEntry();
if (entry == null) {
break;
} else if (entryName.equals(entry.getName())) {
stream = zipStream;
break;
}
}
*/
} else if (0 != (myParent.myArchiveType & ArchiveType.TAR)) {
InputStream base = myParent.getInputStream();
if (base != null) {
return new ZLTarInputStream(base, myName);
}
}
return null;
}
protected ZLDir createUnexistingDirectory() {
return null;
}
}

View file

@ -51,9 +51,6 @@ abstract class ZLFSUtil {
static int findArchiveFileNameDelimiter(String path) {
int index = path.lastIndexOf(':');
if (path.startsWith(ZLibrary.JAR_DATA_PREFIX)) {
return (index < ZLibrary.JAR_DATA_PREFIX.length()) ? -1 : index;
}
return (index == 1) ? -1 : index;
}

View file

@ -20,13 +20,11 @@
package org.geometerplus.zlibrary.core.filesystem;
import java.io.*;
import java.util.*;
import org.amse.ys.zip.*;
import org.geometerplus.zlibrary.core.util.*;
import org.geometerplus.zlibrary.core.library.ZLibrary;
public class ZLFile {
public abstract class ZLFile {
public interface ArchiveType {
int NONE = 0;
int GZIP = 0x0001;
@ -37,196 +35,84 @@ public class ZLFile {
int ARCHIVE = 0xff00;
};
private final String myPath;
private final String myNameWithExtension;
private String myNameWithoutExtension;
private String myExtension;
private int myArchiveType;
private ZLFileInfo myInfo;
private static final HashMap ourForcedFiles = new HashMap();
private static HashMap<String,ZipFile> ourZipFileMap = new HashMap<String,ZipFile>();
protected int myArchiveType;
static ZipFile getZipFile(String fileName) throws IOException {
synchronized (ourZipFileMap) {
ZipFile zf = ourZipFileMap.get(fileName);
if (zf == null) {
zf = new ZipFile(fileName);
ourZipFileMap.put(fileName, zf);
}
return zf;
protected void init() {
final String name = getNameWithExtension();
final int index = name.lastIndexOf('.');
myNameWithoutExtension = (index != -1) ? name.substring(0, index) : name;
myExtension = (index != -1) ? name.substring(index + 1).toLowerCase().intern() : "";
/*
if (lowerCaseName.endsWith(".gz")) {
myNameWithoutExtension = myNameWithoutExtension.substring(0, myNameWithoutExtension.length() - 3);
lowerCaseName = lowerCaseName.substring(0, lowerCaseName.length() - 3);
myArchiveType = myArchiveType | ArchiveType.GZIP;
}
}
public boolean removeFile(String path) {
File file = new File(path);
return file.delete();
}
public ZLFSDir createNewDirectory(String path) {
File file = new File(path);
file.mkdirs();
return new ZLFSDir(path);
}
private static ZLFileInfo getFileInfo(String path) {
ZLFileInfo info = new ZLFileInfo();
File file = new File(path);
info.Exists = file.exists() || ZLFSUtil.getRootDirectoryPath().equals(path);;
info.Size = file.length();
info.IsDirectory = file.isDirectory() || ZLFSUtil.getRootDirectoryPath().equals(path);
return info;
}
private void fillInfo() {
int index = ZLFSUtil.findArchiveFileNameDelimiter(myPath);
if (index == -1) {
myInfo = getFileInfo(myPath);
} else {
myInfo = getFileInfo(myPath.substring(0, index));
myInfo.IsDirectory = false;
if (lowerCaseName.endsWith(".bz2")) {
myNameWithoutExtension = myNameWithoutExtension.substring(0, myNameWithoutExtension.length() - 4);
lowerCaseName = lowerCaseName.substring(0, lowerCaseName.length() - 4);
myArchiveType = myArchiveType | ArchiveType.BZIP2;
}
*/
int archiveType = ArchiveType.NONE;
if (myExtension == "zip") {
archiveType |= ArchiveType.ZIP;
} else if (myExtension == "oebzip") {
archiveType |= ArchiveType.ZIP;
} else if (myExtension == "epub") {
archiveType |= ArchiveType.ZIP;
} else if (myExtension == "tar") {
archiveType |= ArchiveType.TAR;
//} else if (lowerCaseName.endsWith(".tgz")) {
//nothing to-do myNameWithoutExtension = myNameWithoutExtension.substr(0, myNameWithoutExtension.length() - 3) + "tar";
//myArchiveType = myArchiveType | ArchiveType.TAR | ArchiveType.GZIP;
}
myArchiveType = archiveType;
}
public ZLFile(File file) {
myPath = file.getPath();
myInfo = new ZLFileInfo();
myInfo.Exists = file.exists();
myInfo.Size = file.length();
myInfo.IsDirectory = file.isDirectory();
myNameWithExtension = file.getName();
init();
}
public ZLFile(String path) {
if (path.startsWith(ZLibrary.JAR_DATA_PREFIX)) {
myInfo = new ZLFileInfo();
myInfo.Exists = true;
myPath = path;
myNameWithExtension = path;
} else {
myPath = path;
int index = ZLFSUtil.findLastFileNameDelimiter(myPath);
if (index < myPath.length() - 1) {
myNameWithExtension = myPath.substring(index + 1);
} else {
myNameWithExtension = myPath;
}
public static ZLFile createFile(String path) {
if (path == null) {
return null;
}
init();
}
private void init() {
myNameWithoutExtension = myNameWithExtension;
Integer value = (Integer)ourForcedFiles.get(myPath);
if (value != null) {
myArchiveType = value.intValue();
} else {
myArchiveType = ArchiveType.NONE;
String lowerCaseName = myNameWithoutExtension.toLowerCase();
if (lowerCaseName.endsWith(".gz")) {
myNameWithoutExtension = myNameWithoutExtension.substring(0, myNameWithoutExtension.length() - 3);
lowerCaseName = lowerCaseName.substring(0, lowerCaseName.length() - 3);
myArchiveType = myArchiveType | ArchiveType.GZIP;
}
/*
if (lowerCaseName.endsWith(".bz2")) {
myNameWithoutExtension = myNameWithoutExtension.substring(0, myNameWithoutExtension.length() - 4);
lowerCaseName = lowerCaseName.substring(0, lowerCaseName.length() - 4);
myArchiveType = myArchiveType | ArchiveType.BZIP2;
}
*/
if (lowerCaseName.endsWith(".zip")) {
myArchiveType = myArchiveType | ArchiveType.ZIP;
} else if (lowerCaseName.endsWith(".tar")) {
myArchiveType = myArchiveType | ArchiveType.TAR;
} else if (lowerCaseName.endsWith(".tgz")) {
//nothing to-do myNameWithoutExtension = myNameWithoutExtension.substr(0, myNameWithoutExtension.length() - 3) + "tar";
myArchiveType = myArchiveType | ArchiveType.TAR | ArchiveType.GZIP;
}
if (!path.startsWith("/")) {
return ZLResourceFile.createResourceFile(path);
}
int index = myNameWithoutExtension.lastIndexOf('.');
if (index > 0) {
myExtension = myNameWithoutExtension.substring(index + 1);
myNameWithoutExtension = myNameWithoutExtension.substring(0, index);
} else {
myExtension = "";
}
}
public boolean exists() {
if (myInfo == null)
fillInfo();
return myInfo.Exists;
}
public long size() {
if (myInfo == null)
fillInfo();
return myInfo.Size;
}
public void forceArchiveType(int type) {
if (myArchiveType != type) {
myArchiveType = type;
ourForcedFiles.put(myPath, myArchiveType);
int index = path.lastIndexOf(':');
if (index > 1) {
return new ZLArchiveEntryFile(createFile(path.substring(0, index)), path.substring(index + 1));
}
return new ZLPhysicalFile(path);
}
public boolean isCompressed() {
public abstract boolean exists();
public abstract boolean isDirectory();
public abstract String getPath();
public abstract ZLFile getParent();
public abstract ZLPhysicalFile getPhysicalFile();
public abstract InputStream getInputStream() throws IOException;
public final boolean isCompressed() {
return (0 != (myArchiveType & ArchiveType.COMPRESSED));
}
public boolean isDirectory() {
if (myInfo == null)
fillInfo();
return myInfo.IsDirectory;
}
public boolean isArchive() {
public final boolean isArchive() {
return (0 != (myArchiveType & ArchiveType.ARCHIVE));
}
public boolean remove() {
if (removeFile(myPath)) {
myInfo = null;
return true;
} else {
return false;
}
}
public String getPath() {
return myPath;
protected abstract String getNameWithExtension();
public final String getName(boolean hideExtension) {
return hideExtension ? myNameWithoutExtension : getNameWithExtension();
}
public String getName(boolean hideExtension) {
return hideExtension ? myNameWithoutExtension : myNameWithExtension;
}
public String getExtension() {
public final String getExtension() {
return myExtension;
}
public String getPhysicalFilePath() {
String path = myPath;
if (path.startsWith(ZLibrary.JAR_DATA_PREFIX)) {
return path;
}
int index;
while ((index = ZLFSUtil.findArchiveFileNameDelimiter(path)) != -1) {
path = path.substring(0, index);
}
return path;
}
/*
public InputStream getInputStream() throws IOException {
if (isDirectory()) {
return null;
}
InputStream stream = null;
int index = ZLFSUtil.findArchiveFileNameDelimiter(myPath);
if (index == -1) {
@ -240,7 +126,7 @@ public class ZLFile {
/*
ZipEntry entry = zf.getEntry(myPath.substring(index+1));
stream = zf.getInputStream(entry);
*/
* /
final String entryName = myPath.substring(index + 1);
stream = zf.getInputStream(entryName);
/*
@ -253,7 +139,7 @@ public class ZLFile {
break;
}
}
*/
* /
} else if (0 != (baseFile.myArchiveType & ArchiveType.TAR)) {
stream = new ZLTarInputStream(base, myPath.substring(index + 1));
}
@ -270,24 +156,22 @@ public class ZLFile {
}
return stream;
}
*/
public ZLDir getDirectory() {
return getDirectory(false);
}
public ZLDir getDirectory(boolean createUnexisting) {
public final ZLDir getDirectory(boolean createUnexisting) {
if (exists()) {
if (isDirectory()) {
return new ZLFSDir(myPath);
return new ZLFSDir(getPath());
} else if (0 != (myArchiveType & ArchiveType.ZIP)) {
return new ZLZipDir(myPath);
return new ZLZipDir(getPath());
} else if (0 != (myArchiveType & ArchiveType.TAR)) {
return new ZLTarDir(myPath);
return new ZLTarDir(getPath());
}
} else if (createUnexisting) {
myInfo = null;
return createNewDirectory(myPath);
return createUnexistingDirectory();
}
return null;
}
protected abstract ZLDir createUnexistingDirectory();
}

View file

@ -0,0 +1,78 @@
/*
* Copyright (C) 2007-2009 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.zlibrary.core.filesystem;
import java.io.*;
import org.geometerplus.zlibrary.core.library.ZLibrary;
public final class ZLPhysicalFile extends ZLFile {
private final File myFile;
ZLPhysicalFile(String path) {
this(new File(path));
}
public ZLPhysicalFile(File file) {
myFile = file;
init();
}
public boolean exists() {
return myFile.exists();
}
public long size() {
return myFile.length();
}
public boolean isDirectory() {
return myFile.isDirectory();
}
public boolean remove() {
return myFile.delete();
}
public String getPath() {
return myFile.getPath();
}
public String getNameWithExtension() {
return myFile.getName();
}
public ZLFile getParent() {
return isDirectory() ? null : new ZLPhysicalFile(myFile.getParent());
}
public ZLPhysicalFile getPhysicalFile() {
return this;
}
public InputStream getInputStream() throws IOException {
return new FileInputStream(myFile);
}
protected ZLDir createUnexistingDirectory() {
myFile.mkdirs();
return new ZLFSDir(getPath());
}
}

View file

@ -19,8 +19,45 @@
package org.geometerplus.zlibrary.core.filesystem;
class ZLFileInfo {
public boolean Exists;
public boolean IsDirectory;
public long Size;
import java.io.*;
import org.geometerplus.zlibrary.core.library.ZLibrary;
public abstract class ZLResourceFile extends ZLFile {
public static ZLResourceFile createResourceFile(String path) {
return ZLibrary.Instance().createResourceFile(path);
}
private final String myPath;
private final String myName;
protected ZLResourceFile(String path) {
myPath = path;
myName = path.substring(path.lastIndexOf('/') + 1);
init();
}
public boolean isDirectory() {
return false;
}
public String getPath() {
return myPath;
}
public String getNameWithExtension() {
return myName;
}
public ZLFile getParent() {
return null;
}
public ZLPhysicalFile getPhysicalFile() {
return null;
}
protected ZLDir createUnexistingDirectory() {
return null;
}
}

View file

@ -36,7 +36,7 @@ class ZLTarDir extends ZLDir {
ArrayList names = new ArrayList();
try {
InputStream stream = new ZLFile(getPath()).getInputStream();
InputStream stream = ZLFile.createFile(getPath()).getInputStream();
if (stream != null) {
ZLTarHeader header = new ZLTarHeader();
while (header.read(stream)) {

View file

@ -41,7 +41,7 @@ public class ZLZipDir extends ZLDir {
public ArrayList/*<String>*/ collectFiles() {
ZipFile zf = null;
try {
zf = ZLFile.getZipFile(myFile.getCanonicalPath());
zf = ZLArchiveEntryFile.getZipFile(myFile.getCanonicalPath());
} catch (IOException e) {
}
if (zf == null) {

View file

@ -19,26 +19,18 @@
package org.geometerplus.zlibrary.core.html;
import java.io.*;
import org.geometerplus.zlibrary.core.util.*;
import org.geometerplus.zlibrary.core.library.ZLibrary;
import java.io.InputStream;
import java.io.IOException;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
public abstract class ZLHtmlProcessor {
public abstract boolean read(ZLHtmlReader xmlReader, InputStream stream);
/*public boolean read(ZLHTMLReader xmlReader, String fileName) {
InputStream stream = ZLibrary.Instance().getInputStream(fileName);
return (stream != null) ? read(xmlReader, stream) : false;
}*/
public boolean read(ZLHtmlReader htmlReader, String filename) {
public boolean read(ZLHtmlReader htmlReader, ZLFile file) {
try {
InputStream stream = ZLibrary.Instance().getInputStream(filename);
//InputStream stream = new FileInputStream(filename);
InputStream stream = file.getInputStream();
return (stream != null) ? read(htmlReader, stream) : false;
} catch (Exception e) {
//System.out.println(e);
//e.printStackTrace();
} catch (IOException e) {
}
return false;
}

View file

@ -20,10 +20,11 @@
package org.geometerplus.zlibrary.core.html;
import org.geometerplus.zlibrary.core.xml.ZLStringMap;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
public class ZLHtmlReaderAdapter implements ZLHtmlReader {
public boolean read(String fileName) {
return ZLHtmlProcessorFactory.getInstance().createHtmlProcessor().read(this, fileName);
public boolean read(ZLFile file) {
return ZLHtmlProcessorFactory.getInstance().createHtmlProcessor().read(this, file);
}
public boolean dontCacheAttributeValues() {

View file

@ -22,36 +22,43 @@ package org.geometerplus.zlibrary.core.image;
import java.io.*;
import java.util.*;
import org.geometerplus.zlibrary.core.util.*;
import org.geometerplus.zlibrary.core.library.ZLibrary;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
public class ZLFileImage extends ZLSingleImage {
private final String myPath;
private final ZLFile myFile;
public ZLFileImage(String mimeType, String path) {
this(mimeType, ZLFile.createFile(path));
}
public ZLFileImage(String mimeType, ZLFile file) {
super(mimeType);
myPath = path;
myFile = file;
}
public byte [] byteData() {
final InputStream stream = ZLibrary.Instance().getInputStream(myPath);
if (stream == null) {
return new byte[0];
}
final ArrayList data = new ArrayList();
byte[] buffer;
int sizeOfBufferData;
try {
final InputStream stream = myFile.getInputStream();
if (stream == null) {
return new byte[0];
}
final ArrayList data = new ArrayList();
byte[] buffer;
int sizeOfBufferData;
do {
buffer = new byte[4096];
sizeOfBufferData = stream.read(buffer);
data.add(buffer);
} while (sizeOfBufferData == 4096);
final int dataSizeMinusOne = data.size() - 1;
buffer = new byte[dataSizeMinusOne * 4096 + sizeOfBufferData];
for (int i = 0; i < dataSizeMinusOne; ++i) {
final int dataSizeMinus1 = data.size() - 1;
buffer = new byte[dataSizeMinus1 * 4096 + sizeOfBufferData];
for (int i = 0; i < dataSizeMinus1; ++i) {
System.arraycopy(data.get(i), 0, buffer, i * 4096, 4096);
}
System.arraycopy(data.get(dataSizeMinusOne), 0, buffer, dataSizeMinusOne * 4096, sizeOfBufferData);
System.arraycopy(data.get(dataSizeMinus1), 0, buffer, dataSizeMinus1 * 4096, sizeOfBufferData);
stream.close();
return buffer;
} catch (IOException e) {
}

View file

@ -22,8 +22,7 @@ package org.geometerplus.zlibrary.core.language;
import java.io.File;
import java.util.*;
import org.geometerplus.zlibrary.core.filesystem.ZLDir;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.filesystem.*;
import org.geometerplus.zlibrary.core.library.ZLibrary;
import org.geometerplus.zlibrary.core.resources.ZLResource;
@ -63,7 +62,6 @@ public abstract class ZLLanguageList {
}
public static ZLDir patternsDirectory() {
String dirName = ZLibrary.JAR_DATA_PREFIX + "data/languagePatterns.tar";
return new ZLFile(dirName).getDirectory();
return ZLResourceFile.createResourceFile("data/languagePatterns.tar").getDirectory(false);
}
}

View file

@ -17,94 +17,94 @@
* 02110-1301, USA.
*/
package org.geometerplus.zlibrary.core.language;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
public abstract class ZLLanguageMatcher {
public static final String UTF8_ENCODING_NAME = "UTF-8";
public ZLLanguageMatcher(ZLLanguageDetector.LanguageInfo info) {
myInfo = info;
}
public abstract void reset();
public abstract int criterion();
public ZLLanguageDetector.LanguageInfo info() {
return myInfo;
}
protected ZLLanguageDetector.LanguageInfo myInfo;
//subclasses
static abstract class ZLWordBasedMatcher extends ZLLanguageMatcher {
public ZLWordBasedMatcher(ZLLanguageDetector.LanguageInfo info) {
super(info);
}
public abstract void processWord(String word, int length);
};
static class ZLLanguagePatternBasedMatcher extends ZLWordBasedMatcher {
public ZLLanguagePatternBasedMatcher(String fileName, ZLLanguageDetector.LanguageInfo info) {
super(info);
try{
InputStream dictionaryStream = new ZLFile(fileName).getInputStream();
if (dictionaryStream == null) {
return;
}
final int BUFFER_SIZE = 20480;
byte[] buffer = new byte[BUFFER_SIZE];
final int start = 0;
final int end = dictionaryStream.read(buffer, 0,BUFFER_SIZE);
dictionaryStream.close();
int wordStart = 0;
for (int ptr = start; ptr != end; ++ptr) {
if (buffer[ptr] == '\n') {
String str = new String(buffer,wordStart, ptr - wordStart);
if (myDictionary.contains(str)) {
myDictionary.add(str);
}
wordStart = ptr + 1;
}
}}catch (IOException e) {
e.printStackTrace();
}
reset();
}
public void reset() {
myProCounter = 1;
myContraCounter = 1;
}
public void processWord(String word, int length) {
if (length < 5) {
if (myDictionary.contains(word)) {
++myProCounter;
} else {
++myContraCounter;
}
}
}
public int criterion() {
return myProCounter * 2000 / (myProCounter + myContraCounter) - 1000;
}
private int myProCounter;
private int myContraCounter;
private ArrayList/*<String>*/ myDictionary = new ArrayList();
};
}
package org.geometerplus.zlibrary.core.language;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
public abstract class ZLLanguageMatcher {
public static final String UTF8_ENCODING_NAME = "UTF-8";
public ZLLanguageMatcher(ZLLanguageDetector.LanguageInfo info) {
myInfo = info;
}
public abstract void reset();
public abstract int criterion();
public ZLLanguageDetector.LanguageInfo info() {
return myInfo;
}
protected ZLLanguageDetector.LanguageInfo myInfo;
//subclasses
static abstract class ZLWordBasedMatcher extends ZLLanguageMatcher {
public ZLWordBasedMatcher(ZLLanguageDetector.LanguageInfo info) {
super(info);
}
public abstract void processWord(String word, int length);
};
static class ZLLanguagePatternBasedMatcher extends ZLWordBasedMatcher {
public ZLLanguagePatternBasedMatcher(String fileName, ZLLanguageDetector.LanguageInfo info) {
super(info);
try{
InputStream dictionaryStream = ZLFile.createFile(fileName).getInputStream();
if (dictionaryStream == null) {
return;
}
final int BUFFER_SIZE = 20480;
byte[] buffer = new byte[BUFFER_SIZE];
final int start = 0;
final int end = dictionaryStream.read(buffer, 0,BUFFER_SIZE);
dictionaryStream.close();
int wordStart = 0;
for (int ptr = start; ptr != end; ++ptr) {
if (buffer[ptr] == '\n') {
String str = new String(buffer,wordStart, ptr - wordStart);
if (myDictionary.contains(str)) {
myDictionary.add(str);
}
wordStart = ptr + 1;
}
}}catch (IOException e) {
e.printStackTrace();
}
reset();
}
public void reset() {
myProCounter = 1;
myContraCounter = 1;
}
public void processWord(String word, int length) {
if (length < 5) {
if (myDictionary.contains(word)) {
++myProCounter;
} else {
++myContraCounter;
}
}
}
public int criterion() {
return myProCounter * 2000 / (myProCounter + myContraCounter) - 1000;
}
private int myProCounter;
private int myContraCounter;
private ArrayList/*<String>*/ myDictionary = new ArrayList();
};
}

View file

@ -23,13 +23,14 @@ import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import org.geometerplus.zlibrary.core.filesystem.ZLResourceFile;
import org.geometerplus.zlibrary.core.application.ZLApplication;
import org.geometerplus.zlibrary.core.view.ZLPaintContext;
import org.geometerplus.zlibrary.core.xml.ZLStringMap;
import org.geometerplus.zlibrary.core.xml.ZLXMLReaderAdapter;
public abstract class ZLibrary {
public static final String JAR_DATA_PREFIX = "#JAR#://";
//public static final String JAR_DATA_PREFIX = "#JAR#://";
private final HashMap myProperties = new HashMap();
public static ZLibrary Instance() {
@ -53,16 +54,7 @@ public abstract class ZLibrary {
return null;
}
public final InputStream getInputStream(String fileName) {
if (fileName.startsWith(JAR_DATA_PREFIX)) {
return getResourceInputStream(fileName.substring(JAR_DATA_PREFIX.length()));
} else {
return getFileInputStream(fileName);
}
}
abstract protected InputStream getResourceInputStream(String fileName);
abstract protected InputStream getFileInputStream(String fileName);
abstract public ZLResourceFile createResourceFile(String path);
//abstract public String getVersionName();
abstract public ZLPaintContext getPaintContext();

View file

@ -24,6 +24,7 @@ import java.util.*;
import org.geometerplus.zlibrary.core.library.ZLibrary;
import org.geometerplus.zlibrary.core.xml.ZLStringMap;
import org.geometerplus.zlibrary.core.xml.ZLXMLReaderAdapter;
import org.geometerplus.zlibrary.core.filesystem.*;
final class ZLTreeResource extends ZLResource {
public static ZLTreeResource ourRoot;
@ -47,8 +48,8 @@ final class ZLTreeResource extends ZLResource {
public static void loadData(String language) {
final String fileName = language + ".xml";
ResourceTreeReader reader = new ResourceTreeReader();
reader.readDocument(ourRoot, ZLibrary.JAR_DATA_PREFIX + "data/resources/zlibrary/" + fileName);
reader.readDocument(ourRoot, ZLibrary.JAR_DATA_PREFIX + "data/resources/application/" + fileName);
reader.readDocument(ourRoot, ZLResourceFile.createResourceFile("data/resources/zlibrary/" + fileName));
reader.readDocument(ourRoot, ZLResourceFile.createResourceFile("data/resources/application/" + fileName));
}
private ZLTreeResource(String name, String value) {
@ -84,10 +85,10 @@ final class ZLTreeResource extends ZLResource {
private static final String NODE = "node";
private final ArrayList<ZLTreeResource> myStack = new ArrayList<ZLTreeResource>();
public void readDocument(ZLTreeResource root, String fileName) {
public void readDocument(ZLTreeResource root, ZLFile file) {
myStack.clear();
myStack.add(root);
read(fileName);
read(file);
}
public boolean dontCacheAttributeValues() {

View file

@ -30,15 +30,11 @@ public abstract class ZLXMLProcessor {
public abstract boolean read(ZLXMLReader xmlReader, InputStream stream);
public boolean read(ZLXMLReader xmlReader, String fileName) {
public boolean read(ZLXMLReader xmlReader, ZLFile file) {
InputStream stream = null;
if (fileName.lastIndexOf(ZLibrary.JAR_DATA_PREFIX) != -1) {
stream = ZLibrary.Instance().getInputStream(fileName);
} else {
try {
stream = (new ZLFile(fileName)).getInputStream();
} catch (IOException e) {
}
try {
stream = file.getInputStream();
} catch (IOException e) {
}
if (stream == null) {
return false;

View file

@ -22,9 +22,11 @@ package org.geometerplus.zlibrary.core.xml;
import java.util.*;
import java.io.InputStream;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
public class ZLXMLReaderAdapter implements ZLXMLReader {
public boolean read(String fileName) {
return ZLXMLProcessorFactory.getInstance().createXMLProcessor().read(this, fileName);
public boolean read(ZLFile file) {
return ZLXMLProcessorFactory.getInstance().createXMLProcessor().read(this, file);
}
public boolean read(InputStream stream) {

View file

@ -180,7 +180,7 @@ final class ZLOwnXMLParser {
//entityMap.put("FBReaderVersion", ZLibrary.Instance().getVersionName().toCharArray());
final int dtdListSize = dtdList.size();
for (int i = 0; i < dtdListSize; ++i) {
InputStream stream = new ZLFile((String)dtdList.get(i)).getInputStream();
InputStream stream = ZLResourceFile.createResourceFile((String)dtdList.get(i)).getInputStream();
if (stream != null) {
new ZLOwnDTDParser().doIt(stream, entityMap);
}

View file

@ -22,6 +22,7 @@ package org.geometerplus.zlibrary.text.hyphenation;
import java.util.*;
import org.geometerplus.zlibrary.core.util.ZLMiscUtil;
import org.geometerplus.zlibrary.core.library.ZLibrary;
import org.geometerplus.zlibrary.core.filesystem.ZLResourceFile;
public final class ZLTextTeXHyphenator extends ZLTextHyphenator {
private final HashMap myPatternTable = new HashMap();
@ -42,8 +43,9 @@ public final class ZLTextTeXHyphenator extends ZLTextHyphenator {
unload();
if (language != null) {
final String path = ZLibrary.JAR_DATA_PREFIX + "data/hyphenationPatterns/" + language + ".pattern";
new ZLTextHyphenationReader(this).read(path);
new ZLTextHyphenationReader(this).read(ZLResourceFile.createResourceFile(
"data/hyphenationPatterns/" + language + ".pattern"
));
}
}

View file

@ -24,6 +24,7 @@ import org.geometerplus.zlibrary.core.util.ZLBoolean3;
import org.geometerplus.zlibrary.core.xml.*;
import org.geometerplus.zlibrary.text.model.ZLTextAlignmentType;
import org.geometerplus.zlibrary.text.view.ZLTextStyle;
import org.geometerplus.zlibrary.core.filesystem.ZLResourceFile;
public class ZLTextStyleCollection {
private static ZLTextStyleCollection ourInstance = null;
@ -32,7 +33,7 @@ public class ZLTextStyleCollection {
private final ZLTextStyleDecoration[] myDecorationMap = new ZLTextStyleDecoration[256];
private ZLTextStyleCollection() {
new TextStyleReader(this).read(ZLibrary.JAR_DATA_PREFIX + "data/default/styles.xml");
new TextStyleReader(this).read(ZLResourceFile.createResourceFile("data/default/styles.xml"));
if (myBaseStyle == null) {
myBaseStyle = new ZLTextBaseStyle("", 20);
}