KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > dotnet > DotnetResource


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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  */

18 package org.apache.tools.ant.taskdefs.optional.dotnet;
19
20 import org.apache.tools.ant.BuildException;
21
22 import java.io.File JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import org.apache.tools.ant.types.FileSet;
25 import java.util.Iterator JavaDoc;
26 import org.apache.tools.ant.Project;
27 import org.apache.tools.ant.DirectoryScanner;
28
29 /**
30  * Used by {@link DotnetCompile} to name resources.
31  * Could be upgraded to a datatype in the distant future.
32  * A resource maps to /res:file,name
33  */

34 public class DotnetResource {
35
36     /**
37      * name of resource
38      */

39     private File JavaDoc file;
40
41     /**
42      * embed (default) or link the resource
43      */

44     private boolean embed = true;
45
46     /**
47      * this is used in VBC and JSC
48      */

49     private Boolean JavaDoc isPublic = null;
50
51     /**
52      * name of the object
53      */

54     private String JavaDoc name = null;
55
56     /**
57      * A list of filesets with resources.
58      */

59     private ArrayList JavaDoc fileSets = new ArrayList JavaDoc();
60
61     /**
62      * a namespace to be used with <filesets>
63      */

64     private String JavaDoc namespace = null;
65
66     /**
67      * Return the embed attribute.
68      * @return the embed value.
69      */

70     public boolean isEmbed() {
71         return embed;
72     }
73
74     /**
75      * embed the resource in the assembly (default, true) or just link to it.
76      *
77      * @param embed a <code>boolean</code> value.
78      */

79     public void setEmbed(boolean embed) {
80         this.embed = embed;
81     }
82
83     /**
84      * The file resource.
85      * @return the file resource.
86      */

87     public File JavaDoc getFile() {
88         return file;
89     }
90
91     /**
92      * name the resource
93      *
94      * @param file the file.
95      */

96     public void setFile(File JavaDoc file) {
97         this.file = file;
98     }
99
100     /**
101      * Get the public attribute.
102      * @return the public attribute.
103      */

104     public Boolean JavaDoc 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 JavaDoc aPublic) {
114         isPublic = aPublic;
115     }
116
117     /**
118      * The name of the resource.
119      * @return the name of the resource.
120      */

121     public String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc namespace) {
150         if (namespace == null) {
151             this.namespace = null;
152         } else {
153             this.namespace = (namespace.length() == 0 || namespace.endsWith(".") ? namespace
154                     : namespace + '.');
155         }
156     }
157
158     private void checkParameters() {
159         if (hasFilesets()) {
160             if (getName() != null) {
161                 throw new BuildException(
162                         "Cannot use <resource name=\"...\"> attribute with filesets");
163             }
164             if (getFile() != null) {
165                 throw new BuildException(
166                         "Cannot use <resource file=\"...\"> attribute with filesets");
167             }
168         } else {
169             if (getNamespace() != null) {
170                 throw new BuildException(
171                         "Cannot use <resource namespace=\"...\"> attribute without filesets");
172             }
173         }
174     }
175
176     /**
177      * build the C# style parameter (which has no public/private option)
178      * @param p the current project.
179      * @param command the command.
180      * @param csharpStyle a <code>boolean</code> attribute.
181      */

182     public void getParameters(Project p, NetCommand command, boolean csharpStyle) {
183         checkParameters();
184         if (hasFilesets()) {
185             for (Iterator JavaDoc listIter = fileSets.iterator(); listIter.hasNext();) {
186                 FileSet fs = (FileSet) listIter.next();
187                 String JavaDoc baseDirectory = fs.getDir(p).toString();
188                 String JavaDoc namespace = getNamespace(); // ends with '.' or null
189
DirectoryScanner ds = fs.getDirectoryScanner(p);
190                 String JavaDoc[] files = ds.getIncludedFiles();
191                 for (int i = 0; i < files.length; i++) {
192                     String JavaDoc file = files[i];
193                     command.addArgument(getParameter(baseDirectory + File.separatorChar + file,
194                             (namespace == null ? null : namespace
195                                     + file.replace(File.separatorChar, '.')), csharpStyle));
196                 }
197             }
198         } else {
199             command.addArgument(getParameter(getFile().toString(), getName(), csharpStyle));
200         }
201     }
202
203     private String JavaDoc getParameter(String JavaDoc fileName, String JavaDoc name, boolean csharpStyle) {
204         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
205         buffer.append(isEmbed() ? "/resource" : "/linkresource");
206         buffer.append(':');
207         buffer.append(fileName);
208         if (name != null) {
209             buffer.append(',');
210             buffer.append(name);
211             if (csharpStyle) {
212                 if (getPublic() != null) {
213                     throw new BuildException("This compiler does not support the "
214                             + "public/private option.");
215                 } else {
216                     if (getPublic() != null) {
217                         buffer.append(',');
218                         buffer.append(getPublic().booleanValue() ? "public" : "private");
219
220                     }
221                 }
222             } else if (getPublic() != null) {
223                 throw new BuildException("You cannot have a public or private "
224                         + "option without naming the resource");
225             }
226         }
227         return buffer.toString();
228     }
229
230     /**
231      * Adds a resource file set.
232      *
233      * @param fileset
234      * FileSet
235      */

236     public void addFileset(FileSet fileset) {
237         fileSets.add(fileset);
238     }
239
240     /**
241      * Checks that <resource> node has embedded <filesets>
242      *
243      * @return boolean
244      */

245     public boolean hasFilesets() {
246         return fileSets.size() > 0;
247     }
248 }
249
Popular Tags