/**
   * Write an EPUB 3.0 OPF package document.
   *
   * @param book the book to write the package document for
   * @param serializer the XML serialiser to write the package document to
   * @throws IOException if an I/O error occurs
   */
  public static void write(final XmlSerializer serializer, final Book book) throws IOException {
    Identifier bookId = book.getMetadata().getBookIdIdentifier();

    serializer.startDocument(Constants.CHARACTER_ENCODING, false);

    serializer.setPrefix(PREFIX_OPF, NAMESPACE_OPF);
    serializer.setPrefix(PREFIX_DUBLIN_CORE, NAMESPACE_DUBLIN_CORE);

    serializer.startTag(NAMESPACE_OPF, OPFElements.PACKAGE);
    serializer.attribute(PREFIX_EMPTY, OPFAttributes.VERSION, VERSION);
    serializer.attribute(
        PREFIX_EMPTY, OPFAttributes.UNIQUE_IDENTIFIER, bookId != null ? bookId.getId() : BOOK_ID);

    PackageDocumentMetadataWriter.writeMetaData(book, serializer);

    writeManifest(serializer, book);
    writeSpine(serializer, book);
    writeGuide(serializer, book);

    serializer.endTag(NAMESPACE_OPF, OPFElements.PACKAGE);

    serializer.endDocument();

    serializer.flush();
  }
  public void updateMetaUi() {
    Book book = mSpritzer.getBook();
    Metadata meta = book.getMetadata();
    Author author = meta.getAuthors().get(0);
    int curChapter = mSpritzer.getCurrentChapter();

    mAuthorView.setText(author.getFirstname() + " " + author.getLastname());
    mTitleView.setText(meta.getFirstTitle());
    String chapterText;
    if (book.getSpine().getResource(curChapter).getTitle() == null
        || book.getSpine().getResource(curChapter).getTitle().trim().compareTo("") == 0) {
      chapterText = String.format("Chapter %d", curChapter);
    } else {
      chapterText = book.getSpine().getResource(curChapter).getTitle();
    }

    int startSpan = chapterText.length();
    chapterText =
        String.format(
            "%s  %s m left",
            chapterText,
            (mSpritzer.getMinutesRemainingInQueue() == 0)
                ? "<1"
                : String.valueOf(mSpritzer.getMinutesRemainingInQueue()));
    int endSpan = chapterText.length();
    Spannable spanRange = new SpannableString(chapterText);
    TextAppearanceSpan tas = new TextAppearanceSpan(mChapterView.getContext(), R.style.MinutesToGo);
    spanRange.setSpan(tas, startSpan, endSpan, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    mChapterView.setText(spanRange);

    mProgress.setMax(mSpritzer.getMaxChapter());
    mProgress.setProgress(curChapter);
  }
  @Override
  public void storeBook(String fileName, Book book, boolean updateLastRead, boolean copyFile)
      throws IOException {

    File bookFile = new File(fileName);

    boolean hasBook = hasBook(bookFile.getName());

    if (hasBook && !updateLastRead) {
      return;
    } else if (hasBook) {
      helper.updateLastRead(bookFile.getName(), -1);
      return;
    }

    Metadata metaData = book.getMetadata();

    String authorFirstName = "Unknown author";
    String authorLastName = "";

    if (metaData.getAuthors().size() > 0) {
      authorFirstName = metaData.getAuthors().get(0).getFirstname();
      authorLastName = metaData.getAuthors().get(0).getLastname();
    }

    byte[] thumbNail = null;

    try {
      if (book.getCoverImage() != null && book.getCoverImage().getSize() < MAX_COVER_SIZE) {
        thumbNail = resizeImage(book.getCoverImage().getData());
        book.getCoverImage().close();
      }
    } catch (IOException io) {

    } catch (OutOfMemoryError err) {
      // If the image resource is too big, just import without a cover.
    }

    String description = "";

    if (!metaData.getDescriptions().isEmpty()) {
      description = metaData.getDescriptions().get(0);
    }

    String title = book.getTitle();

    if (title.trim().length() == 0) {
      title = fileName.substring(fileName.lastIndexOf('/') + 1);
    }

    if (copyFile) {
      bookFile = copyToLibrary(fileName, authorLastName + ", " + authorFirstName, title);
    }

    this.helper.storeNewBook(
        bookFile.getAbsolutePath(),
        authorFirstName,
        authorLastName,
        title,
        description,
        thumbNail,
        updateLastRead);
  }
Beispiel #4
0
  /**
   * Generate an ebook from an RSS DOM Document.
   *
   * @param url The URL from where the Document was fetched (used only to set the author metadata)
   * @param doc The DOM Document of the feed.
   * @return An ebook.
   * @throws IllegalArgumentException
   * @throws FeedException
   * @throws IOException
   */
  private static Book createBookFromFeed(URL url, Document doc, List<Keyword> keywords)
      throws IllegalArgumentException, FeedException, IOException {
    Book book = new Book();
    // start parsing our feed and have the above onItem methods called
    SyndFeedInput input = new SyndFeedInput();
    SyndFeed feed = input.build(doc);

    System.out.println(feed);

    // Set the title
    book.getMetadata().addTitle(feed.getTitle());

    // Add an Author
    String author = feed.getAuthor();
    if (author == null || "".equals(author.trim())) {
      author = url.getHost();
    }
    book.getMetadata().addAuthor(new Author(author));

    if (feed.getPublishedDate() != null) {
      book.getMetadata().addDate(new nl.siegmann.epublib.domain.Date(feed.getPublishedDate()));
    }

    if (feed.getDescription() != null) {
      book.getMetadata().addDescription(feed.getDescription());
    }

    if (feed.getCopyright() != null) {
      book.getMetadata().getRights().add(feed.getCopyright());
    }

    // Set cover image - This has never worked.
    // if (feed.getImage() != null) {
    // System.out.println("There is an image for the feed");

    // Promise<HttpResponse> futureImgResponse =
    // WS.url(feed.getImage().getUrl()).getAsync();
    // HttpResponse imgResponse = await(futureImgResponse);
    // System.out.println("Content-type: " + imgResponse.getContentType());
    // if (imgResponse.getContentType().startsWith("image/")) {
    // String extension =
    // imgResponse.getContentType().substring("image/".length());
    // InputStream imageStream = imgResponse.getStream();
    // book.getMetadata().setCoverImage(new Resource(imageStream, "cover." +
    // extension));

    // System.out.println("Using default cover");
    // imageStream =
    // VirtualFile.fromRelativePath("assets/cover.png").inputstream();
    // if (imageStream != null) {
    // System.out.println("Using default cover");
    // book.getMetadata().setCoverImage(new Resource(imageStream,
    // "cover.png"));
    // } else {
    // System.out.println("Could not load default cover");
    // }

    // }
    // }

    int entryNumber = 0;
    List<SyndEntry> entries = feed.getEntries();

    for (SyndEntry entry : entries) {
      if (matchesKeyword(entry, keywords)) {

        StringBuilder title = new StringBuilder(100);
        if (entry.getTitle() != null) {
          title.append(entry.getTitle());
        }
        if (entry.getAuthor() != null) {
          title.append(" - ").append(entry.getAuthor());
        }
        StringBuilder content = new StringBuilder();

        // Add title inside text
        content.append("<h2>").append(title).append("</h2>");

        if (entry.getDescription() != null) {
          SyndContent syndContent = (SyndContent) entry.getDescription();
          if (!syndContent.getType().contains("html")) {
            content.append("<pre>\n");
          }
          content.append(syndContent.getValue());
          if (!syndContent.getType().contains("html")) {
            content.append("\n</pre>");
          }
          content.append("<hr/>");
        }

        if (entry.getContents().size() > 0) {
          SyndContent syndContent = (SyndContent) entry.getContents().get(0);
          if (!syndContent.getType().contains("html")) {
            content.append("<pre>\n");
          }
          content.append(syndContent.getValue());
          if (!syndContent.getType().contains("html")) {
            content.append("\n</pre>");
          }
        }
        String strContent = clean(content.toString());
        // Add Chapter
        try {
          entryNumber++;
          book.addSection(
              title.toString(),
              new Resource(new StringReader(strContent), "entry" + entryNumber + ".xhtml"));
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }

    return book;
  }