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