Beispiel #1
0
 @Override
 public void done() {
   Toolkit.getDefaultToolkit().beep();
   startButton.setEnabled(true);
   setCursor(null);
   EpubUtils.outputMessage(taskOutput, "Done");
 }
Beispiel #2
0
 private boolean addId2Map(String id, String fileName) {
   if (ids.containsKey(id)) {
     EpubUtils.outputMessage(taskOutput, "Id'et " + id + " findes flere gange i dokumentet");
     return false;
   }
   ids.put(id, fileName);
   return true;
 }
Beispiel #3
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;
  }
Beispiel #4
0
  private boolean saveDocs() {
    try {
      // save split documents
      for (Map.Entry<String, Document> entry : docList.entrySet()) {
        if (!EpubUtils.saveDocument(
            entry.getValue(),
            new File(EpubUtils.EPUB_FOLDER + File.separator + entry.getKey()),
            taskOutput)) {
          return false;
        }
      }
      return true;
    } catch (Exception e) {
      EpubUtils.outputMessage(
          taskOutput, "Could not save documents. An Exception occurred: " + e.getMessage());
    }

    return false;
  }
Beispiel #5
0
  private boolean mapRefs() {
    try {
      EpubUtils.outputMessage(taskOutput, "Editing references");

      for (Map.Entry<String, Document> entry : docList.entrySet()) {
        NodeList refNodes = null;
        Document doc = entry.getValue();

        // Get references
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        try {
          refNodes =
              (NodeList) xpath.evaluate("//*[local-name() = 'a']", doc, XPathConstants.NODESET);
        } catch (Exception e) {
          EpubUtils.outputMessage(taskOutput, "Error in Xml document: " + e.getMessage());
          return false;
        }

        // Check referencer mod samlingen af Id'er
        for (int i = 0; i < refNodes.getLength(); i++) {
          Node ref = refNodes.item(i);
          String href = getAttributeFromNode(ref, "href");
          href = href.replace("#", "");

          String fileName = GetReferenceFileName(href);
          if (fileName != "") {
            // Change reference
            Attr attHref = doc.createAttribute("href");
            attHref.setValue(fileName + "#" + href);
            NamedNodeMap bodyAttrs = ref.getAttributes();
            bodyAttrs.setNamedItem(attHref);
          }
        }
      }
      return true;
    } catch (Exception e) {
      EpubUtils.outputMessage(
          taskOutput, "Could not edit references. An Exception occurred: " + e.getMessage());
    }

    return false;
  }
Beispiel #6
0
  public static void main(String[] args) {
    EpubUtils.EPUB = new File(args[0]);
    EpubUtils.EPUB_FOLDER = args[1];
    EpubUtils.prepare("splitter", "split");

    javax.swing.SwingUtilities.invokeLater(
        new Runnable() {
          public void run() {
            createAndShowGUI();
          }
        });
  }
Beispiel #7
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;
    }