1. /*
  2. * Copyright 2002-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs.optional.windows;
  18. import org.apache.tools.ant.BuildException;
  19. import org.apache.tools.ant.taskdefs.ExecuteOn;
  20. import org.apache.tools.ant.taskdefs.condition.Os;
  21. import org.apache.tools.ant.types.FileSet;
  22. import java.io.File;
  23. /**
  24. * Attrib equivalent for Win32 environments.
  25. * Note: Attrib parameters /S and /D are not handled.
  26. *
  27. *
  28. * @since Ant 1.6
  29. */
  30. public class Attrib extends ExecuteOn {
  31. private static final String ATTR_READONLY = "R";
  32. private static final String ATTR_ARCHIVE = "A";
  33. private static final String ATTR_SYSTEM = "S";
  34. private static final String ATTR_HIDDEN = "H";
  35. private static final String SET = "+";
  36. private static final String UNSET = "-";
  37. private boolean haveAttr = false;
  38. public Attrib() {
  39. super.setExecutable("attrib");
  40. super.setParallel(false);
  41. }
  42. public void setFile(File src) {
  43. FileSet fs = new FileSet();
  44. fs.setFile(src);
  45. addFileset(fs);
  46. }
  47. /** set the ReadOnly file attribute */
  48. public void setReadonly(boolean value) {
  49. addArg(value, ATTR_READONLY);
  50. }
  51. /** set the Archive file attribute */
  52. public void setArchive(boolean value) {
  53. addArg(value, ATTR_ARCHIVE);
  54. }
  55. /** set the System file attribute */
  56. public void setSystem(boolean value) {
  57. addArg(value, ATTR_SYSTEM);
  58. }
  59. /** set the Hidden file attribute */
  60. public void setHidden(boolean value) {
  61. addArg(value, ATTR_HIDDEN);
  62. }
  63. protected void checkConfiguration() {
  64. if (!haveAttr()) {
  65. throw new BuildException("Missing attribute parameter",
  66. getLocation());
  67. }
  68. super.checkConfiguration();
  69. }
  70. /**
  71. * @ant.attribute ignore="true"
  72. */
  73. public void setExecutable(String e) {
  74. throw new BuildException(getTaskType()
  75. + " doesn\'t support the executable attribute", getLocation());
  76. }
  77. /**
  78. * @ant.attribute ignore="true"
  79. */
  80. public void setCommand(String e) {
  81. throw new BuildException(getTaskType()
  82. + " doesn\'t support the command attribute", getLocation());
  83. }
  84. /**
  85. * @ant.attribute ignore="true"
  86. */
  87. public void setAddsourcefile(boolean b) {
  88. throw new BuildException(getTaskType()
  89. + " doesn\'t support the addsourcefile attribute", getLocation());
  90. }
  91. /**
  92. * @ant.attribute ignore="true"
  93. */
  94. public void setSkipEmptyFilesets(boolean skip) {
  95. throw new BuildException(getTaskType() + " doesn\'t support the "
  96. + "skipemptyfileset attribute",
  97. getLocation());
  98. }
  99. /**
  100. * @ant.attribute ignore="true"
  101. */
  102. public void setParallel(boolean parallel) {
  103. throw new BuildException(getTaskType()
  104. + " doesn\'t support the parallel attribute",
  105. getLocation());
  106. }
  107. /**
  108. * @ant.attribute ignore="true"
  109. */
  110. public void setMaxParallel(int max) {
  111. throw new BuildException(getTaskType()
  112. + " doesn\'t support the maxparallel attribute",
  113. getLocation());
  114. }
  115. protected boolean isValidOs() {
  116. return Os.isFamily("windows") && super.isValidOs();
  117. }
  118. private static String getSignString(boolean attr) {
  119. return (attr == true ? SET : UNSET);
  120. }
  121. private void addArg(boolean sign, String attribute) {
  122. createArg().setValue(getSignString(sign) + attribute);
  123. haveAttr = true;
  124. }
  125. private boolean haveAttr() {
  126. return haveAttr;
  127. }
  128. }