树数据结构的实际应用

电子书目录读取

由来

​ 在epub2规范生成的epub电子书中负责目录导航的文件toc.nxc文件,但是在epub3规范生成的电子书中负责目录导航的文件是toc.xhtml文件。公司的移动端的SDK并不支持toc.xhtml文件的电子书目录导航。我这边需要将toc.xhtml文件转换成toc.ncx文件。

工具准备

  1. 读取电子书文档的jar包 (epublib-core)
  2. 对html文档进行解析操作的jar (Jsoup)

导入maven依赖

        <!-- https://mvnrepository.com/artifact/nl.siegmann.epublib/epublib-core -->
        <dependency>
        <groupId>nl.siegmann.epublib</groupId>
        <artifactId>epublib-core</artifactId>
        <version>3.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/net.sf.kxml/kxml2 -->
        <dependency>
            <groupId>net.sf.kxml</groupId>
            <artifactId>kxml2</artifactId>
            <version>2.3.0</version>
        </dependency>
        
        
        <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.8.3</version>
        </dependency>

代码思路

  1. 目录结构抽象成Jave中的数据模型。很明显是树数据结构
  2. 利用递归进行数据解析

代码实现

主要核心代码如下:

        // 读取电子书
        Book book = epubReader.readEpub(inputStream);
 // 获取电子书目录
        TableOfContents tableOfContents = book.getTableOfContents();
           if (tableOfContents != null && tableOfContents.size() <=0 && !book.getNcxResource().getHref().contains("toc.ncx") ) {
            String data = new String(book.getResources().getById("ncx").getData(), "UTF-8");
            // 用Jsoup 生成文档格式
            Document document = Jsoup.parse(data);
            List<TOCReference> tocReferenceList = new ArrayList<>();
            for (String resourceId : resourceIds) {
                // 获取目录结构
                Element element = document.select("nav#toc").select("a[href=" + resourceId + ".xhtml]").parents().first();
                Elements elements = new Elements(element);
                // 获取该章下得所有目录
                List<TOCReference> referenceList = EbookCatalogUtil.getChapterCalog(elements, book.getResources().getById(resourceId));
                tocReferenceList.add(referenceList.get(0));

            }
            // 给目录添加属性
            tableOfContents.setTocReferences(tocReferenceList);
        }
        

EbookCatalogUtil工具类:

 /**
     * @Author ouyangkang
     * @Description 获取章节目录
     * @Date 10:24 2018/11/2
     * @param elements
     * @return java.util.List<com.zhihuishu.ebook.util.EbookCatalogUtil.ChapterCatalog>
    **/
    public static List<TOCReference> getChapterCalog(Elements elements, Resource resource){
        List<TOCReference> list = new LinkedList<>();
        for (Element element : elements){
            // 获取该章节ID
            String resourceId = element.attributes().get("id");
            // 获取该章节名字
            String name = element.select("#"+resourceId+"> a").text();
            // 获取孩子 element
            Elements elementsTemp = element.select("#" + resourceId + " > ol > li");

            TOCReference tocReference = new TOCReference();

            tocReference.setTitle(name);

            tocReference.setResource(resource);

            tocReference.setFragmentId(UUID.randomUUID().toString());
            //递归调用设置孩子节点
            tocReference.setChildren(getChapterCalog(elementsTemp, resource));

            list.add(tocReference);
        }
        return list;

    }

转载于:https://www.cnblogs.com/Krloypower/p/9896789.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值