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

more "architecture" around book serialization

This commit is contained in:
Nikolay Pultsin 2013-01-10 05:54:40 +04:00
parent ea45b239a1
commit 4d4a495b7c
8 changed files with 134 additions and 19 deletions

View file

@ -19,6 +19,9 @@
package org.geometerplus.android.fbreader.libraryService; package org.geometerplus.android.fbreader.libraryService;
import java.util.Collections;
import java.util.List;
import android.content.*; import android.content.*;
import android.os.IBinder; import android.os.IBinder;
import android.os.RemoteException; import android.os.RemoteException;
@ -46,12 +49,23 @@ public class BookCollectionShadow implements IBookCollection, ServiceConnection
return null; return null;
} }
try { try {
return BookSerializerUtil.deserialize(myInterface.bookById(id)); return SerializerUtil.deserializeBook(myInterface.bookById(id));
} catch (RemoteException e) { } catch (RemoteException e) {
return null; return null;
} }
} }
public synchronized List<Bookmark> allBookmarks() {
if (myInterface == null) {
return Collections.emptyList();
}
try {
return SerializerUtil.deserializeBookmarkList(myInterface.allBookmarks());
} catch (RemoteException e) {
return Collections.emptyList();
}
}
// method from ServiceConnection interface // method from ServiceConnection interface
public synchronized void onServiceConnected(ComponentName name, IBinder service) { public synchronized void onServiceConnected(ComponentName name, IBinder service) {
myInterface = LibraryInterface.Stub.asInterface(service); myInterface = LibraryInterface.Stub.asInterface(service);

View file

@ -4,6 +4,9 @@
package org.geometerplus.android.fbreader.libraryService; package org.geometerplus.android.fbreader.libraryService;
import java.util.List;
interface LibraryInterface { interface LibraryInterface {
String bookById(in long id); String bookById(in long id);
List<String> allBookmarks();
} }

View file

@ -19,6 +19,8 @@
package org.geometerplus.android.fbreader.libraryService; package org.geometerplus.android.fbreader.libraryService;
import java.util.List;
import android.app.Service; import android.app.Service;
import android.content.Intent; import android.content.Intent;
import android.os.IBinder; import android.os.IBinder;
@ -68,7 +70,11 @@ public class LibraryService extends Service {
} }
public String bookById(long id) { public String bookById(long id) {
return BookSerializerUtil.serialize(myCollection.getBookById(id)); return SerializerUtil.serialize(myCollection.getBookById(id));
}
public List<String> allBookmarks() {
return SerializerUtil.serialize(myCollection.allBookmarks());
} }
} }

View file

@ -0,0 +1,28 @@
/*
* Copyright (C) 2007-2013 Geometer Plus <contact@geometerplus.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
package org.geometerplus.fbreader.library;
abstract class AbstractSerializer {
public abstract String serialize(Book book);
public abstract Book deserializeBook(String xml);
public abstract String serialize(Bookmark bookmark);
public abstract Bookmark deserializeBookmark(String xml);
}

View file

@ -19,6 +19,9 @@
package org.geometerplus.fbreader.library; package org.geometerplus.fbreader.library;
import java.util.List;
public interface IBookCollection { public interface IBookCollection {
Book getBookById(long id); Book getBookById(long id);
List<Bookmark> allBookmarks();
} }

View file

@ -183,9 +183,6 @@ public final class Library {
} }
private synchronized void addBookToLibrary(Book book) { private synchronized void addBookToLibrary(Book book) {
final String xml = BookSerializerUtil.serialize(book);
book = BookSerializerUtil.deserialize(xml);
List<Author> authors = book.authors(); List<Author> authors = book.authors();
if (authors.isEmpty()) { if (authors.isEmpty()) {
authors = (List<Author>)myNullList; authors = (List<Author>)myNullList;

View file

@ -0,0 +1,61 @@
/*
* Copyright (C) 2007-2013 Geometer Plus <contact@geometerplus.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
package org.geometerplus.fbreader.library;
import java.util.*;
public abstract class SerializerUtil {
private SerializerUtil() {
}
private static final AbstractSerializer defaultSerializer = new XMLSerializer();
public static String serialize(Book book) {
return book != null ? defaultSerializer.serialize(book) : null;
}
public static Book deserializeBook(String xml) {
return xml != null ? defaultSerializer.deserializeBook(xml) : null;
}
public static String serialize(Bookmark bookmark) {
return bookmark != null ? defaultSerializer.serialize(bookmark) : null;
}
public static Bookmark deserializeBookmark(String xml) {
return xml != null ? defaultSerializer.deserializeBookmark(xml) : null;
}
public static List<String> serialize(List<Bookmark> bookmarks) {
final List<String> serialized = new ArrayList<String>(bookmarks.size());
for (Bookmark b : bookmarks) {
serialized.add(SerializerUtil.serialize(b));
}
return serialized;
}
public static List<Bookmark> deserializeBookmarkList(List<String> xmlList) {
final List<Bookmark> bookmarks = new ArrayList<Bookmark>(xmlList.size());
for (String xml : xmlList) {
bookmarks.add(defaultSerializer.deserializeBookmark(xml));
}
return bookmarks;
}
}

View file

@ -26,15 +26,9 @@ import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.xml.ZLXMLReaderAdapter; import org.geometerplus.zlibrary.core.xml.ZLXMLReaderAdapter;
import org.geometerplus.zlibrary.core.xml.ZLStringMap; import org.geometerplus.zlibrary.core.xml.ZLStringMap;
public abstract class BookSerializerUtil { class XMLSerializer extends AbstractSerializer {
private BookSerializerUtil() { @Override
} public String serialize(Book book) {
public static String serialize(Book book) {
if (book == null) {
return null;
}
final StringBuilder buffer = new StringBuilder(); final StringBuilder buffer = new StringBuilder();
appendTagWithAttributes( appendTagWithAttributes(
buffer, "entry", false, buffer, "entry", false,
@ -84,16 +78,25 @@ public abstract class BookSerializerUtil {
return buffer.toString(); return buffer.toString();
} }
public static Book deserialize(String xml) { @Override
if (xml == null) { public Book deserializeBook(String xml) {
return null;
}
final Deserializer deserializer = new Deserializer(); final Deserializer deserializer = new Deserializer();
deserializer.readQuietly(xml); deserializer.readQuietly(xml);
return deserializer.getBook(); return deserializer.getBook();
} }
@Override
public String serialize(Bookmark bookmark) {
// TODO: implement
return null;
}
@Override
public Bookmark deserializeBookmark(String xml) {
// TODO: implement
return null;
}
private static void appendTagWithContent(StringBuilder buffer, String tag, String content) { private static void appendTagWithContent(StringBuilder buffer, String tag, String content) {
if (content != null) { if (content != null) {
buffer buffer