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:
019: package org.apache.tools.ant.taskdefs.optional.windows;
020:
021: import org.apache.tools.ant.BuildException;
022: import org.apache.tools.ant.taskdefs.ExecuteOn;
023: import org.apache.tools.ant.taskdefs.condition.Os;
024: import org.apache.tools.ant.types.FileSet;
025:
026: import java.io.File;
027:
028: /**
029: * Attrib equivalent for Win32 environments.
030: * Note: Attrib parameters /S and /D are not handled.
031: *
032: * @since Ant 1.6
033: */
034: public class Attrib extends ExecuteOn {
035:
036: private static final String ATTR_READONLY = "R";
037: private static final String ATTR_ARCHIVE = "A";
038: private static final String ATTR_SYSTEM = "S";
039: private static final String ATTR_HIDDEN = "H";
040: private static final String SET = "+";
041: private static final String UNSET = "-";
042:
043: private boolean haveAttr = false;
044:
045: /** Constructor for Attrib. */
046: public Attrib() {
047: super .setExecutable("attrib");
048: super .setParallel(false);
049: }
050:
051: /**
052: * A file to be attribed.
053: * @param src a file
054: */
055: public void setFile(File src) {
056: FileSet fs = new FileSet();
057: fs.setFile(src);
058: addFileset(fs);
059: }
060:
061: /**
062: * Set the ReadOnly file attribute.
063: * @param value a <code>boolean</code> value
064: */
065: public void setReadonly(boolean value) {
066: addArg(value, ATTR_READONLY);
067: }
068:
069: /**
070: * Set the Archive file attribute.
071: * @param value a <code>boolean</code> value
072: */
073: public void setArchive(boolean value) {
074: addArg(value, ATTR_ARCHIVE);
075: }
076:
077: /**
078: * Set the System file attribute.
079: * @param value a <code>boolean</code> value
080: */
081: public void setSystem(boolean value) {
082: addArg(value, ATTR_SYSTEM);
083: }
084:
085: /**
086: * Set the Hidden file attribute.
087: * @param value a <code>boolean</code> value
088: */
089: public void setHidden(boolean value) {
090: addArg(value, ATTR_HIDDEN);
091: }
092:
093: /**
094: * Check the attributes.
095: */
096: protected void checkConfiguration() {
097: if (!haveAttr()) {
098: throw new BuildException("Missing attribute parameter",
099: 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 e) {
111: throw new BuildException(getTaskType()
112: + " doesn\'t support the executable attribute",
113: getLocation());
114: }
115:
116: /**
117: * Set the executable.
118: * This is not allowed, and it always throws a BuildException.
119: * @param e ignored
120: * @ant.attribute ignore="true"
121: */
122: public void setCommand(String e) {
123: throw new BuildException(getTaskType()
124: + " doesn\'t support the command attribute",
125: getLocation());
126: }
127:
128: /**
129: * Add source file.
130: * This is not allowed, and it always throws a BuildException.
131: * @param b ignored
132: * @ant.attribute ignore="true"
133: */
134: public void setAddsourcefile(boolean b) {
135: throw new BuildException(getTaskType()
136: + " doesn\'t support the addsourcefile attribute",
137: getLocation());
138: }
139:
140: /**
141: * Set skip empty file sets.
142: * This is not allowed, and it always throws a BuildException.
143: * @param skip ignored
144: * @ant.attribute ignore="true"
145: */
146: public void setSkipEmptyFilesets(boolean skip) {
147: throw new BuildException(getTaskType()
148: + " doesn\'t support the "
149: + "skipemptyfileset attribute", getLocation());
150: }
151:
152: /**
153: * Set parallel.
154: * This is not allowed, and it always throws a BuildException.
155: * @param parallel ignored
156: * @ant.attribute ignore="true"
157: */
158: public void setParallel(boolean parallel) {
159: throw new BuildException(getTaskType()
160: + " doesn\'t support the parallel attribute",
161: getLocation());
162: }
163:
164: /**
165: * Set max parallel.
166: * This is not allowed, and it always throws a BuildException.
167: * @param max ignored
168: * @ant.attribute ignore="true"
169: */
170: public void setMaxParallel(int max) {
171: throw new BuildException(getTaskType()
172: + " doesn\'t support the maxparallel attribute",
173: getLocation());
174: }
175:
176: /**
177: * Check if the os is valid.
178: * Always include windows
179: * @return true if the os is valid.
180: */
181: protected boolean isValidOs() {
182: return Os.isFamily("windows") && super .isValidOs();
183: }
184:
185: private static String getSignString(boolean attr) {
186: return (attr ? SET : UNSET);
187: }
188:
189: private void addArg(boolean sign, String attribute) {
190: createArg().setValue(getSignString(sign) + attribute);
191: haveAttr = true;
192: }
193:
194: private boolean haveAttr() {
195: return haveAttr;
196: }
197:
198: }
|