KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > windows > Attrib


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
19 package org.apache.tools.ant.taskdefs.optional.windows;
20
21 import org.apache.tools.ant.BuildException;
22 import org.apache.tools.ant.taskdefs.ExecuteOn;
23 import org.apache.tools.ant.taskdefs.condition.Os;
24 import org.apache.tools.ant.types.FileSet;
25
26 import java.io.File JavaDoc;
27
28 /**
29  * Attrib equivalent for Win32 environments.
30  * Note: Attrib parameters /S and /D are not handled.
31  *
32  * @since Ant 1.6
33  */

34 public class Attrib extends ExecuteOn {
35
36     private static final String JavaDoc ATTR_READONLY = "R";
37     private static final String JavaDoc ATTR_ARCHIVE = "A";
38     private static final String JavaDoc ATTR_SYSTEM = "S";
39     private static final String JavaDoc ATTR_HIDDEN = "H";
40     private static final String JavaDoc SET = "+";
41     private static final String JavaDoc UNSET = "-";
42
43     private boolean haveAttr = false;
44
45     /** Constructor for Attrib. */
46     public Attrib() {
47         super.setExecutable("attrib");
48         super.setParallel(false);
49     }
50
51     /**
52      * A file to be attribed.
53      * @param src a file
54      */

55     public void setFile(File JavaDoc src) {
56         FileSet fs = new FileSet();
57         fs.setFile(src);
58         addFileset(fs);
59     }
60
61     /**
62      * Set the ReadOnly file attribute.
63      * @param value a <code>boolean</code> value
64      */

65     public void setReadonly(boolean value) {
66         addArg(value, ATTR_READONLY);
67     }
68
69     /**
70      * Set the Archive file attribute.
71      * @param value a <code>boolean</code> value
72      */

73     public void setArchive(boolean value) {
74         addArg(value, ATTR_ARCHIVE);
75     }
76
77     /**
78      * Set the System file attribute.
79      * @param value a <code>boolean</code> value
80      */

81     public void setSystem(boolean value) {
82         addArg(value, ATTR_SYSTEM);
83     }
84
85     /**
86      * Set the Hidden file attribute.
87      * @param value a <code>boolean</code> value
88      */

89     public void setHidden(boolean value) {
90         addArg(value, ATTR_HIDDEN);
91     }
92
93     /**
94      * Check the attributes.
95      */

96     protected void checkConfiguration() {
97         if (!haveAttr()) {
98             throw new BuildException("Missing attribute parameter",
99                                      getLocation());
100         }
101         super.checkConfiguration();
102     }
103
104     /**
105      * Set the executable.
106      * This is not allowed, and it always throws a BuildException.
107      * @param e ignored
108      * @ant.attribute ignore="true"
109      */

110     public void setExecutable(String JavaDoc e) {
111         throw new BuildException(getTaskType()
112             + " doesn\'t support the executable attribute", getLocation());
113     }
114
115     /**
116      * Set the executable.
117      * This is not allowed, and it always throws a BuildException.
118      * @param e ignored
119      * @ant.attribute ignore="true"
120      */

121     public void setCommand(String JavaDoc e) {
122         throw new BuildException(getTaskType()
123             + " doesn\'t support the command attribute", getLocation());
124     }
125
126     /**
127      * Add source file.
128      * This is not allowed, and it always throws a BuildException.
129      * @param b ignored
130      * @ant.attribute ignore="true"
131      */

132     public void setAddsourcefile(boolean b) {
133         throw new BuildException(getTaskType()
134             + " doesn\'t support the addsourcefile attribute", getLocation());
135     }
136
137     /**
138      * Set skip empty file sets.
139      * This is not allowed, and it always throws a BuildException.
140      * @param skip ignored
141      * @ant.attribute ignore="true"
142      */

143     public void setSkipEmptyFilesets(boolean skip) {
144         throw new BuildException(getTaskType() + " doesn\'t support the "
145                                  + "skipemptyfileset attribute",
146                                  getLocation());
147     }
148
149     /**
150      * Set parallel.
151      * This is not allowed, and it always throws a BuildException.
152      * @param parallel ignored
153      * @ant.attribute ignore="true"
154      */

155     public void setParallel(boolean parallel) {
156         throw new BuildException(getTaskType()
157                                  + " doesn\'t support the parallel attribute",
158                                  getLocation());
159     }
160
161     /**
162      * Set max parallel.
163      * This is not allowed, and it always throws a BuildException.
164      * @param max ignored
165      * @ant.attribute ignore="true"
166      */

167     public void setMaxParallel(int max) {
168         throw new BuildException(getTaskType()
169                                  + " doesn\'t support the maxparallel attribute",
170                                  getLocation());
171     }
172
173     /**
174      * Check if the os is valid.
175      * Always include windows
176      * @return true if the os is valid.
177      */

178     protected boolean isValidOs() {
179         return Os.isFamily("windows") && super.isValidOs();
180     }
181
182     private static String JavaDoc getSignString(boolean attr) {
183         return (attr ? SET : UNSET);
184     }
185
186     private void addArg(boolean sign, String JavaDoc attribute) {
187         createArg().setValue(getSignString(sign) + attribute);
188         haveAttr = true;
189     }
190
191     private boolean haveAttr() {
192         return haveAttr;
193     }
194
195 }
196
Popular Tags