Esempio n. 1
0
  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;
  }
Esempio n. 2
0
 @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;
 }