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))); }
private static Count modify(Element e, Count c) { List<Node> o = e.childNodes(); if (o.size() == 0 && e.textNodes().size() == 0) return new Count(c.getCount(), c.getPgCount()); for (Node n : o) { if (n instanceof TextNode) { TextNode nd = (TextNode) n; String[] arr = nd.text().trim().split("\\s"); String txt = ""; List<Node> nodes = new ArrayList<Node>(); int j = 0; TextNode ndTemp = new TextNode("", " "); nodes.add(j, ndTemp); for (int i = 0; i < arr.length; i++) { if (arr[i].length() > 0) c.incrementCount(); if (c.getCount() > PAGE_COUNT) { ((TextNode) nodes.get(j)).text(((TextNode) nodes.get(j)).text() + " "); j++; nodes.add(j, new Element(Tag.valueOf("pageid=" + c.getPgCount()), "")); j++; nodes.add(j, new TextNode(" " + arr[i] + " ", "")); // "<!--page id="+c.getPgCount()+ "--!>" + " " + arr[i]); // txt = txt + " " + "<!--page id="+c.getPgCount()+ "--!>" + " " + arr[i]; //<div // style='visibility:hidden'>Page="+pageCount+"</div> c.incrementPgCount(); c.setCount(0); } else { // txt = txt + " " + arr[i]; ((TextNode) nodes.get(j)).text(((TextNode) nodes.get(j)).text() + " " + arr[i]); } } if (nodes.size() > 1) { Element etemp = new Element(Tag.valueOf("span"), ""); nd.replaceWith(etemp); for (Node d : nodes) { etemp.appendChild(d); } } // nd.text(ndTemp.text()); } else if (n instanceof Element) { Count ctemp = modify((Element) n, c); c.setCount(ctemp.getCount()); c.setPgCount(ctemp.getPgCount()); } } return c; }