use of com.github.hmdev.info.BookInfoHistory in project AozoraEpub3 by hmdev.
the class AozoraEpub3Applet method convertFile.
/** 内部用変換関数 Appletの設定を引数に渡す
* @param srcFile 変換するファイル txt,zip,cbz,(rar,cbr)
* @param dstPath 出力先パス
* @param txtIdx Zip内テキストファイルの位置
*/
private void convertFile(File srcFile, File dstPath, String ext, int txtIdx, boolean imageOnly) {
//パラメータ設定
if (!"txt".equals(ext) && !"txtz".equals(ext) && !"zip".equals(ext) && !"cbz".equals(ext) && !"rar".equals(ext)) {
if (!"png".equals(ext) && !"jpg".equals(ext) && !"jpeg".equals(ext) && !"gif".equals(ext)) {
LogAppender.println("txt, txtz, zip, cbz rar 以外は変換できません");
}
return;
}
//表紙にする挿絵の位置-1なら挿絵は使わない
int coverImageIndex = -1;
//表紙情報追加
String coverFileName = this.jComboCover.getEditor().getItem().toString();
if (coverFileName.equals(this.jComboCover.getItemAt(0).toString())) {
//先頭の挿絵
coverFileName = "";
coverImageIndex = 0;
} else if (coverFileName.equals(this.jComboCover.getItemAt(1).toString())) {
//入力ファイルと同じ名前+.jpg/.png
coverFileName = AozoraEpub3.getSameCoverFileName(srcFile);
} else if (coverFileName.equals(this.jComboCover.getItemAt(2).toString())) {
//表紙無し
coverFileName = null;
}
boolean isFile = "txt".equals(ext);
ImageInfoReader imageInfoReader = new ImageInfoReader(isFile, srcFile);
//zip内の画像をロード
try {
if (!isFile) {
if ("rar".equals(ext)) {
//Rar内の画像情報読み込み 画像のみならファイル順も格納
imageInfoReader.loadRarImageInfos(srcFile, imageOnly);
} else {
//Zip内の画像情報読み込み 画像のみならファイル順も格納
imageInfoReader.loadZipImageInfos(srcFile, imageOnly);
}
}
} catch (Exception e) {
e.printStackTrace();
LogAppender.error(e.getMessage());
}
//BookInfo取得
BookInfo bookInfo = null;
try {
if (!imageOnly) {
//テキストファイルからメタ情報や画像単独ページ情報を取得
bookInfo = AozoraEpub3.getBookInfo(srcFile, ext, txtIdx, imageInfoReader, this.aozoraConverter, this.jComboEncType.getSelectedItem().toString(), BookInfo.TitleType.indexOf(this.jComboTitle.getSelectedIndex()), jCheckPubFirst.isSelected());
}
} catch (Exception e) {
LogAppender.error("ファイルが読み込めませんでした : " + srcFile.getPath());
return;
}
if (this.convertCanceled) {
LogAppender.println("変換処理を中止しました : " + srcFile.getAbsolutePath());
return;
}
Epub3Writer writer = this.epub3Writer;
try {
if (!isFile) {
//Zip内の画像情報をbookInfoに設定
if (imageOnly) {
LogAppender.println("画像のみのePubファイルを生成します");
//画像出力用のBookInfo生成
bookInfo = new BookInfo(srcFile);
bookInfo.imageOnly = true;
//Writerを画像出力用派生クラスに入れ替え
writer = this.epub3ImageWriter;
if (imageInfoReader.countImageFileInfos() == 0) {
LogAppender.error("画像がありませんでした");
return;
}
//名前順で並び替え
imageInfoReader.sortImageFileNames();
//先頭画像をbookInfoに設定しておく
//if (coverImageIndex == 0) {
// bookInfo.coverImage = imageInfoReader.getImage(0);
//}
//画像数をプログレスバーに設定 xhtml出力で+1 画像出力で+10
this.jProgressBar.setMaximum(imageInfoReader.countImageFileInfos() * 11);
jProgressBar.setValue(0);
jProgressBar.setStringPainted(true);
} else {
//画像がなければプレビュー表示しないようにindexを-1に
if (imageInfoReader.countImageFileNames() == 0)
coverImageIndex = -1;
//zipテキストならzip内の注記以外の画像も追加
imageInfoReader.addNoNameImageFileName();
}
}
} catch (Exception e) {
e.printStackTrace();
LogAppender.error(e.getMessage());
}
if (bookInfo == null) {
LogAppender.error("書籍の情報が取得できませんでした");
return;
}
//テキストなら行数/100と画像数をプログレスバーに設定
if (bookInfo.totalLineNum > 0) {
if (isFile)
this.jProgressBar.setMaximum(bookInfo.totalLineNum / 10 + imageInfoReader.countImageFileNames() * 10);
else
this.jProgressBar.setMaximum(bookInfo.totalLineNum / 10 + imageInfoReader.countImageFileInfos() * 10);
jProgressBar.setValue(0);
jProgressBar.setStringPainted(true);
}
//表紙目次ページ出力設定
bookInfo.insertCoverPage = this.jCheckCoverPage.isSelected();
bookInfo.insertTocPage = this.jCheckTocPage.isSelected();
bookInfo.insertCoverPageToc = this.jCheckCoverPageToc.isSelected();
bookInfo.insertTitleToc = this.jCheckTitleToc.isSelected();
//表題の見出しが非表示で行が追加されていたら削除
if (!bookInfo.insertTitleToc && bookInfo.titleLine >= 0) {
bookInfo.removeChapterLineInfo(bookInfo.titleLine);
}
//目次縦書き
bookInfo.setTocVertical(this.jRadioTocV.isSelected());
//縦書き横書き設定追加
bookInfo.vertical = this.jRadioVertical.isSelected();
this.aozoraConverter.vertical = bookInfo.vertical;
//表題左右中央
if (!this.jCheckTitlePage.isSelected()) {
bookInfo.titlePageType = BookInfo.TITLE_NONE;
} else if (this.jRadioTitleNormal.isSelected()) {
bookInfo.titlePageType = BookInfo.TITLE_NORMAL;
} else if (this.jRadioTitleMiddle.isSelected()) {
bookInfo.titlePageType = BookInfo.TITLE_MIDDLE;
} else if (this.jRadioTitleHorizontal.isSelected()) {
bookInfo.titlePageType = BookInfo.TITLE_HORIZONTAL;
}
//先頭からの場合で指定行数以降なら表紙無し
if ("".equals(coverFileName) && !imageOnly) {
try {
int maxCoverLine = Integer.parseInt(this.jTextMaxCoverLine.getText());
if (maxCoverLine > 0 && (bookInfo.firstImageLineNum == -1 || bookInfo.firstImageLineNum >= maxCoverLine)) {
coverImageIndex = -1;
coverFileName = null;
} else {
coverImageIndex = bookInfo.firstImageIdx;
}
} catch (Exception e) {
}
}
//表紙ページの情報をbookInfoに設定
bookInfo.coverFileName = coverFileName;
bookInfo.coverImageIndex = coverImageIndex;
String[] titleCreator = BookInfo.getFileTitleCreator(srcFile.getName());
if (jCheckUseFileName.isSelected()) {
//ファイル名優先ならテキスト側の情報は不要
bookInfo.title = "";
bookInfo.creator = "";
if (titleCreator[0] != null)
bookInfo.title = titleCreator[0];
if (titleCreator[1] != null)
bookInfo.creator = titleCreator[1];
} else {
//テキストから取得できなければファイル名を利用
if (bookInfo.title == null || bookInfo.title.length() == 0) {
bookInfo.title = titleCreator[0] == null ? "" : titleCreator[0];
if (bookInfo.creator == null || bookInfo.creator.length() == 0)
bookInfo.creator = titleCreator[1] == null ? "" : titleCreator[1];
}
}
if (this.convertCanceled) {
LogAppender.println("変換処理を中止しました : " + srcFile.getAbsolutePath());
return;
}
//前回の変換設定を反映
BookInfoHistory history = this.getBookInfoHistory(bookInfo);
if (history != null) {
if (bookInfo.title.length() == 0)
bookInfo.title = history.title;
bookInfo.titleAs = history.titleAs;
if (bookInfo.creator.length() == 0)
bookInfo.creator = history.creator;
bookInfo.creatorAs = history.creatorAs;
if (bookInfo.publisher == null)
bookInfo.publisher = history.publisher;
//表紙設定
if (jCheckCoverHistory.isSelected()) {
bookInfo.coverEditInfo = history.coverEditInfo;
bookInfo.coverFileName = history.coverFileName;
bookInfo.coverExt = history.coverExt;
bookInfo.coverImageIndex = history.coverImageIndex;
//確認ダイアログ表示しない場合はイメージを生成
if (!this.jCheckConfirm.isSelected() && bookInfo.coverEditInfo != null) {
try {
this.jConfirmDialog.jCoverImagePanel.setBookInfo(bookInfo);
if (bookInfo.coverImageIndex >= 0 && bookInfo.coverImageIndex < imageInfoReader.countImageFileNames()) {
bookInfo.coverImage = imageInfoReader.getImage(bookInfo.coverImageIndex);
} else if (bookInfo.coverImage == null && bookInfo.coverFileName != null) {
bookInfo.loadCoverImage(bookInfo.coverFileName);
}
bookInfo.coverImage = this.jConfirmDialog.jCoverImagePanel.getModifiedImage(this.coverW, this.coverH);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
String outExt = this.jComboExt.getEditor().getItem().toString().trim();
////////////////////////////////
//Kindleチェック
File kindlegen = null;
writer.setIsKindle(false);
if (outExt.startsWith(".mobi")) {
kindlegen = new File(this.jarPath + "kindlegen.exe");
if (!kindlegen.isFile()) {
kindlegen = new File(this.jarPath + "kindlegen");
if (!kindlegen.isFile()) {
kindlegen = null;
}
}
if (kindlegen == null) {
JOptionPane.showMessageDialog(this, "kindlegenがありません\nkindlegen.exeをjarファイルの場所にコピーしてください", "kindlegenエラー", JOptionPane.WARNING_MESSAGE);
LogAppender.println("変換処理をキャンセルしました");
return;
}
writer.setIsKindle(true);
}
//確認ダイアログ 変換ボタン押下時にbookInfo更新
if (this.jCheckConfirm.isSelected()) {
//表題と著者設定 ファイル名から設定
String title = "";
String creator = "";
if (bookInfo.title != null)
title = bookInfo.title;
if (bookInfo.creator != null)
creator = bookInfo.creator;
this.jConfirmDialog.setChapterCheck(jCheckChapterSection.isSelected(), jCheckChapterH.isSelected(), jCheckChapterH1.isSelected(), jCheckChapterH2.isSelected(), jCheckChapterH3.isSelected(), jCheckChapterName.isSelected(), jCheckChapterNumOnly.isSelected() || jCheckChapterNumTitle.isSelected() || jCheckChapterNumParen.isSelected() || jCheckChapterNumParenTitle.isSelected(), jCheckChapterPattern.isSelected());
this.jConfirmDialog.showDialog(srcFile, (dstPath != null ? dstPath.getAbsolutePath() : srcFile.getParentFile().getAbsolutePath()) + File.separator, title, creator, this.jComboTitle.getSelectedIndex(), jCheckPubFirst.isSelected(), bookInfo, imageInfoReader, this.jFrameParent.getLocation(), coverW, coverH);
//ダイアログが閉じた後に再開
if (this.jConfirmDialog.canceled) {
this.convertCanceled = true;
LogAppender.println("変換処理を中止しました : " + srcFile.getAbsolutePath());
return;
}
if (this.jConfirmDialog.skipped) {
this.setBookInfoHistory(bookInfo);
LogAppender.println("変換をスキップしました : " + srcFile.getAbsolutePath());
return;
}
//変換前確認のチェックを反映
if (!this.jConfirmDialog.jCheckConfirm2.isSelected())
jCheckConfirm.setSelected(false);
//確認ダイアログの値をBookInfoに設定
bookInfo.title = this.jConfirmDialog.getMetaTitle();
bookInfo.creator = this.jConfirmDialog.getMetaCreator();
bookInfo.titleAs = this.jConfirmDialog.getMetaTitleAs();
bookInfo.creatorAs = this.jConfirmDialog.getMetaCreatorAs();
bookInfo.publisher = this.jConfirmDialog.getMetaPublisher();
//著者が空欄なら著者行もクリア
if (bookInfo.creator.length() == 0)
bookInfo.creatorLine = -1;
//プレビューでトリミングされていたらbookInfo.coverImageにBufferedImageを設定 それ以外はnullにする
BufferedImage coverImage = this.jConfirmDialog.jCoverImagePanel.getModifiedImage(this.coverW, this.coverH);
if (coverImage != null) {
//Epub3Writerでイメージを出力
bookInfo.coverImage = coverImage;
//元の表紙は残す
if (this.jConfirmDialog.jCheckReplaceCover.isSelected())
bookInfo.coverImageIndex = -1;
} else {
bookInfo.coverImage = null;
}
this.setBookInfoHistory(bookInfo);
} else {
//表題の見出しが非表示で行が追加されていたら削除
if (!bookInfo.insertTitleToc && bookInfo.titleLine >= 0) {
bookInfo.removeChapterLineInfo(bookInfo.titleLine);
}
}
boolean autoFileName = this.jCheckAutoFileName.isSelected();
boolean overWrite = this.jCheckOverWrite.isSelected();
//出力ファイル
File outFile = null;
//Kindleは一旦tmpファイルに出力
File outFileOrg = null;
if (kindlegen != null) {
outFile = AozoraEpub3.getOutFile(srcFile, dstPath, bookInfo, autoFileName, ".epub");
File mobiFile = new File(outFile.getAbsolutePath().substring(0, outFile.getAbsolutePath().length() - 4) + "mobi");
if (!overWrite && (mobiFile.exists() || (outExt.endsWith(".epub") && outFile.exists()))) {
LogAppender.println("変換中止: " + srcFile.getAbsolutePath());
if (mobiFile.exists())
LogAppender.println("ファイルが存在します: " + mobiFile.getAbsolutePath());
else
LogAppender.println("ファイルが存在します: " + outFile.getAbsolutePath());
return;
}
outFileOrg = outFile;
try {
outFile = File.createTempFile("kindle", ".epub", outFile.getParentFile());
if (!outExt.endsWith(".epub"))
outFile.deleteOnExit();
} catch (IOException e) {
outFile = outFileOrg;
outFileOrg = null;
}
} else {
outFile = AozoraEpub3.getOutFile(srcFile, dstPath, bookInfo, autoFileName, outExt);
//上書き確認
if (!overWrite && outFile.exists()) {
LogAppender.println("変換中止: " + srcFile.getAbsolutePath());
LogAppender.println("ファイルが存在します: " + outFile.getAbsolutePath());
return;
}
}
/*
if (overWrite && outFile.exists()) {
int ret = JOptionPane.showConfirmDialog(this, "ファイルが存在します\n上書きしますか?\n(取り消しで変換キャンセル)", "上書き確認", JOptionPane.YES_NO_CANCEL_OPTION);
if (ret == JOptionPane.NO_OPTION) {
LogAppender.println("変換中止: "+srcFile.getAbsolutePath());
return;
} else if (ret == JOptionPane.CANCEL_OPTION) {
LogAppender.println("変換中止: "+srcFile.getAbsolutePath());
convertCanceled = true;
LogAppender.println("変換処理をキャンセルしました");
return;
}
}*/
////////////////////////////////
//変換実行
AozoraEpub3.convertFile(srcFile, ext, outFile, this.aozoraConverter, writer, this.jComboEncType.getSelectedItem().toString(), bookInfo, imageInfoReader, txtIdx);
imageInfoReader = null;
//画像は除去
bookInfo.coverImage = null;
//変換中にキャンセルされた場合
if (this.convertCanceled) {
LogAppender.println("変換処理を中止しました : " + srcFile.getAbsolutePath());
return;
}
//kindlegen.exeがあれば実行
try {
if (kindlegen != null) {
long time = System.currentTimeMillis();
String outFileName = outFile.getAbsolutePath();
LogAppender.println("kindlegenを実行します : " + kindlegen.getName() + " \"" + outFileName + "\"");
ProcessBuilder pb = new ProcessBuilder(kindlegen.getAbsolutePath(), "-locale", "en", "-verbose", outFileName);
this.kindleProcess = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(this.kindleProcess.getInputStream()));
String line;
int idx = 0;
int cnt = 0;
String msg = "";
while ((line = br.readLine()) != null) {
if (line.length() > 0) {
System.out.println(line);
if (msg.startsWith("Error"))
msg += line;
else
msg = line;
if (idx++ % 2 == 0) {
if (cnt++ > 100) {
cnt = 1;
LogAppender.println();
}
LogAppender.append(".");
}
}
}
br.close();
if (convertCanceled) {
LogAppender.println("\n" + msg + "\nkindlegenの変換を中断しました");
} else {
if (outFileOrg != null) {
//mobiリネーム
File mobiTmpFile = new File(outFile.getAbsolutePath().substring(0, outFile.getAbsolutePath().length() - 4) + "mobi");
File mobiFile = new File(outFileOrg.getAbsolutePath().substring(0, outFileOrg.getAbsolutePath().length() - 4) + "mobi");
if (mobiFile.exists())
mobiFile.delete();
mobiTmpFile.renameTo(mobiFile);
if (outExt.endsWith(".epub")) {
//epubリネーム
if (outFileOrg.exists())
outFileOrg.delete();
outFile.renameTo(outFileOrg);
} else {
outFile.delete();
}
LogAppender.println("\n" + msg + "\nkindlegen変換完了 [" + (((System.currentTimeMillis() - time) / 100) / 10f) + "s] -> " + mobiFile.getName());
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (this.kindleProcess != null)
this.kindleProcess.destroy();
this.kindleProcess = null;
}
}
use of com.github.hmdev.info.BookInfoHistory in project AozoraEpub3 by hmdev.
the class AozoraEpub3Applet method setBookInfoHistory.
void setBookInfoHistory(BookInfo bookInfo) {
String key = bookInfo.srcFile.getAbsolutePath();
if (bookInfo.textEntryName != null)
key += "/" + bookInfo.textEntryName;
mapBookInfoHistory.put(key, new BookInfoHistory(bookInfo));
}
Aggregations