1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import org.apache.tools.ant.BuildException; 22 import org.apache.tools.ant.ProjectHelper; 23 import org.apache.tools.ant.Project; 24 import org.apache.tools.ant.Task; 25 import org.apache.tools.ant.util.FileUtils; 26 27 import java.io.File ; 28 import java.util.Vector ; 29 30 55 public class ImportTask extends Task { 56 private String file; 57 private boolean optional; 58 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 59 60 66 public void setOptional(boolean optional) { 67 this.optional = optional; 68 } 69 70 75 public void setFile(String file) { 76 this.file = file; 79 } 80 81 85 public void execute() { 86 if (file == null) { 87 throw new BuildException("import requires file attribute"); 88 } 89 if (getOwningTarget() == null 90 || !"".equals(getOwningTarget().getName())) { 91 throw new BuildException("import only allowed as a top-level task"); 92 } 93 94 ProjectHelper helper = 95 (ProjectHelper) getProject(). 96 getReference(ProjectHelper.PROJECTHELPER_REFERENCE); 97 98 if (helper == null) { 99 throw new BuildException("import requires support in ProjectHelper"); 101 } 102 103 Vector importStack = helper.getImportStack(); 104 105 if (importStack.size() == 0) { 106 throw new BuildException("import requires support in ProjectHelper"); 109 } 110 111 if (getLocation() == null || getLocation().getFileName() == null) { 112 throw new BuildException("Unable to get location of import task"); 113 } 114 115 File buildFile = new File (getLocation().getFileName()).getAbsoluteFile(); 116 117 120 File buildFileParent = new File (buildFile.getParent()); 121 File importedFile = FILE_UTILS.resolveFile(buildFileParent, file); 122 123 getProject().log("Importing file " + importedFile + " from " 124 + buildFile.getAbsolutePath(), Project.MSG_VERBOSE); 125 126 if (!importedFile.exists()) { 127 String message = 128 "Cannot find " + file + " imported from " 129 + buildFile.getAbsolutePath(); 130 if (optional) { 131 getProject().log(message, Project.MSG_VERBOSE); 132 return; 133 } else { 134 throw new BuildException(message); 135 } 136 } 137 138 if (importStack.contains(importedFile)) { 139 getProject().log( 140 "Skipped already imported file:\n " 141 + importedFile + "\n", Project.MSG_VERBOSE); 142 return; 143 } 144 145 try { 146 helper.parse(getProject(), importedFile); 147 } catch (BuildException ex) { 148 throw ProjectHelper.addLocationToBuildException( 149 ex, getLocation()); 150 } 151 } 152 153 } 154 | Popular Tags |