1 28 29 package com.idaremedia.antx.mktemp; 30 31 import java.io.BufferedReader ; 32 import java.io.File ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 36 import org.apache.tools.ant.BuildException; 37 import org.apache.tools.ant.Project; 38 import org.apache.tools.ant.types.FileList; 39 import org.apache.tools.ant.util.FileUtils; 40 41 import com.idaremedia.antx.AntX; 42 import com.idaremedia.antx.AntXFixture; 43 import com.idaremedia.antx.AssertableTask; 44 import com.idaremedia.antx.FlexString; 45 import com.idaremedia.antx.apis.Requester; 46 import com.idaremedia.antx.helpers.Tk; 47 import com.idaremedia.antx.parameters.FeedbackLevel; 48 import com.idaremedia.antx.parameters.RecoveryEnabled; 49 import com.idaremedia.antx.starters.ListFriendly; 50 51 70 71 public final class MkdirsTask extends AssertableTask implements RecoveryEnabled 72 { 73 private static final int BY_NAME=0; 74 private static final int BY_NAMELIST=BY_NAME+1; 75 private static final int BY_NAMEFILE=BY_NAMELIST+1; 76 77 78 81 public MkdirsTask() 82 { 83 super(AntX.mktemp+"MkdirsTask:"); 84 } 85 86 87 91 public void setHaltIfError(boolean halt) 92 { 93 m_haltIfError = halt; 94 } 95 96 97 103 public boolean isHaltIfError() 104 { 105 return m_haltIfError; 106 } 107 108 109 116 public void setFeedback(String fb) 117 { 118 require_(fb!=null,"setFeedback- nonzro value"); 119 FeedbackLevel fbLevel = FeedbackLevel.from(fb); 120 if (fbLevel==null) { 121 String error = getAntXMsg("task.illegal.param.value", 122 getTaskName(), fb, "feedback"); 123 log(error, Project.MSG_ERR); 124 throw new BuildException(error,getLocation()); 125 } 126 m_fbLevel = fbLevel; 127 } 128 129 130 138 public FileList createFileList() 139 { 140 FileList fl = new FileList(); 141 fl.setProject(getProject()); 142 143 m_dirs.add(fl); 144 return fl; 145 } 146 147 148 153 private FileList builtinFileList() 154 { 155 if (m_builtin==null) { 156 m_builtin = createFileList(); 157 m_builtin.setDir(getProject().getBaseDir()); 158 } 159 return m_builtin; 160 } 161 162 163 164 169 public void setBase(File baseDir) 170 { 171 require_(baseDir!=null,"setBase- nonzro dirname"); 172 builtinFileList().setDir(baseDir); 173 } 174 175 176 177 183 public void setNames(String subdirNames) 184 { 185 require_(subdirNames!=null,"setNames- nonzro namelst"); 186 setByMethod(BY_NAME,subdirNames); 187 } 188 189 190 194 private void handleByNames() 195 { 196 List nl = Tk.splitList(m_sourceString); 197 if (!nl.isEmpty()) { 198 FileList builtin = builtinFileList(); 199 for (int i=0,N=nl.size();i<N;i++) { 200 FileList.FileName fn = new FileList.FileName(); 201 fn.setName((String )nl.get(i)); 202 builtin.addConfiguredFile(fn); 203 } 204 } 205 } 206 207 208 209 215 public void setNamesList(String listsRefId) 216 { 217 require_(listsRefId!=null,"setNamesList- nonzro refid"); 218 setByMethod(BY_NAMELIST,listsRefId); 219 } 220 221 222 223 228 private void handleByNamesList() 229 { 230 ListFriendly list = (ListFriendly)getReferencedObject 231 (null,m_sourceString,ListFriendly.class); 232 if (!list.isEmpty()) { 233 FileList builtin = builtinFileList(); 234 Iterator itr= list.readonlyStringIterator(getProject()); 235 while (itr.hasNext()) { 236 FileList.FileName fn = new FileList.FileName(); 237 fn.setName(itr.next().toString()); 238 builtin.addConfiguredFile(fn); 239 } 240 } 241 } 242 243 244 245 251 public void setNamesFile(String listsFile) 252 { 253 require_(listsFile!=null,"setNamesFile- nonzro filename"); 254 setByMethod(BY_NAMEFILE,listsFile); 255 } 256 257 258 259 265 private void handleByNamesFile() 266 { 267 final Project myproject = getProject(); 268 File file = myproject.resolveFile(m_sourceString); 269 if (file.exists()) { 270 if (!file.canRead()) { 271 String message = getAntXMsg("task.bad.configfile",file.getPath(), 272 "Cannot read file"); 273 if (isHaltIfError()) { 274 log(message,Project.MSG_ERR); 275 throw new BuildException(message,getLocation()); 276 } 277 log(message,Project.MSG_WARN); 278 return; 279 } 280 try { 281 BufferedReader fr = new BufferedReader (new java.io.FileReader (file)); 282 FileList builtin = builtinFileList(); 283 FlexString filter = (FlexString)m_modifiers.clone(); 284 String next; 285 while ((next=fr.readLine())!=null) { 286 filter.set(next); 287 next = filter.getValue(); 288 if (next!=null) { 289 FileList.FileName fn = new FileList.FileName(); 290 fn.setName(Tk.resolveString(myproject,next)); 291 builtin.addConfiguredFile(fn); 292 } 293 } 294 } catch(Exception ioxOrbuildX) { 295 String message = getAntXMsg("task.bad.configfile",file.getPath(), 296 ioxOrbuildX.getMessage()); 297 if (isHaltIfError()) { 298 log(message,Project.MSG_ERR); 299 throw new BuildException(message,getLocation()); 300 } 301 log(message,Project.MSG_WARN); 302 } 303 } } 305 306 307 314 private void setByMethod(int method, String source) 315 { 316 if (m_byMethod>=BY_NAME) { 317 String error = getAntXMsg("task.only.oneof.attr", 318 "names|nameslist|namesfile"); 319 log(error,Project.MSG_ERR); 320 throw new BuildException(error,getLocation()); 321 } 322 m_byMethod = method; 323 m_sourceString = source; 324 } 325 326 327 328 334 public void execute() 335 { 336 verifyCanExecute_("exec"); 337 338 if (m_sourceString!=null) { 339 switch(m_byMethod) { 340 case BY_NAME: handleByNames(); break; 341 case BY_NAMELIST: handleByNamesList(); break; 342 default: handleByNamesFile(); 343 } 344 } else if (m_builtin!=null) { 345 m_dirs.remove(m_builtin); m_builtin = null; 347 } 348 349 final Project myproject = getProject(); 350 Requester rqlink = new Requester.ForComponent(this); 351 FileUtils fu = AntXFixture.fileUtils(); 352 353 for (int i=0,N=m_dirs.size();i<N;i++) { 354 FileList fl = (FileList)m_dirs.get(i); 355 String [] dirnames = fl.getFiles(myproject); 356 File basedir = fl.getDir(myproject); 357 for (int j=0;j<dirnames.length;j++) { 358 File dir = fu.resolveFile(basedir,dirnames[j]); 359 Mkdirs.run(dir,m_haltIfError,m_fbLevel,rqlink); 360 } 361 } 362 } 363 364 365 366 370 protected void verifyCanExecute_(String calr) 371 { 372 super.verifyCanExecute_(calr); 373 374 if (!FeedbackLevel.isQuietish(m_fbLevel,false,false)) { 375 if (m_sourceString==null && m_builtin!=null && m_dirs.size()==1) { 376 String alert = getAntXMsg("mktemp.mkdirs.nothin"); 377 log(alert,Project.MSG_INFO); 378 } 379 } 380 381 defaultFileListDirs(); 382 } 383 384 385 386 392 private void defaultFileListDirs() 393 { 394 if (m_builtin!=null) { 395 final Project myproject = getProject(); 396 File defaultDir= m_builtin.getDir(myproject); 397 if (defaultDir!=myproject.getBaseDir()) { for (int i=0,N=m_dirs.size();i<N;i++) { 399 FileList fl= (FileList)m_dirs.get(i); 400 if (fl!=m_builtin && !fl.isReference() && fl.getDir(myproject)==null) { 401 fl.setDir(defaultDir); 402 } 403 } 404 } 405 } 406 } 407 408 409 private boolean m_haltIfError=false; 410 private List m_dirs = AntXFixture.newList(); 411 private FileList m_builtin; private String m_sourceString; 413 private int m_byMethod= -1; 414 private FeedbackLevel m_fbLevel= FeedbackLevel.NORMAL; 415 private FlexString m_modifiers = new FlexString(); 416 } 417 418 | Popular Tags |