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.Project; 35 36 import com.idaremedia.antx.AntX; 37 import com.idaremedia.antx.AntXFixture; 38 import com.idaremedia.antx.AssertableTask; 39 import com.idaremedia.antx.apis.AntLibFriendly; 40 import com.idaremedia.antx.helpers.Tk; 41 import com.idaremedia.antx.ownhelpers.LocalTk; 42 import com.idaremedia.antx.parameters.FlexExistenceEnabled; 43 44 71 72 public final class ParentDirTask extends AssertableTask 73 implements FlexExistenceEnabled, AntLibFriendly 74 { 75 78 public ParentDirTask() 79 { 80 super(AntX.mktemp+"ParentDirTask:"); 81 } 82 83 84 90 public void setOf(String fileref) 91 { 92 require_(fileref!=null,"setOf- nonzro path descriptor"); 93 m_fileref = fileref; 94 } 95 96 97 98 102 public String getOf() 103 { 104 return m_fileref; 105 } 106 107 108 109 114 public void setPathProperty(String property) 115 { 116 require_(property!=null,"setProp- nonzro nam"); 117 m_updateProperty = property; 118 } 119 120 121 122 126 public String getPathProperty() 127 { 128 return m_updateProperty; 129 } 130 131 132 133 138 public void setUrlProperty(String urlproperty) 139 { 140 require_(urlproperty!=null,"setUrlProp- nonzro name"); 141 m_updateUrlProperty = urlproperty; 142 } 143 144 145 146 151 public final String getUrlPathProperty() 152 { 153 return m_updateUrlProperty; 154 } 155 156 157 158 163 public void setMustExist(boolean mustExist) 164 { 165 m_mustExist = mustExist; 166 } 167 168 169 170 174 public boolean getMustExist() 175 { 176 return m_mustExist; 177 } 178 179 180 181 186 public void setPop(String levelDesc) 187 { 188 require_(levelDesc!=null,"setPop- nonzro level descriptor"); 189 m_popLevelDesc = levelDesc; 190 } 191 192 193 194 200 public void setAppend(String subpath) 201 { 202 require_(subpath!=null,"setAppend- nonzro subpath"); 203 final int N= subpath.length(); 204 int i=0; 205 while (i<N) { 206 if (subpath.charAt(0)==File.separatorChar) { 207 subpath= subpath.substring(1); 208 } 209 i++; 210 } 211 m_subpath = subpath; 212 } 213 214 215 216 217 221 public final String getAppendedSubpath() 222 { 223 return m_subpath; 224 } 225 226 227 228 235 protected void verifyCanExecute_(String calr) 236 { 237 verifyInProject_(calr); 238 239 if (getPathProperty()==null && getUrlPathProperty()==null) { 240 String e = getAntXMsg("task.needs.this.attr", getTaskName(), 241 "pathproperty|urlproperty"); 242 log(e, Project.MSG_ERR); 243 throw new BuildException(e,getLocation()); 244 } 245 246 if (getOf()==null) { 247 m_fileref = getProject().getProperty("basedir"); 248 verify_(m_fileref!=null,calr+"- Ant's basedir defined"); 249 } 250 } 251 252 253 254 private File theParent(File f) 255 { 256 File pf = f; 257 int i = Tk.integerFrom(m_popLevelDesc,-1); 258 while (pf!=null && i>0) { 259 pf = pf.getParentFile(); 260 i--; 261 } 262 return pf; 263 } 264 265 266 267 276 public void execute() 277 { 278 verifyCanExecute_("exec"); 279 280 File f= null; 281 String filepath= getOf(); 282 283 if (filepath.startsWith("file:")) { 284 filepath = AntXFixture.fileUtils().fromURI(filepath); 285 f = getProject().resolveFile(filepath); 286 } 287 else if (filepath.startsWith("resource:")) { 288 f = LocalTk.getSystemResourceSource(filepath.substring(9),getProject()); 289 } 290 else if (filepath.startsWith("tmp:")) { 291 File tmp = TempLocator.getSystemTempDir(); 292 filepath = filepath.substring(4); 293 if (filepath.startsWith("/")) { 294 filepath = filepath.substring(1); 295 } 296 filepath = tmp.getPath() + File.separator + filepath; 297 f = getProject().resolveFile(filepath); 298 } else { 299 f = getProject().resolveFile(filepath); 300 } 301 302 File pf = theParent(f); 303 if (getMustExist() && (pf==null || !pf.canRead())) { 304 String e = getAntXMsg("mktemp.parent.notfound",m_popLevelDesc,getOf()); 305 log(e, Project.MSG_ERR); 306 throw new BuildException(e,getLocation()); 307 } 308 309 String parentPath = ""; 310 if (pf!=null) { 311 parentPath = pf.getPath(); 312 } else if (f!=null) { 313 parentPath = getProject().resolveFile("/").getPath(); 315 } 316 if (!Tk.isWhitespace(m_subpath)) { 317 parentPath += File.separator + m_subpath; 318 parentPath = getProject().resolveFile(parentPath).getPath(); 319 } 320 saveFinalPath(parentPath,true); 321 } 322 323 324 325 330 private void saveFinalPath(String finalPath, boolean strict) 331 { 332 if (getPathProperty()!=null) { 333 checkIfProperty_(getPathProperty(),!strict); 334 getProject().setNewProperty(getPathProperty(),finalPath); 335 } 336 if (getUrlPathProperty()!=null) { 337 checkIfProperty_(getUrlPathProperty(),!strict); 338 String urlPath = AntXFixture.fileUtils().toURI(finalPath); 339 getProject().setNewProperty(getUrlPathProperty(),urlPath); 340 } 341 } 342 343 344 private String m_fileref; 345 private String m_updateProperty; 346 private String m_updateUrlProperty; 347 private boolean m_mustExist; 348 private String m_popLevelDesc= "1"; private String m_subpath;} 351 352 353 | Popular Tags |