001: /*
002: * transformica 2
003: * Code generator
004: * Copyright (C) 2004 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.pavelvlasov.com/pv/content/menu.show@id=products.transformica.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.transformica;
024:
025: import java.io.File;
026: import java.io.FileInputStream;
027: import java.io.IOException;
028: import java.io.InputStream;
029: import java.text.ParseException;
030: import java.text.SimpleDateFormat;
031: import java.util.Date;
032: import java.util.StringTokenizer;
033: import java.util.zip.Adler32;
034:
035: /**
036: * @author Pavel Vlasov
037: * @version $Revision: 1.1 $
038: */
039: public class FileInfo {
040: public static final char FIELD_SEPARATOR = '\t';
041:
042: private String name;
043: private long size;
044: private long checkSum;
045: private long lastModified;
046:
047: /**
048: * Parses line using default field separator
049: * @param line String to parse
050: */
051: public FileInfo(String line) throws ParseException {
052: this (line, FIELD_SEPARATOR);
053: }
054:
055: /**
056: * Parses line
057: * @param line String to parse
058: * @param separator Field separator
059: */
060: public FileInfo(String line, char separator) throws ParseException {
061: StringTokenizer st = new StringTokenizer(line, String
062: .valueOf(separator));
063: lastModified = sdf.parse(st.nextToken().trim()).getTime();
064: size = Long.parseLong(st.nextToken().trim());
065: checkSum = Long.parseLong(st.nextToken().trim());
066: name = st.nextToken();
067: }
068:
069: /**
070: *
071: */
072: public FileInfo(String name, long size, long checkSum,
073: long lastModified) {
074: this .name = name;
075: this .size = size;
076: this .checkSum = checkSum;
077: this .lastModified = lastModified;
078: }
079:
080: /**
081: *
082: */
083: public FileInfo(File genRoot, File file)
084: throws TransformicaException {
085: if (genRoot == null) {
086: name = file.getAbsolutePath().replace(File.separatorChar,
087: '/');
088: } else if (file.getAbsolutePath().startsWith(
089: genRoot.getAbsolutePath())) {
090: name = file.getAbsolutePath().substring(
091: genRoot.getAbsolutePath().length() + 1).replace(
092: File.separatorChar, '/');
093: } else {
094: throw new TransformicaException("File "
095: + file.getAbsolutePath()
096: + " must be under 'genRoot' "
097: + genRoot.getAbsolutePath());
098: }
099:
100: if (file.exists() && file.isFile()) {
101: lastModified = file.lastModified();
102: size = file.length();
103: try {
104: InputStream is = new FileInputStream(file);
105: byte[] buf = new byte[4096];
106: long l = 0;
107: Adler32 adler32 = new Adler32();
108:
109: int len;
110: while ((len = is.read(buf)) != -1) {
111: l += len;
112: adler32.update(buf, 0, len);
113: }
114: is.close();
115:
116: if (l != size) {
117: throw new TransformicaException(
118: "Number of bytes read differs from file size");
119: }
120: checkSum = adler32.getValue();
121: } catch (IOException e) {
122: throw new TransformicaException(
123: "Can't calculate checksum", e);
124: }
125: } else {
126: }
127: }
128:
129: /**
130: * @return
131: */
132: public long getCheckSum() {
133: return checkSum;
134: }
135:
136: /**
137: * @return
138: */
139: public String getName() {
140: return name;
141: }
142:
143: /**
144: * @return
145: */
146: public long getSize() {
147: return size;
148: }
149:
150: boolean isModified(FileInfo anotherFileInfo, boolean ignoreTimeStamp) {
151: if (anotherFileInfo.size != size) {
152: return true;
153: }
154:
155: if (anotherFileInfo.checkSum != checkSum) {
156: return true;
157: }
158:
159: if (anotherFileInfo.lastModified != lastModified
160: && !ignoreTimeStamp) {
161: return true;
162: }
163:
164: return false;
165: }
166:
167: public boolean exists(File genRoot) {
168: return genRoot == null ? new File(name).exists() : new File(
169: genRoot, name).exists();
170: }
171:
172: /**
173: * @return
174: */
175: public long getLastModified() {
176: return lastModified;
177: }
178:
179: private SimpleDateFormat sdf = new SimpleDateFormat(
180: "yyyy/MM/dd HH:mm:ss.S");
181:
182: /**
183: * Converts FileInfo to String with default field separator
184: */
185: public String toString() {
186: return toString(FIELD_SEPARATOR);
187: }
188:
189: /**
190: * Converts FileInfo to String
191: * @param separator Field Separator
192: * @return String representation of FileInfo
193: */
194: public String toString(char separator) {
195: return sdf.format(new Date(lastModified)) + separator + size
196: + separator + checkSum + separator + name;
197: }
198:
199: public static final String HEADER = "# Date \tSize \tChecksum \tName";
200: }
|