-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
376 lines (318 loc) · 11.1 KB
/
Main.java
File metadata and controls
376 lines (318 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/**
* Ýòà ñèñòåìà ïðåäíàçíà÷åíà äëÿ ïðåîáðàçîâàíèÿ âõîäíîãî xml-ôàéëà òàêèì îáðàçîì, ÷òîáû â ðåçóëüòàòå ïðåîáðàçîâàííûé òåêñò ñîñòîÿë èç äâóõ ñòðîê, ãäå ïåðâàÿ ñòðîêà - xml-çàãîëîâîê, à âòîðàÿ ñòðîêà - âåñü xml-êîíòåíò äîêóìåíòà.
* Ñèñòåìà äîëæíà ïîçâîëÿòü ñîçäàâàòü íîâûé ôàéë, ëèáî ïåðåçàïèñûâàòü òåêóùèé.
* Ñèñòåìà äîëæíà ïðîâåðÿòü äîñòóïíîå ìåñòî íà äèñêå. Åñëè ìåñòà íåäîñòàòî÷íî, òî ïðîãðàììà äîëæíà ïðåðûâàòü ðàáîòó è ïèñàòü â êîíñîëü ñîîáùåíèå î íåõâàòêè ïàìÿòè; ïðè ýòîì îíà äîëæíà âûâîäèòü òåêóùèé îáú¸ì ïàìÿòè è íåîáõîäèìûé îáú¸ì äëÿ íîâîãî ôàéëà.
* Ñèñòåìà äîëæíà âåðíóòü âåñü ñòåê âûçîâîâ, åñëè ïðîèçîøëî íåîáðàáîòàííîå èñêëþ÷åíèå.
*/
package main;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.time.LocalTime;
import java.util.Optional;
import org.apache.commons.cli.*;
/**
* @author Artyom
*
*/
public class Main {
/*
* "/" - Unix "\\" - Windows
*/
private static final String WINDOWS_OS_PATH_DELIMITER = "\\";
private static final String UNIX_OS_PATH_DELIMITER = "\\";
private static final boolean isDebug = true;
private static final boolean isDebugProcess = true;
/**
*
*/
public static void main(String[] args) throws Exception {
Options options = new Options();
Option inputFileNameOption = new Option("i", "inputFile", true, "input file path");
inputFileNameOption.setRequired(true);
options.addOption(inputFileNameOption);
Option outputFileNameOption = new Option("o", "outputFile", true, "output file");
outputFileNameOption.setRequired(false);
options.addOption(outputFileNameOption);
/*
* Option modeOption = new Option("m", "mode", true, "output file");
* modeOption.setRequired(false); options.addOption(modeOption);
*/
Option rewriteIfExistsOption = new Option("r", "rewriteIfExists", false,
"If output file exists - then rewrite it");
rewriteIfExistsOption.setRequired(false);
options.addOption(rewriteIfExistsOption);
CommandLineParser parser = new DefaultParser();
HelpFormatter formatter = new HelpFormatter();
CommandLine cmd;
try {
cmd = parser.parse(options, args);
String inputFileName = cmd.getOptionValue("inputFile");
String outputFileName = cmd.getOptionValue("outputFile");
/* String modeValue = cmd.getOptionValue("mode"); */
boolean isRewriteIfExists = cmd.hasOption("rewriteIfExists");
//boolean isRewriteIfExists = Optional.ofNullable(cmd.hasOption("rewriteIfExists")).orElse(false);
if (isDebug) {
/** debug */
System.out.println(inputFileName);
System.out.println(outputFileName);
/* System.out.println(modeValue); */
System.out.println(isRewriteIfExists);
/** debug */
}
if (inputFileName.length() == 0) {
System.out.println("Input file name is empty.");
System.exit(1);
}
File inputFile = new File(inputFileName);
if (inputFile.isDirectory()) {
System.out.println("inputFileName' is a directory.");
System.exit(2);
}
if (outputFileName != null) {
if (outputFileName.length() == 0) {
System.out.println("Output file name is empty.");
System.exit(3);
}
File outputFile = new File(outputFileName);
if (outputFile.isDirectory()) {
System.out.println("'outputFileName' is a directory.");
System.exit(4);
}
}
/*
* if (modeValue.length() > 1) { throw new
* Exception("'mode' should be represented as single character"); } char mode =
* modeValue.charAt(0);
*
* if (isDebug) {
*//** debug */
/*
* System.out.println(mode);
*//** debug *//*
* }
*/
/*
* Ïðîâåðèì, ÷òî íà÷àëüíûé è êîíå÷íûé ôàéëû íå ñîâïàäàþò. Íåîáõîäèìî òàêæå
* ïðîâåðèòü âîçìîæíîñòü ññûëêè - êîãäà ïóòü ÿâëÿåòñÿ ññûëêîé, à íå ÿâíûì ïóò¸ì.
*/
if (outputFileName != null && new File(inputFileName).toPath().toRealPath().equals(new File(outputFileName).toPath().toRealPath()))
{
System.out.println("Input file name specified and output file name are the same. Need omit ouptutFileName to rewrite original file.");
System.exit(6);
}
/*
* Ïðîâåðèì, ÷òî öåëåâîé óêàçàííûé ôàéë íå ñóùåñòâóåò, ëèáî óêàçàí ôëàã ïåðåçàïèñè "rewriteIfExists"
*/
if (outputFileName != null && new File(outputFileName).exists() && !isRewriteIfExists)
{
System.out.println(
"Output file exists already and rewriteIfExists option is not present. Specify not existing file name or -rewriteIfExists flag.");
System.exit(7);
}
/*
* Åñëè âûõîäíîé ôàéë íå çàäàí, òî öåëèìñÿ â èñõîäíûé ôàéë.
* Óñòàíîâèì ôëàã ïåðåçàïèñè, ðàç ÿâíî íóæíî ïåðåçàïèñàòü èñõîäíûé ôàéë
*/
if (outputFileName == null)
{
outputFileName = inputFileName;
isRewriteIfExists = true;
}
/*
* Ïðîâåðèì, ÷òî íà äèñêå äîñòàòî÷íî ìåñòà
*/
long inputFileSize = inputFile.length();
if (isDebug) {
/** debug */
System.out.println("inputFileSize = " + inputFileSize + " B");
/** debug */
}
/*
* Îïðåäåëèì, êàêîé ñëýø èñïîëüçóåòñÿ â òåêóùåé ñèñòåìå äëÿ òîãî, ÷òîáû ïîëó÷èòü
* ïóòü ê êîðíþ äèñêà, íà êîòîðûé íóæíî ïèñàòü ôàéë.
*/
String OSpathDelimeter = null;
if (System.getProperty("os.name").matches("Windows"))
{
OSpathDelimeter = WINDOWS_OS_PATH_DELIMITER;
} else {
OSpathDelimeter = UNIX_OS_PATH_DELIMITER;
}
/*
* Âîçüì¸ì êîëè÷åñòâî áàéò, ñâîáîäíûõ â êîðíå.
*/
long outputFilePathFreeSpace = (new File(outputFileName.substring(0, outputFileName.indexOf(OSpathDelimeter)))).getFreeSpace();
if (isDebug)
{
/** debug */
System.out.println("getFreeSpace = " + outputFilePathFreeSpace);
System.out.println("getFreeSpace/1024/1024 = " + outputFilePathFreeSpace / 1024 / 1024);
System.out.println();
/** debug */
}
/*
* Åñëè ðàçìåð âõîäíîãî ôàéëà áîëüøå, ÷åì ìåñòà äëÿ âûõîäíîãî ôàéëà:
*/
if (inputFileSize > outputFilePathFreeSpace) {
System.out.println("Need additional freespace to write new file. Need " + inputFileSize / 1024 / 1024
+ " MB more.");
System.exit(5);
}
/*
* Çàïóñêàåì îáðàáîòêó ôàéëà
*/
if (isDebugProcess) {
processFile(inputFileName, outputFileName, /*mode, */isRewriteIfExists);
}
}
catch (ParseException e)
{
System.out.println(e.getMessage());
formatter.printHelp("utility-name", options);
System.exit(8);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
System.exit(9);
}
}
/**
* @param inputFileName - ïîëíûé ïóòü ê èñõîäíîìó ôàéëó
* @param outputFileName - ïîëíûé ïóòü ê íîâîìó ôàéëó
* @param isRewriteIfExists - åñëè true - ïåðåçàïèñàòü ôàéë, åñëè îí ñóùåñòâóåò; èíà÷å - ñãåíåðèðîâàòü èñêëþ÷åíèå
*/
/*
* @param mode - ðåæèì, â êîòîðîì äîëæíà ðàáîòàòü ïðîãðàììà. Êîíêðåòíûé ðåæèì
* îïðåäåëÿåò ïîâåäåíèå ïðîãðàììû â îòíîøåíèè ñîçäàíèÿ ôàéëà è ïåðåçàïèñè
* èõîäíîãî ôàéëà. Ïàðàìåòð ÿâëÿåòñÿ àíãëèéñêîé áóêâîé è ìîæåò áûòü â ëþáîì
* ðåãèñòðå. <ol> <li>r - (replace) ïåðåçàïèñàòü âõîäíîé ôàéë. Ïðè ýòîì
* rewriteIfExists ñ÷èòàåòñÿ ðàâíûì true <li>c - (create) ñîçäàòü íîâûé ôàéë ñ
* óêàçàííûì èìåíåì, íå èçìåíÿòü âõîäíîé ôàéë </ol>
*/
@SuppressWarnings("unused")
public static void processFile(String inputFileName, String outputFileName,
/* char mode, */boolean isRewriteIfExists) {
System.out.println("Start of work: " + String.valueOf(LocalTime.now()));
if (false) {
Charset ch = Charset.defaultCharset();
System.out.println(ch.name());
System.out.println(ch.displayName());
System.out.println(ch.toString());
try {
File tempFile = File.createTempFile(new File(inputFileName).getName(), ".tmp");
System.out.println(tempFile.getPath());
} catch (IOException e) {
e.printStackTrace();
System.exit(10);
}
} else {
try {
/*
* Ïîëó÷èì óíèêàëüíîå èìÿ âðåìåííîãî ôàéëà  íåãî áóäåì ïèñàòü òåêñò, ïîñëå ÷åãî
* ïåðåèìåíóåì ôàéë â òðåáóåìîå íàçâàíèå
*/
File tempFile = File.createTempFile(new File(inputFileName).getName(), ".tmp");
/* TimeUnit.SECONDS.sleep(3); */
/*
* Îòêðûâàåì áóôåðíîå ÷òåíèå äëÿ èñõîäíîãî ôàéëà
*/
BufferedReader bufferedReader = new BufferedReader(new FileReader(
inputFileName/* "J:\\MyTemp\\XML_test\\XML-bill_3813158_OOO_MELON_TEST.xml" *//*
* "J:\\MyTemp\\XML_test\\XML-bill 70021563 OOO LSR Nedvizhimost 03.01.2018.xml"
*/));
/*
* Ñîçäà¸ì öåëåâîé ôàéë
*/
PrintWriter writer = new PrintWriter(/* outputFile */tempFile/*
* "J:\\MyTemp\\XML_test\\XML-bill 70021563 OOO LSR Nedvizhimost 03.01.2018 new.xml"
*//* , "UTF-8" */);
/*
* ×èòàåì ñòðîêó èç èñõîäíîãî ôàéëà è çàïèñûâàåì å¸ â êîíå÷íûé ôàéë, ïîêà íå
* äîéä¸ì äî êîíöà ôàéëà:
*/
/*
* Ïåðâóþ ñòðîêó çàïèøåì ñ ïåðåíîñîì êàðåòêè, ò.ê. ýòî äîëæåí áûòü
* XML-çàãîëîâîê.
*/
/*
* Ñíà÷àëà âñòðå÷àåò òàêîå. È ýòî íóæíî îáúåäèíèòü â îäíó îòäåëüíóþ ñòðîêó.
* <?xml version="1.0" encoding="wi ndows-1251" ?>
*/
String currentLine = bufferedReader.readLine();
writer.print(currentLine); // <?xml version="1.0" encoding="wi
// System.out.println(currentLine);
currentLine = bufferedReader.readLine();
writer.println(currentLine); // ndows-1251" ?>
// System.out.println(currentLine);
/*
* ×èòàåì ñëåäóþùóþ ñòðîêó - óæå ñ ïåðâîé ñòðîêîé, ê êîòîðîé íóæíî ïðèêëåÿòü
* îñòàëüíûå
*/
currentLine = bufferedReader.readLine();
while (currentLine != null) {
/*
* Çàïèñûâàåì ïîëó÷åííóþ ñòðîêó â öåëåâîé ôàéë
*/
writer.print(currentLine);
/*
* ×èòàåì ñëåäóþùóþ ñòðîêó
*/
currentLine = bufferedReader.readLine();
}
writer.println();
/*
* Çàêðûâàåì öåëåâîé ôàéë
*/
writer.close();
/*
* Çàêðûâàåì áóôåðíûé ÷èòàëüùèê
*/
bufferedReader.close();
/*
* Ïåðåíîñèì âðåìåííûé ôàéë â çàïðîøåííîå ìåñòî
*/
// tempFile
Path temp = null;
String tempFilePath = tempFile.getPath();
String outputFilePath = new File(outputFileName).getPath();
if (isRewriteIfExists) {
temp = Files.move(Paths.get(tempFilePath), // separatorChar
Paths.get(outputFilePath), StandardCopyOption.REPLACE_EXISTING);
} else {
temp = Files.move(Paths.get(tempFilePath), Paths.get(outputFilePath));
}
if (temp != null) {
System.out
.println("File " + tempFilePath + " renamed and moved successfully into " + outputFilePath);
} else {
System.out.println("Failed to move the file " + tempFilePath + " into " + outputFilePath);
System.exit(11);
}
}
/*
* catch (InterruptedException e) { System.out.println("Can't sleep");
* e.printStackTrace(); System.exit(12); }
*/
catch (IOException e) {
System.out.println("Error while reading or writing file");
e.printStackTrace();
System.exit(13);
}
}
System.out.println("End of work: " + String.valueOf(LocalTime.now()));
}
}