]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/DistributerMap.java
Restructuring for better separation of Tool packages.
[mirror_edk2.git] / Tools / Java / Source / Cpptasks / net / sf / antcontrib / cpptasks / DistributerMap.java
1 /*
2 *
3 * Copyright 2004 The Ant-Contrib project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package net.sf.antcontrib.cpptasks;
18
19 import java.io.File;
20 import java.io.IOException;
21
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.types.DataType;
24
25 /**
26 * Local to remote filename mapping (Experimental).
27 *
28 */
29 public final class DistributerMap
30 extends DataType {
31 /**
32 * if property.
33 */
34 private String ifCond;
35
36 /**
37 * unless property.
38 */
39 private String unlessCond;
40
41 /**
42 * local directory name.
43 *
44 */
45 private File localName;
46
47 /**
48 * Canonical local file name.
49 */
50 private String canonicalPath;
51
52 /**
53 * remote name.
54 *
55 */
56 private String remoteName;
57
58 /**
59 * Separator (/ or \) character on remote system.
60 */
61 private char remoteSeparator = File.separatorChar;
62
63 /**
64 * hosts that for which this map is valid.
65 *
66 */
67 private String hosts;
68
69 /**
70 * Constructor.
71 *
72 */
73 public DistributerMap() {
74 }
75
76 /**
77 * Required by documentation generator.
78 */
79 public void execute() {
80 throw new org.apache.tools.ant.BuildException(
81 "Not an actual task, but looks like one for documentation purposes");
82 }
83
84 /**
85 * Returns true if the if and unless conditions (if any) are
86 * satisfied.
87 *
88 * @return true if this object is active.
89 */
90 public boolean isActive() {
91 return CUtil.isActive(getProject(), ifCond, unlessCond);
92 }
93
94 /**
95 * Sets the property name for the 'if' condition.
96 *
97 * This object will be ignored unless the property is defined.
98 *
99 * The value of the property is insignificant, but values that would imply
100 * misinterpretation ("false", "no") will throw an exception when
101 * evaluated.
102 *
103 * @param propName
104 * property name
105 */
106 public void setIf(final String propName) {
107 ifCond = propName;
108 }
109
110 /**
111 * Set the property name for the 'unless' condition.
112 *
113 * If named property is set, the define will be ignored.
114 *
115 * The value of the property is insignificant, but values that would imply
116 * misinterpretation ("false", "no") of the behavior will throw an
117 * exception when evaluated.
118 *
119 * @param propName
120 * name of property
121 */
122 public void setUnless(final String propName) {
123 unlessCond = propName;
124 }
125
126 /**
127 * Gets local directory.
128 * @return local directory, may be null.
129 *
130 */
131 public File getLocal() {
132 return localName;
133 }
134
135 /**
136 * Gets remote name for directory.
137 * @return remote name, may be null.
138 *
139 */
140 public String getRemote() {
141 return remoteName;
142 }
143
144 /**
145 * Converts the local file name to the remote name for the same file.
146 *
147 * @param host host
148 * @param localFile local file
149 * @return remote name for local file, null if unknown.
150 */
151 public String toRemote(final String host, final File localFile) {
152 if (remoteName != null
153 && (hosts == null || hosts.indexOf(host) >= 0)) {
154 try {
155 String canonical = localFile.getCanonicalPath();
156 if (canonical.startsWith(canonicalPath)) {
157 if (isActive()) {
158 return remoteName
159 + canonical.substring(canonicalPath.length()).replace(File.
160 separatorChar, remoteSeparator);
161 }
162 }
163 } catch (IOException ex) {
164 return null;
165 }
166 }
167 return null;
168 }
169
170 /**
171 * Sets local directory for base of mapping.
172 *
173 * @param value value
174 */
175 public void setLocal(final File value) {
176 if (value == null) {
177 throw new NullPointerException("value");
178 }
179 if (value.exists() && !value.isDirectory()) {
180 throw new BuildException("local should be a directory");
181 }
182 localName = value;
183 try {
184 canonicalPath = localName.getCanonicalPath();
185 } catch (IOException ex) {
186 throw new BuildException(ex);
187 }
188 }
189
190 /**
191 * Sets remote name for directory.
192 * @param value remote name for directory
193 */
194 public void setRemote(final String value) {
195 remoteName = value;
196 }
197
198 /**
199 * Sets the separator character (/ or \) for the remote system.
200 * @param value separator character
201 */
202 public void setRemoteSeparator(final String value) {
203 if (value != null && value.length() != 1) {
204 throw new BuildException("remote separator must be a single character");
205 }
206 remoteSeparator = value.charAt(0);
207 }
208
209 /**
210 * Sets hosts for which this mapping is valid.
211 *
212 * @param value hosts
213 */
214 public void setHosts(final String value) {
215 hosts = value;
216 }
217
218 }