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.ide;
  18. import java.util.Enumeration;
  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.types.FileSet;
  23. /**
  24. * Import source, class files, and resources to the Visual Age for Java
  25. * workspace.
  26. * <p>
  27. * Example:
  28. * <pre>
  29. * <vajimport project="MyVAProject">
  30. * <fileset dir="src">
  31. * <include name="org/foo/subsystem1/**" />
  32. * <exclude name="/org/foo/subsystem1/test/**" />
  33. * </fileset>
  34. * </vajexport>
  35. * </pre>
  36. * import all source and resource files from the "src" directory
  37. * which start with 'org.foo.subsystem1', except of these starting with
  38. * 'org.foo.subsystem1.test' into the project MyVAProject.
  39. * </p>
  40. * <p>If MyVAProject isn't loaded into the Workspace, a new edition is
  41. * created in the repository and automatically loaded into the Workspace.
  42. * There has to be at least one nested FileSet element.
  43. * </p>
  44. * <p>Parameters:
  45. * <table border="1" cellpadding="2" cellspacing="0">
  46. * <tr>
  47. * <td valign="top"><b>Attribute</b></td>
  48. * <td valign="top"><b>Description</b></td>
  49. * <td align="center" valign="top"><b>Required</b></td>
  50. * </tr>
  51. * <tr>
  52. * <td valign="top">project</td>
  53. * <td valign="top">the name of the Project to import to</td>
  54. * <td align="center" valign="top">Yes</td>
  55. * </tr>
  56. * <tr>
  57. * <td valign="top">importSources</td>
  58. * <td valign="top">import Java sources, defaults to "yes"</td>
  59. * <td align="center" valign="top">No</td>
  60. * </tr>
  61. * <tr>
  62. * <td valign="top">importResources</td>
  63. * <td valign="top">import resource files (anything that doesn't
  64. * end with .java or .class), defaults to "yes"</td>
  65. * <td align="center" valign="top">No</td>
  66. * </tr>
  67. * <tr>
  68. * <td valign="top">importClasses</td>
  69. * <td valign="top">import class files, defaults to "no"</td>
  70. * <td align="center" valign="top">No</td>
  71. * </tr>
  72. * <tr>
  73. * <td valign="top">remote</td>
  74. * <td valign="top">remote tool server to run this command against
  75. * (format: <servername> : <port no>)</td>
  76. * <td align="center" valign="top">No</td>
  77. * </tr>
  78. * <tr>
  79. * <td valign="top">haltonerror</td>
  80. * <td valign="top">stop the build process if an error occurs,
  81. * defaults to "yes"</td>
  82. * <td align="center" valign="top">No</td>
  83. * </tr>
  84. * </table>
  85. *
  86. */
  87. public class VAJImport extends VAJTask {
  88. protected Vector filesets = new Vector();
  89. protected boolean importSources = true;
  90. protected boolean importResources = true;
  91. protected boolean importClasses = false;
  92. protected String importProject = null;
  93. protected boolean useDefaultExcludes = true;
  94. /**
  95. * Extended DirectoryScanner that has accessors for the
  96. * includes and excludes fields.
  97. *
  98. * This is kindof a hack to get includes and excludes
  99. * from the directory scanner. In order to keep
  100. * the URLs short we only want to send the patterns to the
  101. * remote tool server and let him figure out the files.
  102. *
  103. * This replaces the former reflection hack that
  104. * didn't compile for old JDKs.
  105. *
  106. * @see VAJImport#importFileSet(FileSet)
  107. */
  108. private static class LocalDirectoryScanner extends DirectoryScanner {
  109. public String[] getIncludes() { return includes; }
  110. public String[] getExcludes() { return excludes; }
  111. }
  112. /**
  113. * The VisualAge for Java Project name to import into.
  114. */
  115. public void setProject(String projectName) {
  116. this.importProject = projectName;
  117. }
  118. /**
  119. * Adds a set of files (nested fileset attribute).
  120. */
  121. public void addFileset(FileSet set) {
  122. filesets.addElement(set);
  123. }
  124. /**
  125. * Flag to import .class files; optional, default false.
  126. */
  127. public void setImportClasses(boolean importClasses) {
  128. this.importClasses = importClasses;
  129. }
  130. /**
  131. * Import resource files (anything that doesn't end in
  132. * .class or .java); optional, default true.
  133. */
  134. public void setImportResources(boolean importResources) {
  135. this.importResources = importResources;
  136. }
  137. /**
  138. * Import .java files; optional, default true.
  139. */
  140. public void setImportSources(boolean importSources) {
  141. this.importSources = importSources;
  142. }
  143. /**
  144. * Sets whether default exclusions should be used or not.
  145. *
  146. * @param useDefaultExcludes "true"|"on"|"yes" when default exclusions
  147. * should be used, "false"|"off"|"no" when they
  148. * shouldn't be used.
  149. */
  150. public void setDefaultexcludes(boolean useDefaultExcludes) {
  151. this.useDefaultExcludes = useDefaultExcludes;
  152. }
  153. /**
  154. * Do the import.
  155. */
  156. public void execute() throws BuildException {
  157. if (filesets.size() == 0) {
  158. throw new BuildException("At least one fileset is required!");
  159. }
  160. if (importProject == null || "".equals(importProject)) {
  161. throw new BuildException("The VisualAge for Java Project name is required!");
  162. }
  163. try {
  164. for (Enumeration e = filesets.elements(); e.hasMoreElements();) {
  165. importFileset((FileSet) e.nextElement());
  166. }
  167. } catch (BuildException ex) {
  168. if (haltOnError) {
  169. throw ex;
  170. } else {
  171. log(ex.toString());
  172. }
  173. }
  174. }
  175. /**
  176. * Import all files from the fileset into the Project in the
  177. * Workspace.
  178. */
  179. protected void importFileset(FileSet fileset) {
  180. LocalDirectoryScanner ds = new LocalDirectoryScanner();
  181. fileset.setupDirectoryScanner(ds, this.getProject());
  182. ds.scan();
  183. if (ds.getIncludedFiles().length == 0) {
  184. return;
  185. }
  186. String[] includes = ds.getIncludes();
  187. String[] excludes = ds.getExcludes();
  188. getUtil().importFiles(importProject, ds.getBasedir(),
  189. includes, excludes,
  190. importClasses, importResources, importSources,
  191. useDefaultExcludes);
  192. }
  193. }