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.tf;
034:
035: import org.libresource.so6.core.WsConnection;
036: import org.libresource.so6.core.command.Command;
037: import org.libresource.so6.core.command.Macro;
038: import org.libresource.so6.core.command.NeutralCommand;
039: import org.libresource.so6.core.command.fs.FsCommand;
040: import org.libresource.so6.core.command.fs.Remove;
041: import org.libresource.so6.core.command.fs.Rename;
042: import org.libresource.so6.core.command.text.UpdateTextFile;
043: import org.libresource.so6.core.command.xml.UpdateXmlFile;
044: import org.libresource.so6.core.engine.util.ObjectCloner;
045:
046: import java.lang.reflect.Method;
047:
048: /**
049: * @author Ecoo Team Loria
050: *
051: * Describe class <code>TransformationFunctions</code> here. Responsability:
052: * Implements transformation function Collaboration: Log (merge)
053: * @author <a href="mailto:molli@loria.fr">Pascal Molli </a>, <a
054: * href="mailto:oster@loria.fr">Gerald Oster </a>, <a
055: * href="mailto:jourdain@loria.fr">S�bastien Jourdain </a>
056: * @version 1.0
057: */
058: public class TransformationFunctions {
059: protected WsConnection ws;
060: private FileSystemFunctions fsTF;
061: private TextFileFunctions txtTF;
062: private XmlFileFunctions xmlTF;
063:
064: public TransformationFunctions(WsConnection ws) {
065: this .ws = ws;
066: this .fsTF = new FileSystemFunctions(ws);
067: this .txtTF = new TextFileFunctions(ws);
068: this .xmlTF = new XmlFileFunctions(ws);
069: }
070:
071: /**
072: * c2 is the remote Command, c1 is the local command (already executed)
073: */
074: public Command transp(Command c1, Command c2) throws Exception {
075: Command res = null;
076: Method m = null;
077:
078: //System.out.println("tf( " + c1 + " , " + c2 + " )");
079: try {
080: if ((c1 instanceof NeutralCommand)
081: || (c2 instanceof NeutralCommand)) {
082: return c1;
083: } else if ((c1 instanceof Macro) || (c2 instanceof Macro)) {
084: if (c1 instanceof Macro) {
085: // tf(x.seq, op) = tf(x, op).tf(seq, tf(op, x))
086: Command cmd1 = transp(((Macro) c1).getCommand(1),
087: c2);
088: Command cmd2 = transp(((Macro) c1).getCommand(2),
089: transp(c2, ((Macro) c1).getCommand(1)));
090: Macro cr = (Macro) ObjectCloner.deepCopy(c1);
091: cr.setCommand(cmd1, 1);
092: cr.setCommand(cmd2, 2);
093:
094: return cr;
095: } else {
096: // tf(op, x.seq) = tf(tf(op, x), seq)
097: return transp(
098: transp(c1, ((Macro) c2).getCommand(1)),
099: ((Macro) c2).getCommand(2));
100: }
101: } else if ((c1 instanceof FsCommand)
102: || (c2 instanceof Remove) || (c2 instanceof Rename)) {
103: if (c2 instanceof FsCommand) {
104: return fsTF.transp(c1, (FsCommand) c2);
105: } else {
106: return c1;
107: }
108: } else if (c1 instanceof UpdateTextFile) {
109: if (c2 instanceof UpdateTextFile) {
110: return txtTF.transp((UpdateTextFile) c1,
111: (UpdateTextFile) c2);
112: } else {
113: return c1;
114: }
115: } else if (c1 instanceof UpdateXmlFile) {
116: if (c2 instanceof UpdateXmlFile) {
117: return xmlTF.transp((UpdateXmlFile) c1,
118: (UpdateXmlFile) c2);
119: } else {
120: return c1;
121: }
122: } else {
123: throw new Exception(
124: "Unable to find transformation function for:\n "
125: + c1 + " - " + c2);
126: }
127: } catch (NoSuchMethodException e) {
128: throw new Exception("Fail to find transformation for:\n "
129: + c1 + " - " + c2);
130: }
131: }
132: }
|