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( final Cursor cursor = myDatabase.query(
BOOKS_TABLE, BOOKS_TABLE,
BOOKS_COLUMNS, BOOKS_COLUMNS,
FILE_NAME_CONDITION, new String[] { description.FileName }, FILE_NAME_CONDITION, new String[] { description.File.getPath() },
null, null, null, null null, null, null, null
); );
long id = -1; long id = -1;

View file

@ -20,6 +20,7 @@
package org.geometerplus.zlibrary.core.sqliteconfig; package org.geometerplus.zlibrary.core.sqliteconfig;
import android.content.Context; import android.content.Context;
import android.database.Cursor;
import android.database.SQLException; import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement; 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 (?, ?, ?)"); mySetValueStatement = myDatabase.compileStatement("INSERT OR REPLACE INTO config (groupName, name, value) VALUES (?, ?, ?)");
myUnsetValueStatement = myDatabase.compileStatement("DELETE FROM config WHERE groupName = ? AND name = ?"); myUnsetValueStatement = myDatabase.compileStatement("DELETE FROM config WHERE groupName = ? AND name = ?");
myDeleteGroupStatement = myDatabase.compileStatement("DELETE FROM config WHERE groupName = ?"); 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) { 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.application.ZLApplication;
import org.geometerplus.zlibrary.core.config.ZLConfig; 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.R;
import org.geometerplus.zlibrary.ui.android.application.ZLAndroidApplicationWindow; import org.geometerplus.zlibrary.ui.android.application.ZLAndroidApplicationWindow;
@ -50,7 +51,7 @@ public abstract class ZLAndroidActivity extends Activity {
((ZLAndroidApplication)getApplication()).myMainWindow = new ZLAndroidApplicationWindow(application); ((ZLAndroidApplication)getApplication()).myMainWindow = new ZLAndroidApplicationWindow(application);
application.initWindow(); application.initWindow();
} else if (fileToOpen != null) { } else if (fileToOpen != null) {
ZLApplication.Instance().openFile(fileToOpen); ZLApplication.Instance().openFile(ZLFile.createFile(fileToOpen));
} }
ZLApplication.Instance().refreshWindow(); ZLApplication.Instance().refreshWindow();
} }

View file

@ -27,6 +27,7 @@ import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import org.geometerplus.zlibrary.core.library.ZLibrary; 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.core.application.ZLApplication;
import org.geometerplus.zlibrary.ui.android.R; import org.geometerplus.zlibrary.ui.android.R;
@ -66,27 +67,6 @@ public final class ZLAndroidLibrary extends ZLibrary {
return myWidget; 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) { public void openInBrowser(String reference) {
Intent intent = new Intent(Intent.ACTION_VIEW); Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(reference)); intent.setData(Uri.parse(reference));
@ -96,4 +76,33 @@ public final class ZLAndroidLibrary extends ZLibrary {
//public String getVersionName() { //public String getVersionName() {
// return myApplication.getResources().getString(android.R.attr.versionName); // 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 = new ArrayList();
myBookTextModels.add(BookTextModel); myBookTextModels.add(BookTextModel);
Description = description; Description = description;
ZLFile file = new ZLFile(description.FileName); FormatPlugin plugin = PluginCollection.instance().getPlugin(description.File);
FormatPlugin plugin = PluginCollection.instance().getPlugin(file);
if (plugin != null) { if (plugin != null) {
plugin.readModel(description, this); plugin.readModel(description, this);
} }

View file

@ -22,10 +22,10 @@ package org.geometerplus.fbreader.collection;
import java.util.*; import java.util.*;
import java.io.File; 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.zlibrary.core.library.ZLibrary;
import org.geometerplus.fbreader.formats.PluginCollection; import org.geometerplus.fbreader.formats.*;
public class BookCollection { public class BookCollection {
private static BookCollection ourInstance; private static BookCollection ourInstance;
@ -52,23 +52,39 @@ public class BookCollection {
myDoRebuild = true; myDoRebuild = true;
} }
public String getHelpFileName() { public ZLResourceFile getHelpFile() {
final String fileName = ZLibrary.JAR_DATA_PREFIX + "data/help/MiniHelp." + Locale.getDefault().getLanguage() + ".fb2"; final ZLResourceFile file = ZLResourceFile.createResourceFile(
if (new ZLFile(fileName).exists()) { "data/help/MiniHelp." + Locale.getDefault().getLanguage() + ".fb2"
return fileName; );
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, private void addDescription(LinkedList<BookDescription> list,
ZLFile physicalFile, ZLFile bookFile,
String fileName,
Map<String,BookDescription> saved) { Map<String,BookDescription> saved) {
BookDescription description = BookDescription.getDescription(fileName, physicalFile, saved.get(fileName), false); BookDescription description = saved.get(bookFile.getPath());
if (description != null) { boolean doReadMetaInfo = false;
list.add(description); 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() { private List<BookDescription> collectBookDescriptions() {
@ -77,7 +93,7 @@ public class BookCollection {
final Map<String,BookDescription> savedDescriptions = BooksDatabase.Instance().listBooks(); final Map<String,BookDescription> savedDescriptions = BooksDatabase.Instance().listBooks();
final LinkedList<BookDescription> bookDescriptions = new LinkedList<BookDescription>(); final LinkedList<BookDescription> bookDescriptions = new LinkedList<BookDescription>();
addDescription(bookDescriptions, null, getHelpFileName(), savedDescriptions); addDescription(bookDescriptions, getHelpFile(), savedDescriptions);
dirNameQueue.offer(BookDirectory); dirNameQueue.offer(BookDirectory);
while (!dirNameQueue.isEmpty()) { while (!dirNameQueue.isEmpty()) {
@ -98,16 +114,17 @@ public class BookCollection {
if (i.isDirectory()) { if (i.isDirectory()) {
dirNameQueue.add(fileName); dirNameQueue.add(fileName);
} else { } else {
final ZLFile file = new ZLFile(i); final ZLPhysicalFile file = new ZLPhysicalFile(i);
if (PluginCollection.instance().getPlugin(file) != null) { if (PluginCollection.instance().getPlugin(file) != null) {
addDescription(bookDescriptions, null, fileName, savedDescriptions); addDescription(bookDescriptions, file, savedDescriptions);
} else if (file.isArchive()) { } else if (file.isArchive()) {
if (!BookDescriptionUtil.checkInfo(file)) { if (!BookDescriptionUtil.checkInfo(file)) {
BookDescriptionUtil.resetZipInfo(file); BookDescriptionUtil.resetZipInfo(file);
BookDescriptionUtil.saveInfo(file); BookDescriptionUtil.saveInfo(file);
// TODO: reset book information for all entries
} }
for (String entryName : BookDescriptionUtil.listZipEntries(file)) { 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 java.util.*;
import org.geometerplus.zlibrary.core.util.ZLMiscUtil; 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.*; import org.geometerplus.fbreader.formats.*;
public class BookDescription { public class BookDescription {
public static BookDescription getDescription(String fileName) { 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) { if (fileName == null) {
return null; return null;
} }
final ZLFile bookFile = new ZLFile(fileName); return getDescription(ZLFile.createFile(fileName));
String physicalFileName; }
if (file == null) {
physicalFileName = bookFile.getPhysicalFilePath(); public static BookDescription getDescription(ZLFile bookFile) {
file = new ZLFile(physicalFileName); if (bookFile == null) {
if (!file.exists()) {
return null; return null;
} }
} else {
physicalFileName = file.getPath(); final ZLPhysicalFile physicalFile = bookFile.getPhysicalFile();
if ((physicalFile != null) && !physicalFile.exists()) {
return null;
} }
if (description == null) { final BookDescription description = new BookDescription(bookFile, true);
description = new BookDescription(fileName, readFromDB);
}
if (BookDescriptionUtil.checkInfo(file) && description.myIsSaved) { if (BookDescriptionUtil.checkInfo(physicalFile) && description.myIsSaved) {
return description; return description;
} }
if (physicalFileName != fileName) { if ((physicalFile != null) && (physicalFile != bookFile)) {
BookDescriptionUtil.resetZipInfo(file); BookDescriptionUtil.resetZipInfo(physicalFile);
BookDescriptionUtil.saveInfo(physicalFile);
} }
BookDescriptionUtil.saveInfo(file);
final FormatPlugin plugin = PluginCollection.instance().getPlugin(bookFile); final FormatPlugin plugin = PluginCollection.instance().getPlugin(bookFile);
if ((plugin == null) || !plugin.readDescription(fileName, description)) { if ((plugin == null) || !plugin.readDescription(bookFile, description)) {
return null; return null;
} }
@ -73,7 +68,7 @@ public class BookDescription {
return description; return description;
} }
public final String FileName; public final ZLFile File;
private long myBookId; private long myBookId;
@ -86,17 +81,17 @@ public class BookDescription {
private boolean myIsSaved; 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; myBookId = bookId;
FileName = fileName; File = file;
myTitle = title; myTitle = title;
myEncoding = encoding; myEncoding = encoding;
myLanguage = language; myLanguage = language;
myIsSaved = true; myIsSaved = true;
} }
private BookDescription(String fileName, boolean createFromDatabase) { BookDescription(ZLFile file, boolean createFromDatabase) {
FileName = fileName; File = file;
if (createFromDatabase) { if (createFromDatabase) {
final BooksDatabase database = BooksDatabase.Instance(); final BooksDatabase database = BooksDatabase.Instance();
myBookId = database.loadBook(this); myBookId = database.loadBook(this);
@ -262,7 +257,7 @@ public class BookDescription {
if (myBookId >= 0) { if (myBookId >= 0) {
database.updateBookInfo(myBookId, myEncoding, myLanguage, myTitle); database.updateBookInfo(myBookId, myEncoding, myLanguage, myTitle);
} else { } else {
myBookId = database.insertBookInfo(FileName, myEncoding, myLanguage, myTitle); myBookId = database.insertBookInfo(File.getPath(), myEncoding, myLanguage, myTitle);
} }
long index = 0; long index = 0;

View file

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

View file

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

View file

@ -22,9 +22,9 @@ package org.geometerplus.fbreader.encoding;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import org.geometerplus.zlibrary.core.util.*;
import org.geometerplus.zlibrary.core.config.ZLConfig; 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.library.ZLibrary;
import org.geometerplus.zlibrary.core.options.ZLBooleanOption; import org.geometerplus.zlibrary.core.options.ZLBooleanOption;
import org.geometerplus.zlibrary.core.xml.ZLStringMap; import org.geometerplus.zlibrary.core.xml.ZLStringMap;
@ -48,8 +48,9 @@ public class ZLEncodingCollection {
} }
return ourInstance; 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() { public ArrayList<ZLEncodingSet> sets() {
@ -87,7 +88,7 @@ public class ZLEncodingCollection {
if (mySets.isEmpty()) { if (mySets.isEmpty()) {
// String prefix = encodingDescriptionPath() + File.separator; // String prefix = encodingDescriptionPath() + File.separator;
// System.out.println("trying to read " + prefix + "Encodings.xml"); // 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) { } catch (IOException e) {
} }
} }
if (!openFile(fileName) && !openFile(myBookNameOption.getValue())) { if (!openFile(fileName) &&
openFile(BookCollection.Instance().getHelpFileName()); !openFile(myBookNameOption.getValue())) {
openFile(BookCollection.Instance().getHelpFile());
} }
} }
@ -217,7 +218,7 @@ public final class FBReader extends ZLApplication {
Model = null; Model = null;
Model = new BookModel(description); Model = new BookModel(description);
final String fileName = description.FileName; final String fileName = description.File.getPath();
myBookNameOption.setValue(fileName); myBookNameOption.setValue(fileName);
ZLTextHyphenator.Instance().load(description.getLanguage()); ZLTextHyphenator.Instance().load(description.getLanguage());
BookTextView.setModels(Model.getBookTextModels(), fileName); 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 @Override
public boolean openFile(String fileName) { public boolean openFile(ZLFile file) {
if (fileName == null) { if (file == null) {
return false; return false;
} }
BookDescription description = BookDescription.getDescription(fileName); BookDescription description = BookDescription.getDescription(file);
if (description == null) { if (description == null) {
final ZLFile file = new ZLFile(fileName);
if (file.isArchive()) { if (file.isArchive()) {
final ZLDir directory = file.getDirectory(); final ZLDir directory = file.getDirectory(false);
if (directory != null) { if (directory != null) {
final ArrayList items = directory.collectFiles(); final ArrayList items = directory.collectFiles();
final int size = items.size(); final int size = items.size();

View file

@ -30,14 +30,10 @@ import org.geometerplus.zlibrary.core.language.ZLLanguageDetector;
public abstract class FormatPlugin { public abstract class FormatPlugin {
public abstract boolean acceptsFile(ZLFile file); public abstract boolean acceptsFile(ZLFile file);
public abstract boolean readDescription(ZLFile file, BookDescription description);
public abstract boolean readDescription(String path, BookDescription description);
public abstract boolean readModel(BookDescription description, BookModel model); 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 { public static void detectEncodingAndLanguage(BookDescription description, InputStream stream) throws IOException {
String language = description.getLanguage(); String language = description.getLanguage();
String encoding = description.getEncoding(); String encoding = description.getEncoding();
@ -70,7 +66,7 @@ public abstract class FormatPlugin {
} }
} }
//Last working version //Last working version
/*public static void detectEncodingAndLanguage(BookDescription description, InputStream stream) { public static void detectEncodingAndLanguage(BookDescription description, InputStream stream) {
String encoding = description.getEncoding(); String encoding = description.getEncoding();
if (encoding.length() == 0) { if (encoding.length() == 0) {
encoding = EncodingDetector.detect(stream, PluginCollection.instance().DefaultLanguageOption.getValue()); 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 java.util.*;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.xml.*; import org.geometerplus.zlibrary.core.xml.*;
import org.geometerplus.fbreader.collection.BookDescription; import org.geometerplus.fbreader.collection.BookDescription;
@ -53,13 +54,13 @@ public class FB2DescriptionReader extends ZLXMLReaderAdapter {
return true; return true;
} }
public boolean readDescription(String fileName) { public boolean readDescription(ZLFile file) {
myReadState = READ_NOTHING; myReadState = READ_NOTHING;
myAuthorNames[0] = ""; myAuthorNames[0] = "";
myAuthorNames[1] = ""; myAuthorNames[1] = "";
myAuthorNames[2] = ""; myAuthorNames[2] = "";
myBuffer.delete(0, myBuffer.length()); myBuffer.delete(0, myBuffer.length());
return readDocument(fileName); return readDocument(file);
} }
public boolean startElementHandler(String tagName, ZLStringMap attributes) { 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(); final ZLXMLProcessor processor = ZLXMLProcessorFactory.getInstance().createXMLProcessor();
processor.setBufferSize(512); 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()); return "fb2".equals(file.getExtension());
} }
public boolean readDescription(String path, BookDescription description) { public boolean readDescription(ZLFile file, BookDescription description) {
return new FB2DescriptionReader(description).readDescription(path); return new FB2DescriptionReader(description).readDescription(file);
} }
public boolean readModel(BookDescription description, BookModel model) { 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; package org.geometerplus.fbreader.formats.fb2;
import java.util.*; import java.util.*;
import org.geometerplus.zlibrary.core.library.ZLibrary; 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.fbreader.bookmodel.*;
import org.geometerplus.zlibrary.core.xml.*; import org.geometerplus.zlibrary.core.xml.*;
import org.geometerplus.zlibrary.core.util.*;
import org.geometerplus.zlibrary.text.model.ZLTextParagraph; import org.geometerplus.zlibrary.text.model.ZLTextParagraph;
public final class FB2Reader extends BookReader implements ZLXMLReader { public final class FB2Reader extends BookReader implements ZLXMLReader {
@ -51,10 +53,10 @@ public final class FB2Reader extends BookReader implements ZLXMLReader {
super(model); super(model);
} }
boolean readBook(String fileName) { boolean readBook(ZLFile file) {
Base64EncodedImage.resetCounter(); Base64EncodedImage.resetCounter();
final ZLXMLProcessor processor = ZLXMLProcessorFactory.getInstance().createXMLProcessor(); final ZLXMLProcessor processor = ZLXMLProcessorFactory.getInstance().createXMLProcessor();
return processor.read(this, fileName); return processor.read(this, file);
} }
public void startDocumentHandler() { public void startDocumentHandler() {
@ -397,7 +399,7 @@ public final class FB2Reader extends BookReader implements ZLXMLReader {
public ArrayList externalDTDs() { public ArrayList externalDTDs() {
if (ourExternalDTDs.isEmpty()) { if (ourExternalDTDs.isEmpty()) {
ourExternalDTDs.add(ZLibrary.JAR_DATA_PREFIX + "data/formats/fb2/FBReaderVersion.ent"); ourExternalDTDs.add("data/formats/fb2/FBReaderVersion.ent");
} }
return ourExternalDTDs; return ourExternalDTDs;
} }

View file

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

View file

@ -19,11 +19,10 @@
package org.geometerplus.fbreader.formats.html; 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.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 { public class HtmlDescriptionReader extends ZLXMLReaderAdapter {
private final BookDescription myDescription; private final BookDescription myDescription;
@ -39,9 +38,9 @@ public class HtmlDescriptionReader extends ZLXMLReaderAdapter {
return true; return true;
} }
public boolean readDescription(String fileName) { public boolean readDescription(ZLFile file) {
myReadTitle = false; myReadTitle = false;
return readDocument(fileName); return readDocument(file);
} }
public boolean startElementHandler(String tagName, ZLStringMap attributes) { 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() final ZLXMLProcessor processor = ZLXMLProcessorFactory.getInstance()
.createXMLProcessor(); .createXMLProcessor();
return processor.read(this, fileName); return processor.read(this, file);
} }
} }

View file

@ -37,10 +37,8 @@ public class HtmlPlugin extends FormatPlugin {
} }
@Override @Override
public boolean readDescription(String path, BookDescription description) { public boolean readDescription(ZLFile file, BookDescription description) {
return new HtmlDescriptionReader(description).readDescription(path); return new HtmlDescriptionReader(description).readDescription(file);
// always true =)
//return true;
} }
@Override @Override
@ -49,6 +47,6 @@ public class HtmlPlugin extends FormatPlugin {
if (!description.getEncoding().equals(AUTO)) { if (!description.getEncoding().equals(AUTO)) {
//new BookDescription.BookInfo(description.FileName).EncodingOption.setValue(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.BookReader;
import org.geometerplus.fbreader.bookmodel.FBTextKind; import org.geometerplus.fbreader.bookmodel.FBTextKind;
import org.geometerplus.fbreader.formats.html.HtmlTag; 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.ZLStringMap;
import org.geometerplus.zlibrary.core.xml.ZLXMLProcessor; import org.geometerplus.zlibrary.core.xml.ZLXMLProcessor;
import org.geometerplus.zlibrary.core.xml.ZLXMLProcessorFactory; import org.geometerplus.zlibrary.core.xml.ZLXMLProcessorFactory;
@ -58,13 +59,13 @@ public class HtmlReader extends BookReader implements ZLHtmlReader {
public boolean read() { public boolean read() {
final ZLHtmlProcessor processor = ZLHtmlProcessorFactory.getInstance() final ZLHtmlProcessor processor = ZLHtmlProcessorFactory.getInstance()
.createHtmlProcessor(); .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() final ZLHtmlProcessor processor = ZLHtmlProcessorFactory.getInstance()
.createHtmlProcessor(); .createHtmlProcessor();
return processor.read(this, fileName); return processor.read(this, file);
// return readDocument(fileName); // return readDocument(fileName);
} }
@ -328,7 +329,7 @@ public class HtmlReader extends BookReader implements ZLHtmlReader {
if (":\\".equals(ref.substring(1, 3))) { if (":\\".equals(ref.substring(1, 3))) {
addImage(ref, new ZLFileImage("image/auto", ref)); addImage(ref, new ZLFileImage("image/auto", ref));
} else { } else {
String fileName = Model.Description.FileName; String fileName = Model.Description.File.getPath();
addImage(ref, new ZLFileImage("image/auto", addImage(ref, new ZLFileImage("image/auto",
fileName.substring(0, fileName.lastIndexOf('\\') + 1) + ref)); fileName.substring(0, fileName.lastIndexOf('\\') + 1) + ref));
} }

View file

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

View file

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

View file

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

View file

@ -30,7 +30,7 @@ public class PdbInputStream extends InputStream {
private final int mySize; private final int mySize;
public PdbInputStream(ZLFile file) throws IOException { public PdbInputStream(ZLFile file) throws IOException {
mySize = (int)file.size(); mySize = 0;//(int)file.size();
myBase = file.getInputStream(); myBase = file.getInputStream();
} }

View file

@ -37,7 +37,7 @@ public abstract class PdbPlugin extends FormatPlugin {
String fileName = file.getPath(); String fileName = file.getPath();
int index = fileName.indexOf(':'); 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); boolean upToDate = true;//BookDescriptionUtil.checkInfo(baseFile);
ZLStringOption palmTypeOption = new ZLStringOption(file.getPath(), "PalmType", ""); ZLStringOption palmTypeOption = new ZLStringOption(file.getPath(), "PalmType", "");

View file

@ -25,26 +25,27 @@ import java.io.InputStream;
import org.geometerplus.fbreader.formats.pdb.DocDecompressor; import org.geometerplus.fbreader.formats.pdb.DocDecompressor;
import org.geometerplus.zlibrary.core.image.ZLSingleImage; import org.geometerplus.zlibrary.core.image.ZLSingleImage;
import org.geometerplus.zlibrary.core.library.ZLibrary; import org.geometerplus.zlibrary.core.library.ZLibrary;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
public class DocCompressedFileImage extends ZLSingleImage { public class DocCompressedFileImage extends ZLSingleImage {
private final String myPath; private final ZLFile myFile;
private final int myOffset; private final int myOffset;
private final int myCompressedSize; private final int myCompressedSize;
public DocCompressedFileImage(String mimeType, final String path, final int offset, final int compressedSize) { public DocCompressedFileImage(String mimeType, final ZLFile file, final int offset, final int compressedSize) {
super(mimeType); super(mimeType);
myPath = path; myFile = file;
myOffset = offset; myOffset = offset;
myCompressedSize = compressedSize; myCompressedSize = compressedSize;
} }
public byte[] byteData() { public byte[] byteData() {
final InputStream stream = ZLibrary.Instance().getInputStream(myPath); try {
final InputStream stream = myFile.getInputStream();
if (stream == null) { if (stream == null) {
return new byte[0]; return new byte[0];
} }
try {
stream.skip(myOffset); stream.skip(myOffset);
byte [] targetBuffer = new byte[65535]; byte [] targetBuffer = new byte[65535];
final int size = DocDecompressor.decompress(stream, targetBuffer, myCompressedSize); final int size = DocDecompressor.decompress(stream, targetBuffer, myCompressedSize);
@ -58,5 +59,4 @@ public class DocCompressedFileImage extends ZLSingleImage {
return new byte[0]; return new byte[0];
} }
} }

View file

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

View file

@ -23,27 +23,27 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import org.geometerplus.zlibrary.core.image.ZLSingleImage; import org.geometerplus.zlibrary.core.image.ZLSingleImage;
import org.geometerplus.zlibrary.core.library.ZLibrary; import org.geometerplus.zlibrary.core.filesystem.ZLFile;
public class PluckerFileImage extends ZLSingleImage { public class PluckerFileImage extends ZLSingleImage {
private final String myPath; private final ZLFile myFile;
private final int myOffset; private final int myOffset;
private final int mySize; private final int mySize;
public PluckerFileImage(String mimeType, final String path, final int offset, final int size) { public PluckerFileImage(String mimeType, final ZLFile file, final int offset, final int size) {
super(mimeType); super(mimeType);
myPath = path; myFile = file;
myOffset = offset; myOffset = offset;
mySize = size; mySize = size;
} }
public byte[] byteData() { public byte[] byteData() {
final InputStream stream = ZLibrary.Instance().getInputStream(myPath); try {
final InputStream stream = myFile.getInputStream();
if (stream == null) { if (stream == null) {
return new byte[0]; return new byte[0];
} }
try {
stream.skip(myOffset); stream.skip(myOffset);
byte [] buffer = new byte[mySize]; byte [] buffer = new byte[mySize];
stream.read(buffer, 0, mySize); stream.read(buffer, 0, mySize);
@ -52,5 +52,4 @@ public class PluckerFileImage extends ZLSingleImage {
return new byte[0]; return new byte[0];
} }
} }

View file

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

View file

@ -27,22 +27,23 @@ import java.util.zip.Inflater;
import org.geometerplus.zlibrary.core.image.ZLSingleImage; import org.geometerplus.zlibrary.core.image.ZLSingleImage;
import org.geometerplus.zlibrary.core.library.ZLibrary; import org.geometerplus.zlibrary.core.library.ZLibrary;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
public class ZCompressedFileImage extends ZLSingleImage { public class ZCompressedFileImage extends ZLSingleImage {
private final String myPath; private final ZLFile myFile;
private final int myOffset; private final int myOffset;
private final int myCompressedSize; private final int myCompressedSize;
public ZCompressedFileImage(String mimeType, final String path, final int offset, final int compressedSize) { public ZCompressedFileImage(String mimeType, final ZLFile file, final int offset, final int compressedSize) {
super(mimeType); super(mimeType);
myPath = path; myFile = file;
myOffset = offset; myOffset = offset;
myCompressedSize = compressedSize; myCompressedSize = compressedSize;
} }
public byte[] byteData() { public byte[] byteData() {
final InputStream stream = ZLibrary.Instance().getInputStream(myPath); try {
final InputStream stream = myFile.getInputStream();
if (stream == null) { if (stream == null) {
return new byte[0]; return new byte[0];
} }
@ -50,7 +51,7 @@ final InputStream stream = ZLibrary.Instance().getInputStream(myPath);
final ArrayList data = new ArrayList(); final ArrayList data = new ArrayList();
byte[] buffer; byte[] buffer;
int sizeOfBufferData; int sizeOfBufferData;
try {
stream.skip(myOffset); stream.skip(myOffset);
byte [] targetBuffer = new byte[myCompressedSize]; byte [] targetBuffer = new byte[myCompressedSize];
stream.read(targetBuffer, 0, myCompressedSize); stream.read(targetBuffer, 0, myCompressedSize);
@ -62,18 +63,16 @@ final InputStream stream = ZLibrary.Instance().getInputStream(myPath);
data.add(buffer); data.add(buffer);
} while (sizeOfBufferData == 4096); } while (sizeOfBufferData == 4096);
decompressor.end(); decompressor.end();
final int dataSizeMinusOne = data.size() - 1; final int dataSizeMinus1 = data.size() - 1;
buffer = new byte[dataSizeMinusOne * 4096 + sizeOfBufferData]; buffer = new byte[dataSizeMinus1 * 4096 + sizeOfBufferData];
for (int i = 0; i < dataSizeMinusOne; ++i) { for (int i = 0; i < dataSizeMinus1; ++i) {
System.arraycopy(data.get(i), 0, buffer, i * 4096, 4096); 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);
return buffer; return buffer;
} catch (IOException e) { } catch (IOException e) {
} catch (DataFormatException e) { } catch (DataFormatException e) {
} }
return new byte[0]; return new byte[0];
} }
} }

View file

@ -23,7 +23,7 @@ import org.geometerplus.zlibrary.core.filesystem.ZLFile;
public class MiscUtil { public class MiscUtil {
public static String htmlDirectoryPrefix(String fileName) { public static String htmlDirectoryPrefix(String fileName) {
ZLFile file = new ZLFile(fileName); ZLFile file = ZLFile.createFile(fileName);
String shortName = file.getName(false); String shortName = file.getName(false);
String path = file.getPath(); String path = file.getPath();
int index = -1; 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.library.ZLibrary;
import org.geometerplus.zlibrary.core.xml.*; import org.geometerplus.zlibrary.core.xml.*;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.fbreader.bookmodel.*; import org.geometerplus.fbreader.bookmodel.*;
import org.geometerplus.fbreader.formats.util.MiscUtil; import org.geometerplus.fbreader.formats.util.MiscUtil;
@ -133,18 +134,18 @@ public class XHTMLReader extends ZLXMLReaderAdapter {
return myPathPrefix; return myPathPrefix;
} }
public boolean readFile(String filePath, String referenceName) { public boolean readFile(ZLFile file, String referenceName) {
myModelReader.addHyperlinkLabel(referenceName); myModelReader.addHyperlinkLabel(referenceName);
fillTagTable(); fillTagTable();
myPathPrefix = MiscUtil.htmlDirectoryPrefix(filePath); myPathPrefix = MiscUtil.htmlDirectoryPrefix(file.getPath());
myReferenceName = referenceName; myReferenceName = referenceName;
myPreformatted = false; myPreformatted = false;
myInsideBody = false; myInsideBody = false;
return read(filePath); return read(file);
} }
/* /*
@ -225,9 +226,9 @@ cycle:
public ArrayList externalDTDs() { public ArrayList externalDTDs() {
if (ourExternalDTDs.isEmpty()) { if (ourExternalDTDs.isEmpty()) {
ourExternalDTDs.add(ZLibrary.JAR_DATA_PREFIX + "data/formats/xhtml/xhtml-lat1.ent"); ourExternalDTDs.add("data/formats/xhtml/xhtml-lat1.ent");
ourExternalDTDs.add(ZLibrary.JAR_DATA_PREFIX + "data/formats/xhtml/xhtml-special.ent"); ourExternalDTDs.add("data/formats/xhtml/xhtml-special.ent");
ourExternalDTDs.add(ZLibrary.JAR_DATA_PREFIX + "data/formats/xhtml/xhtml-symbol.ent"); ourExternalDTDs.add("data/formats/xhtml/xhtml-symbol.ent");
} }
return ourExternalDTDs; 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.xml.ZLStringMap;
import org.geometerplus.zlibrary.core.image.ZLFileImage; import org.geometerplus.zlibrary.core.image.ZLFileImage;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.fbreader.bookmodel.BookReader; import org.geometerplus.fbreader.bookmodel.BookReader;
@ -44,7 +45,7 @@ class XHTMLTagImageAction extends XHTMLTagAction {
} }
final String fullfileName = reader.getPathPrefix() + fileName; final String fullfileName = reader.getPathPrefix() + fileName;
modelReader.addImageReference(fullfileName, (short)0); 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) { if (flag) {
modelReader.beginParagraph(); modelReader.beginParagraph();
} }

View file

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

View file

@ -20,8 +20,7 @@
package org.geometerplus.zlibrary.core.application; package org.geometerplus.zlibrary.core.application;
import java.util.*; 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.library.ZLibrary;
import org.geometerplus.zlibrary.core.xml.ZLStringMap; import org.geometerplus.zlibrary.core.xml.ZLStringMap;
import org.geometerplus.zlibrary.core.xml.ZLXMLReaderAdapter; import org.geometerplus.zlibrary.core.xml.ZLXMLReaderAdapter;
@ -49,6 +48,6 @@ class ZLKeyBindingsReader extends ZLXMLReaderAdapter {
} }
public void readBindings() { 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) { static int findArchiveFileNameDelimiter(String path) {
int index = path.lastIndexOf(':'); 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; return (index == 1) ? -1 : index;
} }

View file

@ -20,13 +20,11 @@
package org.geometerplus.zlibrary.core.filesystem; package org.geometerplus.zlibrary.core.filesystem;
import java.io.*; import java.io.*;
import java.util.*;
import org.amse.ys.zip.*;
import org.geometerplus.zlibrary.core.util.*; import org.geometerplus.zlibrary.core.util.*;
import org.geometerplus.zlibrary.core.library.ZLibrary; import org.geometerplus.zlibrary.core.library.ZLibrary;
public class ZLFile { public abstract class ZLFile {
public interface ArchiveType { public interface ArchiveType {
int NONE = 0; int NONE = 0;
int GZIP = 0x0001; int GZIP = 0x0001;
@ -37,196 +35,84 @@ public class ZLFile {
int ARCHIVE = 0xff00; int ARCHIVE = 0xff00;
}; };
private final String myPath;
private final String myNameWithExtension;
private String myNameWithoutExtension; private String myNameWithoutExtension;
private String myExtension; private String myExtension;
private int myArchiveType; protected int myArchiveType;
private ZLFileInfo myInfo;
private static final HashMap ourForcedFiles = new HashMap(); protected void init() {
private static HashMap<String,ZipFile> ourZipFileMap = new HashMap<String,ZipFile>(); 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() : "";
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;
}
}
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;
}
}
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;
}
}
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")) { if (lowerCaseName.endsWith(".gz")) {
myNameWithoutExtension = myNameWithoutExtension.substring(0, myNameWithoutExtension.length() - 3); myNameWithoutExtension = myNameWithoutExtension.substring(0, myNameWithoutExtension.length() - 3);
lowerCaseName = lowerCaseName.substring(0, lowerCaseName.length() - 3); lowerCaseName = lowerCaseName.substring(0, lowerCaseName.length() - 3);
myArchiveType = myArchiveType | ArchiveType.GZIP; myArchiveType = myArchiveType | ArchiveType.GZIP;
} }
/*
if (lowerCaseName.endsWith(".bz2")) { if (lowerCaseName.endsWith(".bz2")) {
myNameWithoutExtension = myNameWithoutExtension.substring(0, myNameWithoutExtension.length() - 4); myNameWithoutExtension = myNameWithoutExtension.substring(0, myNameWithoutExtension.length() - 4);
lowerCaseName = lowerCaseName.substring(0, lowerCaseName.length() - 4); lowerCaseName = lowerCaseName.substring(0, lowerCaseName.length() - 4);
myArchiveType = myArchiveType | ArchiveType.BZIP2; myArchiveType = myArchiveType | ArchiveType.BZIP2;
} }
*/ */
if (lowerCaseName.endsWith(".zip")) { int archiveType = ArchiveType.NONE;
myArchiveType = myArchiveType | ArchiveType.ZIP; if (myExtension == "zip") {
} else if (lowerCaseName.endsWith(".tar")) { archiveType |= ArchiveType.ZIP;
myArchiveType = myArchiveType | ArchiveType.TAR; } else if (myExtension == "oebzip") {
} else if (lowerCaseName.endsWith(".tgz")) { 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"; //nothing to-do myNameWithoutExtension = myNameWithoutExtension.substr(0, myNameWithoutExtension.length() - 3) + "tar";
myArchiveType = myArchiveType | ArchiveType.TAR | ArchiveType.GZIP; //myArchiveType = myArchiveType | ArchiveType.TAR | ArchiveType.GZIP;
} }
myArchiveType = archiveType;
} }
int index = myNameWithoutExtension.lastIndexOf('.'); public static ZLFile createFile(String path) {
if (index > 0) { if (path == null) {
myExtension = myNameWithoutExtension.substring(index + 1); return null;
myNameWithoutExtension = myNameWithoutExtension.substring(0, index);
} else {
myExtension = "";
} }
if (!path.startsWith("/")) {
return ZLResourceFile.createResourceFile(path);
}
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 exists() { public abstract boolean exists();
if (myInfo == null) public abstract boolean isDirectory();
fillInfo(); public abstract String getPath();
return myInfo.Exists; public abstract ZLFile getParent();
} public abstract ZLPhysicalFile getPhysicalFile();
public abstract InputStream getInputStream() throws IOException;
public long size() { public final boolean isCompressed() {
if (myInfo == null)
fillInfo();
return myInfo.Size;
}
public void forceArchiveType(int type) {
if (myArchiveType != type) {
myArchiveType = type;
ourForcedFiles.put(myPath, myArchiveType);
}
}
public boolean isCompressed() {
return (0 != (myArchiveType & ArchiveType.COMPRESSED)); return (0 != (myArchiveType & ArchiveType.COMPRESSED));
} }
public boolean isDirectory() { public final boolean isArchive() {
if (myInfo == null)
fillInfo();
return myInfo.IsDirectory;
}
public boolean isArchive() {
return (0 != (myArchiveType & ArchiveType.ARCHIVE)); return (0 != (myArchiveType & ArchiveType.ARCHIVE));
} }
public boolean remove() { protected abstract String getNameWithExtension();
if (removeFile(myPath)) { public final String getName(boolean hideExtension) {
myInfo = null; return hideExtension ? myNameWithoutExtension : getNameWithExtension();
return true;
} else {
return false;
}
} }
public String getPath() { public final String getExtension() {
return myPath;
}
public String getName(boolean hideExtension) {
return hideExtension ? myNameWithoutExtension : myNameWithExtension;
}
public String getExtension() {
return myExtension; 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 { public InputStream getInputStream() throws IOException {
if (isDirectory()) {
return null;
}
InputStream stream = null; InputStream stream = null;
int index = ZLFSUtil.findArchiveFileNameDelimiter(myPath); int index = ZLFSUtil.findArchiveFileNameDelimiter(myPath);
if (index == -1) { if (index == -1) {
@ -270,24 +156,22 @@ public class ZLFile {
} }
return stream; return stream;
} }
*/
public ZLDir getDirectory() { public final ZLDir getDirectory(boolean createUnexisting) {
return getDirectory(false);
}
public ZLDir getDirectory(boolean createUnexisting) {
if (exists()) { if (exists()) {
if (isDirectory()) { if (isDirectory()) {
return new ZLFSDir(myPath); return new ZLFSDir(getPath());
} else if (0 != (myArchiveType & ArchiveType.ZIP)) { } else if (0 != (myArchiveType & ArchiveType.ZIP)) {
return new ZLZipDir(myPath); return new ZLZipDir(getPath());
} else if (0 != (myArchiveType & ArchiveType.TAR)) { } else if (0 != (myArchiveType & ArchiveType.TAR)) {
return new ZLTarDir(myPath); return new ZLTarDir(getPath());
} }
} else if (createUnexisting) { } else if (createUnexisting) {
myInfo = null; return createUnexistingDirectory();
return createNewDirectory(myPath);
} }
return null; 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; package org.geometerplus.zlibrary.core.filesystem;
class ZLFileInfo { import java.io.*;
public boolean Exists;
public boolean IsDirectory; import org.geometerplus.zlibrary.core.library.ZLibrary;
public long Size;
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(); ArrayList names = new ArrayList();
try { try {
InputStream stream = new ZLFile(getPath()).getInputStream(); InputStream stream = ZLFile.createFile(getPath()).getInputStream();
if (stream != null) { if (stream != null) {
ZLTarHeader header = new ZLTarHeader(); ZLTarHeader header = new ZLTarHeader();
while (header.read(stream)) { while (header.read(stream)) {

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -58,7 +58,7 @@ public abstract class ZLLanguageMatcher {
public ZLLanguagePatternBasedMatcher(String fileName, ZLLanguageDetector.LanguageInfo info) { public ZLLanguagePatternBasedMatcher(String fileName, ZLLanguageDetector.LanguageInfo info) {
super(info); super(info);
try{ try{
InputStream dictionaryStream = new ZLFile(fileName).getInputStream(); InputStream dictionaryStream = ZLFile.createFile(fileName).getInputStream();
if (dictionaryStream == null) { if (dictionaryStream == null) {
return; return;
} }

View file

@ -23,13 +23,14 @@ import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.util.HashMap; import java.util.HashMap;
import org.geometerplus.zlibrary.core.filesystem.ZLResourceFile;
import org.geometerplus.zlibrary.core.application.ZLApplication; import org.geometerplus.zlibrary.core.application.ZLApplication;
import org.geometerplus.zlibrary.core.view.ZLPaintContext; import org.geometerplus.zlibrary.core.view.ZLPaintContext;
import org.geometerplus.zlibrary.core.xml.ZLStringMap; import org.geometerplus.zlibrary.core.xml.ZLStringMap;
import org.geometerplus.zlibrary.core.xml.ZLXMLReaderAdapter; import org.geometerplus.zlibrary.core.xml.ZLXMLReaderAdapter;
public abstract class ZLibrary { 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(); private final HashMap myProperties = new HashMap();
public static ZLibrary Instance() { public static ZLibrary Instance() {
@ -53,16 +54,7 @@ public abstract class ZLibrary {
return null; return null;
} }
public final InputStream getInputStream(String fileName) { abstract public ZLResourceFile createResourceFile(String path);
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 String getVersionName(); //abstract public String getVersionName();
abstract public ZLPaintContext getPaintContext(); 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.library.ZLibrary;
import org.geometerplus.zlibrary.core.xml.ZLStringMap; import org.geometerplus.zlibrary.core.xml.ZLStringMap;
import org.geometerplus.zlibrary.core.xml.ZLXMLReaderAdapter; import org.geometerplus.zlibrary.core.xml.ZLXMLReaderAdapter;
import org.geometerplus.zlibrary.core.filesystem.*;
final class ZLTreeResource extends ZLResource { final class ZLTreeResource extends ZLResource {
public static ZLTreeResource ourRoot; public static ZLTreeResource ourRoot;
@ -47,8 +48,8 @@ final class ZLTreeResource extends ZLResource {
public static void loadData(String language) { public static void loadData(String language) {
final String fileName = language + ".xml"; final String fileName = language + ".xml";
ResourceTreeReader reader = new ResourceTreeReader(); ResourceTreeReader reader = new ResourceTreeReader();
reader.readDocument(ourRoot, ZLibrary.JAR_DATA_PREFIX + "data/resources/zlibrary/" + fileName); reader.readDocument(ourRoot, ZLResourceFile.createResourceFile("data/resources/zlibrary/" + fileName));
reader.readDocument(ourRoot, ZLibrary.JAR_DATA_PREFIX + "data/resources/application/" + fileName); reader.readDocument(ourRoot, ZLResourceFile.createResourceFile("data/resources/application/" + fileName));
} }
private ZLTreeResource(String name, String value) { private ZLTreeResource(String name, String value) {
@ -84,10 +85,10 @@ final class ZLTreeResource extends ZLResource {
private static final String NODE = "node"; private static final String NODE = "node";
private final ArrayList<ZLTreeResource> myStack = new ArrayList<ZLTreeResource>(); 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.clear();
myStack.add(root); myStack.add(root);
read(fileName); read(file);
} }
public boolean dontCacheAttributeValues() { public boolean dontCacheAttributeValues() {

View file

@ -30,16 +30,12 @@ public abstract class ZLXMLProcessor {
public abstract boolean read(ZLXMLReader xmlReader, InputStream stream); 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; InputStream stream = null;
if (fileName.lastIndexOf(ZLibrary.JAR_DATA_PREFIX) != -1) {
stream = ZLibrary.Instance().getInputStream(fileName);
} else {
try { try {
stream = (new ZLFile(fileName)).getInputStream(); stream = file.getInputStream();
} catch (IOException e) { } catch (IOException e) {
} }
}
if (stream == null) { if (stream == null) {
return false; return false;
} }

View file

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

View file

@ -180,7 +180,7 @@ final class ZLOwnXMLParser {
//entityMap.put("FBReaderVersion", ZLibrary.Instance().getVersionName().toCharArray()); //entityMap.put("FBReaderVersion", ZLibrary.Instance().getVersionName().toCharArray());
final int dtdListSize = dtdList.size(); final int dtdListSize = dtdList.size();
for (int i = 0; i < dtdListSize; ++i) { 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) { if (stream != null) {
new ZLOwnDTDParser().doIt(stream, entityMap); new ZLOwnDTDParser().doIt(stream, entityMap);
} }

View file

@ -22,6 +22,7 @@ package org.geometerplus.zlibrary.text.hyphenation;
import java.util.*; import java.util.*;
import org.geometerplus.zlibrary.core.util.ZLMiscUtil; import org.geometerplus.zlibrary.core.util.ZLMiscUtil;
import org.geometerplus.zlibrary.core.library.ZLibrary; import org.geometerplus.zlibrary.core.library.ZLibrary;
import org.geometerplus.zlibrary.core.filesystem.ZLResourceFile;
public final class ZLTextTeXHyphenator extends ZLTextHyphenator { public final class ZLTextTeXHyphenator extends ZLTextHyphenator {
private final HashMap myPatternTable = new HashMap(); private final HashMap myPatternTable = new HashMap();
@ -42,8 +43,9 @@ public final class ZLTextTeXHyphenator extends ZLTextHyphenator {
unload(); unload();
if (language != null) { if (language != null) {
final String path = ZLibrary.JAR_DATA_PREFIX + "data/hyphenationPatterns/" + language + ".pattern"; new ZLTextHyphenationReader(this).read(ZLResourceFile.createResourceFile(
new ZLTextHyphenationReader(this).read(path); "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.core.xml.*;
import org.geometerplus.zlibrary.text.model.ZLTextAlignmentType; import org.geometerplus.zlibrary.text.model.ZLTextAlignmentType;
import org.geometerplus.zlibrary.text.view.ZLTextStyle; import org.geometerplus.zlibrary.text.view.ZLTextStyle;
import org.geometerplus.zlibrary.core.filesystem.ZLResourceFile;
public class ZLTextStyleCollection { public class ZLTextStyleCollection {
private static ZLTextStyleCollection ourInstance = null; private static ZLTextStyleCollection ourInstance = null;
@ -32,7 +33,7 @@ public class ZLTextStyleCollection {
private final ZLTextStyleDecoration[] myDecorationMap = new ZLTextStyleDecoration[256]; private final ZLTextStyleDecoration[] myDecorationMap = new ZLTextStyleDecoration[256];
private ZLTextStyleCollection() { 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) { if (myBaseStyle == null) {
myBaseStyle = new ZLTextBaseStyle("", 20); myBaseStyle = new ZLTextBaseStyle("", 20);
} }