1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.waarp.openr66.context.task;
21
22 import org.waarp.common.logging.WaarpLogger;
23 import org.waarp.common.logging.WaarpLoggerFactory;
24 import org.waarp.common.transcode.CharsetsUtil;
25 import org.waarp.common.utility.FileConvert;
26 import org.waarp.openr66.context.ErrorCode;
27 import org.waarp.openr66.context.R66Result;
28 import org.waarp.openr66.context.R66Session;
29 import org.waarp.openr66.database.data.DbTaskRunner;
30 import org.waarp.openr66.protocol.configuration.Configuration;
31 import org.waarp.openr66.protocol.exception.OpenR66ProtocolSystemException;
32
33 import java.io.File;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 public class TranscodeTask extends AbstractTask {
65
66
67
68 private static final WaarpLogger logger =
69 WaarpLoggerFactory.getLogger(TranscodeTask.class);
70
71
72
73
74
75
76
77 public TranscodeTask(final String argRule, final int delay,
78 final String argTransfer, final R66Session session) {
79 super(TaskType.TRANSCODE, delay, argRule, argTransfer, session);
80 }
81
82 @Override
83 public final void run() {
84 final boolean success;
85 final DbTaskRunner runner = session.getRunner();
86 String arg = argRule;
87 arg = getReplacedValue(arg, BLANK.split(argTransfer));
88 final String[] args = BLANK.split(arg);
89 boolean dos2unix = false;
90 boolean unix2dos = false;
91 String fromCharset = null;
92 String toCharset = null;
93 String newfilename = null;
94 String extension = null;
95 for (int i = 0; i < args.length; i++) {
96 if ("-from".equalsIgnoreCase(args[i])) {
97 i++;
98 if (i < args.length) {
99 fromCharset = args[i];
100 }
101 } else if ("-to".equalsIgnoreCase(args[i])) {
102 i++;
103 if (i < args.length) {
104 toCharset = args[i];
105 }
106 } else if ("-newfile".equalsIgnoreCase(args[i])) {
107 i++;
108 if (i < args.length) {
109 newfilename = args[i];
110 }
111 } else if ("-extension".equalsIgnoreCase(args[i])) {
112 i++;
113 if (i < args.length) {
114 extension = args[i];
115 }
116 } else if ("-dos2unix".equalsIgnoreCase(args[i])) {
117 dos2unix = true;
118 } else if ("-unix2dos".equalsIgnoreCase(args[i])) {
119 unix2dos = true;
120 }
121 }
122 if (dos2unix && unix2dos) {
123 final R66Result result =
124 new R66Result(session, false, ErrorCode.Warning, runner);
125 futureCompletion.setResult(result);
126 logger.warn(
127 "Dos2Unix and Unix2Dos cannot be used simultaneously in Transcode: " +
128 runner.toShortString());
129 futureCompletion.setFailure(new OpenR66ProtocolSystemException(
130 "Dos2Unix and Unix2Dos cannot be used simultaneously in Transcode"));
131 return;
132 }
133 if (fromCharset == null || toCharset == null) {
134 if (!(dos2unix || unix2dos)) {
135 final R66Result result =
136 new R66Result(session, false, ErrorCode.Warning, runner);
137 futureCompletion.setResult(result);
138 logger.warn(
139 "Not enough argument in Transcode: " + runner.toShortString());
140 futureCompletion.setFailure(new OpenR66ProtocolSystemException(
141 "Not enough argument in Transcode"));
142 } else {
143
144 final FileConvert convert =
145 new FileConvert(null, unix2dos, false, null);
146 final File from = session.getFile().getTrueFile();
147 if (convert.convert(from, unix2dos)) {
148 futureCompletion.setSuccess();
149 } else {
150 final R66Result result =
151 new R66Result(session, false, ErrorCode.Internal, runner);
152 futureCompletion.setResult(result);
153 logger.error(
154 "Cannot Transcode " + argRule + ':' + argTransfer + " and " +
155 session);
156 futureCompletion.setFailure(
157 new OpenR66ProtocolSystemException("Cannot Transcode file"));
158 }
159 }
160 return;
161 }
162 final File from = session.getFile().getTrueFile();
163 String finalname = newfilename;
164 if (newfilename != null) {
165 finalname = newfilename;
166 } else if (extension != null) {
167 finalname = from.getAbsolutePath() + '.' + extension;
168 } else {
169 finalname = from.getAbsolutePath() + ".transcode";
170 }
171 success =
172 CharsetsUtil.transcode(from.getAbsolutePath(), fromCharset, finalname,
173 toCharset, Configuration.BUFFERSIZEDEFAULT);
174 if (success && (dos2unix || unix2dos)) {
175
176
177 final FileConvert convert = new FileConvert(null, unix2dos, false, null);
178 final File to = new File(finalname);
179 if (convert.convert(to, unix2dos)) {
180 futureCompletion.setSuccess();
181 } else {
182
183 logger.warn("Cannot Unix/Dos Transcode " + to + " : " + argRule + ':' +
184 argTransfer + " and " + session);
185 futureCompletion.setSuccess();
186 }
187 return;
188 }
189 if (success) {
190 futureCompletion.setSuccess();
191 } else {
192 logger.error("Cannot Transcode from " + fromCharset + " to " + toCharset +
193 " with " + argRule + ':' + argTransfer + " and " + session);
194 futureCompletion.setFailure(
195 new OpenR66ProtocolSystemException("Cannot Transcode file"));
196 }
197 }
198
199 }