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.compress;
034:
035: import org.libresource.so6.core.engine.OpVectorFsImpl;
036: import org.libresource.so6.core.engine.PatchFile;
037:
038: import java.io.File;
039: import java.io.FileWriter;
040: import java.io.OutputStreamWriter;
041:
042: /**
043: * @author smack
044: */
045: public class CompressUtil {
046: /**
047: * Compress an OpVectorFsImpl
048: *
049: * @param log
050: * @throws Exception
051: */
052: public static void compressLog(OpVectorFsImpl opVector)
053: throws Exception {
054: int size = opVector.size();
055: CompressEngine ce = new CompressEngine();
056: int i;
057: int j;
058:
059: //System.out.println("Compress " + size);
060: for (i = 1; i < size; i++) {
061: // i is the indice of the op that we want to move
062: //System.out.print(".");
063: //System.out.println("av: "+arrayLog[i - 1]+" : "+ arrayLog[i]);
064: CompressEngine.TranspResult tr = ce.transp(opVector
065: .getCommand(i - 1), opVector.getCommand(i));
066: opVector.update(tr.getCmd1(), i - 1);
067: opVector.update(tr.getCmd2(), i);
068:
069: //System.out.println("ap: "+arrayLog[i - 1]+" : "+ arrayLog[i]);
070: j = i;
071:
072: while ((tr.isKeepGoing()) && (j > 1)) {
073: j--;
074:
075: //System.out.println("up av: "+arrayLog[j - 1]+" : "+
076: // arrayLog[j]);
077: tr = ce.transp(opVector.getCommand(j - 1), opVector
078: .getCommand(j));
079: opVector.update(tr.getCmd1(), j - 1);
080: opVector.update(tr.getCmd2(), j);
081: }
082: }
083:
084: //System.out.println();
085: }
086:
087: /**
088: * Concat two patch file in a third one.
089: *
090: * @param patch1
091: * @param patch2
092: * @param resultPatch
093: * @param baseWorkingDir
094: * tmp dir.
095: * @throws Exception
096: */
097: public static void mergePatch(File patch1, File patch2,
098: File resultPatch, File baseWorkingDir) throws Exception {
099: File attachDir = new File(baseWorkingDir, "ATTACH");
100: File cmdsDir = new File(baseWorkingDir, "CMDS");
101:
102: if (!attachDir.exists() && !attachDir.mkdir()) {
103: throw new Exception("Unable to create directory "
104: + attachDir.getPath());
105: }
106:
107: if (!cmdsDir.exists() && !cmdsDir.mkdir()) {
108: throw new Exception("Unable to create directory "
109: + attachDir.getPath());
110: }
111:
112: //
113: OpVectorFsImpl opv = new OpVectorFsImpl(cmdsDir.getPath());
114: PatchFile pf = new PatchFile(patch1.getPath());
115: pf.buildOpVector(opv, attachDir.getPath(), null);
116: pf = new PatchFile(patch2.getPath());
117: pf.buildOpVector(opv, attachDir.getPath(), null);
118:
119: //
120: compressLog(opv);
121:
122: OutputStreamWriter osw = new FileWriter(resultPatch);
123: PatchFile.makePatch(opv, osw, null, opv.getFromTicket(), opv
124: .getToTicket(), "compress", null);
125: osw.close();
126: }
127: }
|