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

book serialization is now usable

This commit is contained in:
Nikolay Pultsin 2013-01-08 00:16:48 +00:00
parent ff3ea90821
commit 59a7c6b697
8 changed files with 60 additions and 23 deletions

View file

@ -243,7 +243,7 @@ public class BookInfoActivity extends Activity {
setupInfoPair(R.id.book_authors, "authors", buffer, authors.size()); setupInfoPair(R.id.book_authors, "authors", buffer, authors.size());
final SeriesInfo series = book.getSeriesInfo(); final SeriesInfo series = book.getSeriesInfo();
setupInfoPair(R.id.book_series, "series", series == null ? null : series.Name); setupInfoPair(R.id.book_series, "series", series == null ? null : series.Title);
String seriesIndexString = null; String seriesIndexString = null;
if (series != null && series.Index != null) { if (series != null && series.Index != null) {
seriesIndexString = series.Index.toString(); seriesIndexString = series.Index.toString();

View file

@ -496,10 +496,10 @@ public final class SQLiteBooksDatabase extends BooksDatabase {
} else { } else {
long seriesId; long seriesId;
try { try {
myGetSeriesIdStatement.bindString(1, seriesInfo.Name); myGetSeriesIdStatement.bindString(1, seriesInfo.Title);
seriesId = myGetSeriesIdStatement.simpleQueryForLong(); seriesId = myGetSeriesIdStatement.simpleQueryForLong();
} catch (SQLException e) { } catch (SQLException e) {
myInsertSeriesStatement.bindString(1, seriesInfo.Name); myInsertSeriesStatement.bindString(1, seriesInfo.Title);
seriesId = myInsertSeriesStatement.executeInsert(); seriesId = myInsertSeriesStatement.executeInsert();
} }
myInsertBookSeriesStatement.bindLong(1, bookId); myInsertBookSeriesStatement.bindLong(1, bookId);
@ -516,7 +516,7 @@ public final class SQLiteBooksDatabase extends BooksDatabase {
final Cursor cursor = myDatabase.rawQuery("SELECT Series.name,BookSeries.book_index FROM BookSeries INNER JOIN Series ON Series.series_id = BookSeries.series_id WHERE BookSeries.book_id = ?", new String[] { "" + bookId }); final Cursor cursor = myDatabase.rawQuery("SELECT Series.name,BookSeries.book_index FROM BookSeries INNER JOIN Series ON Series.series_id = BookSeries.series_id WHERE BookSeries.book_id = ?", new String[] { "" + bookId });
SeriesInfo info = null; SeriesInfo info = null;
if (cursor.moveToNext()) { if (cursor.moveToNext()) {
info = new SeriesInfo(cursor.getString(0), SeriesInfo.createIndex(cursor.getString(1))); info = SeriesInfo.createSeriesInfo(cursor.getString(0), cursor.getString(1));
} }
cursor.close(); cursor.close();
return info; return info;

View file

@ -284,8 +284,8 @@ public class Book {
return mySeriesInfo; return mySeriesInfo;
} }
void setSeriesInfoWithNoCheck(String name, BigDecimal index) { void setSeriesInfoWithNoCheck(String name, String index) {
mySeriesInfo = new SeriesInfo(name, index); mySeriesInfo = SeriesInfo.createSeriesInfo(name, index);
} }
public void setSeriesInfo(String name, String index) { public void setSeriesInfo(String name, String index) {
@ -301,7 +301,7 @@ public class Book {
} else if (name == null) { } else if (name == null) {
mySeriesInfo = null; mySeriesInfo = null;
myIsSaved = false; myIsSaved = false;
} else if (!name.equals(mySeriesInfo.Name) || mySeriesInfo.Index != index) { } else if (!name.equals(mySeriesInfo.Title) || mySeriesInfo.Index != index) {
mySeriesInfo = new SeriesInfo(name, index); mySeriesInfo = new SeriesInfo(name, index);
myIsSaved = false; myIsSaved = false;
} }
@ -382,7 +382,7 @@ public class Book {
if (myTitle != null && ZLMiscUtil.matchesIgnoreCase(myTitle, pattern)) { if (myTitle != null && ZLMiscUtil.matchesIgnoreCase(myTitle, pattern)) {
return true; return true;
} }
if (mySeriesInfo != null && ZLMiscUtil.matchesIgnoreCase(mySeriesInfo.Name, pattern)) { if (mySeriesInfo != null && ZLMiscUtil.matchesIgnoreCase(mySeriesInfo.Title, pattern)) {
return true; return true;
} }
if (myAuthors != null) { if (myAuthors != null) {

View file

@ -34,7 +34,8 @@ public abstract class BookSerializerUtil {
final StringBuilder buffer = new StringBuilder(); final StringBuilder buffer = new StringBuilder();
appendTagWithAttributes( appendTagWithAttributes(
buffer, "entry", false, buffer, "entry", false,
"xmlns:dc", XMLNamespaces.DublinCore "xmlns:dc", XMLNamespaces.DublinCore,
"xmlns:calibre", XMLNamespaces.CalibreMetadata
); );
appendTagWithContent(buffer, "id", String.valueOf(book.getId())); appendTagWithContent(buffer, "id", String.valueOf(book.getId()));
@ -57,7 +58,13 @@ public abstract class BookSerializerUtil {
); );
} }
// TODO: serialize series info final SeriesInfo seriesInfo = book.getSeriesInfo();
if (seriesInfo != null) {
appendTagWithContent(buffer, "calibre:series", seriesInfo.Title);
if (seriesInfo.Index != null) {
appendTagWithContent(buffer, "calibre:series_index", String.valueOf(seriesInfo.Index));
}
}
// TODO: serialize description (?) // TODO: serialize description (?)
// TODO: serialize cover (?) // TODO: serialize cover (?)
@ -131,6 +138,8 @@ public abstract class BookSerializerUtil {
READ_AUTHOR, READ_AUTHOR,
READ_AUTHOR_URI, READ_AUTHOR_URI,
READ_AUTHOR_NAME, READ_AUTHOR_NAME,
READ_SERIES_TITLE,
READ_SERIES_INDEX,
} }
private State myState = State.READ_NOTHING; private State myState = State.READ_NOTHING;
@ -141,8 +150,11 @@ public abstract class BookSerializerUtil {
private StringBuilder myLanguage = new StringBuilder(); private StringBuilder myLanguage = new StringBuilder();
private StringBuilder myEncoding = new StringBuilder(); private StringBuilder myEncoding = new StringBuilder();
private ArrayList<Author> myAuthors = new ArrayList<Author>(); private ArrayList<Author> myAuthors = new ArrayList<Author>();
private ArrayList<Tag> myTags = new ArrayList<Tag>();
private StringBuilder myAuthorSortKey = new StringBuilder(); private StringBuilder myAuthorSortKey = new StringBuilder();
private StringBuilder myAuthorName = new StringBuilder(); private StringBuilder myAuthorName = new StringBuilder();
private StringBuilder mySeriesTitle = new StringBuilder();
private StringBuilder mySeriesIndex = new StringBuilder();
private Book myBook; private Book myBook;
@ -167,7 +179,10 @@ public abstract class BookSerializerUtil {
clear(myTitle); clear(myTitle);
clear(myLanguage); clear(myLanguage);
clear(myEncoding); clear(myEncoding);
clear(mySeriesTitle);
clear(mySeriesIndex);
myAuthors.clear(); myAuthors.clear();
myTags.clear();
myState = State.READ_NOTHING; myState = State.READ_NOTHING;
} }
@ -187,8 +202,10 @@ public abstract class BookSerializerUtil {
for (Author author : myAuthors) { for (Author author : myAuthors) {
myBook.addAuthorWithNoCheck(author); myBook.addAuthorWithNoCheck(author);
} }
// TODO: add tags for (Tag tag : myTags) {
// TODO: add series info myBook.addTagWithNoCheck(tag);
}
myBook.setSeriesInfoWithNoCheck(string(mySeriesTitle), string(mySeriesIndex));
} }
@Override @Override
@ -214,7 +231,14 @@ public abstract class BookSerializerUtil {
clear(myAuthorName); clear(myAuthorName);
clear(myAuthorSortKey); clear(myAuthorSortKey);
} else if ("category".equals(tag)) { } else if ("category".equals(tag)) {
// TODO: implement final String term = attributes.getValue("term");
if (term != null) {
myTags.add(Tag.getTag(term.split("/")));
}
} else if ("calibre:series".equals(tag)) {
myState = State.READ_SERIES_TITLE;
} else if ("calibre:series_index".equals(tag)) {
myState = State.READ_SERIES_INDEX;
} else if ("link".equals(tag)) { } else if ("link".equals(tag)) {
// TODO: use "rel" attribute // TODO: use "rel" attribute
myUrl = attributes.getValue("href"); myUrl = attributes.getValue("href");
@ -288,6 +312,12 @@ public abstract class BookSerializerUtil {
case READ_AUTHOR_NAME: case READ_AUTHOR_NAME:
myAuthorName.append(ch, start, length); myAuthorName.append(ch, start, length);
break; break;
case READ_SERIES_TITLE:
mySeriesTitle.append(ch, start, length);
break;
case READ_SERIES_INDEX:
mySeriesIndex.append(ch, start, length);
break;
} }
} }
} }

View file

@ -50,7 +50,7 @@ public abstract class BooksDatabase {
book.addTagWithNoCheck(tag); book.addTagWithNoCheck(tag);
} }
protected void setSeriesInfo(Book book, String series, String index) { protected void setSeriesInfo(Book book, String series, String index) {
book.setSeriesInfoWithNoCheck(series, SeriesInfo.createIndex(index)); book.setSeriesInfoWithNoCheck(series, index);
} }
protected abstract void executeAsATransaction(Runnable actions); protected abstract void executeAsATransaction(Runnable actions);

View file

@ -238,11 +238,11 @@ public final class Library {
return; return;
} }
final String xml = BookSerializerUtil.serialize(book);
BookSerializerUtil.deserialize(xml);
myBooks.put(book.File, book); myBooks.put(book.File, 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;
@ -253,7 +253,7 @@ public final class Library {
if (seriesInfo == null) { if (seriesInfo == null) {
authorTree.getBookSubTree(book, false); authorTree.getBookSubTree(book, false);
} else { } else {
authorTree.getSeriesSubTree(seriesInfo.Name).getBookInSeriesSubTree(book); authorTree.getSeriesSubTree(seriesInfo.Title).getBookInSeriesSubTree(book);
} }
} }
@ -266,7 +266,7 @@ public final class Library {
ROOT_BY_SERIES ROOT_BY_SERIES
); );
} }
seriesRoot.getSeriesSubTree(seriesInfo.Name).getBookInSeriesSubTree(book); seriesRoot.getSeriesSubTree(seriesInfo.Title).getBookInSeriesSubTree(book);
} }
if (myDoGroupTitlesByFirstLetter) { if (myDoGroupTitlesByFirstLetter) {

View file

@ -22,6 +22,13 @@ package org.geometerplus.fbreader.library;
import java.math.BigDecimal; import java.math.BigDecimal;
public final class SeriesInfo { public final class SeriesInfo {
public static SeriesInfo createSeriesInfo(String title, String index) {
if (title == null) {
return null;
}
return new SeriesInfo(title, createIndex(index));
}
public static BigDecimal createIndex(String index) { public static BigDecimal createIndex(String index) {
try { try {
return index != null ? new BigDecimal(index).stripTrailingZeros() : null; return index != null ? new BigDecimal(index).stripTrailingZeros() : null;
@ -30,11 +37,11 @@ public final class SeriesInfo {
} }
} }
public final String Name; public final String Title;
public final BigDecimal Index; public final BigDecimal Index;
public SeriesInfo(String name, BigDecimal index) { public SeriesInfo(String title, BigDecimal index) {
Name = name; Title = title;
Index = index; Index = index;
} }
} }

View file

@ -59,7 +59,7 @@ public final class SeriesTree extends LibraryTree {
return false; return false;
} }
final SeriesInfo info = book.getSeriesInfo(); final SeriesInfo info = book.getSeriesInfo();
return info != null && Series.equals(info.Name); return info != null && Series.equals(info.Title);
} }
@Override @Override