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 }