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.conflict.editor;
034:
035: import org.libresource.so6.core.tf.TextFileFunctions;
036:
037: import java.io.Writer;
038:
039: import java.util.Iterator;
040:
041: /**
042: * @author smack
043: */
044: public class ConflictNode extends AbstractNode {
045: private int conflictType = -1;
046:
047: public ConflictNode() {
048: super ();
049: children.add(new TextNode());
050: children.add(new TextNode());
051: }
052:
053: public int getConflictType() {
054: return conflictType;
055: }
056:
057: public void setConflictType(int conflictType) {
058: this .conflictType = conflictType;
059: }
060:
061: public boolean getAllowsChildren() {
062: return true;
063: }
064:
065: public void toDocument(Writer writer, int depth) throws Exception {
066: // init
067: AbstractNode child1 = (AbstractNode) children.get(0);
068: AbstractNode child2 = (AbstractNode) children.get(1);
069:
070: // Start the conflict bloc
071: if (enabled) {
072: writePadding(writer, depth);
073: writer.write(TextFileFunctions.CONFLICT_BLOC_START);
074:
075: switch (conflictType) {
076: case TextFileFunctions.ADD_ADD:
077: writer.write(TextFileFunctions.CONFLICT_TYPE_ADD_ADD);
078:
079: break;
080:
081: case TextFileFunctions.ADD_REMOVE:
082: writer
083: .write(TextFileFunctions.CONFLICT_TYPE_ADD_REMOVE);
084:
085: break;
086:
087: case TextFileFunctions.REMOVE_ADD:
088: writer
089: .write(TextFileFunctions.CONFLICT_TYPE_REMOVE_ADD);
090:
091: break;
092: }
093:
094: writer.write("\n");
095: }
096:
097: // write first child
098: child1.toDocument(writer, enabled ? (depth + 1) : depth);
099:
100: if (child1.isEnabled()) {
101: writer.write("\n");
102: }
103:
104: // write split child
105: if (enabled) {
106: writer.write(TextFileFunctions.CONFLICT_BLOC_SPLIT
107: + child1.getOrigine()
108: + TextFileFunctions.CONFLICT_BLOC_ORIGINE_SEPARATOR
109: + child2.getOrigine());
110: writer.write("\n");
111: }
112:
113: // write second child
114: child2.toDocument(writer, enabled ? (depth + 1) : depth);
115:
116: if (enabled) {
117: if (child2.isEnabled()) {
118: writer.write("\n");
119: }
120:
121: // End the conflict bloc
122: writePadding(writer, depth);
123: writer.write(TextFileFunctions.CONFLICT_BLOC_END);
124: }
125:
126: writer.flush();
127: }
128:
129: public void compute() {
130: if (lines != null) {
131: // check if conflict or Txt node
132: if (!((String) lines.get(0))
133: .startsWith(TextFileFunctions.CONFLICT_BLOC_START)) {
134: TextNode node = new TextNode();
135:
136: for (Iterator i = lines.iterator(); i.hasNext();) {
137: node.appendLine((String) i.next());
138: }
139:
140: node.setOrigine(getOrigine());
141: updateNode(node);
142: } else {
143: // Compute children
144: String line;
145: AbstractNode currentNode = null;
146:
147: for (Iterator i = lines.iterator(); i.hasNext();) {
148: line = (String) i.next();
149:
150: if (line
151: .startsWith(TextFileFunctions.CONFLICT_BLOC_START)) {
152: setConflictType(TextFileFunctions
153: .getConflictType(line));
154: currentNode = new ConflictNode();
155: setChild(0, currentNode);
156: } else if (line
157: .startsWith(TextFileFunctions.CONFLICT_BLOC_END)) {
158: // end op the conflict
159: ((AbstractNode) getChildAt(0)).compute();
160: ((AbstractNode) getChildAt(1)).compute();
161: } else if (line
162: .startsWith(TextFileFunctions.CONFLICT_BLOC_SPLIT)) {
163: currentNode = new ConflictNode();
164: setChild(1, currentNode);
165:
166: String[] origine = TextFileFunctions
167: .getOrigineBlocNames(line);
168: ((AbstractNode) getChildAt(0))
169: .setOrigine(origine[0]);
170: ((AbstractNode) getChildAt(1))
171: .setOrigine(origine[1]);
172: } else {
173: currentNode.appendLine(line.substring(1));
174: }
175:
176: //
177: }
178: }
179: }
180:
181: lines = null;
182: }
183:
184: public String toString() {
185: switch (getConflictType()) {
186: case TextFileFunctions.ADD_ADD:
187: return "Conflict Add/Add";
188:
189: case TextFileFunctions.ADD_REMOVE:
190: return "Conflict Add/Remove";
191:
192: case TextFileFunctions.REMOVE_ADD:
193: return "Conflict Remove/Add";
194: }
195:
196: return "?";
197: }
198: }
|