forked from dita-ot/dita-ot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDITAOTCopy.java
153 lines (141 loc) · 4.03 KB
/
DITAOTCopy.java
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
/*
* This file is part of the DITA Open Toolkit project.
*
* Copyright 2005 IBM Corporation
*
* See the accompanying LICENSE file for applicable license.
*/
package org.dita.dost.ant;
import static org.dita.dost.util.Constants.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.util.FileUtils;
import org.dita.dost.util.Job;
/**
* Class description goes here.
*
* @author Wu, Zhi Qiang
*/
public final class DITAOTCopy extends Task {
private String includes = null;
private File includesFile = null;
private String relativePaths = null;
/** Destination directory */
private File destDir = null;
/**
* Default Constructor.
*
*/
public DITAOTCopy() {}
/**
* Set the copy files.
* @param incld The includes to set.
*/
public void setIncludes(final String incld) {
includes = incld;
}
/**
* Set the copy files list file.
* @param includesFile list file for includes to set.
*/
public void setIncludesfile(final File includesFile) {
this.includesFile = includesFile;
}
/**
* Set the destination directory.
* @param destdir the destination directory.
*/
public void setTodir(final File destdir) {
destDir = destdir;
}
/**
* Set the relative path from output directory.
* @param relPaths the relative path .
*/
public void setRelativePaths(final String relPaths) {
if (!relPaths.trim().isEmpty()) {
relativePaths = relPaths;
}
}
/**
* @see org.apache.tools.ant.Task#execute()
*/
@Override
public void execute() throws BuildException {
if (includes == null && includesFile == null) {
return;
}
if (destDir == null) {
throw new BuildException("Destination directory not defined");
}
if (!destDir.exists()) {
try {
Files.createDirectories(destDir.toPath());
} catch (FileAlreadyExistsException e) {
// Ignore
} catch (IOException e) {
throw new BuildException(e);
}
}
try {
final FileUtils fileUtils = FileUtils.newFileUtils();
final List<String> incs = getIncludes();
if (relativePaths == null) {
for (final String inc : incs) {
final File srcFile = new File(inc);
if (srcFile.exists()) {
final File destFile = new File(destDir, srcFile.getName());
fileUtils.copyFile(srcFile, destFile);
}
}
} else {
for (final String inc : incs) {
final File srcFile = new File(inc);
File destFile = null;
for (final String rel : relativePaths.split(COMMA)) {
final File temp = new File(destDir, rel);
if (temp.getName().equalsIgnoreCase(srcFile.getName())) {
destFile = temp;
break;
}
}
if (srcFile.exists() && destFile != null) {
fileUtils.copyFile(srcFile, destFile);
}
}
}
} catch (final IOException e) {
throw new BuildException(e.getMessage(), e);
}
}
private List<String> getIncludes() throws IOException {
if (includes == null && includesFile == null) {
final Job job = getProject().getReference(ANT_REFERENCE_JOB);
return job.getFileInfo(fi -> fi.isFlagImage).stream().map(fi -> fi.file.toString()).collect(Collectors.toList());
}
if (includesFile != null) {
final List<String> res = new ArrayList<>();
try (BufferedReader r = new BufferedReader(new FileReader(includesFile))) {
String line;
while ((line = r.readLine()) != null) {
if (!line.trim().isEmpty()) {
res.add(line.trim());
}
}
}
return res;
} else {
return Arrays.asList(includes.split(COMMA));
}
}
}