boolean readBook(ZLFile file) { myFilePrefix = MiscUtil.htmlDirectoryPrefix(file); myIdToHref.clear(); myHtmlFileNames.clear(); myParagraphIndexList.clear(); myNCXTOCFileName = null; myTourTOC.clear(); myGuideTOC.clear(); myState = READ_NONE; if (!read(file)) { return false; } myModelReader.setMainTextModel(); myModelReader.pushKind(FBTextKind.REGULAR); int count = 0; for (String name : myHtmlFileNames) { final ZLFile xhtmlFile = ZLFile.createFileByPath(myFilePrefix + name); if (xhtmlFile == null) { // NPE fix: null for bad attributes in .opf XML file return false; } if (count++ == 0 && xhtmlFile.getPath().equals(myCoverFileName)) { continue; } final XHTMLReader reader = new XHTMLReader(myModelReader, myFileNumbers); final String referenceName = reader.getFileAlias(MiscUtil.archiveEntryName(xhtmlFile.getPath())); myModelReader.addHyperlinkLabel(referenceName); myParagraphIndexList.add(myModelReader.Model.BookTextModel.getParagraphsNumber()); myTOCLabels.put(referenceName, myModelReader.Model.BookTextModel.getParagraphsNumber()); reader.readFile(xhtmlFile, referenceName + '#'); myModelReader.insertEndOfSectionParagraph(); } final FBReaderApp fbreader = (FBReaderApp) ZLApplication.Instance(); // fbreader.setTOCLabels(myTOCLabels); fbreader.setParagraphIndexList(myParagraphIndexList); fbreader.setHtmlFileNames(myHtmlFileNames, myFilePrefix, myCoverFileName); generateTOC(); return true; }
@Override public boolean startElementHandler(String tag, ZLStringMap xmlattributes, String[] tagStack) { tag = tag.toLowerCase(); if (myOPFSchemePrefix != null && tag.startsWith(myOPFSchemePrefix)) { tag = tag.substring(myOPFSchemePrefix.length()); } tag = tag.intern(); if (MANIFEST == tag) { myState = READ_MANIFEST; } else if (SPINE == tag) { myNCXTOCFileName = myIdToHref.get(xmlattributes.getValue("toc")); myState = READ_SPINE; } else if (GUIDE == tag) { myState = READ_GUIDE; } else if (TOUR == tag) { myState = READ_TOUR; } else if (myState == READ_MANIFEST && ITEM == tag) { final String id = xmlattributes.getValue("id"); String href = xmlattributes.getValue("href"); if ((id != null) && (href != null)) { href = MiscUtil.decodeHtmlReference(href); myIdToHref.put(id, href); } } else if (myState == READ_SPINE && ITEMREF == tag) { final String id = xmlattributes.getValue("idref"); if (id != null) { final String fileName = myIdToHref.get(id); if (fileName != null) { myHtmlFileNames.add(fileName); } } } else if (myState == READ_GUIDE && REFERENCE == tag) { final String type = xmlattributes.getValue("type"); final String title = xmlattributes.getValue("title"); String href = xmlattributes.getValue("href"); if (href != null) { href = MiscUtil.decodeHtmlReference(href); if (title != null) { myGuideTOC.add(new Reference(title, href)); } if (COVER.equals(type)) { final ZLFile imageFile = ZLFile.createFileByPath(myFilePrefix + href); myCoverFileName = imageFile.getPath(); final String imageName = imageFile.getLongName(); final ZLFileImage image = XHTMLImageFinder.getCoverImage(imageFile); if (image != null) { myModelReader.setMainTextModel(); myModelReader.addImageReference(imageName, (short) 0, true); myModelReader.addImage(imageName, image); myModelReader.insertEndOfSectionParagraph(); } } else if (COVER_IMAGE.equals(type)) { final ZLFile imageFile = ZLFile.createFileByPath(myFilePrefix + href); myCoverFileName = imageFile.getPath(); final String imageName = imageFile.getLongName(); myModelReader.setMainTextModel(); myModelReader.addImageReference(imageName, (short) 0, true); myModelReader.addImage(imageName, new ZLFileImage(MimeType.IMAGE_AUTO, imageFile)); myModelReader.insertEndOfSectionParagraph(); } } } else if (myState == READ_TOUR && SITE == tag) { final String title = xmlattributes.getValue("title"); String href = xmlattributes.getValue("href"); if ((title != null) && (href != null)) { href = MiscUtil.decodeHtmlReference(href); myTourTOC.add(new Reference(title, href)); } } return false; }
private void generateTOC() { if (myNCXTOCFileName != null) { final NCXReader ncxReader = new NCXReader(myModelReader); if (ncxReader.readFile(myFilePrefix + myNCXTOCFileName)) { final Map<Integer, NCXReader.NavPoint> navigationMap = ncxReader.navigationMap(); if (!navigationMap.isEmpty()) { int level = 0; for (NCXReader.NavPoint point : navigationMap.values()) { final BookModel.Label label = getTOCLabel(point.ContentHRef); int index = (label != null) ? label.ParagraphIndex : -1; while (level > point.Level) { myModelReader.endContentsParagraph(); --level; } while (++level <= point.Level) { myModelReader.beginContentsParagraph(-2); myModelReader.addContentsData(Dots); myModelReader.addPathData(Dots); } myModelReader.beginContentsParagraph(index); myModelReader.addContentsData(point.Text.toCharArray()); myModelReader.addPathData(point.ContentHRef.toCharArray()); } while (level > 0) { myModelReader.endContentsParagraph(); --level; } return; } } } for (Reference ref : myTourTOC.isEmpty() ? myGuideTOC : myTourTOC) { final BookModel.Label label = getTOCLabel(ref.HRef); if (label != null) { final int index = label.ParagraphIndex; if (index != -1) { myModelReader.beginContentsParagraph(index); myModelReader.addContentsData(ref.Title.toCharArray()); myModelReader.addPathData(ref.HRef.toCharArray()); myModelReader.endContentsParagraph(); } } } }