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.command.text;
034:
035: import jlibdiff.HunkAdd;
036:
037: import org.libresource.so6.core.WsConnection;
038: import org.libresource.so6.core.engine.DBType;
039: import org.libresource.so6.core.engine.util.Base64;
040: import org.libresource.so6.core.engine.util.FileUtils;
041:
042: import java.io.File;
043: import java.io.FileInputStream;
044: import java.io.FileOutputStream;
045: import java.io.FileReader;
046: import java.io.FileWriter;
047: import java.io.IOException;
048: import java.io.InputStream;
049: import java.io.InputStreamReader;
050: import java.io.LineNumberInputStream;
051: import java.io.LineNumberReader;
052: import java.io.OutputStreamWriter;
053: import java.io.PrintWriter;
054: import java.io.Writer;
055:
056: import java.nio.charset.Charset;
057:
058: import java.util.ArrayList;
059: import java.util.Collection;
060: import java.util.Iterator;
061:
062: public class AddBlock extends UpdateTextFile {
063: private static final long serialVersionUID = 3;
064: private int insertPoint = -1;
065: private ArrayList linesToAdd = null;
066:
067: public AddBlock(long ticket, String path, String wsName, long time,
068: boolean conflict, int insertPoint, ArrayList linesToAdd) {
069: super (ticket, path, wsName, time, conflict, null);
070: this .insertPoint = insertPoint;
071: this .linesToAdd = linesToAdd;
072: }
073:
074: public AddBlock(String path, WsConnection ws, int insertPoint,
075: Collection c) {
076: super (path, ws);
077: this .insertPoint = insertPoint;
078: this .linesToAdd = new ArrayList(c);
079: }
080:
081: public AddBlock(String path, WsConnection ws, HunkAdd ha) {
082: super (path, ws);
083: this .insertPoint = ha.getLD2(); //+1;
084: this .linesToAdd = new ArrayList(ha.getNewContent());
085: }
086:
087: public int getSize() {
088: return linesToAdd.size();
089: }
090:
091: public void setInsertPoint(int insertPoint) {
092: this .insertPoint = insertPoint;
093: }
094:
095: public int getInsertPoint() {
096: return this .insertPoint;
097: }
098:
099: public void slide(int inc) {
100: this .insertPoint += inc;
101: }
102:
103: public void setContent(Collection c) {
104: this .linesToAdd = new ArrayList(c);
105: }
106:
107: public Collection getContent() {
108: return this .linesToAdd;
109: }
110:
111: public void execute(String dir, DBType dbt) throws Exception {
112: //for(Iterator i =linesToAdd.iterator();i.hasNext();)
113: // System.out.println(i.next());
114: doTheJobOnFile(dir + File.separator + path);
115: }
116:
117: public void doTheJobOnFile(String path) throws Exception {
118: File tmpFile = File.createTempFile("AddBlock", null);
119: File f = new File(path);
120: InputStreamReader isr = new InputStreamReader(
121: new FileInputStream(f), Charset.forName(System
122: .getProperty("file.encoding")));
123: LineNumberReader input = new LineNumberReader(isr);
124: OutputStreamWriter osw = new OutputStreamWriter(
125: new FileOutputStream(tmpFile), Charset.forName(System
126: .getProperty("file.encoding")));
127: PrintWriter output = new PrintWriter(osw);
128:
129: String tmpLine;
130:
131: if (f.length() != 0) {
132: while ((input.getLineNumber() + 1) < this .insertPoint) {
133: tmpLine = input.readLine();
134:
135: if (tmpLine == null) {
136: break;
137: }
138:
139: output.println(tmpLine);
140: }
141: }
142:
143: for (Iterator i = linesToAdd.iterator(); i.hasNext();) {
144: output.println((String) i.next());
145: }
146:
147: String line = null;
148:
149: while ((line = input.readLine()) != null) {
150: output.println(line);
151: }
152:
153: input.close();
154: output.close();
155: FileUtils.copy(tmpFile.getPath(), path);
156:
157: if (!tmpFile.delete()) {
158: tmpFile.deleteOnExit();
159: }
160: }
161:
162: public String toString() {
163: return "AddBlock(" + path + "," + getInsertPoint() + ","
164: + getSize() + ")";
165: }
166:
167: public String toStringVerbose() {
168: return "AddBlock(" + path + "," + getInsertPoint() + ","
169: + getSize() + "," + getContent() + ")";
170: }
171:
172: public boolean equals(Object o) {
173: if (o instanceof AddBlock) {
174: AddBlock a = (AddBlock) o;
175: boolean b = path.equals(a.path)
176: && (insertPoint == a.insertPoint)
177: && linesToAdd.equals(a.linesToAdd);
178:
179: // String msg =
180: // ""
181: // + "\n AddBlock Equals ["
182: // + Boolean.toString(b)
183: // + "]"
184: // + "\n this"
185: // + "\n path:"
186: // + path
187: // + "\n insertPoint:"
188: // + insertPoint
189: // + "\n linesToAdd:"
190: // + linesToAdd.toString()
191: // + "\n param"
192: // + "\n path:"
193: // + a.path
194: // + "\n insertPoint:"
195: // + a.insertPoint
196: // + "\n linesToAdd:"
197: // + a.linesToAdd.toString()
198: // + "\n";
199: // Logger.getLogger("arf").info(msg);
200: return b;
201: } else {
202: return false;
203: }
204: }
205:
206: public void toXML(Writer osw) throws IOException {
207: super .toXML(osw);
208: osw.write("<insertpoint>" + insertPoint + "</insertpoint>");
209: osw.write("<contrib>");
210: osw.write(Long.toString(getSize()));
211: osw.write("</contrib>");
212: osw.write("<linesToAdd>");
213:
214: Iterator it = linesToAdd.iterator();
215:
216: while (it.hasNext()) {
217: String line = (String) it.next();
218: osw.write("<line>");
219: osw.write(Base64.encodeBytes(line.getBytes("UTF-8")));
220: osw.write("</line>");
221: }
222:
223: osw.write("</linesToAdd>");
224: osw.flush();
225: }
226: }
|