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.BufferedReader;
026: import java.io.File;
027: import java.io.FileReader;
028: import java.io.FileWriter;
029: import java.io.IOException;
030: import java.io.PrintWriter;
031: import java.text.ParseException;
032: import java.util.Date;
033: import java.util.Iterator;
034: import java.util.Map;
035: import java.util.TreeMap;
036:
037: /**
038: * Stores file information in a text file.
039: * @author Pavel Vlasov
040: * @version $Revision: 1.1 $
041: */
042: public class FileTouchDetector extends AbstractTouchDetector implements
043: TouchDetector {
044: private File fileInfoFile;
045: private Map fileMap = new TreeMap();
046:
047: /**
048: *
049: */
050: public FileTouchDetector(File fileInfoFile, File genRoot,
051: boolean ignoreTimeStamp) {
052: this .fileInfoFile = fileInfoFile;
053: this .genRoot = genRoot;
054: this .ignoreTimeStamp = ignoreTimeStamp;
055: }
056:
057: /* (non-Javadoc)
058: * @see biz.hammurapi.mda.TimeStampRepo#init(java.lang.String, java.io.File)
059: */
060: public void init() throws TransformicaException {
061: if (fileInfoFile.exists()) {
062: int lineNo = 0;
063: try {
064: BufferedReader br = new BufferedReader(new FileReader(
065: fileInfoFile));
066: String line;
067: while ((line = br.readLine()) != null) {
068: lineNo++;
069: if (!(line.startsWith("#") || line.trim().length() == 0)) {
070: FileInfo fileInfo = new FileInfo(line);
071: fileMap.put(fileInfo.getName(), fileInfo);
072: }
073: }
074: br.close();
075: } catch (IOException e) {
076: throw new TransformicaException(
077: "Unable to load file info file " + fileInfoFile,
078: e);
079: } catch (ParseException e) {
080: throw new TransformicaException("Unable to parse line "
081: + lineNo, e);
082: }
083: }
084: }
085:
086: /* (non-Javadoc)
087: * @see biz.hammurapi.mda.TimeStampRepo#register(java.io.File)
088: */
089: public void register(File file) throws TransformicaException {
090: FileInfo fileInfo = new FileInfo(genRoot, file);
091: fileMap.put(fileInfo.getName(), fileInfo);
092: }
093:
094: /* (non-Javadoc)
095: * @see biz.hammurapi.mda.TimeStampRepo#destroy()
096: */
097: public void destroy() throws TransformicaException {
098: try {
099: PrintWriter p = new PrintWriter(
100: new FileWriter(fileInfoFile));
101: p.println("# transformica 2 file info file");
102: p.println("# Created " + new Date());
103: if (genRoot != null) {
104: p.println("# Root: " + genRoot.getAbsolutePath());
105: }
106: p.println(FileInfo.HEADER);
107: Iterator it = fileMap.values().iterator();
108: while (it.hasNext()) {
109: FileInfo fileInfo = (FileInfo) it.next();
110: if (fileInfo.exists(genRoot)) {
111: p.println(fileInfo);
112: } else {
113: System.out.println("Does not exist: "
114: + fileInfo.getName());
115: }
116: }
117: p.close();
118: } catch (IOException e) {
119: throw new TransformicaException(
120: "Unable to save file info file "
121: + fileInfoFile.getAbsolutePath(), e);
122: }
123: }
124:
125: /* (non-Javadoc)
126: * @see biz.hammurapi.mda.AbstractTouchDetector#lookupFileInfo(java.lang.String)
127: */
128: protected FileInfo lookupFileInfo(String fileName) {
129: return (FileInfo) fileMap.get(fileName);
130: }
131: }
|