1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.github.maven_nar.cpptasks;
21
22 import java.io.File;
23 import java.io.IOException;
24
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.types.DataType;
27
28
29
30
31
32 public final class DistributerMap extends DataType {
33
34
35
36 private String ifCond;
37
38
39
40
41 private String unlessCond;
42
43
44
45
46
47 private File localName;
48
49
50
51
52 private String canonicalPath;
53
54
55
56
57
58 private String remoteName;
59
60
61
62
63 private char remoteSeparator = File.separatorChar;
64
65
66
67
68
69 private String hosts;
70
71
72
73
74
75 public DistributerMap() {
76 }
77
78
79
80
81 public void execute() {
82 throw new org.apache.tools.ant.BuildException("Not an actual task, but looks like one for documentation purposes");
83 }
84
85
86
87
88
89
90
91 public File getLocal() {
92 return this.localName;
93 }
94
95
96
97
98
99
100
101 public String getRemote() {
102 return this.remoteName;
103 }
104
105
106
107
108
109
110
111 public boolean isActive() {
112 return CUtil.isActive(getProject(), this.ifCond, this.unlessCond);
113 }
114
115
116
117
118
119
120
121 public void setHosts(final String value) {
122 this.hosts = value;
123 }
124
125
126
127
128
129
130
131
132
133
134
135
136
137 public void setIf(final String propName) {
138 this.ifCond = propName;
139 }
140
141
142
143
144
145
146
147 public void setLocal(final File value) {
148 if (value == null) {
149 throw new NullPointerException("value");
150 }
151 if (value.exists() && !value.isDirectory()) {
152 throw new BuildException("local should be a directory");
153 }
154 this.localName = value;
155 try {
156 this.canonicalPath = this.localName.getCanonicalPath();
157 } catch (final IOException ex) {
158 throw new BuildException(ex);
159 }
160 }
161
162
163
164
165
166
167
168 public void setRemote(final String value) {
169 this.remoteName = value;
170 }
171
172
173
174
175
176
177
178 public void setRemoteSeparator(final String value) {
179 if (value != null && value.length() != 1) {
180 throw new BuildException("remote separator must be a single character");
181 }
182 this.remoteSeparator = value.charAt(0);
183 }
184
185
186
187
188
189
190
191
192
193
194
195
196
197 public void setUnless(final String propName) {
198 this.unlessCond = propName;
199 }
200
201
202
203
204
205
206
207
208
209
210 public String toRemote(final String host, final File localFile) {
211 if (this.remoteName != null && (this.hosts == null || this.hosts.contains(host))) {
212 try {
213 final String canonical = localFile.getCanonicalPath();
214 if (canonical.startsWith(this.canonicalPath)) {
215 if (isActive()) {
216 return this.remoteName
217 + canonical.substring(this.canonicalPath.length()).replace(File.separatorChar, this.remoteSeparator);
218 }
219 }
220 } catch (final IOException ex) {
221 return null;
222 }
223 }
224 return null;
225 }
226
227 }