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     */
016    package org.opengion.fukurou.process;
017    
018    import org.opengion.fukurou.util.FileUtil;
019    import org.opengion.fukurou.util.Closer;
020    import org.opengion.fukurou.util.HybsDateUtil;
021    
022    import org.opengion.fukurou.security.HybsCryptography ;         // 5.7.2.1 (2014/01/17)
023    
024    import java.io.File;
025    import java.io.BufferedReader;
026    import java.io.IOException;
027    // import java.util.Date;
028    // import java.util.Locale;
029    // import java.text.DateFormat;
030    // import java.text.SimpleDateFormat;
031    
032    /**
033     * FileLineModel は、LineModel を継承した ファイルリスト専用の
034     * LineModel の実?ラスです?
035     *
036     * FileLineModel オブジェクトには、ファイル属?(Level,File,Length,Modify)
037     * が設定されます?
038     *
039     * ??タの?行??FileLineModel に割り当てます?
040     * カラ?号は? から始まります?カラ?よりカラ?号を求める?合に?
041     * 存在しな??合??1 を返します?
042     * カラ?号?-1 の場合?、??行いません?
043     *
044     * 注意:このクラスは、同期??れて?せん?
045     *
046     * @version  4.0
047     * @author   Kazuhiko Hasegawa
048     * @since    JDK5.0,
049     */
050    public class FileLineModel extends LineModel {
051            // 5.7.2.1 (2014/01/17) MD5 ?追?
052    //      private static final String[] KEYS = new String[] { "Level","File","Length","Modify","LineCnt","Biko" };
053            private static final String[] KEYS = new String[] { "Level","File","Length","Modify","LineCnt","Biko","MD5" };
054    
055            private static final int LEVEL    = 0;
056            private static final int FILE     = 1;
057            private static final int LENGTH   = 2;
058            private static final int MODIFY   = 3;
059            private static final int LINECNT  = 4;
060            private static final int BIKO     = 5;
061            private static final int MD5      = 6;          // 5.7.2.1 (2014/01/17)
062    
063    //      private final DateFormat formatter = new SimpleDateFormat( "yyyy/MM/dd HH:mm:ss",Locale.JAPAN );
064    
065            private final boolean useLineCnt ;
066            private final boolean useMD5 ;                          // 5.7.2.1 (2014/01/17) MD5 ?追?
067    
068            /**
069             * コンストラクターです?
070             * ラインカウントとMD5計算?初期値は、false です?
071             *
072             * @og.rev 5.7.2.1 (2014/01/17) MD5対?
073             *
074             */
075            public FileLineModel() {
076    //              this( false );
077                    this( false,false );                                    // 5.7.2.1 (2014/01/17)
078    //              super();
079    //              init( KEYS );
080            }
081    
082            /**
083             * ラインカウント?有無を指定した?コンストラクターです?
084             * MD5計算?初期値は、false です?
085             *
086             * @og.rev 4.2.2.0 (2008/05/10) 行数カウント?使用有無
087             * @og.rev 5.7.2.1 (2014/01/17) MD5対?
088             *
089             * @param       isLineCnt       行数カウント?使用有無
090             */
091            public FileLineModel( final boolean isLineCnt ) {
092                    this( isLineCnt,false );                                // 5.7.2.1 (2014/01/17)
093    
094                    // 4.3.4.4 (2009/01/01)
095    //              super();
096    //              useLineCnt = isLineCnt;
097    //              init( KEYS );
098            }
099    
100            /**
101             * ラインカウント?有無と、MD5計算?有無を指定した?コンストラクターです?
102             *
103             * @og.rev 5.7.2.1 (2014/01/17) 新規追?MD5対?
104             *
105             * @param       isLineCnt       行数カウント?使用有無
106             * @param       isMD5           ファイルのMD5の使用有無
107             */
108            public FileLineModel( final boolean isLineCnt,final boolean isMD5 ) {
109                    // 4.3.4.4 (2009/01/01)
110    //              super();
111                    useLineCnt = isLineCnt;
112                    useMD5     = isMD5;                             // 5.7.2.1 (2014/01/17)
113                    init( KEYS );
114            }
115    
116            /**
117             * LineModel を?に、FileLineModel を構築します?
118             * これは、?ファイル等にセーブされた FileLineModel 形式を
119             * ?戻す簡易コンストラクタです?
120             *
121             * @og.rev 4.2.3.0 (2008/05/26) 新規追?
122             * @og.rev 5.7.2.1 (2014/01/17) MD5の設定???
123             *
124             * @param       model   ??LineModel
125             */
126            public FileLineModel( final LineModel model ) {
127                    // 4.3.4.4 (2009/01/01)
128    //              super();
129                    init( model.getNames() );
130    
131                    Object[] obj = model.getValues();
132    
133                    setValue( LEVEL   ,Integer.valueOf( (String)obj[LEVEL] ) );
134                    setValue( FILE    ,new File((String)obj[FILE]) );
135                    setValue( LENGTH  ,Long.valueOf( (String)obj[LENGTH] ) );
136                    setValue( MODIFY  ,(String)obj[MODIFY] );
137    
138                    String cnt = (String)obj[LINECNT] ;
139    //              useLineCnt = ( cnt != null && cnt.length() > 0 && ! "null".equals( cnt ) );
140                    useLineCnt = ( cnt != null && cnt.length() > 0 && ! "null".equalsIgnoreCase( cnt ) );
141                    if( useLineCnt ) { setValue( LINECNT ,cnt ); }
142    
143                    setValue( BIKO  ,(String)obj[BIKO] );
144    
145                    // 5.7.2.1 (2014/01/17) 
146                    String md5Data = (String)obj[MD5] ;
147                    useMD5 = ( md5Data != null && md5Data.length() > 0 && ! "null".equalsIgnoreCase( md5Data ) );
148                    if( useMD5 ) { setValue( MD5 ,md5Data ); }
149            }
150    
151            /**
152             * File属?値をセ?します?
153             * LEVEL,FILE,LENGTH,MODIFY,LINECNT,MD5 の??を設定します?
154             *
155             * @og.rev 4.2.2.0 (2008/05/10) 行数カウント?使用有無
156             * @og.rev 5.5.7.2 (2012/10/09) HybsDateUtil を利用するように修正します?
157             * @og.rev 5.7.2.1 (2014/01/17) MD5計算???追?
158             *
159             * @param       level   ファイルの?レクトリ階層
160             * @param       file    ファイルオブジェク?
161             */
162            public void setFileVals( final int level, final File file ) {
163                    setValue( LEVEL  ,Integer.valueOf( level ) );
164                    setValue( FILE   ,file );
165                    setValue( LENGTH ,Long.valueOf( file.length() ) );
166    //              setValue( MODIFY ,formatter.format( new Date( file.lastModified() ) ) );
167                    setValue( MODIFY ,HybsDateUtil.getDate( file.lastModified(),"yyyy/MM/dd HH:mm:ss" ) );          // 5.5.7.2 (2012/10/09) HybsDateUtil を利用する
168                    if( useLineCnt ) {
169                            setValue( LINECNT ,getLineCnt( file ) );
170                    }
171    
172                    // 5.7.2.1 (2014/01/17) MD5計算がtrue で、かつ、ファイルの場合?MD5 計算を行います?
173                    if( useMD5 && file.isFile() ) {
174                            setValue( MD5 ,HybsCryptography.getMD5( file ) );
175                    }
176            }
177    
178            /**
179             * File属?値をセ?します?
180             *
181             * @param       file    ファイルオブジェク?
182             */
183            public void setFile( final File file ) {
184                    setValue( FILE,file );
185            }
186    
187            /**
188             * 備???属?値をセ?します?
189             *
190             * @og.rev 4.2.2.0 (2008/05/10) 行数カウント?使用有無
191             *
192             * @param       biko    備???
193             */
194            public void setBiko( final String biko ) {
195                    setValue( BIKO,biko );
196            }
197    
198            /**
199             * レベル File属?値を取得します?
200             *
201             * @return      ファイルの?レクトリ階層
202             */
203            public int getLebel() {
204                    return ((Integer)getValue( LEVEL )).intValue();
205            }
206    
207            /**
208             * ファイルを取得します?
209             *
210             * @return      ファイル
211             */
212            public File getFile() {
213                    return (File)getValue( FILE );
214            }
215    
216            /**
217             * ファイルサイズ File属?値を取得します?
218             *
219             * @return      ファイルサイズ
220             */
221            public long getLength() {
222                    return ((Long)getValue( LENGTH )).longValue();
223            }
224    
225            /**
226             * 更新日?File属?値を取得します?
227             *
228             * @return      更新日?yyyy/MM/dd HH:mm:ss)
229             */
230            public String getModify() {
231                    return (String)getValue( MODIFY );
232            }
233    
234            /**
235             * MD5 File属?値を取得します?
236             * ただし?useMD5 ?true でな?値は返しません?
237             *
238             * @og.rev 5.7.2.1 (2014/01/17) 新規追?MD5対?
239             *
240             * @return      MD5の値
241             */
242            public String getMD5() {
243                    return (String)getValue( MD5 );
244            }
245    
246            /**
247             * 行数を取得します?
248             *
249             * @param       file    行数を数えるファイルオブジェク?
250             *
251             * @return  行数
252             */
253            private String getLineCnt( final File file ) {
254                    int cnt = 0;
255    
256                    BufferedReader reader = FileUtil.getBufferedReader( file,"JISAutoDetect" );
257    
258                    try {
259                            if( ! file.isDirectory() ) {
260    //                              String line ;   // findbugs で、意味の無?入チェ?がかかりますが、OKです?
261    //                              while((line = reader.readLine()) != null) {
262    //                                      cnt++;
263    //                              }
264                                    while( reader.readLine() != null) {
265                                            cnt++;
266                                    }
267                            }
268                    }
269                    catch( IOException ex ) {
270                            String errMsg = "ファイルカウント中に例外が発生しました?" + file + "]" ;
271                            throw new RuntimeException( errMsg,ex );
272                    }
273                    finally {
274                            Closer.ioClose( reader ) ;
275                    }
276    
277                    return String.valueOf( cnt );
278            }
279    }