Example #1
0
  public void navigateTo(String rawHref) {

    this.prevIndex = this.getIndex();
    this.prevPos = this.getPosition();

    // URLDecode the href, so it does not contain %20 etc.
    String href =
        URLDecoder.decode(StringUtil.substringBefore(rawHref, Constants.FRAGMENT_SEPARATOR_CHAR));

    // Don't decode the anchor.
    String anchor = StringUtil.substringAfterLast(rawHref, Constants.FRAGMENT_SEPARATOR_CHAR);

    if (!"".equals(anchor)) {
      this.storedAnchor = anchor;
    }

    // Just an anchor and no href; resolve it on this page
    if (href.length() == 0) {
      restorePosition();
    } else {

      this.strategy.clearText();
      this.strategy.setPosition(0);

      if (this.spine.navigateByHref(href)) {
        loadText();
      } else {
        executeTask(new LoadTextTask(), href);
      }
    }
  }
 /**
  * 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;
 }
 private static void writeGuideReference(GuideReference reference, XmlSerializer serializer)
     throws IOException {
   if (reference != null) {
     serializer.startTag(NAMESPACE_OPF, OPFElements.REFERENCE);
     serializer.attribute(PREFIX_EMPTY, OPFAttributes.TYPE, reference.getType());
     serializer.attribute(PREFIX_EMPTY, OPFAttributes.HREF, reference.getCompleteHref());
     if (StringUtil.isNotBlank(reference.getTitle())) {
       serializer.attribute(PREFIX_EMPTY, OPFAttributes.TITLE, reference.getTitle());
     }
     serializer.endTag(NAMESPACE_OPF, OPFElements.REFERENCE);
   }
 }
Example #4
0
 public String toString() {
   return StringUtil.toString(
       "id",
       id,
       "title",
       title,
       "encoding",
       inputEncoding,
       "mediaType",
       mediaType,
       "href",
       href,
       "size",
       (data == null ? 0 : data.length));
 }
  /**
   * 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);
  }
Example #6
0
    @Override
    protected List<List<Integer>> doInBackground(Object... params) {

      try {
        List<List<Integer>> offsets = new ArrayList<List<Integer>>();

        for (int i = 0; i < spine.size(); i++) {
          offsets.add(getOffsetsForResource(i));
        }

        String file = StringUtil.substringAfterLast(fileName, '/');
        configuration.setPageOffsets(file, offsets);

        return offsets;

      } catch (IOException io) {
        LOG.error("Could not read pagenumers", io);
      }

      return null;
    }
Example #7
0
    private void setBook(Book book) {

      BookView.this.book = book;
      BookView.this.spine = new PageTurnerSpine(book);

      String file = StringUtil.substringAfterLast(fileName, '/');

      BookView.this.spine.navigateByIndex(BookView.this.storedIndex);

      if (configuration.isShowPageNumbers()) {

        List<List<Integer>> offsets = configuration.getPageOffsets(file);

        if (offsets != null && offsets.size() > 0) {
          spine.setPageOffsets(offsets);
          needToCalcPageNumbers = false;
        } else {
          needToCalcPageNumbers = true;
        }
      }
    }
Example #8
0
  private String getPackageResourceHref(Resources resources) {
    String defaultResult = "OEBPS/content.opf";
    String result = defaultResult;

    Resource containerResource = resources.remove("META-INF/container.xml");
    if (containerResource == null) {
      return result;
    }
    try {
      Document document = ResourceUtil.getAsDocument(containerResource);
      Element rootFileElement =
          (Element)
              ((Element) document.getDocumentElement().getElementsByTagName("rootfiles").item(0))
                  .getElementsByTagName("rootfile")
                  .item(0);
      result = rootFileElement.getAttribute("full-path");
    } catch (Exception e) {
      log.error(e.getMessage(), e);
    }
    if (StringUtil.isBlank(result)) {
      result = defaultResult;
    }
    return result;
  }