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.hayabusa.common.HybsSystem;
024import org.opengion.hayabusa.report2.DocConverter_OOO;
025
026/**
027 * ドキュメントの変換・マージを行うタグです。
028 *
029 * 変換を行うことのできる入出力のフォーマット以下の通りです。
030 *
031 * [対応フォーマット]
032 *  入力[Calc(ods)   ,Excel(xls)     ] ⇒ 出力[Calc(ods)   ,Excel(xls)     ,PDF]
033 *  入力[Writer(odt) ,Word(doc)      ] ⇒ 出力[Writer(odt) ,Word(doc)      ,PDF]
034 *  入力[Impress(odp),PowerPoint(ppt)] ⇒ 出力[Impress(odp),PowerPoint(ppt),PDF]
035 *
036 * 入出力のフォーマットは、入出力ファイルの拡張子により自動判別されます。
037 * また、入出力ファイル名が同じ場合は何も処理されません。
038 *
039 * 入力ファイルを、カンマ区切りで複数指定することで、複数の入力ファイルをマージして
040 * 出力することもできます。
041 *
042 * ※2 現状は、ファイルのマージは、入力ファイルがExcelまたはCalcの場合のみ対応しています。
043 * ※1 この機能を利用するには、OpenOfficeが正しく設定されている必要があります。
044 *
045 * @og.formSample
046 * ●形式:<og:docConvert fileURL="…" inputFile="…" outputFile="…" />
047 * ●body:なし
048 *
049 * ●Tag定義:
050 *   <og:docConvert
051 *       fileURL            【TAG】操作するファイルのディレクトリを指定します (初期値:FILE_URL[=filetemp/])
052 *       inputFile        ○【TAG】入力ファイル名を指定します(必須)。
053 *       outputFile       ○【TAG】出力ファイル名を指定します(必須)。
054 *       delInput           【TAG】ドキュメント変換後、元のファイルを削除するかどうかを指定します(初期値:false[=削除しない])
055 *       debug              【TAG】デバッグ情報を出力するかどうか[true/false]を指定します(初期値:false)
056 *   />
057 *
058 * ●使用例
059 *    ・Calc(ods)ファイルをPDFに変換
060 *        <og:docConvert inputFile="temp.ods" outputFile="out.pdf" />
061 *
062 *    ・Excel(xls)ファイルを連結
063 *        <og:docConvert inputFile="temp1.xls,temp2.xls" outputFile="out.xls" />
064 *
065 * @og.group その他部品
066 *
067 * @version  4.0
068 * @author       Hiroki Nakamura
069 * @since    JDK5.0,
070 */
071public class DocConvertTag extends CommonTagSupport {
072        //* このプログラムのVERSION文字列を設定します。   {@value} */
073        private static final String VERSION = "5.1.6.0 (2010/05/06)" ;
074
075        private static final long serialVersionUID = 516020100506L ;
076
077        private String  fileURL         = HybsSystem.sys( "FILE_URL" );
078        private String[]inputFile       = null;
079        private String  outputFile      = null;
080        private boolean delInput        = false;
081
082        /**
083         * Taglibの開始タグが見つかったときに処理する doStartTag() を オーバーライドします。
084         *
085         * @return      後続処理の指示( SKIP_BODY )
086         */
087        @Override
088        public int doStartTag() {
089                return ( SKIP_BODY );
090        }
091
092        /**
093         * Taglibの終了タグが見つかったときに処理する doEndTag() を オーバーライドします。
094         *
095         * @return      後続処理の指示
096         */
097        @Override
098        public int doEndTag() {
099                debugPrint();
100
101                // 出力ファイルで拡張子だけが指定された場合は、入力ファイル名を付加する
102                if( outputFile.indexOf( '.' ) < 0 ) {
103                        int inSufIdx = inputFile[0].lastIndexOf( '.' );
104                        if( inSufIdx >= 0 ) {
105                                outputFile = inputFile[0].substring( 0, inSufIdx ) + "." + outputFile;
106                        }
107
108                }
109
110                String directory = HybsSystem.url2dir( fileURL );
111                String[] input = new String[inputFile.length];
112                for( int i=0; i<input.length; i++ ) {
113                        input[i] = StringUtil.urlAppend( directory,inputFile[i] );
114                }
115                String output = StringUtil.urlAppend( directory,outputFile );
116
117                // ドキュメント変換
118                DocConverter_OOO.convert( input, output );
119
120                if( delInput ) {
121                        for( int i=0; i<input.length; i++ ) {
122                                if( !( new File( input[i] ) ).delete() ) {
123                                        System.err.println( "入力ファイルの削除に失敗しました。file=[" + input[i] + "]" );
124                                }
125                        }
126                }
127
128                return( EVAL_PAGE );
129        }
130
131        /**
132         * タグリブオブジェクトをリリースします。
133         * キャッシュされて再利用されるので、フィールドの初期設定を行います。
134         *
135         */
136        @Override
137        protected void release2() {
138                super.release2();
139                fileURL         = HybsSystem.sys( "FILE_URL" );
140                inputFile       = null;
141                outputFile      = null;
142                delInput        = false;
143        }
144
145        /**
146         * 【TAG】操作するファイルのディレクトリを指定します
147         *              (初期値:FILE_URL[={@og.value org.opengion.hayabusa.common.SystemData#FILE_URL}])。
148         *
149         * @og.tag
150         * この属性で指定されるディレクトリのファイルを操作します。
151         * 指定方法は、通常の fileURL 属性と同様に、先頭が、'/' (UNIX) または、2文字目が、
152         * ":" (Windows)の場合は、指定のURLそのままのディレクトリに、そうでない場合は、
153         * (初期値:システム定数のFILE_URL[={@og.value org.opengion.hayabusa.common.SystemData#FILE_URL}])。
154         *
155         * @param       url ファイルURL
156         * @see         org.opengion.hayabusa.common.SystemData#FILE_URL
157         */
158        public void setFileURL( final String url ) {
159                String furl = nval( getRequestParameter( url ),null );
160                if( furl != null ) {
161                        char ch = furl.charAt( furl.length()-1 );
162                        if( ch != '/' && ch != '\\' ) { furl = furl + "/"; }
163                        fileURL = StringUtil.urlAppend( fileURL,furl );
164                }
165        }
166
167        /**
168         * 【TAG】入力ファイル名を指定します。
169         *
170         * @og.tag
171         * 入力ファイル名を指定します。
172         *
173         * 入力ファイルは、カンマ区切りで複数指定することができます。
174         * この場合、複数の入力ファイルをマージして出力を行います。
175         *
176         * ※現状は、ファイルのマージは、入力ファイルがExcelまたはCalcの場合のみ対応しています。
177         *   また、マージを行う場合、印刷範囲など、ドキュメントに関連する情報は、1つ目のファイルの
178         *   情報が使用されます。
179         *
180         * @param       fname 入力ファイル名
181         */
182        public void setInputFile( final String fname ) {
183                inputFile = getCSVParameter( fname );
184        }
185
186        /**
187         * 【TAG】出力ファイル名を指定します。
188         *
189         * @og.tag
190         * 出力ファイル名を指定します。
191         * 出力ファイルには、拡張子のみ(xls,ods等)を指定することもできます。
192         * この場合、出力ファイル名は、入力ファイル名と同じになります。(拡張子のみが変換される)
193         *
194         * @param       fname 出力ファイル名
195         */
196        public void setOutputFile( final String fname ) {
197                outputFile = nval( getRequestParameter( fname ),outputFile );
198        }
199
200        /**
201         * 【TAG】ドキュメント変換後、元のファイルを削除するかどうかを指定します(初期値:false[=削除しない])。
202         *
203         * @og.tag
204         * ドキュメント変換後、元のファイルを削除するかどうかを指定します。
205         * (初期値:false(削除しない))
206         *
207         * @param       flg 出力元のファイルを削除するかどうか名
208         */
209        public void setDelInput( final String flg ) {
210                delInput = nval( getRequestParameter( flg ),delInput );
211        }
212
213        /**
214         * このオブジェクトの文字列表現を返します。
215         * 基本的にデバッグ目的に使用します。
216         *
217         * @return このクラスの文字列表現
218         */
219        @Override
220        public String toString() {
221                return org.opengion.fukurou.util.ToString.title( this.getClass().getName() )
222                                .println( "VERSION"             ,VERSION        )
223                                .println( "fileURL"             ,fileURL        )
224                                .println( "inputFile"   ,inputFile              )
225                                .println( "outputFile"  ,outputFile             )
226                                .println( "Other..."    ,getAttributes().getAttribute() )
227                                .fixForm().toString() ;
228        }
229}