001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs.optional.dotnet;
019:
020: import org.apache.tools.ant.BuildException;
021:
022: import java.io.File;
023: import java.util.ArrayList;
024: import org.apache.tools.ant.types.FileSet;
025: import java.util.Iterator;
026: import org.apache.tools.ant.Project;
027: import org.apache.tools.ant.DirectoryScanner;
028:
029: /**
030: * Used by {@link DotnetCompile} to name resources.
031: * Could be upgraded to a datatype in the distant future.
032: * A resource maps to /res:file,name
033: */
034: public class DotnetResource {
035:
036: /**
037: * name of resource
038: */
039: private File file;
040:
041: /**
042: * embed (default) or link the resource
043: */
044: private boolean embed = true;
045:
046: /**
047: * this is used in VBC and JSC
048: */
049: private Boolean isPublic = null;
050:
051: /**
052: * name of the object
053: */
054: private String name = null;
055:
056: /**
057: * A list of filesets with resources.
058: */
059: private ArrayList fileSets = new ArrayList();
060:
061: /**
062: * a namespace to be used with <filesets>
063: */
064: private String namespace = null;
065:
066: /**
067: * Return the embed attribute.
068: * @return the embed value.
069: */
070: public boolean isEmbed() {
071: return embed;
072: }
073:
074: /**
075: * embed the resource in the assembly (default, true) or just link to it.
076: *
077: * @param embed a <code>boolean</code> value.
078: */
079: public void setEmbed(boolean embed) {
080: this .embed = embed;
081: }
082:
083: /**
084: * The file resource.
085: * @return the file resource.
086: */
087: public File getFile() {
088: return file;
089: }
090:
091: /**
092: * name the resource
093: *
094: * @param file the file.
095: */
096: public void setFile(File file) {
097: this .file = file;
098: }
099:
100: /**
101: * Get the public attribute.
102: * @return the public attribute.
103: */
104: public Boolean getPublic() {
105: return isPublic;
106: }
107:
108: /**
109: * VB and J# only: is a resource public or not?
110: *
111: * @param aPublic a <code>boolean</code> value.
112: */
113: public void setPublic(Boolean aPublic) {
114: isPublic = aPublic;
115: }
116:
117: /**
118: * The name of the resource.
119: * @return the name of the resource.
120: */
121: public String getName() {
122: return name;
123: }
124:
125: /**
126: * should the resource have a name?
127: *
128: * @param name the name of the resource.
129: */
130: public void setName(String name) {
131: this .name = name;
132: }
133:
134: /**
135: * Filesets root namespace. The value always ends with '.' .
136: *
137: * @return String namespace name
138: */
139: public String getNamespace() {
140: return namespace;
141: }
142:
143: /**
144: * Sets filesets root namespace.
145: *
146: * @param namespace
147: * String root namespace
148: */
149: public void setNamespace(String namespace) {
150: if (namespace == null) {
151: this .namespace = null;
152: } else {
153: this .namespace = (namespace.length() == 0
154: || namespace.endsWith(".") ? namespace
155: : namespace + '.');
156: }
157: }
158:
159: private void checkParameters() {
160: if (hasFilesets()) {
161: if (getName() != null) {
162: throw new BuildException(
163: "Cannot use <resource name=\"...\"> attribute with filesets");
164: }
165: if (getFile() != null) {
166: throw new BuildException(
167: "Cannot use <resource file=\"...\"> attribute with filesets");
168: }
169: } else {
170: if (getNamespace() != null) {
171: throw new BuildException(
172: "Cannot use <resource namespace=\"...\"> attribute without filesets");
173: }
174: }
175: }
176:
177: /**
178: * build the C# style parameter (which has no public/private option)
179: * @param p the current project.
180: * @param command the command.
181: * @param csharpStyle a <code>boolean</code> attribute.
182: */
183: public void getParameters(Project p, NetCommand command,
184: boolean csharpStyle) {
185: checkParameters();
186: if (hasFilesets()) {
187: for (Iterator listIter = fileSets.iterator(); listIter
188: .hasNext();) {
189: FileSet fs = (FileSet) listIter.next();
190: String baseDirectory = fs.getDir(p).toString();
191: String namespace = getNamespace(); // ends with '.' or null
192: DirectoryScanner ds = fs.getDirectoryScanner(p);
193: String[] files = ds.getIncludedFiles();
194: for (int i = 0; i < files.length; i++) {
195: String file = files[i];
196: command.addArgument(getParameter(baseDirectory
197: + File.separatorChar + file,
198: (namespace == null ? null : namespace
199: + file.replace(File.separatorChar,
200: '.')), csharpStyle));
201: }
202: }
203: } else {
204: command.addArgument(getParameter(getFile().toString(),
205: getName(), csharpStyle));
206: }
207: }
208:
209: private String getParameter(String fileName, String name,
210: boolean csharpStyle) {
211: StringBuffer buffer = new StringBuffer();
212: buffer.append(isEmbed() ? "/resource" : "/linkresource");
213: buffer.append(':');
214: buffer.append(fileName);
215: if (name != null) {
216: buffer.append(',');
217: buffer.append(name);
218: if (csharpStyle) {
219: if (getPublic() != null) {
220: throw new BuildException(
221: "This compiler does not support the "
222: + "public/private option.");
223: } else {
224: if (getPublic() != null) {
225: buffer.append(',');
226: buffer
227: .append(getPublic().booleanValue() ? "public"
228: : "private");
229:
230: }
231: }
232: } else if (getPublic() != null) {
233: throw new BuildException(
234: "You cannot have a public or private "
235: + "option without naming the resource");
236: }
237: }
238: return buffer.toString();
239: }
240:
241: /**
242: * Adds a resource file set.
243: *
244: * @param fileset
245: * FileSet
246: */
247: public void addFileset(FileSet fileset) {
248: fileSets.add(fileset);
249: }
250:
251: /**
252: * Checks that <resource> node has embedded <filesets>
253: *
254: * @return boolean
255: */
256: public boolean hasFilesets() {
257: return fileSets.size() > 0;
258: }
259: }
|