1 28 29 package com.idaremedia.antx.mktemp; 30 31 import java.io.File ; 32 33 import org.apache.tools.ant.BuildException; 34 import org.apache.tools.ant.DirectoryScanner; 35 import org.apache.tools.ant.Project; 36 import org.apache.tools.ant.types.FileSet; 37 38 import com.idaremedia.antx.AntX; 39 import com.idaremedia.antx.AntXFixture; 40 import com.idaremedia.antx.AssertableTask; 41 import com.idaremedia.antx.FixtureExaminer; 42 import com.idaremedia.antx.apis.Requester; 43 import com.idaremedia.antx.helpers.Tk; 44 import com.idaremedia.antx.parameters.TransformHelper; 45 import com.idaremedia.antx.parameters.ValueTransform; 46 47 81 82 public final class ListDirTask extends AssertableTask 83 { 84 87 public ListDirTask() 88 { 89 super(AntX.mktemp+"ListDirTask"); 90 } 91 92 93 94 100 public void setPath(String pathOrUrl) 101 { 102 require_(pathOrUrl!=null,"setDir- nonzro path"); 103 m_path = new File (TransformHelper.apply(ValueTransform.OSPATH, 104 pathOrUrl, getProject())); 105 } 106 107 108 109 113 public final File getPath() 114 { 115 return m_path; 116 } 117 118 119 120 121 129 public final File getListDirectory() 130 { 131 if (m_path!=null) { 132 return m_path; 133 } 134 if (m_fspec!=null) { 135 return m_fspec.getDir(getProject()); 136 } 137 return null; 138 } 139 140 141 142 147 public void setToRefId(String refid) 148 { 149 require_(!Tk.isWhitespace(refid), "setToRefId- nonzro name"); 150 m_outlistRefId = refid; 151 } 152 153 154 155 160 public final String getOutListRefId() 161 { 162 return m_outlistRefId; 163 } 164 165 166 173 public void setOnlyFiles(boolean onlyFiles) 174 { 175 verifyNotDefined(m_onlyDirs); 176 m_onlyFiles = onlyFiles; 177 } 178 179 180 181 185 public void setOnlyDirs(boolean onlyDirs) 186 { 187 verifyNotDefined(m_onlyFiles); 188 m_onlyDirs = onlyDirs; 189 } 190 191 192 193 198 public void setBaseNamesOnly(boolean onlyBase) 199 { 200 m_onlyBasename = onlyBase; 201 } 202 203 204 205 210 public final boolean isBaseNamesOnly() 211 { 212 return m_onlyBasename; 213 } 214 215 216 217 223 public FileSet createFilter() 224 { 225 if (m_fspec!=null) { 226 String error = getAntXMsg("task.one.specialtask", 227 getTaskName(), "filter"); 228 log(error, Project.MSG_ERR); 229 throw new BuildException(error, getLocation()); 230 } 231 m_fspec = new FileSet(); 232 if (m_path!=null) { 233 m_fspec.setDir(m_path); 234 } 235 return m_fspec; 236 } 237 238 239 245 protected void verifyCanExecute_(String calr) 246 { 247 super.verifyCanExecute_(calr); 248 249 final Project P = getProject(); 250 String refid = getOutListRefId(); 251 String error = null; 252 253 if (refid==null) { 255 error = getAntXMsg("task.needs.this.attr",getTaskName(),"outlist"); 256 } else { 257 FixtureExaminer.checkIfReference(P,m_rqlink,refid,true); 259 260 if (m_fspec==null && m_path==null) { 262 error = getAntXMsg("mktemp.ls.needs.path",getTaskName()); 263 } 264 else if (m_fspec!=null) { 265 File specDir = m_fspec.getDir(P); 267 if (specDir==null) { 268 if (m_path==null) { 269 error = getAntXMsg("mktemp.ls.needs.path",getTaskName()); 270 } else { 271 m_fspec.setDir(m_path); 272 } 273 } else if (m_path!=null && 275 !AntXFixture.fileUtils().fileNameEquals(m_path,specDir)) { 276 error = getAntXMsg("mktemp.ls.diff.paths",m_path.getPath(), 277 specDir.getPath()); 278 } 279 } else { 280 if (m_fspec==null) { 281 createFilter().setDir(m_path); 282 } 283 } 284 } 285 if (error==null) { 287 File lsDir = getListDirectory(); 288 if (!lsDir.isDirectory()) { 289 error = getAntXMsg("mktemp.ls.bad.dirpath",lsDir.getPath()); 290 } 291 } 292 if (error!=null) { 293 log(error, Project.MSG_ERR); 294 throw new BuildException(error, getLocation()); 295 } 296 } 297 298 299 300 307 public void execute() 308 { 309 verifyCanExecute_("exec"); 310 311 if (!m_fspec.hasPatterns() && !m_fspec.hasSelectors()) { 313 m_fspec.createInclude().setName("*"); 314 } 315 316 DirectoryListing list = new DirectoryListing(m_onlyBasename); 317 DirectoryScanner ds = m_fspec.getDirectoryScanner(getProject()); 318 319 if (m_onlyFiles || !m_onlyDirs) { 320 list.addIncludes(ds.getIncludedFiles(),ds); 321 } 322 if (m_onlyDirs || !m_onlyFiles) { 323 list.addIncludes(ds.getIncludedDirectories(),ds); 324 } 325 326 getProject().addReference(getOutListRefId(),list); 327 } 328 329 330 331 332 335 private void verifyNotDefined(boolean other) 336 { 337 if (other) { 338 String error = getAntXMsg("task.one.or.other.attr", 339 "onlyfiles", "onlydirs"); 340 log(error, Project.MSG_ERR); 341 throw new BuildException(error, getLocation()); 342 } 343 } 344 345 346 private Requester m_rqlink = new Requester.ForComponent(this); 347 private String m_outlistRefId; private File m_path; private FileSet m_fspec; 350 private boolean m_onlyDirs, m_onlyFiles, m_onlyBasename; 351 } 352 353 | Popular Tags |