1. /*
  2. * Copyright 2001-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.ccm;
  18. import java.io.File;
  19. import java.util.Vector;
  20. import org.apache.tools.ant.BuildException;
  21. import org.apache.tools.ant.DirectoryScanner;
  22. import org.apache.tools.ant.Project;
  23. import org.apache.tools.ant.taskdefs.Execute;
  24. import org.apache.tools.ant.types.Commandline;
  25. import org.apache.tools.ant.types.FileSet;
  26. /**
  27. * Class common to all check commands (checkout, checkin,checkin default task);
  28. * @ant.task ignore="true"
  29. */
  30. public class CCMCheck extends Continuus {
  31. private File file = null;
  32. private String comment = null;
  33. private String task = null;
  34. protected Vector filesets = new Vector();
  35. public CCMCheck() {
  36. super();
  37. }
  38. /**
  39. * Get the value of file.
  40. * @return value of file.
  41. */
  42. public File getFile() {
  43. return file;
  44. }
  45. /**
  46. * Sets the path to the file that the command will operate on.
  47. * @param v Value to assign to file.
  48. */
  49. public void setFile(File v) {
  50. log("working file " + v, Project.MSG_VERBOSE);
  51. this.file = v;
  52. }
  53. /**
  54. * Get the value of comment.
  55. * @return value of comment.
  56. */
  57. public String getComment() {
  58. return comment;
  59. }
  60. /**
  61. * Specifies a comment.
  62. * @param v Value to assign to comment.
  63. */
  64. public void setComment(String v) {
  65. this.comment = v;
  66. }
  67. /**
  68. * Get the value of task.
  69. * @return value of task.
  70. */
  71. public String getTask() {
  72. return task;
  73. }
  74. /**
  75. * Specifies the task number used to check
  76. * in the file (may use 'default').
  77. * @param v Value to assign to task.
  78. */
  79. public void setTask(String v) {
  80. this.task = v;
  81. }
  82. /**
  83. * Adds a set of files to copy.
  84. */
  85. public void addFileset(FileSet set) {
  86. filesets.addElement(set);
  87. }
  88. /**
  89. * Executes the task.
  90. * <p>
  91. * Builds a command line to execute ccm and then calls Exec's run method
  92. * to execute the command line.
  93. * </p>
  94. */
  95. public void execute() throws BuildException {
  96. if (file == null && filesets.size() == 0) {
  97. throw new BuildException(
  98. "Specify at least one source - a file or a fileset.");
  99. }
  100. if (file != null && file.exists() && file.isDirectory()) {
  101. throw new BuildException("CCMCheck cannot be generated for directories");
  102. }
  103. if (file != null && filesets.size() > 0) {
  104. throw new BuildException("Choose between file and fileset !");
  105. }
  106. if (getFile() != null) {
  107. doit();
  108. return;
  109. }
  110. int sizeofFileSet = filesets.size();
  111. for (int i = 0; i < sizeofFileSet; i++) {
  112. FileSet fs = (FileSet) filesets.elementAt(i);
  113. DirectoryScanner ds = fs.getDirectoryScanner(getProject());
  114. String[] srcFiles = ds.getIncludedFiles();
  115. for (int j = 0; j < srcFiles.length; j++) {
  116. File src = new File(fs.getDir(getProject()), srcFiles[j]);
  117. setFile(src);
  118. doit();
  119. }
  120. }
  121. }
  122. /**
  123. * check the file given by getFile().
  124. */
  125. private void doit() {
  126. Commandline commandLine = new Commandline();
  127. // build the command line from what we got the format is
  128. // ccm co /t .. files
  129. // as specified in the CCM.EXE help
  130. commandLine.setExecutable(getCcmCommand());
  131. commandLine.createArgument().setValue(getCcmAction());
  132. checkOptions(commandLine);
  133. int result = run(commandLine);
  134. if (Execute.isFailure(result)) {
  135. String msg = "Failed executing: " + commandLine.toString();
  136. throw new BuildException(msg, getLocation());
  137. }
  138. }
  139. /**
  140. * Check the command line options.
  141. */
  142. private void checkOptions(Commandline cmd) {
  143. if (getComment() != null) {
  144. cmd.createArgument().setValue(FLAG_COMMENT);
  145. cmd.createArgument().setValue(getComment());
  146. }
  147. if (getTask() != null) {
  148. cmd.createArgument().setValue(FLAG_TASK);
  149. cmd.createArgument().setValue(getTask());
  150. }
  151. if (getFile() != null) {
  152. cmd.createArgument().setValue(file.getAbsolutePath());
  153. }
  154. }
  155. /**
  156. * -comment flag -- comment to attach to the file
  157. */
  158. public static final String FLAG_COMMENT = "/comment";
  159. /**
  160. * -task flag -- associate checkout task with task
  161. */
  162. public static final String FLAG_TASK = "/task";
  163. }