001/* 002 * Copyright (c) 2009 The openGion Project. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 013 * either express or implied. See the License for the specific language 014 * governing permissions and limitations under the License. 015 */ 016package org.opengion.hayabusa.taglib; 017 018import static org.opengion.fukurou.util.StringUtil.nval; 019 020import java.io.File; 021 022import org.opengion.fukurou.util.StringUtil; 023import org.opengion.fukurou.util.ToString; // 6.1.1.0 (2015/01/17) 024import org.opengion.hayabusa.common.HybsSystem; 025import org.opengion.hayabusa.report2.DocConverter_OOO; 026// import org.opengion.hayabusa.io.HybsFileOperationFactory; // 8.0.0.1 (2021/10/08) , 8.0.1.0 (2021/10/29) 廃止 027// import org.opengion.fukurou.model.FileOperation; // 8.0.0.1 (2021/10/08) 028// import org.opengion.fukurou.util.FileUtil; // 8.0.0.1 (2021/10/08) 029 030/** 031 * ドキュメントの変換・マージを行うタグです。 032 * 033 * 変換を行うことのできる入出力のフォーマット以下の通りです。 034 * 035 * [対応フォーマット] 036 * 入力[Calc(ods) ,Excel(xls) ] ⇒ 出力[Calc(ods) ,Excel(xls,xlsx) ,PDF] 037 * 入力[Writer(odt) ,Word(doc) ] ⇒ 出力[Writer(odt) ,Word(doc,docx) ,PDF] 038 * 入力[Impress(odp),PowerPoint(ppt)] ⇒ 出力[Impress(odp),PowerPoint(ppt,pptx) ,PDF] 039 * 040 * 入出力のフォーマットは、入出力ファイルの拡張子により自動判別されます。 041 * また、入出力ファイル名が同じ場合は何も処理されません。 042 * 043 * 入力ファイルを、CSV形式で複数指定することで、複数の入力ファイルをマージして 044 * 出力することもできます。 045 * 046 * ※2 現状は、ファイルのマージは、入力ファイルがExcelまたはCalcの場合のみ対応しています。 047 * ※1 この機能を利用するには、OpenOfficeが正しく設定されている必要があります。 048 * 049 * @og.formSample 050 * ●形式:<og:docConvert fileURL="…" inputFile="…" outputFile="…" /> 051 * ●body:なし 052 * 053 * ●Tag定義: 054 * <og:docConvert 055 * fileURL 【TAG】操作するファイルのディレクトリを指定します (初期値:FILE_URL[=filetemp/]) 056 * inputFile ○【TAG】入力ファイル名を指定します(必須)。 057 * outputFile ○【TAG】出力ファイル名を指定します(必須)。 058 * delInput 【TAG】ドキュメント変換後、元のファイルを削除するかどうかを指定します(初期値:false[=削除しない]) 059 * debug 【TAG】デバッグ情報を出力するかどうか[true/false]を指定します(初期値:false) 060 * /> 061 * 062 * ●使用例 063 * ・Calc(ods)ファイルをPDFに変換 064 * <og:docConvert inputFile="temp.ods" outputFile="out.pdf" /> 065 * 066 * ・Excel(xls)ファイルを連結 067 * <og:docConvert inputFile="temp1.xls,temp2.xls" outputFile="out.xls" /> 068 * 069 * @og.group その他部品 070 * 071 * @version 4.0 072 * @author Hiroki Nakamura 073 * @since JDK5.0, 074 */ 075public class DocConvertTag extends CommonTagSupport { 076 /** このプログラムのVERSION文字列を設定します。 {@value} */ 077 private static final String VERSION = "8.0.1.0 (2021/10/29)" ; 078 private static final long serialVersionUID = 801020211029L ; 079 080 private String fileURL = HybsSystem.sys( "FILE_URL" ); 081 private String[]inputFile ; 082 private String outputFile ; 083 private boolean delInput ; 084 085 /** 086 * デフォルトコンストラクター 087 * 088 * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor. 089 */ 090 public DocConvertTag() { super(); } // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。 091 092 /** 093 * Taglibの開始タグが見つかったときに処理する doStartTag() を オーバーライドします。 094 * 095 * @return 後続処理の指示( SKIP_BODY ) 096 */ 097 @Override 098 public int doStartTag() { 099 return SKIP_BODY ; 100 } 101 102 /** 103 * Taglibの終了タグが見つかったときに処理する doEndTag() を オーバーライドします。 104 * 105 * @og.rev 8.0.0.2 (2021/10/15) ローカルファイルとクラウドファイル間の移動 106 * 107 * @return 後続処理の指示 108 */ 109 @Override 110 public int doEndTag() { 111 debugPrint(); 112 113 // 出力ファイルで拡張子だけが指定された場合は、入力ファイル名を付加する 114 if( outputFile.indexOf( '.' ) < 0 ) { 115 final int inSufIdx = inputFile[0].lastIndexOf( '.' ); 116 if( inSufIdx >= 0 ) { 117 outputFile = inputFile[0].substring( 0, inSufIdx ) + "." + outputFile; 118 } 119 } 120 121 final String directory = HybsSystem.url2dir( fileURL ); 122 String[] input = new String[inputFile.length]; 123 for( int i=0; i<input.length; i++ ) { 124 input[i] = StringUtil.urlAppend( directory,inputFile[i] ); 125 } 126 final String output = StringUtil.urlAppend( directory,outputFile ); 127 128// // 8.0.1.0 (2021/10/29) 廃止 129// // 8.0.0.2 (2021/10/15) ローカルファイルとクラウドファイル間の移動 130// if( !useLocal && HybsFileOperationFactory.useCloud() ) { 131// // 5.10.9.0 (2019/03/01) ADD クラウドストレージ指定の場合、ファイルをローカルにダウンロードします。 132// for( final String in: input ) { 133// HybsFileOperationFactory.cloud2local( false,() -> new File( in ) ); // false:システム定義のクラウド設定に従う。 134// } 135// } 136// final FileOperation inFile = HybsFileOperationFactory.create( input[0] ); 137// // 5.10.9.0 (2019/03/01) ADD クラウドストレージ指定の場合、ファイルをローカルにダウンロードします。 138// if( inFile.isCloud() ) { 139// for( final String in: input ) { 140// final File fromFile = HybsFileOperationFactory.create( in ); 141// final File toFile = new File( in ); 142// toFile.mkdirs(); // toFileのディレクトリを作成する必要あり? 143// FileUtil.copy( fromFile, toFile ); 144// } 145// } 146 147 // ドキュメント変換 148 DocConverter_OOO.convert( input, output ); 149 150// // 8.0.1.0 (2021/10/29) 廃止 151// // 8.0.0.1 (2021/10/08) cloud対応 152// // 5.10.9.0 (2019/03/01) ADD クラウドストレージ指定の場合、ファイルをクラウドストレージにアップロードします。 153// // 8.0.0.2 (2021/10/15) ローカルファイルとクラウドファイル間の移動 154// HybsFileOperationFactory.local2cloud( useLocal,() -> new File( output ) ); 155// // final FileOperation outFile = HybsFileOperationFactory.create( output ); 156// // if( outFile.isCloud() ) { 157// // final File localFile = new File( output ); 158// // FileUtil.copy( localFile, outFile ); 159// // localFile.delete(); 160// // } 161 162 if( delInput ) { 163 for( int i=0; i<input.length; i++ ) { 164 if( !( new File( input[i] ) ).delete() ) { 165 System.err.println( "入力ファイルの削除に失敗しました。file=[" + input[i] + "]" ); 166 } 167 } 168 } 169 170 return EVAL_PAGE ; 171 } 172 173 /** 174 * タグリブオブジェクトをリリースします。 175 * キャッシュされて再利用されるので、フィールドの初期設定を行います。 176 * 177 */ 178 @Override 179 protected void release2() { 180 super.release2(); 181 fileURL = HybsSystem.sys( "FILE_URL" ); 182 inputFile = null; 183 outputFile = null; 184 delInput = false; 185 } 186 187 /** 188 * 【TAG】操作するファイルのディレクトリを指定します 189 * (初期値:FILE_URL[={@og.value SystemData#FILE_URL}])。 190 * 191 * @og.tag 192 * この属性で指定されるディレクトリのファイルを操作します。 193 * 指定方法は、通常の fileURL 属性と同様に、先頭が、'/' (UNIX) または、2文字目が、 194 * ":" (Windows)の場合は、指定のURLそのままのディレクトリに、そうでない場合は、 195 * (初期値:システム定数のFILE_URL[={@og.value SystemData#FILE_URL}])。 196 * 197 * @og.rev 6.4.2.1 (2016/02/05) URLの最後に、"/" を追加する処理を廃止。 198 * 199 * @param url ファイルURL 200 * @see org.opengion.hayabusa.common.SystemData#FILE_URL 201 */ 202 public void setFileURL( final String url ) { 203 final String furl = nval( getRequestParameter( url ),null ); 204 if( furl != null ) { 205 fileURL = StringUtil.urlAppend( fileURL,furl ); 206 } 207 } 208 209 /** 210 * 【TAG】入力ファイル名を指定します。 211 * 212 * @og.tag 213 * 入力ファイル名を指定します。 214 * 215 * 入力ファイルは、CSV形式で複数指定することができます。 216 * この場合、複数の入力ファイルをマージして出力を行います。 217 * 218 * ※現状は、ファイルのマージは、入力ファイルがExcelまたはCalcの場合のみ対応しています。 219 * また、マージを行う場合、印刷範囲など、ドキュメントに関連する情報は、1つ目のファイルの 220 * 情報が使用されます。 221 * 222 * @param fname 入力ファイル名 223 */ 224 public void setInputFile( final String fname ) { 225 inputFile = getCSVParameter( fname ); 226 } 227 228 /** 229 * 【TAG】出力ファイル名を指定します。 230 * 231 * @og.tag 232 * 出力ファイル名を指定します。 233 * 出力ファイルには、拡張子のみ(xls,ods等)を指定することもできます。 234 * この場合、出力ファイル名は、入力ファイル名と同じになります。(拡張子のみが変換される) 235 * 236 * @param fname 出力ファイル名 237 */ 238 public void setOutputFile( final String fname ) { 239 outputFile = nval( getRequestParameter( fname ),outputFile ); 240 } 241 242 /** 243 * 【TAG】ドキュメント変換後、元のファイルを削除するかどうかを指定します(初期値:false[=削除しない])。 244 * 245 * @og.tag 246 * ドキュメント変換後、元のファイルを削除するかどうかを指定します。 247 * 初期値は、false(削除しない)です。 248 * 249 * @param flag 元ファイルの削除 [true:する/false:しない] 250 */ 251 public void setDelInput( final String flag ) { 252 delInput = nval( getRequestParameter( flag ),delInput ); 253 } 254 255// /** 256// * 【TAG】システム定数でクラウド設定されていても、クラウド環境を使用しない場合、trueを指定します(初期値:false)。 257// * 258// * @og.tag 259// * クラウド設定は、システム定数の『CLOUD_TARGET』と『CLOUD_BUCKET』の設定で自動的に使用しますが、 260// * どうしてもローカルでのみ使いたい場合は、この属性を true に設定します。 261// * 標準はfalse:設定どおりとなります。 262// * 263// * true/false以外を指定した場合はfalse扱いとします。 264// * 265// * @og.rev 8.0.1.0 (2021/10/29) useLocal 属性を追加。 266// * 267// * @param flag ローカル環境のみ [true:ローカルのみ/false:設定どおり] 268// */ 269// public void setUseLocal( final String flag ) { 270// useLocal = nval( getRequestParameter( flag ),useLocal ); 271// } 272 273 /** 274 * このオブジェクトの文字列表現を返します。 275 * 基本的にデバッグ目的に使用します。 276 * 277 * @return このクラスの文字列表現 278 * @og.rtnNotNull 279 */ 280 @Override 281 public String toString() { 282 return ToString.title( this.getClass().getName() ) 283 .println( "VERSION" ,VERSION ) 284 .println( "fileURL" ,fileURL ) 285 .println( "inputFile" ,inputFile ) 286 .println( "outputFile" ,outputFile ) 287 .println( "Other..." ,getAttributes().getAttribute() ) 288 .fixForm().toString() ; 289 } 290}