1 28 29 package com.idaremedia.antx.solo; 30 31 import java.util.Iterator ; 32 import java.util.Map ; 33 import java.util.Properties ; 34 35 import org.apache.tools.ant.BuildException; 36 import org.apache.tools.ant.Project; 37 import org.apache.tools.ant.types.Mapper; 38 import org.apache.tools.ant.util.FileNameMapper; 39 40 import com.idaremedia.antx.AntX; 41 import com.idaremedia.antx.AssertableTask; 42 import com.idaremedia.antx.ExportedProperties; 43 44 70 71 public final class ImportTask extends AssertableTask 72 { 73 76 public ImportTask() 77 { 78 super(AntX.nopackage); 79 } 80 81 82 86 public ImportTask(String iam) 87 { 88 super(iam); 89 } 90 91 92 99 public void setName(String name) 100 { 101 require_(name!=null,"setNam- nonzro nam"); 102 if (getPrefix()!=null) { 103 String ermsg = getAntXMsg("import.one.from.attr"); 104 log(ermsg,Project.MSG_ERR); 105 throw new BuildException(ermsg,getLocation()); 106 } 107 m_name = name; 108 } 109 110 111 115 public final String getName() 116 { 117 return m_name; 118 } 119 120 121 126 public void setPrefix(String prefix) 127 { 128 require_(prefix!=null && prefix.length()>0,"setPfx- nonzro pfx"); 129 if (getName()!=null) { 130 String ermsg = getAntXMsg("import.one.from.attr"); 131 log(ermsg,Project.MSG_ERR); 132 throw new BuildException(ermsg,getLocation()); 133 } 134 m_prefix = prefix; 135 } 136 137 138 142 public final String getPrefix() 143 { 144 return m_prefix; 145 } 146 147 148 152 public void setStrip(boolean remove) 153 { 154 m_stripPrefix = remove; 155 } 156 157 158 162 public final boolean willStrip() { 164 return m_stripPrefix; 165 } 166 167 168 169 174 public Mapper createMapper() 175 { 176 if (m_nameMapper!=null) { 177 String error = uistrs().get("taskset.only.one.specialtask", 178 "mapper","mapper"); 179 log(error,Project.MSG_ERR); 180 throw new BuildException(error,getLocation()); 181 } 182 m_nameMapper = new Mapper(getProject()); 183 return m_nameMapper; 184 } 185 186 187 188 195 public void setHaltIfMissing(boolean stop) 196 { 197 m_haltIfNone = stop; 198 } 199 200 201 205 public final boolean isHaltIfMissing() 206 { 207 return m_haltIfNone; 208 } 209 210 211 216 public void setProperty(String property) 217 { 218 require_(property!=null,"setProp- nonzro nam"); 219 m_updateProperty = property; 220 } 221 222 223 228 public final String getUpdateProperty() 229 { 230 return m_updateProperty; 231 } 232 233 234 241 public void setIsMove(boolean is) 242 { 243 m_likeMove = is; 244 } 245 246 247 252 public final boolean isMove() 253 { 254 return m_likeMove; 255 } 256 257 258 264 public void execute() throws BuildException 265 { 266 verifyCanExecute_("execute"); 267 268 Project P= getProject(); 269 int N=0; 270 boolean zapVar= isMove(); 271 272 if (getPrefix()!=null) { 273 final String pfx = getPrefix(); 274 final boolean strip = willStrip(); 275 final int PFXLEN= pfx.length(); 276 277 FileNameMapper mapper= null; 278 if (m_nameMapper!=null) { 279 mapper = m_nameMapper.getImplementation(); 280 } 281 282 Properties allP = ExportedProperties.copy(null); 283 Iterator itr= allP.entrySet().iterator(); 284 285 log("Import trying to convert properties starting with '"+pfx+ 286 "' to project; "+allP.size()+" candidates", Project.MSG_DEBUG); 287 288 while (itr.hasNext()) { 289 Map.Entry mE = (Map.Entry )itr.next(); 290 String key = mE.getKey().toString(); 291 if (key.startsWith(pfx)) { 292 if (strip) { 293 key = key.substring(PFXLEN); 294 verify_(key.length()>0,"import valid key aftr strip"); 295 } 296 if (mapper!=null) { 297 String [] rslt = mapper.mapFileName(key); 298 if (rslt!=null) { 299 key = rslt[0]; 300 } 301 } 302 P.setNewProperty(key, mE.getValue().toString()); 303 N++; 304 if (zapVar) { 305 ExportedProperties.unset(mE.getKey().toString()); 306 } 307 } 308 } 309 log("Import tried to suck in ("+N+") variables into project", 310 Project.MSG_DEBUG); 311 allP.clear(); 312 313 } else { 314 String varkey = getName(); 315 String value = ExportedProperties.readstring(varkey); 316 if (value!=null) { 317 String property = getUpdateProperty(); 318 if (property==null) { 319 property = varkey; 320 } 321 if (m_nameMapper!=null) { 322 FileNameMapper mapper = m_nameMapper.getImplementation(); 323 String [] rslt = mapper.mapFileName(property); 324 if (rslt!=null) { 325 property = rslt[0]; 326 } 327 } 328 log("Import trying to convert variable '"+varkey+ 329 "' to property '"+property+"'",Project.MSG_DEBUG); 330 331 P.setNewProperty(property, value); 332 N++; 333 if (zapVar) { 334 ExportedProperties.unset(varkey); 335 } 336 } 337 } 338 339 if (N==0 && isHaltIfMissing()) { 340 String ermsg; 341 if (getMsgId()!=null) { 342 ermsg = getMsg(); 343 } else { 344 ermsg= getAntXMsg("import.none",getName()); 345 } 346 log(ermsg,Project.MSG_ERR); 347 throw new BuildException(ermsg,getLocation()); 348 } 349 } 350 351 352 353 359 protected void verifyCanExecute_(String calr) 360 { 361 super.verifyCanExecute_(calr); 362 363 if (getPrefix()==null && getName()==null) { 364 String ermsg = getAntXMsg("task.needs.this.attr",getTaskName(),"name|prefix"); 365 log(ermsg,Project.MSG_ERR); 366 throw new BuildException(ermsg, getLocation()); 367 } 368 } 369 370 371 private boolean m_haltIfNone; private String m_prefix; 373 private String m_name; 374 private String m_updateProperty; 375 private boolean m_stripPrefix, m_likeMove; 376 private Mapper m_nameMapper; 377 } 378 379 380 | Popular Tags |