Exemplo n.º 1
0
  private boolean splitConcatDocument(File concatFile) {
    try {
      EpubUtils.outputMessage(taskOutput, "Splitting concat document");

      // create concat document
      Document sourceDoc = EpubUtils.createDocument(concatFile, taskOutput);
      if (sourceDoc == null) {
        return false;
      }

      // Find Source body
      XPathFactory factory = XPathFactory.newInstance();
      XPath xpath = factory.newXPath();

      Node sourceBody =
          (Node)
              xpath.evaluate(
                  "//*[local-name() = 'body']",
                  sourceDoc.getDocumentElement(),
                  XPathConstants.NODE);
      NodeList bodyNodes = sourceBody.getChildNodes();

      for (int i = 0; i < bodyNodes.getLength(); i++) {
        Node sectionNodeAtLevel1 = bodyNodes.item(i);
        if (sectionNodeAtLevel1.getNodeType() == Node.ELEMENT_NODE) {
          createNewEpubDoc(sectionNodeAtLevel1);
        }
      }
      return true;
    } catch (XPathExpressionException xpee) {
      EpubUtils.outputMessage(
          taskOutput,
          "Could not split concat document. An XPathExpressionException occurred: "
              + xpee.getMessage());
    } catch (AuthorOperationException aoe) {
      EpubUtils.outputMessage(
          taskOutput,
          "Could not split concat document. An AuthorOperationException occurred: "
              + aoe.getMessage());
    } catch (Exception e) {
      EpubUtils.outputMessage(
          taskOutput, "Could not split concat document. An Exception occurred: " + e.getMessage());
    }

    return false;
  }
Exemplo n.º 2
0
    @Override
    public Void doInBackground() {
      if (!EpubUtils.start(taskOutput)) return null;

      if (!EpubUtils.unzip(taskOutput)) return null;

      if (!EpubUtils.canSplit(taskOutput)) return null;

      if (!EpubUtils.backup(taskOutput)) return null;

      EpubUtils.outputProcess("PREPARING AND PARSING", true, taskOutput);

      // create package handler instance
      packageHandler = new PackageHandler();
      if (!EpubUtils.parseFile(
          new File(EpubUtils.EPUB_FOLDER + File.separator + EpubUtils.PACKAGE_FILENAME),
          packageHandler,
          taskOutput)) return null;

      docNumber = 0;
      docList = new TreeMap<String, Document>();
      ids = new HashMap<String, String>();

      // get concat xhtml file from extracted zip file
      listOfFiles = EpubUtils.getFiles(true, false);

      // create split handler instance
      splitHandler = new SplitHandler();

      // prepare source file
      if (!EpubUtils.prepareFile(listOfFiles[0], taskOutput)) return null;

      // parse source file
      if (!EpubUtils.parseFile(listOfFiles[0], splitHandler, taskOutput)) return null;

      if (splitHandler.getMetaNodes().containsKey("dc:identifier")) {
        idPrefix = splitHandler.getMetaNodes().get("dc:identifier");
      }

      EpubUtils.outputProcess("BUILDING SPLIT DOCUMENTS", true, taskOutput);

      if (!splitConcatDocument(listOfFiles[0])) return null;

      if (!mapRefs()) return null;

      if (!saveDocs()) return null;

      EpubUtils.outputProcess("MODIFYING PACKAGE DOCUMENT", true, taskOutput);

      listOfFiles = EpubUtils.getFiles(false, true);
      if (listOfFiles.length == 0) {
        EpubUtils.outputMessage(taskOutput, "No split files found");
        return null;
      } else if (listOfFiles.length == 1) {
        EpubUtils.outputMessage(
            taskOutput, "Only one file found - concat file have not been splitted correctly");
        return null;
      }

      Document packageDoc =
          EpubUtils.createDocument(
              new File(EpubUtils.EPUB_FOLDER + File.separator + EpubUtils.PACKAGE_FILENAME),
              taskOutput);
      if (packageDoc == null) {
        return null;
      }

      for (int i = 0; i < listOfFiles.length; i++) {
        // add split document to opf document
        if (!EpubUtils.addOpfItem(packageDoc, listOfFiles[i].getName(), i + 1, taskOutput))
          return null;
      }

      // remove concat document from opf document
      if (!EpubUtils.removeOpfItem(packageDoc, EpubUtils.CONCAT_FILENAME, taskOutput)) return null;

      // remove fallback from non xhtml spine elements
      if (!EpubUtils.removeFallbackFromOpf(packageDoc, taskOutput)) return null;

      // save opf document
      if (!EpubUtils.saveDocument(
          packageDoc,
          new File(EpubUtils.EPUB_FOLDER + File.separator + EpubUtils.PACKAGE_FILENAME),
          taskOutput)) return null;

      EpubUtils.outputProcess("MODIFYING EPUB", true, taskOutput);

      // obtain the global configuration
      TConfig config = TConfig.get();
      config.setArchiveDetector(
          new TArchiveDetector("epub", new JarDriver(IOPoolLocator.SINGLETON)));

      // get epub file destination
      String epubPath = EpubUtils.EPUB.getPath();
      String epubFolder =
          EpubUtils.EPUB_FOLDER
              .substring(EpubUtils.EPUB_FOLDER.lastIndexOf(File.separator))
              .replace(File.separator, "");
      TFile destination = new TFile(epubPath + File.separator + epubFolder);

      // modify epub file destination
      for (int i = 0; i < listOfFiles.length; i++) {
        if (!EpubUtils.addFileToEpub(new TFile(listOfFiles[i]), destination, taskOutput))
          return null;
      }

      if (!EpubUtils.addFileToEpub(
          new TFile(EpubUtils.EPUB_FOLDER + File.separator + EpubUtils.PACKAGE_FILENAME),
          destination,
          taskOutput)) return null;

      if (!EpubUtils.removeFileFromEpub(
          new TFile(destination, EpubUtils.CONCAT_FILENAME), taskOutput)) return null;

      // commit changes to epub file destination
      if (!EpubUtils.commitChanges(taskOutput)) return null;

      if (!EpubUtils.finish(taskOutput)) return null;

      return null;
    }