Package nl.siegmann.epublib.domain

Examples of nl.siegmann.epublib.domain.Resources


   * @param lazyLoadedTypes a list of the MediaType to load lazily
   * @return this Book without loading all resources into memory.
   * @throws IOException
   */
  public Book readEpubLazy(ZipFile zipFile, String encoding, List<MediaType> lazyLoadedTypes ) throws IOException {
    Resources resources = ResourcesLoader.loadResources(zipFile, encoding, lazyLoadedTypes);
    return readEpub(resources);
  }
View Full Code Here


   * @throws IOException
   */
  public static Resources loadResources(ZipFile zipFile, String defaultHtmlEncoding,
      List<MediaType> lazyLoadedTypes) throws IOException {   
       
    Resources result = new Resources();
        Enumeration<? extends ZipEntry> entries = zipFile.entries();

        while( entries.hasMoreElements() ) {
            ZipEntry zipEntry = entries.nextElement();

      if(zipEntry == null || zipEntry.isDirectory()) {
        continue;
      }
     
      String href = zipEntry.getName();
     
      Resource resource;
     
      if (shouldLoadLazy(href, lazyLoadedTypes)) {
        resource = new LazyResource(zipFile.getName(), zipEntry.getSize(), href);               
      } else {   
        resource = ResourceUtil.createResource(zipEntry, zipFile.getInputStream(zipEntry));
      }
     
      if(resource.getMediaType() == MediatypeService.XHTML) {
        resource.setInputEncoding(defaultHtmlEncoding);
      }
      result.add(resource);
    }
   
    return result;
  }
View Full Code Here

   * @param defaultHtmlEncoding
   * @return
   * @throws IOException
   */
  public static Resources loadResources(ZipInputStream zipInputStream, String defaultHtmlEncoding) throws IOException {
    Resources result = new Resources();
    ZipEntry zipEntry;
    do {
      // get next valid zipEntry
      zipEntry = getNextZipEntry(zipInputStream);
      if((zipEntry == null) || (zipEntry == ERROR_ZIP_ENTRY) || zipEntry.isDirectory()) {
        continue;
      }
     
      // store resource
      Resource resource = ResourceUtil.createResource(zipEntry, zipInputStream);
      if(resource.getMediaType() == MediatypeService.XHTML) {
        resource.setInputEncoding(defaultHtmlEncoding);
      }
      result.add(resource);
    } while(zipEntry != null);

    return result;
  }
View Full Code Here

   * @return a Map with resources, with their id's as key.
   */
  private static Resources readManifest(Document packageDocument, String packageHref,
      EpubReader epubReader, Resources resources, Map<String, String> idMapping) {
    Element manifestElement = DOMUtil.getFirstElementByTagNameNS(packageDocument.getDocumentElement(), NAMESPACE_OPF, OPFTags.manifest);
    Resources result = new Resources();
    if(manifestElement == null) {
      log.error("Package document does not contain element " + OPFTags.manifest);
      return result;
    }
    NodeList itemElements = manifestElement.getElementsByTagNameNS(NAMESPACE_OPF, OPFTags.item);
    for(int i = 0; i < itemElements.getLength(); i++) {
      Element itemElement = (Element) itemElements.item(i);
      String id = DOMUtil.getAttribute(itemElement, NAMESPACE_OPF, OPFAttributes.id);
      String href = DOMUtil.getAttribute(itemElement, NAMESPACE_OPF, OPFAttributes.href);
      try {
        href = URLDecoder.decode(href, Constants.CHARACTER_ENCODING);
      } catch (UnsupportedEncodingException e) {
        log.error(e.getMessage());
      }
      String mediaTypeName = DOMUtil.getAttribute(itemElement, NAMESPACE_OPF, OPFAttributes.media_type);
      Resource resource = resources.remove(href);
      if(resource == null) {
        log.error("resource with href '" + href + "' not found");
        continue;
      }
      resource.setId(id);
      MediaType mediaType = MediatypeService.getMediaTypeByName(mediaTypeName);
      if(mediaType != null) {
        resource.setMediaType(mediaType);
      }
      result.add(resource);
      idMapping.put(id, resource.getId());
    }
    return result;
 
View Full Code Here

      Resources resourcesByHref) {
    int lastSlashPos = packageHref.lastIndexOf('/');
    if(lastSlashPos < 0) {
      return resourcesByHref;
    }
    Resources result = new Resources();
    for(Resource resource: resourcesByHref.getAll()) {
      if(StringUtil.isNotBlank(resource.getHref())
          || resource.getHref().length() > lastSlashPos) {
        resource.setHref(resource.getHref().substring(lastSlashPos + 1));
      }
      result.add(resource);
    }
    return result;
  }
View Full Code Here

   * @throws IOException
   */
  public static Book createBookFromDirectory(FileObject rootDirectory, String encoding) throws IOException {
    Book result = new Book();
    List<TOCReference> sections = new ArrayList<TOCReference>();
    Resources resources = new Resources();
    processDirectory(rootDirectory, rootDirectory, sections, resources, encoding);
    result.setResources(resources);
    TableOfContents tableOfContents = new TableOfContents(sections);
    result.setTableOfContents(tableOfContents);
    result.setSpine(new Spine(tableOfContents));
View Full Code Here

      throw new IllegalArgumentException("No index file found in directory " + chmRootDir + ". (Looked for file ending with extension '.hhc'");
    }
    if(inputHtmlEncoding == null) {
      inputHtmlEncoding = DEFAULT_CHM_HTML_INPUT_ENCODING;
    }
    Resources resources = findResources(chmRootDir, inputHtmlEncoding);
    List<TOCReference> tocReferences = HHCParser.parseHhc(hhcFileObject.getContent().getInputStream(), resources);
    result.setTableOfContents(new TableOfContents(tocReferences));
    result.setResources(resources);
    result.generateSpineFromTableOfContents();
    return result;
View Full Code Here

    return null;
  }
 
 
  private static Resources findResources(FileObject rootDir, String inputEncoding) throws IOException {
    Resources result = new Resources();
    FileObject[] allFiles = rootDir.findFiles(new AllFileSelector());
    for(int i = 0; i < allFiles.length; i++) {
      FileObject file = allFiles[i];
      if (file.getType() == FileType.FOLDER) {
        continue;
      }
      MediaType mediaType = MediatypeService.determineMediaType(file.getName().getBaseName());
      if(mediaType == null) {
        continue;
      }
      String href = file.getName().toString().substring(rootDir.getName().toString().length() + 1);
      byte[] resourceData = IOUtils.toByteArray(file.getContent().getInputStream());
      if(mediaType == MediatypeService.XHTML && ! nl.siegmann.epublib.Constants.CHARACTER_ENCODING.equalsIgnoreCase(inputEncoding)) {
        resourceData = ResourceUtil.recode(inputEncoding, nl.siegmann.epublib.Constants.CHARACTER_ENCODING, resourceData);
      }
      Resource fileResource = new Resource(null, resourceData, href, mediaType);
      result.add(fileResource);
    }
    return result;
  }
View Full Code Here

  public void testLoadResources_InputStream() throws FileNotFoundException, IOException {
    // given
    InputStream inputStream = new FileInputStream(new File(testBookFilename));
   
    // when
    Resources resources = ResourcesLoader.loadResources(inputStream, encoding);
   
    // then
    verifyResources(resources);
  }
View Full Code Here

  public void testLoadResources_ZipInputStream() throws FileNotFoundException, IOException {
    // given
    ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(new File(testBookFilename)));
   
    // when
    Resources resources = ResourcesLoader.loadResources(zipInputStream, encoding);
   
    // then
    verifyResources(resources);
  }
View Full Code Here

TOP

Related Classes of nl.siegmann.epublib.domain.Resources

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.