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.exec;
034:
035: import org.libresource.so6.core.WsConnection;
036: import org.libresource.so6.core.engine.DBType;
037: import org.libresource.so6.core.tf.TextFileFunctions;
038:
039: import java.io.File;
040: import java.io.FileReader;
041: import java.io.LineNumberReader;
042:
043: import java.util.ArrayList;
044: import java.util.Collection;
045: import java.util.Enumeration;
046: import java.util.Iterator;
047: import java.util.Properties;
048:
049: /**
050: * The <code>FindConflict</code> class is used to search into a Workspace if
051: * some files have conflict inside or if two version of a file are in conflict.
052: * <p>
053: * The result is presented in a list of file on the standard output.
054: * <ul>
055: * <li>When it's written conflict "on" file then it means thats a version
056: * conflict.</li>
057: * <li>When it's written conflict "in" file then it means there's conflicts
058: * inside the text file.</li>
059: * </ul>
060: *
061: * @author Smack
062: * @version 1.0, 26/05/04
063: * @see org.libresource.so6.core.exec.Main
064: * @since JDK1.4
065: */
066: public class FindConflict {
067: /**
068: * The <code>FindConflict</code> class is used to search into a Workspace
069: * if some files have conflict inside or if two version of a file are in
070: * conflict.
071: * <p>
072: * The result is presented in a list of file on the standard output.
073: * <ul>
074: * <li>When it's written conflict "on" file then it means thats a version
075: * conflict.</li>
076: * <li>When it's written conflict "in" file then it means there's conflicts
077: * inside the text file.</li>
078: * </ul>
079: *
080: * @param args
081: * <ul>
082: * <li>The WsConnection property file path
083: * (.so6/1/so6.properties)</li>
084: * </ul>
085: * @throws Exception
086: */
087: public static void main(String[] args) throws Exception {
088: if (args.length != 1) {
089: System.err.println("Usage: wscPath");
090: System.err.println(" (1) wscPath: path of the file "
091: + WsConnection.SO6_WSC_FILE);
092: } else {
093: String wsPath = args[0];
094: System.out
095: .println("Start searching conflict in workspace : "
096: + wsPath);
097:
098: Collection c = searchConflict(wsPath);
099:
100: for (Iterator i = c.iterator(); i.hasNext();) {
101: System.out.println(i.next());
102: }
103: }
104: }
105:
106: /**
107: * Search into the workspace of a connection the set of conflicts.
108: *
109: * @param wscPath
110: * @return The set of ConflictFile
111: * @throws Exception
112: */
113: public static Collection searchConflict(String wscPath)
114: throws Exception {
115: ArrayList result = new ArrayList();
116: WsConnection ws = new WsConnection(wscPath);
117: DBType dbType = ws.getDBType();
118: dbType.updateFromWalk(ws.getPath(), ws.getXmlAutoDetection());
119:
120: Properties props = dbType.getDBTypeData();
121: File f;
122:
123: for (Enumeration e = props.keys(); e.hasMoreElements();) {
124: String path = (String) e.nextElement();
125: f = new File(ws.getPath(), path);
126:
127: if (!f.exists()) {
128: continue;
129: }
130:
131: int type = Integer.parseInt(props.getProperty(path));
132:
133: switch (type) {
134: case DBType.TYPE_UNKNOWN:
135:
136: // do nothing
137: break;
138:
139: case DBType.TYPE_DIR:
140: case DBType.TYPE_FILE_XML:
141: case DBType.TYPE_FILE:
142: case DBType.TYPE_FILE_BIN:
143:
144: // check file name
145: if (path.indexOf("#") != -1) {
146: result.add(new ConflictFile(path, false));
147: }
148:
149: break;
150:
151: case DBType.TYPE_FILE_TXT:
152:
153: // check file name
154: if (path.indexOf("#") != -1) {
155: result.add(new ConflictFile(path, false));
156: }
157:
158: // check inside
159: if (isFileInConflict(ws.getPath(), path)) {
160: result.add(new ConflictFile(path, true));
161: }
162:
163: break;
164: }
165: }
166:
167: return result;
168: }
169:
170: /**
171: * Check inside a file if it contains at least a bloc of conflict.
172: *
173: * @param basePath
174: * @param path
175: * @return @throws
176: * Exception
177: */
178: public static boolean isFileInConflict(String basePath, String path)
179: throws Exception {
180: File f = new File(basePath + File.separator + path);
181:
182: if (!f.exists()) {
183: //throw new Exception("Error the file " + f.getPath() + " does not
184: // exist");
185: return false;
186: }
187:
188: if (f.length() == 0) {
189: return false;
190: }
191:
192: // check inside
193: LineNumberReader input = null;
194:
195: try {
196: input = new LineNumberReader(new FileReader(f));
197:
198: String startConflict;
199: String firstBlock;
200: String secondBlock;
201:
202: while ((startConflict = input.readLine()) != null) {
203: if (startConflict
204: .startsWith(TextFileFunctions.CONFLICT_BLOC_START)) {
205: // Maybe a conflict -> check further
206: while ((firstBlock = input.readLine()) != null) {
207: if (firstBlock
208: .startsWith(TextFileFunctions.CONFLICT_BLOC_PADDING)) {
209: // inside the first part
210: } else if (firstBlock
211: .startsWith(TextFileFunctions.CONFLICT_BLOC_SPLIT)) {
212: // end of the fisrt part
213: while ((secondBlock = input.readLine()) != null) {
214: if (secondBlock
215: .startsWith(TextFileFunctions.CONFLICT_BLOC_PADDING)) {
216: // inside the second part
217: } else if (secondBlock
218: .startsWith(TextFileFunctions.CONFLICT_BLOC_END)) {
219: // End of the conflict
220: return true;
221: } else {
222: return false;
223: }
224: }
225: } else {
226: return false;
227: }
228: }
229: }
230: }
231: } finally {
232: if (input != null) {
233: input.close();
234: }
235: }
236:
237: return false;
238: }
239:
240: public static class ConflictFile {
241: private String path;
242: private boolean inside;
243:
244: public ConflictFile(String path, boolean conflictInside) {
245: this .path = path;
246: this .inside = conflictInside;
247: }
248:
249: public boolean isInside() {
250: return inside;
251: }
252:
253: public String getPath() {
254: return path;
255: }
256:
257: public void setInside(boolean b) {
258: inside = b;
259: }
260:
261: public void setPath(String string) {
262: path = string;
263: }
264:
265: public String toString() {
266: return "Conflict " + (inside ? "in" : "on") + " file "
267: + path;
268: }
269: }
270: }
|