KDP(電子出版)のメモ 急急如律令

Amazon Kindleダイレクト・パブリッシングでの電子出版や電子書籍の作成販売について、文章やイラストの作成や編集方法について書いています。

AozoraEpub3のテンプレートファイルの読み込みzip出力処理

androidの場合はInputStream inputStream = context.getAssets().open(templatePath+fileName); このコードをInputStreamに書き換える

 private void writeFile(ZipArchiveOutputStream zos, String fileName) throws IOException
    {
        zos.putArchiveEntry(new ZipArchiveEntry(fileName));
        //customファイル優先
        File file = new File(templatePath+fileName);
        int idx = fileName.lastIndexOf('/');
        if (idx > 0) {
            File customFile = new File(templatePath+fileName.substring(0, idx)+"_custom/"+fileName.substring(idx+1));
            if (customFile.exists()) file = customFile;
        }
        FileInputStream fis = new FileInputStream(file);
        //IOUtils.copy(fis, zos);
        fis.transferTo(zos);
        fis.close();
        zos.closeArchiveEntry();
    }

カスタムファイルの機能を見送った。

zos.putArchiveEntry(new ZipArchiveEntry(fileName));

// customファイルを優先するInputStreamを取得する
InputStream is = context.getAssets().open(templatePath+fileName);

// データをコピー
is.transferTo(zos);

// ストリームを閉じる
is.close();
zos.closeArchiveEntry();

logタグをnullにしていたらnullにはできないとエラーが出た。 Velocity.evaluate(velocityContext, bw, null, template);

this.templatePath+OPS_PATH+XHTML_PATH+XHTML_HEADER_VMの部分

java.lang.NullPointerException: logTag (i.e. template name) cannot be null, you must provide an identifier for the content being evaluated

/** セクション開始.   */
    void startSection(int lineNum, int pageType, int imagePageType, String srcImageFilePath) throws IOException
    {
        this.sectionIndex++;
        String sectionId = decimalFormat.format(this.sectionIndex);
        //package.opf用にファイル名
        SectionInfo sectionInfo = new SectionInfo(sectionId);
        //次の行が単一画像なら画像専用指定
        switch (imagePageType) {
        case PageBreakType.IMAGE_PAGE_W:
            //幅100%指定
            sectionInfo.setImagePage(true);
            sectionInfo.setImageFitW(true);
            break;
        case PageBreakType.IMAGE_PAGE_H:
            //高さ100%指定
            sectionInfo.setImagePage(true);
            sectionInfo.setImageFitH(true);
            break;
        case PageBreakType.IMAGE_PAGE_NOFIT:
            sectionInfo.setImagePage(true);
            break;
        }
        if (pageType == PageBreakType.PAGE_MIDDLE) sectionInfo.setMiddle(true);
        else if (pageType == PageBreakType.PAGE_BOTTOM) sectionInfo.setBottom(true);
        this.sectionInfos.add(sectionInfo);
        //セクション開始は名称がnullなので改ページ処理で文字列が設定されなければ出力されない 階層レベルは1
        //this.addChapter(null, null, 1);

        this.zos.putArchiveEntry(new ZipArchiveEntry(OPS_PATH+XHTML_PATH+sectionId+".xhtml"));

        //ヘッダ出力
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(zos, StandardCharsets.UTF_8));
        //出力開始するセクションに対応したSectionInfoを設定
        this.velocityContext.put("sectionInfo", sectionInfo);
        //Velocity.mergeTemplate(this.templatePath+OPS_PATH+XHTML_PATH+XHTML_HEADER_VM, "UTF-8", velocityContext, bw);
        String template = inputStreemToString(context.getAssets().open(this.templatePath+OPS_PATH+XHTML_PATH+XHTML_HEADER_VM), StandardCharsets.UTF_8);
        //LogAppender.println(template);
        Velocity.evaluate(velocityContext, bw, "XHTML_HEADER", template);
        bw.flush();
    }

epubcheckがmimetypeエラーだった。バイナリエディターでみても圧縮はされていない。展開するとmimetypeの文字に改行が入っていた。これが原因?

qiita.com EPUBチェックエラーの修正 FileInputStream fisをInputStream isに置き換えて。アンドロイドのアセットの読み込み方にした。 InputStream is = context.getAssets().open(templatePath+MIMETYPE_PATH);

     ZipArchiveEntry mimeTypeEntry = new ZipArchiveEntry(MIMETYPE_PATH);
        FileInputStream fis = new FileInputStream(templatePath+MIMETYPE_PATH);
        byte[] b = new byte[256];
        int len = fis.read(b);
        fis.close();
     ZipArchiveEntry mimeTypeEntry = new ZipArchiveEntry(MIMETYPE_PATH);
        InputStream mime = context.getAssets().open(templatePath+MIMETYPE_PATH);
        FileInputStream fis;
        // mimetype の内容
        byte[] b = new byte[256];
        int len = mime.read(b);
        mime.close();