0001: /**
0002: * LibreSource
0003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
0004: * http://www.libresource.org - contact@artenum.com
0005: *
0006: * This file is part of the LibreSource software,
0007: * which can be used and distributed under license conditions.
0008: * The license conditions are provided in the LICENSE.TXT file
0009: * at the root path of the packaging that enclose this file.
0010: * More information can be found at
0011: * - http://dev.libresource.org/home/license
0012: *
0013: * Initial authors :
0014: *
0015: * Guillaume Bort / INRIA
0016: * Francois Charoy / Universite Nancy 2
0017: * Julien Forest / Artenum
0018: * Claude Godart / Universite Henry Poincare
0019: * Florent Jouille / INRIA
0020: * Sebastien Jourdain / INRIA / Artenum
0021: * Yves Lerumeur / Artenum
0022: * Pascal Molli / Universite Henry Poincare
0023: * Gerald Oster / INRIA
0024: * Mariarosa Penzi / Artenum
0025: * Gerard Sookahet / Artenum
0026: * Raphael Tani / INRIA
0027: *
0028: * Contributors :
0029: *
0030: * Stephane Bagnier / Artenum
0031: * Amadou Dia / Artenum-IUP Blois
0032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0033: */package org.libresource.so6.core.compress;
0034:
0035: import org.libresource.so6.core.command.Command;
0036: import org.libresource.so6.core.command.EmptyOp;
0037: import org.libresource.so6.core.command.NeutralCommand;
0038: import org.libresource.so6.core.command.UpdateFile;
0039: import org.libresource.so6.core.command.fs.AddBinaryFile;
0040: import org.libresource.so6.core.command.fs.AddDir;
0041: import org.libresource.so6.core.command.fs.AddFile;
0042: import org.libresource.so6.core.command.fs.Remove;
0043: import org.libresource.so6.core.command.fs.Rename;
0044: import org.libresource.so6.core.command.fs.UpdateBinaryFile;
0045: import org.libresource.so6.core.command.text.AddBlock;
0046: import org.libresource.so6.core.command.text.AddTxtFile;
0047: import org.libresource.so6.core.command.text.DelBlock;
0048: import org.libresource.so6.core.command.text.UpdateTextFile;
0049: import org.libresource.so6.core.command.xml.AddXmlFile;
0050: import org.libresource.so6.core.command.xml.DeleteAttribute;
0051: import org.libresource.so6.core.command.xml.DeleteNode;
0052: import org.libresource.so6.core.command.xml.InsertAttribute;
0053: import org.libresource.so6.core.command.xml.InsertNode;
0054: import org.libresource.so6.core.command.xml.UpdateAttribute;
0055: import org.libresource.so6.core.command.xml.UpdateXmlFile;
0056:
0057: import java.lang.reflect.Method;
0058:
0059: import java.util.ArrayList;
0060: import java.util.Collection;
0061: import java.util.Iterator;
0062: import java.util.logging.Logger;
0063:
0064: /**
0065: * Describe class <code>CompressEngine</code> here. Responsability: Implements
0066: * transformation function for compression Collaboration: CompressUtil
0067: *
0068: * @author Smack
0069: * @version 1.0
0070: */
0071: public class CompressEngine {
0072: public CompressEngine() {
0073: }
0074:
0075: /*
0076: * c1 is the oldest Op c2 is the newest Op
0077: */
0078: public TranspResult transp(Command c1, Command c2) throws Exception {
0079: Command res = null;
0080: Method m = null;
0081:
0082: try {
0083: try {
0084: m = this .getClass().getMethod("transp",
0085: new Class[] { c1.getClass(), c2.getClass() });
0086: } catch (NoSuchMethodException e1) {
0087: //System.out.println("catch 1");
0088: try {
0089: m = this .getClass().getMethod(
0090: "transp",
0091: new Class[] {
0092: c1.getClass().getSuperclass(),
0093: c2.getClass() });
0094: } catch (NoSuchMethodException e2) {
0095: //System.out.println("catch 2");
0096: try {
0097: m = this
0098: .getClass()
0099: .getMethod(
0100: "transp",
0101: new Class[] {
0102: c1.getClass(),
0103: c2
0104: .getClass()
0105: .getSuperclass() });
0106: } catch (NoSuchMethodException e3) {
0107: //System.out.println("catch 3");
0108: try {
0109: m = this .getClass().getMethod(
0110: "transp",
0111: new Class[] {
0112: c1.getClass()
0113: .getSuperclass(),
0114: c2.getClass()
0115: .getSuperclass() });
0116: } catch (NoSuchMethodException e4) {
0117: //System.out.println("catch 4");
0118: try {
0119: m = this
0120: .getClass()
0121: .getMethod(
0122: "transp",
0123: new Class[] {
0124: c1
0125: .getClass()
0126: .getSuperclass()
0127: .getSuperclass(),
0128: c2
0129: .getClass()
0130: .getSuperclass() });
0131: } catch (NoSuchMethodException e5) {
0132: //System.out.println("catch 5");
0133: try {
0134: m = this
0135: .getClass()
0136: .getMethod(
0137: "transp",
0138: new Class[] {
0139: c1
0140: .getClass()
0141: .getSuperclass(),
0142: c2
0143: .getClass()
0144: .getSuperclass()
0145: .getSuperclass() });
0146: } catch (NoSuchMethodException e6) {
0147: //System.out.println("catch 6");
0148: try {
0149: m = this
0150: .getClass()
0151: .getMethod(
0152: "transp",
0153: new Class[] {
0154: c1
0155: .getClass()
0156: .getSuperclass()
0157: .getSuperclass(),
0158: c2
0159: .getClass() });
0160: } catch (NoSuchMethodException e7) {
0161: //System.out.println("catch 7");
0162: try {
0163: m = this
0164: .getClass()
0165: .getMethod(
0166: "transp",
0167: new Class[] {
0168: c1
0169: .getClass()
0170: .getSuperclass()
0171: .getSuperclass(),
0172: c2
0173: .getClass() });
0174: } catch (NoSuchMethodException e8) {
0175: //System.out.println("catch 8");
0176: m = this
0177: .getClass()
0178: .getMethod(
0179: "transp",
0180: new Class[] {
0181: c1
0182: .getClass()
0183: .getSuperclass()
0184: .getSuperclass(),
0185: c2
0186: .getClass()
0187: .getSuperclass()
0188: .getSuperclass() });
0189: }
0190: }
0191: }
0192: }
0193: }
0194: }
0195: }
0196: }
0197: } catch (NoSuchMethodException e) {
0198: throw new Exception("No such methode : " + c1 + " / " + c2);
0199: }
0200:
0201: return (TranspResult) m.invoke(this , new Object[] { c1, c2 });
0202: }
0203:
0204: /*
0205: * ===========================================================
0206: */
0207:
0208: // path functions...
0209: public boolean childOf(Command c1, Command c2) {
0210: if (c1.getPath().startsWith(c2.getPath())) {
0211: return true;
0212: } else {
0213: return false;
0214: }
0215: }
0216:
0217: // getParentPath('/1/2/3/4') will return '/1/2/3'
0218: public String getParentPath(String path) {
0219: //System.out.println(path);
0220: int lastIndex = path.lastIndexOf("/");
0221:
0222: if (lastIndex == -1) {
0223: return "";
0224: }
0225:
0226: return path.substring(0, lastIndex + 1);
0227: }
0228:
0229: // getFSName('/1/2/3/4') will return '4'
0230: public String getFSName(String path) {
0231: //return path.substring(path.lastIndexOf(File.pathSeparatorChar)+1);
0232: return path.substring(path.lastIndexOf("/") + 1);
0233: }
0234:
0235: // replacePath('/1/2/3/4','/1/5') will return '/1/5/3/4'
0236: // replacePath('/1/2/3/4','/1/2#')
0237: // commonpath: /1/
0238: // 2/3/4
0239: // FSRenamed: 2#
0240: // result /1/ 2# 2/3/4
0241: public String replacePath(String pathToRename, String newPathToApply) {
0242: String commonPath = getParentPath(newPathToApply);
0243: assert pathToRename.startsWith(commonPath) : "bad replacement of path '"
0244: + pathToRename + "' with '" + newPathToApply + "'";
0245:
0246: String FSRenamed = newPathToApply
0247: .substring(commonPath.length());
0248: String unchangedTail = pathToRename.substring(commonPath
0249: .length());
0250: int index = unchangedTail.indexOf("/");
0251:
0252: if (index == -1) {
0253: unchangedTail = "";
0254: } else {
0255: unchangedTail = unchangedTail.substring(unchangedTail
0256: .indexOf("/"));
0257: }
0258:
0259: Logger.getLogger("divers").info("!!!replacePath:");
0260: Logger.getLogger("divers")
0261: .info(" pathToRename:" + pathToRename);
0262: Logger.getLogger("divers").info(
0263: " newPathToApply:" + newPathToApply);
0264: Logger.getLogger("divers").info(" commonPath:" + commonPath);
0265: Logger.getLogger("divers").info(" FSRenamed:" + FSRenamed);
0266: Logger.getLogger("divers").info(
0267: " unchangedTail:" + unchangedTail);
0268: Logger.getLogger("divers").info(
0269: " return:" + commonPath + FSRenamed + unchangedTail);
0270:
0271: return commonPath + FSRenamed + unchangedTail;
0272: } // generate a unique path which must be the same for uniquePath(c1,c2)
0273:
0274: // and uniquePath(c2,c1)
0275: public String uniquePath(Command c1, Command c2) {
0276: long maxTicket = java.lang.Math.max(c1.getTicket(), c2
0277: .getTicket());
0278:
0279: return new String(c1.getPath() + "#" + maxTicket);
0280: }
0281:
0282: /*
0283: * =========================================== AddFile - AddFile - AddDir -
0284: * UpdateFile (UpdateBinaryFile / AddBlock - DelBlock) - EmptyOp - Id - NoOp -
0285: * Remove - Rename ============================================
0286: */
0287:
0288: // AddFile - Update
0289: public TranspResult transp(AddBinaryFile c1, UpdateBinaryFile c2)
0290: throws Exception {
0291: if (c1.getPath().equals(c2.getPath())) {
0292: c1.setAttachement(c2.getAttachement());
0293:
0294: return new TranspResult(c1, new EmptyOp(c2), false);
0295: }
0296:
0297: return new TranspResult(c2, c1, true);
0298: }
0299:
0300: // AddFile - AddFile
0301: public TranspResult transp(AddTxtFile c1, AddBlock c2)
0302: throws Exception {
0303: if (c1.getPath().equals(c2.getPath())) {
0304: c2.doTheJobOnFile(c1.getAttachement());
0305:
0306: return new TranspResult(c1, new EmptyOp(c2), false);
0307: }
0308:
0309: return new TranspResult(c2, c1, true);
0310: }
0311:
0312: public TranspResult transp(AddTxtFile c1, DelBlock c2)
0313: throws Exception {
0314: if (c1.getPath().equals(c2.getPath())) {
0315: c2.doTheJobOnFile(c1.getAttachement());
0316:
0317: return new TranspResult(c1, new EmptyOp(c2), false);
0318: }
0319:
0320: return new TranspResult(c2, c1, true);
0321: }
0322:
0323: // AddFile - AddFile
0324: public TranspResult transp(AddFile c1, AddFile c2) {
0325: return new TranspResult(c1, c2, false);
0326: }
0327:
0328: // AddFile - AddDir
0329: public TranspResult transp(AddFile c1, AddDir c2) {
0330: // put AddDir before AddFile
0331: return new TranspResult(c2, c1, true);
0332: }
0333:
0334: // AddFile - UpdateBinaryFile
0335: public TranspResult transp(AddFile c1, UpdateFile c2) {
0336: return new TranspResult(c2, c1, true);
0337: }
0338:
0339: public TranspResult transp(AddFile c1, UpdateBinaryFile c2) {
0340: return new TranspResult(c2, c1, true);
0341: }
0342:
0343: // AddFile - EmptyOp
0344: public TranspResult transp(AddFile c1, NeutralCommand c2) {
0345: return new TranspResult(c1, c2, false);
0346: }
0347:
0348: // AddFile - Remove
0349: public TranspResult transp(AddFile c1, Remove c2) {
0350: if (c1.getPath().equals(c2.getPath())) {
0351: // No need to create the file and to keep the remove
0352: return new TranspResult(new EmptyOp(c1), new EmptyOp(c2),
0353: false);
0354: }
0355:
0356: if (childOf(c1, c2)) {
0357: // Remove a parent dir
0358: return new TranspResult(c2, new EmptyOp(c1), true);
0359: }
0360:
0361: return new TranspResult(c2, c1, true);
0362: }
0363:
0364: // AddFile - Rename
0365: public TranspResult transp(AddFile c1, Rename c2) {
0366: if (c1.getPath().equals(c2.getPath())) {
0367: // Rename the AddFile and Delete the Rename
0368: c1.setPath(c2.getNewPath());
0369:
0370: return new TranspResult(c1, new EmptyOp(c2), false);
0371: }
0372:
0373: if (childOf(c1, c2)) {
0374: // rename the parent path concerned by the rename
0375: c1.setPath(replacePath(c1.getPath(), c2.getNewPath()));
0376:
0377: return new TranspResult(c2, c1, true);
0378: }
0379:
0380: return new TranspResult(c2, c1, true);
0381: }
0382:
0383: /*
0384: * =========================================== AddDir - AddDir - AddFile -
0385: * UpdateFile (UpdateBinaryFile / AddBlock - DelBlock) - NeutralOp - Remove -
0386: * Rename ============================================
0387: */
0388:
0389: // AddDir - AddDir
0390: public TranspResult transp(AddDir c1, AddDir c2) {
0391: return new TranspResult(c1, c2, false);
0392: }
0393:
0394: // AddDir - AddFile
0395: public TranspResult transp(AddDir c1, AddFile c2) {
0396: return new TranspResult(c1, c2, false);
0397: }
0398:
0399: // AddDir - UpdateBinaryFile
0400: public TranspResult transp(AddDir c1, UpdateFile c2) {
0401: // Should never happen
0402: return new TranspResult(c1, c2, false);
0403: }
0404:
0405: public TranspResult transp(AddDir c1, UpdateBinaryFile c2) {
0406: // Should never happen
0407: return new TranspResult(c1, c2, false);
0408: }
0409:
0410: public TranspResult transp(AddDir c1, UpdateTextFile c2) {
0411: // Should never happen
0412: return new TranspResult(c1, c2, false);
0413: }
0414:
0415: public TranspResult transp(AddDir c1, UpdateXmlFile c2) {
0416: // Should never happen
0417: return new TranspResult(c1, c2, false);
0418: }
0419:
0420: // AddDir - EmptyOp
0421: public TranspResult transp(AddDir c1, NeutralCommand c2) {
0422: return new TranspResult(c1, c2, false);
0423: }
0424:
0425: // AddDir - Remove
0426: public TranspResult transp(AddDir c1, Remove c2) {
0427: if (c1.getPath().equals(c2.getPath())) {
0428: // No need to create the dir and to keep the remove
0429: return new TranspResult(new EmptyOp(c1), new EmptyOp(c2),
0430: false);
0431: }
0432:
0433: if (childOf(c1, c2)) {
0434: // Remove a parent dir
0435: return new TranspResult(c2, new EmptyOp(c1), true);
0436: }
0437:
0438: return new TranspResult(c2, c1, true);
0439: }
0440:
0441: // AddDir - Rename
0442: public TranspResult transp(AddDir c1, Rename c2) {
0443: if (c1.getPath().equals(c2.getPath())) {
0444: // Rename the AddDir and Delete the Rename
0445: c1.setPath(c2.getNewPath());
0446:
0447: return new TranspResult(c1, new EmptyOp(c2), false);
0448: }
0449:
0450: if (childOf(c1, c2)) {
0451: // rename the parent path concerned by the rename
0452: c1.setPath(replacePath(c1.getPath(), c2.getNewPath()));
0453:
0454: return new TranspResult(c2, c1, true);
0455: }
0456:
0457: return new TranspResult(c2, c1, true);
0458: }
0459:
0460: /*
0461: * =========================================== UpdateBinaryFile - AddDir -
0462: * AddFile - AddBinaryFile - UpdateFile (UpdateBinaryFile / AddBlock -
0463: * DelBlock) - EmptyOp - Id - NoOp - Remove - Rename
0464: * ============================================
0465: */
0466:
0467: // UpdateBinaryFile - UpdateBinaryFile
0468: public TranspResult transp(UpdateBinaryFile c1, UpdateBinaryFile c2) {
0469: if (c1.getPath().equals(c2.getPath())) {
0470: return new TranspResult(c2, new EmptyOp(c1), true);
0471: }
0472:
0473: return new TranspResult(c2, c1, true);
0474: }
0475:
0476: public TranspResult transp(UpdateBinaryFile c1, UpdateFile c2) {
0477: if (c1.getPath().equals(c2.getPath())) {
0478: return new TranspResult(c2, new EmptyOp(c1), true);
0479: }
0480:
0481: return new TranspResult(c1, c2, false);
0482: }
0483:
0484: public TranspResult transp(UpdateBinaryFile c1, UpdateTextFile c2) {
0485: if (c1.getPath().equals(c2.getPath())) {
0486: return new TranspResult(c2, new EmptyOp(c1), true);
0487: }
0488:
0489: return new TranspResult(c1, c2, false);
0490: }
0491:
0492: public TranspResult transp(UpdateBinaryFile c1, UpdateXmlFile c2) {
0493: if (c1.getPath().equals(c2.getPath())) {
0494: return new TranspResult(c2, new EmptyOp(c1), true);
0495: }
0496:
0497: return new TranspResult(c1, c2, false);
0498: }
0499:
0500: public TranspResult transp(UpdateFile c1, UpdateBinaryFile c2) {
0501: if (c1.getPath().equals(c2.getPath())) {
0502: return new TranspResult(c2, new EmptyOp(c1), true);
0503: }
0504:
0505: return new TranspResult(c1, c2, false);
0506: }
0507:
0508: // UpdateBinaryFile - AddDir
0509: public TranspResult transp(UpdateFile c1, AddDir c2) {
0510: return new TranspResult(c2, c1, true);
0511: }
0512:
0513: // UpdateBinaryFile - AddDir
0514: public TranspResult transp(UpdateBinaryFile c1, AddDir c2) {
0515: return new TranspResult(c2, c1, true);
0516: }
0517:
0518: // UpdateBinaryFile - AddFile
0519: public TranspResult transp(UpdateFile c1, AddFile c2) {
0520: return new TranspResult(c2, c1, true);
0521: }
0522:
0523: // UpdateBinaryFile - AddFile
0524: public TranspResult transp(UpdateBinaryFile c1, AddFile c2) {
0525: return new TranspResult(c2, c1, true);
0526: }
0527:
0528: // UpdateBinaryFile - AddBinaryFile
0529: public TranspResult transp(UpdateFile c1, UpdateFile c2) {
0530: if (c1.getPath().equals(c2.getPath())) {
0531: return new TranspResult(c1, c2, false);
0532: }
0533:
0534: return new TranspResult(c2, c1, true);
0535: }
0536:
0537: // UpdateBinaryFile - EmptyOp
0538: public TranspResult transp(UpdateFile c1, NeutralCommand c2) {
0539: return new TranspResult(c1, c2, false);
0540: }
0541:
0542: // UpdateBinaryFile - EmptyOp
0543: public TranspResult transp(UpdateBinaryFile c1, NeutralCommand c2) {
0544: return new TranspResult(c1, c2, false);
0545: }
0546:
0547: // UpdateBinaryFile - Remove
0548: public TranspResult transp(UpdateFile c1, Remove c2) {
0549: if (c1.getPath().equals(c2.getPath())) {
0550: // No need to create the dir and to keep the remove
0551: return new TranspResult(c2, new EmptyOp(c1), true);
0552: }
0553:
0554: if (childOf(c1, c2)) {
0555: // Remove a parent dir
0556: return new TranspResult(c2, new EmptyOp(c1), true);
0557: }
0558:
0559: return new TranspResult(c2, c1, true);
0560: }
0561:
0562: // UpdateBinaryFile - Remove
0563: public TranspResult transp(UpdateBinaryFile c1, Remove c2) {
0564: if (c1.getPath().equals(c2.getPath())) {
0565: // No need to create the dir and to keep the remove
0566: return new TranspResult(c2, new EmptyOp(c1), true);
0567: }
0568:
0569: if (childOf(c1, c2)) {
0570: // Remove a parent dir
0571: return new TranspResult(c2, new EmptyOp(c1), true);
0572: }
0573:
0574: return new TranspResult(c2, c1, true);
0575: }
0576:
0577: // UpdateBinaryFile - Rename
0578: public TranspResult transp(UpdateBinaryFile c1, Rename c2) {
0579: if (c1.getPath().equals(c2.getPath())) {
0580: // Rename the UpdateBinaryFile and propagate the Rename
0581: c1.setPath(c2.getNewPath());
0582:
0583: return new TranspResult(c2, c1, true);
0584: }
0585:
0586: if (childOf(c1, c2)) {
0587: // rename the parent path concerned by the rename
0588: c1.setPath(replacePath(c1.getPath(), c2.getNewPath()));
0589:
0590: return new TranspResult(c2, c1, true);
0591: }
0592:
0593: return new TranspResult(c2, c1, true);
0594: }
0595:
0596: // UpdateBinaryFile - Rename
0597: public TranspResult transp(UpdateFile c1, Rename c2) {
0598: if (c1.getPath().equals(c2.getPath())) {
0599: // Rename the UpdateFile and propagate the Rename
0600: c1.setPath(c2.getNewPath());
0601:
0602: return new TranspResult(c2, c1, true);
0603: }
0604:
0605: if (childOf(c1, c2)) {
0606: // rename the parent path concerned by the rename
0607: c1.setPath(replacePath(c1.getPath(), c2.getNewPath()));
0608:
0609: return new TranspResult(c2, c1, true);
0610: }
0611:
0612: return new TranspResult(c2, c1, true);
0613: }
0614:
0615: /*
0616: * =========================================== AddBlock - AddDir - AddFile -
0617: * AddBinaryFile - UpdateFile (UpdateBinaryFile / AddBlock - DelBlock) -
0618: * EmptyOp - Id - NoOp - Remove - Rename
0619: * ============================================
0620: */
0621:
0622: // AddBlock - AddBlock
0623: public TranspResult transp(AddBlock c1, AddBlock c2) {
0624: if (c1.getPath().equals(c2.getPath())) {
0625: int s1 = c1.getInsertPoint();
0626: int l1 = c1.getSize();
0627: int s2 = c2.getInsertPoint();
0628: int l2 = c2.getSize();
0629:
0630: if ((s1 <= s2) && (s2 <= (s1 + l1))) {
0631: // s2 inside of s1
0632: Collection block = c1.getContent();
0633: Collection blockInside = c2.getContent();
0634: ArrayList newContent = new ArrayList();
0635: Iterator i = block.iterator();
0636:
0637: for (int b = 0; (b < (s2 - s1)) && i.hasNext(); b++) {
0638: newContent.add(i.next());
0639: }
0640:
0641: for (Iterator j = blockInside.iterator(); j.hasNext();) {
0642: newContent.add(j.next());
0643: }
0644:
0645: while (i.hasNext()) {
0646: newContent.add(i.next());
0647: }
0648:
0649: c1.setContent(newContent);
0650:
0651: return new TranspResult(c1, new EmptyOp(c2), false);
0652: } else {
0653: // The two blocks are disjoint
0654: return new TranspResult(c1, c2, false);
0655: }
0656: }
0657:
0658: return new TranspResult(c2, c1, true);
0659: }
0660:
0661: // AddBlock - DelBlock
0662: public TranspResult transp(AddBlock c1, DelBlock c2) {
0663: if (c1.getPath().equals(c2.getPath())) {
0664: int s1 = c1.getInsertPoint();
0665: int l1 = c1.getSize();
0666: int s2 = c2.getDeletePoint();
0667: int l2 = c2.getSize();
0668: Object[] add = c1.getContent().toArray();
0669: Object[] delete = c2.getOldContent().toArray();
0670: Collection addContent = new ArrayList();
0671: Collection deleteContent = new ArrayList();
0672:
0673: if ((s1 <= s2) && (s2 < (s1 + l1))) {
0674: // s2 inside s1
0675: if (l2 > (l1 - (s2 - s1))) {
0676: // The delete is bigger than the add
0677: if (s1 == s2) {
0678: // The delete remove the add
0679: for (int i = add.length; i < delete.length; i++) {
0680: deleteContent.add(delete[i]);
0681: }
0682:
0683: c2.setOldContent(deleteContent);
0684:
0685: return new TranspResult(c2, new EmptyOp(c1),
0686: true);
0687: } else {
0688: // The delete remove the tail of the add
0689: for (int i = 0; i < (s2 - s1); i++) {
0690: addContent.add(add[i]);
0691: }
0692:
0693: for (int i = ((s1 + l1) - (s2 - s1)); i < delete.length; i++) {
0694: deleteContent.add(delete[i]);
0695: }
0696:
0697: c1.setContent(addContent);
0698: c2.setOldContent(deleteContent);
0699: c2.setDeletePoint(s2 - c1.getSize());
0700:
0701: return new TranspResult(c2, c1, true);
0702: }
0703: } else {
0704: // The delete is include in the add
0705: for (int i = 0; i < add.length; i++) {
0706: if (!(((s2 - s1) <= i) && (i < (s2 - s1 + l2)))) {
0707: addContent.add(add[i]);
0708: }
0709: }
0710:
0711: if (addContent.size() == 0) {
0712: return new TranspResult(new EmptyOp(c1),
0713: new EmptyOp(c2), false);
0714: }
0715:
0716: c1.setContent(addContent);
0717:
0718: return new TranspResult(c1, new EmptyOp(c2), false);
0719: }
0720: } else {
0721: // The two blocks are disjoint
0722: if (s1 < s2) {
0723: // Add before del
0724: c2.setDeletePoint(s2 - l1);
0725: }
0726:
0727: return new TranspResult(c2, c1, true);
0728: }
0729: }
0730:
0731: return new TranspResult(c2, c1, true);
0732: }
0733:
0734: /*
0735: * =========================================== DelBlock - AddDir - AddFile -
0736: * AddBinaryFile - UpdateFile (UpdateBinaryFile / AddBlock - DelBlock) -
0737: * EmptyOp - Id - NoOp - Remove - Rename
0738: * ============================================
0739: */
0740:
0741: // DelBlock - DelBlock
0742: public TranspResult transp(DelBlock c1, DelBlock c2) {
0743: int s1 = c1.getDeletePoint();
0744: int s2 = c2.getDeletePoint();
0745:
0746: if (c1.getPath().equals(c2.getPath())) {
0747: if (s1 == s2) {
0748: // 2 in 1
0749: Collection c = c1.getOldContent();
0750: c.add(c2.getOldContent());
0751: c1.setOldContent(c);
0752:
0753: return new TranspResult(c1, new EmptyOp(c2), true);
0754: } else {
0755: return new TranspResult(c1, c2, false);
0756: }
0757: }
0758:
0759: return new TranspResult(c2, c1, true);
0760: }
0761:
0762: // DelBlock - AddBlock
0763: public TranspResult transp(DelBlock c1, AddBlock c2) {
0764: return new TranspResult(c1, c2, false);
0765: }
0766:
0767: /*
0768: * =========================================== NeutralCommand - AddDir -
0769: * AddFile - AddBinaryFile - UpdateFile (UpdateBinaryFile / AddBlock -
0770: * DelBlock) - EmptyOp - Id - NoOp - Remove - Rename
0771: * ============================================
0772: */
0773:
0774: // EmptyOp - AddDir
0775: public TranspResult transp(NeutralCommand c1, AddDir c2) {
0776: return new TranspResult(c2, c1, true);
0777: }
0778:
0779: // EmptyOp - AddFile
0780: public TranspResult transp(NeutralCommand c1, AddFile c2) {
0781: return new TranspResult(c2, c1, true);
0782: }
0783:
0784: // EmptyOp - AddBinaryFile
0785: public TranspResult transp(NeutralCommand c1, AddBinaryFile c2) {
0786: return new TranspResult(c2, c1, true);
0787: }
0788:
0789: // EmptyOp - UpdateBinaryFile
0790: public TranspResult transp(NeutralCommand c1, UpdateBinaryFile c2) {
0791: return new TranspResult(c2, c1, true);
0792: }
0793:
0794: // EmptyOp - AddBlock
0795: public TranspResult transp(NeutralCommand c1, AddBlock c2) {
0796: return new TranspResult(c2, c1, true);
0797: }
0798:
0799: // EmptyOp - DelBlock
0800: public TranspResult transp(NeutralCommand c1, DelBlock c2) {
0801: return new TranspResult(c2, c1, true);
0802: }
0803:
0804: // NeutralCommand - NeutralCommand
0805: public TranspResult transp(NeutralCommand c1, NeutralCommand c2) {
0806: return new TranspResult(new EmptyOp(c2), new EmptyOp(c1), false);
0807: }
0808:
0809: // EmptyOp - Remove
0810: public TranspResult transp(NeutralCommand c1, Remove c2) {
0811: return new TranspResult(c2, c1, true);
0812: }
0813:
0814: // EmptyOp - Rename
0815: public TranspResult transp(NeutralCommand c1, Rename c2) {
0816: return new TranspResult(c2, c1, true);
0817: }
0818:
0819: // EmptyOp - InsertNode
0820: public TranspResult transp(NeutralCommand c1, InsertNode c2) {
0821: return new TranspResult(c2, c1, true);
0822: }
0823:
0824: // EmptyOp - DeleteNode
0825: public TranspResult transp(NeutralCommand c1, DeleteNode c2) {
0826: return new TranspResult(c2, c1, true);
0827: }
0828:
0829: // EmptyOp - InsertAttribute
0830: public TranspResult transp(NeutralCommand c1, InsertAttribute c2) {
0831: return new TranspResult(c2, c1, true);
0832: }
0833:
0834: // EmptyOp - DeleteAttribute
0835: public TranspResult transp(NeutralCommand c1, DeleteAttribute c2) {
0836: return new TranspResult(c2, c1, true);
0837: }
0838:
0839: // EmptyOp - UpdateAttribute
0840: public TranspResult transp(NeutralCommand c1, UpdateAttribute c2) {
0841: return new TranspResult(c2, c1, true);
0842: }
0843:
0844: /*
0845: * =========================================== Remove - AddDir - AddFile -
0846: * AddBinaryFile - UpdateFile (UpdateBinaryFile / AddBlock - DelBlock) -
0847: * EmptyOp - Id - NoOp - Remove - Rename
0848: * ============================================
0849: */
0850:
0851: // Remove - AddDir
0852: public TranspResult transp(Remove c1, AddDir c2) {
0853: return new TranspResult(c1, c2, false);
0854: }
0855:
0856: // Remove - AddFile
0857: public TranspResult transp(Remove c1, AddFile c2) {
0858: return new TranspResult(c1, c2, false);
0859: }
0860:
0861: // Remove - UpdateBinaryFile
0862: public TranspResult transp(Remove c1, UpdateFile c2) {
0863: return new TranspResult(c1, c2, false);
0864: }
0865:
0866: public TranspResult transp(Remove c1, UpdateTextFile c2) {
0867: return new TranspResult(c1, c2, false);
0868: }
0869:
0870: public TranspResult transp(Remove c1, UpdateXmlFile c2) {
0871: return new TranspResult(c1, c2, false);
0872: }
0873:
0874: // Remove - UpdateBinaryFile
0875: public TranspResult transp(Remove c1, UpdateBinaryFile c2) {
0876: return new TranspResult(c1, c2, false);
0877: }
0878:
0879: // Remove - EmptyOp
0880: public TranspResult transp(Remove c1, NeutralCommand c2) {
0881: return new TranspResult(c1, new EmptyOp(c2), false);
0882: }
0883:
0884: // Remove - Remove
0885: public TranspResult transp(Remove c1, Remove c2) {
0886: if (childOf(c1, c2)) {
0887: return new TranspResult(c2, new EmptyOp(c1), true);
0888: }
0889:
0890: return new TranspResult(c2, c1, true);
0891: }
0892:
0893: // Remove - Rename
0894: public TranspResult transp(Remove c1, Rename c2) {
0895: return new TranspResult(c1, c2, false);
0896: }
0897:
0898: /*
0899: * =========================================== Rename - AddDir - AddFile -
0900: * AddBinaryFile - UpdateFile (UpdateBinaryFile / AddBlock - DelBlock) -
0901: * EmptyOp - Id - NoOp - Remove - Rename
0902: * ============================================
0903: */
0904:
0905: // Rename - AddDir
0906: public TranspResult transp(Rename c1, AddDir c2) {
0907: return new TranspResult(c1, c2, false);
0908: }
0909:
0910: // Rename - AddFile
0911: public TranspResult transp(Rename c1, AddFile c2) {
0912: return new TranspResult(c1, c2, false);
0913: }
0914:
0915: // Rename - UpdateBinaryFile
0916: public TranspResult transp(Rename c1, UpdateFile c2) {
0917: return new TranspResult(c1, c2, false);
0918: }
0919:
0920: public TranspResult transp(Rename c1, UpdateTextFile c2) {
0921: return new TranspResult(c1, c2, false);
0922: }
0923:
0924: public TranspResult transp(Rename c1, UpdateXmlFile c2) {
0925: return new TranspResult(c1, c2, false);
0926: }
0927:
0928: // Rename - UpdateBinaryFile
0929: public TranspResult transp(Rename c1, UpdateBinaryFile c2) {
0930: return new TranspResult(c1, c2, false);
0931: }
0932:
0933: // Rename - EmptyOp
0934: public TranspResult transp(Rename c1, NeutralCommand c2) {
0935: return new TranspResult(c1, new EmptyOp(c2), false);
0936: }
0937:
0938: // Rename - Remove
0939: public TranspResult transp(Rename c1, Remove c2) {
0940: return new TranspResult(c1, c2, false);
0941: }
0942:
0943: // Rename - Rename
0944: public TranspResult transp(Rename c1, Rename c2) {
0945: if (c1.getNewPath().equals(c2.getPath())) {
0946: c2.setPath(c1.getPath());
0947:
0948: return new TranspResult(c2, new EmptyOp(c1), true);
0949: }
0950:
0951: return new TranspResult(c1, c2, false);
0952: }
0953:
0954: /*
0955: * =========================================== Rename - AddDir - AddFile -
0956: * AddBinaryFile - UpdateFile (UpdateBinaryFile / AddBlock - DelBlock) -
0957: * EmptyOp - Id - NoOp - Remove - Rename
0958: * ============================================
0959: */
0960: public TranspResult transp(AddXmlFile c1, DeleteAttribute c2)
0961: throws Exception {
0962: if (c1.getPath().equals(c2.getPath())) {
0963: c2.doTheJobOnFile(c1.getAttachement());
0964:
0965: return new TranspResult(c1, new EmptyOp(c2), false);
0966: }
0967:
0968: return new TranspResult(c2, c1, true);
0969: }
0970:
0971: public TranspResult transp(UpdateXmlFile c1, UpdateXmlFile c2)
0972: throws Exception {
0973: if (c1.getPath().equals(c2.getPath())) {
0974: return new TranspResult(c1, c2, false);
0975: }
0976:
0977: return new TranspResult(c2, c1, true);
0978: }
0979:
0980: public TranspResult transp(AddXmlFile c1, DeleteNode c2)
0981: throws Exception {
0982: if (c1.getPath().equals(c2.getPath())) {
0983: c2.doTheJobOnFile(c1.getAttachement());
0984:
0985: return new TranspResult(c1, new EmptyOp(c2), false);
0986: }
0987:
0988: return new TranspResult(c2, c1, true);
0989: }
0990:
0991: public TranspResult transp(AddXmlFile c1, InsertAttribute c2)
0992: throws Exception {
0993: if (c1.getPath().equals(c2.getPath())) {
0994: c2.doTheJobOnFile(c1.getAttachement());
0995:
0996: return new TranspResult(c1, new EmptyOp(c2), false);
0997: }
0998:
0999: return new TranspResult(c2, c1, true);
1000: }
1001:
1002: public TranspResult transp(AddXmlFile c1, UpdateAttribute c2)
1003: throws Exception {
1004: if (c1.getPath().equals(c2.getPath())) {
1005: c2.doTheJobOnFile(c1.getAttachement());
1006:
1007: return new TranspResult(c1, new EmptyOp(c2), false);
1008: }
1009:
1010: return new TranspResult(c2, c1, true);
1011: }
1012:
1013: public TranspResult transp(AddXmlFile c1, InsertNode c2)
1014: throws Exception {
1015: if (c1.getPath().equals(c2.getPath())) {
1016: c2.doTheJobOnFile(c1.getAttachement());
1017:
1018: return new TranspResult(c1, new EmptyOp(c2), false);
1019: }
1020:
1021: return new TranspResult(c2, c1, true);
1022: }
1023:
1024: // ============================================
1025: // Result class of the transposition
1026: // ============================================
1027: public class TranspResult {
1028: private boolean keepGoing = true;
1029: private Command cmd1 = null;
1030: private Command cmd2 = null;
1031:
1032: public TranspResult() {
1033: }
1034:
1035: public TranspResult(Command cmd1, Command cmd2,
1036: boolean keepGoing) {
1037: this .cmd1 = cmd1;
1038: this .cmd2 = cmd2;
1039: this .keepGoing = keepGoing;
1040:
1041: // Set the right ticket to the command
1042: long ticket1 = cmd1.getTicket();
1043: long ticket2 = cmd2.getTicket();
1044:
1045: if (ticket1 < ticket2) {
1046: // order ok
1047: } else {
1048: // change ticket
1049: cmd1.setTicket(ticket2);
1050: cmd2.setTicket(ticket1);
1051: }
1052: }
1053:
1054: public Command getCmd1() {
1055: return cmd1;
1056: }
1057:
1058: public Command getCmd2() {
1059: return cmd2;
1060: }
1061:
1062: public boolean isKeepGoing() {
1063: return keepGoing;
1064: }
1065:
1066: public void setCmd1(Command command) {
1067: cmd1 = command;
1068: }
1069:
1070: public void setCmd2(Command command) {
1071: cmd2 = command;
1072: }
1073:
1074: public void setKeepGoing(boolean b) {
1075: keepGoing = b;
1076: }
1077: }
1078: }
|