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 org.libresource.so6.core.WsConnection;
036: import org.libresource.so6.core.command.fs.AddFile;
037: import org.libresource.so6.core.engine.DBType;
038: import org.libresource.so6.core.engine.util.FileUtils;
039:
040: import java.io.File;
041: import java.io.FileReader;
042: import java.io.IOException;
043: import java.io.LineNumberReader;
044: import java.io.OutputStreamWriter;
045:
046: public class AddTxtFile extends AddFile {
047: private static final long serialVersionUID = 3;
048: private int nbLines;
049:
050: public AddTxtFile(long ticket, String path, String wsName,
051: long time, boolean conflict, String attachement) {
052: super (ticket, path, wsName, time, conflict, attachement);
053:
054: try {
055: nbLines = countNbLine(attachement);
056: } catch (Exception e) {
057: nbLines = 0;
058: }
059: }
060:
061: public AddTxtFile(String path, WsConnection ws) {
062: super (path, ws);
063:
064: String tmppath;
065:
066: try {
067: tmppath = File.createTempFile("txtFile", null).getPath();
068:
069: File f = new File(tmppath);
070: f.deleteOnExit();
071: FileUtils.copy(ws.getPath() + File.separator + path,
072: tmppath);
073: } catch (Exception e) {
074: throw new RuntimeException(e);
075: }
076:
077: this .setAttachement(tmppath);
078:
079: try {
080: nbLines = countNbLine(attachement);
081: } catch (Exception e) {
082: nbLines = 0;
083: }
084: }
085:
086: public AddTxtFile(String path, WsConnection ws, String attachement) {
087: super (path, ws);
088: this .setAttachement(attachement);
089:
090: try {
091: nbLines = countNbLine(attachement);
092: } catch (Exception e) {
093: nbLines = 0;
094: }
095: }
096:
097: public AddTxtFile(String path, WsConnection ws, File tmpDestDir) {
098: super (path, ws);
099:
100: String tmppath;
101:
102: try {
103: tmppath = File.createTempFile("attach", null, tmpDestDir)
104: .getPath();
105:
106: File f = new File(tmppath);
107: FileUtils.copy(ws.getPath() + File.separator + path,
108: tmppath);
109: } catch (Exception e) {
110: throw new RuntimeException(e);
111: }
112:
113: this .setAttachement(tmppath);
114:
115: try {
116: nbLines = countNbLine(attachement);
117: } catch (Exception e) {
118: nbLines = 0;
119: }
120: }
121:
122: public String toString() {
123: return "AddTxtFile(" + path + ")";
124: }
125:
126: public boolean equals(Object o) {
127: if (o instanceof AddTxtFile) {
128: return path.equals(((AddTxtFile) o).path);
129: } else {
130: return false;
131: }
132: }
133:
134: public int getSize() {
135: return nbLines;
136: }
137:
138: public void execute(String dir, DBType dbt) throws Exception {
139: FileUtils.copy(this .getAttachement(), dir + File.separator
140: + path);
141:
142: /*
143: * File f = new File(dir + File.separator + path); if (f.exists()) throw
144: * new Exception("File already exist :" + f); if (!f.createNewFile()) {
145: * throw new Exception("Cannot create :" + f); }
146: */
147: dbt.add(path, DBType.TYPE_FILE_TXT);
148: }
149:
150: public static int countNbLine(String filePath) throws Exception {
151: FileReader fr = new FileReader(filePath);
152: LineNumberReader lnr = new LineNumberReader(fr);
153:
154: while (lnr.readLine() != null)
155: ;
156:
157: fr.close();
158:
159: return lnr.getLineNumber();
160: }
161:
162: public void toXML(OutputStreamWriter osw) throws IOException {
163: super .toXML(osw);
164: osw.write("<contrib>");
165: osw.write(Long.toString(getSize()));
166: osw.write("</contrib>");
167: }
168: }
|