/**
  * Check whether a resource is valid. A valid resource must have a non-blank ID, a non-blank link
  * and a media type.
  *
  * @param book the book the resource is for
  * @param resource the resource to check
  * @return whether the resource is valid
  */
 private static boolean isValidResource(final Book book, final Resource resource) {
   if (resource == null
       || (resource.getMediaType() == MediatypeService.NCX
           && book.getSpine().getTocResource() != null)) {
     return false;
   }
   if (StringUtil.isBlank(resource.getId())) {
     LOGGER.error(
         "resource id must not be blank (href: "
             + resource.getHref()
             + ", mediatype:"
             + resource.getMediaType()
             + ")");
     return false;
   }
   if (StringUtil.isBlank(resource.getHref())) {
     LOGGER.error(
         "resource href must not be blank (id: "
             + resource.getId()
             + ", mediatype:"
             + resource.getMediaType()
             + ")");
     return false;
   }
   if (resource.getMediaType() == null) {
     LOGGER.error(
         "resource media type must be specified (id: "
             + resource.getId()
             + ", href:"
             + resource.getHref()
             + ")");
     return false;
   }
   return true;
 }
 public static void processEpub(String bookPath, String dest)
     throws FileNotFoundException, IOException {
   EpubReader reader = new EpubReader();
   Book b = reader.readEpub(new FileInputStream(new File(bookPath)));
   String content = "";
   int pagecount = 1;
   int tempCounter;
   Count cnt = new Count(0, 0);
   for (Resource res : b.getContents()) {
     content = new String(res.getData());
     Document doc = Jsoup.parse(content, "UTF-8");
     // http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"");
     Element elem = new Element(Tag.valueOf("meta"), "");
     elem.attr("http-equiv", "content-type");
     elem.attr("content", "text/html; charset=utf-8");
     doc.head().after(elem);
     System.out.println(doc.head().data());
     Element ele = doc.body();
     alterElement(ele);
     Count cTemp = modify(ele, cnt);
     cnt.setCount(cTemp.getCount());
     cnt.setPgCount(cTemp.getPgCount());
     doc.body().html(ele.html());
     res.setData(doc.html().getBytes());
     if (res.getMediaType() == null) res.setMediaType(new MediaType("html", "html"));
   }
   EpubWriter wr = new EpubWriter();
   wr.write(b, new FileOutputStream(new File(dest)));
 }
Esempio n. 3
0
    protected Spanned doInBackground(String... hrefs) {

      publishProgress(BookReadPhase.START);

      if (loader != null) {
        loader.clear();
      }

      if (BookView.this.book == null) {
        try {
          initBook();
        } catch (IOException io) {
          this.error = io.getMessage();
          return null;
        }
      }

      this.name = spine.getCurrentTitle();

      Resource resource;

      if (hrefs.length == 0) {
        resource = spine.getCurrentResource();
      } else {
        resource = book.getResources().getByHref(hrefs[0]);
      }

      if (resource == null) {
        return new SpannedString(
            "Sorry, it looks like you clicked a dead link.\nEven books have 404s these days.");
      }

      publishProgress(BookReadPhase.PARSE_TEXT);

      try {
        Spannable result = spanner.fromHtml(resource.getReader());
        loader.load(); // Load all image resources.

        // Highlight search results (if any)
        for (SearchTextTask.SearchResult searchResult : this.searchResults) {
          if (searchResult.getIndex() == spine.getPosition()) {
            result.setSpan(
                new BackgroundColorSpan(Color.YELLOW),
                searchResult.getStart(),
                searchResult.getEnd(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
          }
        }

        publishProgress(BookReadPhase.DONE);

        return result;
      } catch (IOException io) {
        return new SpannableString("Could not load text: " + io.getMessage());
      }
    }
 public void setContextResource(Resource resource) {
   if (resource == null) {
     return;
   }
   if (StringUtils.isNotBlank(resource.getHref())) {
     int lastSlashPos = resource.getHref().lastIndexOf('/');
     if (lastSlashPos >= 0) {
       this.currentFolder = resource.getHref().substring(0, lastSlashPos + 1);
     }
   }
 }
  /**
   * Write a resource as an item element.
   *
   * @param serializer the XML serialiser to write the item element to
   * @param book the book to write the resource for
   * @param resource the resource to write as an item element
   * @throws IOException if an I/O error occurs
   */
  private static void writeItem(
      final XmlSerializer serializer, final Book book, final Resource resource) throws IOException {
    if (!isValidResource(book, resource)) {
      return;
    }

    serializer.startTag(NAMESPACE_OPF, OPFElements.ITEM);
    serializer.attribute(PREFIX_EMPTY, OPFAttributes.ID, resource.getId());
    serializer.attribute(PREFIX_EMPTY, OPFAttributes.HREF, resource.getHref());
    serializer.attribute(PREFIX_EMPTY, OPFAttributes.MEDIA_TYPE, resource.getMediaType().getName());
    if (StringUtil.isNotBlank(resource.getProperties())) {
      serializer.attribute(PREFIX_EMPTY, OPFAttributes.PROPERTIES, resource.getProperties());
    }
    serializer.endTag(NAMESPACE_OPF, OPFElements.ITEM);
  }
 /**
  * Create an Image from the data of the given resource.
  *
  * @param imageResource
  * @return
  */
 private Image createImage(Resource imageResource) {
   Image result = null;
   try {
     result = ImageIO.read(imageResource.getInputStream());
   } catch (IOException e) {
     log.error(e.getMessage());
   }
   return result;
 }
Esempio n. 7
0
  private List<Integer> getOffsetsForResource(int spineIndex) throws IOException {

    HtmlSpanner mySpanner = new HtmlSpanner();
    mySpanner.registerHandler("table", tableHandler);
    mySpanner.registerHandler("img", new ImageTagHandler(true));
    mySpanner.registerHandler("image", new ImageTagHandler(true));

    CharSequence text;

    if (spineIndex == getIndex()) {
      text = strategy.getText();
    } else {
      Resource res = spine.getResourceForIndex(spineIndex);
      text = mySpanner.fromHtml(res.getReader());
      loader.load();
    }

    return FixedPagesStrategy.getPageOffsets(this, text, true);
  }
Esempio n. 8
0
    protected Spanned doInBackground(String... hrefs) {

      if (loader != null) {
        loader.clear();
      }

      if (BookView.this.book == null) {
        try {
          initBook();
        } catch (IOException io) {
          this.error = io.getMessage();
          return null;
        }
      }

      this.name = spine.getCurrentTitle();

      Resource resource;

      if (hrefs.length == 0) {
        resource = spine.getCurrentResource();
      } else {
        resource = book.getResources().getByHref(hrefs[0]);
      }

      if (resource == null) {
        return new SpannedString(
            "Sorry, it looks like you clicked a dead link.\nEven books have 404s these days.");
      }

      try {
        Spanned result = spanner.fromHtml(resource.getReader());
        loader.load(); // Load all image resources.
        return result;
      } catch (IOException io) {
        return new SpannableString("Could not load text: " + io.getMessage());
      }
    }