001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.so6.core.engine;
034:
035: import org.libresource.so6.core.Workspace;
036: import org.libresource.so6.core.engine.util.FileUtils;
037:
038: import java.io.File;
039: import java.io.FileInputStream;
040: import java.io.FileNotFoundException;
041: import java.io.FileOutputStream;
042:
043: import java.util.ArrayList;
044: import java.util.Properties;
045: import java.util.StringTokenizer;
046:
047: /**
048: * @author molli
049: */
050: public class DBType {
051: public static boolean VALIDATE_PATH = false; // Pas si simple, il faut
052: // toucher au transformees...
053: public static final int TYPE_UNKNOWN = -1;
054: public static final int TYPE_DIR = 0;
055: public static final int TYPE_FILE = 1;
056: public static final int TYPE_FILE_BIN = 2;
057: public static final int TYPE_FILE_TXT = 3;
058: public static final int TYPE_FILE_XML = 4;
059:
060: //private Hashtable ht = new Hashtable();
061: private Properties props = new Properties();
062: private String filePath;
063: private ArrayList binExt = new ArrayList();
064:
065: public DBType(String filePath, String binExt) {
066: this .filePath = filePath;
067: updateBinExt(binExt);
068: props.put("", new Integer(TYPE_DIR).toString()); //;-D
069: load();
070: }
071:
072: public void updateBinExt(String binExt) {
073: this .binExt.clear();
074:
075: StringTokenizer t = new StringTokenizer(binExt, " \t");
076:
077: while (t.hasMoreTokens()) {
078: this .binExt.add(t.nextToken());
079: }
080: }
081:
082: public void load() {
083: try {
084: FileInputStream fis = new FileInputStream(filePath);
085: props.load(fis);
086: fis.close();
087: } catch (FileNotFoundException f) {
088: // Ok, no problem...
089: } catch (Exception e) {
090: throw new RuntimeException(e);
091: }
092: }
093:
094: public void save() {
095: try {
096: FileOutputStream fos = new FileOutputStream(filePath);
097: props
098: .store(fos,
099: "Types of files: unknown=-1 dir=0 file=1 binaryFile=2 txtFile=3 xmlFile=4");
100: fos.close();
101: } catch (Exception e) {
102: throw new RuntimeException(e);
103: }
104: }
105:
106: public Properties getDBTypeData() {
107: return (Properties) props.clone();
108: }
109:
110: public String getPath() {
111: return filePath;
112: }
113:
114: public int getNbEntry() {
115: return props.size();
116: }
117:
118: public int computeType(File f, boolean allowXmlDetection)
119: throws Exception {
120: if (!(f.exists())) {
121: throw new Exception("File not exists:" + f.getPath());
122: }
123:
124: if (f.isDirectory()) {
125: return TYPE_DIR;
126: }
127:
128: if (f.isFile()) {
129: if (f.length() == 0) {
130: return TYPE_FILE_BIN;
131: }
132:
133: if (isBinary(f.getName())
134: || !(FileUtils.isTextFile(f.getAbsolutePath()) || (allowXmlDetection && FileUtils
135: .isXmlFile(f.getAbsolutePath())))) {
136: return TYPE_FILE_BIN;
137: } else {
138: if (allowXmlDetection
139: && FileUtils.isXmlFile(f.getAbsolutePath())) {
140: return TYPE_FILE_XML;
141: } else {
142: return TYPE_FILE_TXT;
143: }
144: }
145: } else {
146: throw new Exception(
147: "unkown file type (dir or file) of file :"
148: + f.getPath());
149: }
150: }
151:
152: // Check between DIR <-> FILE
153: public boolean typeHasChanged(String path, File f) throws Exception {
154: if (!(f.exists())) {
155: throw new Exception("File not exists:" + f.getPath());
156: }
157:
158: if (getType(path) == -1) {
159: return true; // File not found in db type -> same as type has
160: // changed
161: }
162:
163: if (f.isDirectory()) {
164: return getType(path) > TYPE_DIR;
165: }
166:
167: if (f.isFile()) {
168: return getType(path) < TYPE_FILE;
169: }
170:
171: throw new Exception("File is not a valide File");
172: }
173:
174: public void add(String path_, int type) {
175: String path = path_.replaceAll("\\\\", "/");
176: String validatePath = path;
177:
178: if (VALIDATE_PATH) {
179: validatePath = path.toLowerCase();
180:
181: if (props.get(validatePath) != null) {
182: throw new RuntimeException(
183: "Try to synchronize two file with the same name. "
184: + path + " <-> " + validatePath);
185: }
186:
187: props.put(validatePath, new Integer(type).toString());
188: } else {
189: props.put(path, new Integer(type).toString());
190: }
191: }
192:
193: public void remove(String path) {
194: if (VALIDATE_PATH) {
195: props.remove(path.toLowerCase());
196: } else {
197: props.remove(path);
198: }
199: }
200:
201: public int getType(String path) {
202: Object o = props.get(path);
203:
204: if (VALIDATE_PATH) {
205: o = props.get(path.toLowerCase());
206: }
207:
208: if (o == null) {
209: return -1;
210: } else {
211: return Integer.parseInt((String) o);
212: }
213: }
214:
215: private boolean isBinary(String path) {
216: if (path.lastIndexOf('.') == -1) {
217: return false;
218: }
219:
220: String ext = path.substring(path.lastIndexOf('.') + 1);
221:
222: return binExt.contains(ext);
223: }
224:
225: public void updateFromDBType(DBType dbType) {
226: props = dbType.getDBTypeData();
227: save();
228: }
229:
230: public void updateFromWalk(String basePath,
231: boolean allowXmlDetection) throws Exception {
232: walk(new File(basePath), new File(basePath), allowXmlDetection);
233: save();
234: }
235:
236: private void walk(File baseDir, File currentDir,
237: boolean allowXmlDetection) throws Exception {
238: String currentFile = currentDir.getPath();
239:
240: if (!baseDir.getPath().equals(currentDir.getPath())) {
241: currentFile = currentDir.getPath().substring(
242: baseDir.getPath().length() + 1);
243:
244: if (currentFile.endsWith(Workspace.SO6PREFIX)) {
245: return;
246: }
247: } else {
248: currentFile = "";
249: }
250:
251: currentFile = currentFile.replaceAll("\\\\", "/");
252:
253: if (props.getProperty(currentFile) == null) {
254: add(currentFile, computeType(currentDir, allowXmlDetection));
255: }
256:
257: if (currentDir.isDirectory()) {
258: File[] entries = currentDir.listFiles();
259:
260: for (int i = 0; i < entries.length; i++) {
261: walk(baseDir, entries[i], allowXmlDetection);
262: }
263: }
264: }
265: }
|