1 28 29 package com.idaremedia.antx.solo; 30 31 import java.io.File ; 32 import java.io.IOException ; 33 import java.io.InputStream ; 34 import java.net.URL ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.Map ; 38 import java.util.Properties ; 39 40 import org.apache.tools.ant.BuildException; 41 import org.apache.tools.ant.Project; 42 import org.apache.tools.ant.types.Reference; 43 44 import com.idaremedia.antx.AntX; 45 import com.idaremedia.antx.AntXFixture; 46 import com.idaremedia.antx.FixtureComponent; 47 import com.idaremedia.antx.FixtureExaminer; 48 import com.idaremedia.antx.Iteration; 49 import com.idaremedia.antx.apis.AntLibFriendly; 50 import com.idaremedia.antx.helpers.GenericParameters; 51 import com.idaremedia.antx.helpers.InnerNameValuePair; 52 import com.idaremedia.antx.helpers.InputFileLoader; 53 import com.idaremedia.antx.helpers.Strings; 54 import com.idaremedia.antx.ownhelpers.LocalTk; 55 import com.idaremedia.antx.ownhelpers.ReferenceHandle; 56 import com.idaremedia.antx.parameters.FlexSourceSupport; 57 import com.idaremedia.antx.starters.ListFriendly; 58 59 92 93 public final class PropertiesList extends GenericParameters 94 implements ListFriendly, FlexSourceSupport, FixtureComponent, AntLibFriendly 95 { 96 private static final String IAM_= AntX.fixture+"PropertiesList:"; 97 98 99 100 public static final String DEFAULT_DELIMITER = Strings.NL; 101 102 103 106 public PropertiesList() 107 { 108 } 109 110 111 112 117 public PropertiesList(Project project) 118 { 119 setProject(project); 120 } 121 122 123 124 129 public void setId(String id) 130 { 131 m_Id= id; 132 } 133 134 135 136 139 public String getId() 140 { 141 return m_Id; 142 } 143 144 145 146 152 public void setDelim(String delimiter) 153 { 154 AntX.require_(delimiter!=null,IAM_,"setDelim- nonzro delim"); 155 m_delim = delimiter; 156 } 157 158 159 160 164 public String getDelim() 165 { 166 return m_delim; 167 } 168 169 170 171 180 public void addConfiguredProperties(ReferenceHandle other) 181 { 182 AntX.require_(other!=null,IAM_,"addProps- nonzro handle"); 183 Reference r = other.getRefId(); 184 if (r==null || r.getRefId()==null || r.getRefId().equals(m_Id)) { 185 String error = Iteration.uistrs().get 186 ("task.bad.nested.item","properties","Invalid 'refid'"); 187 log(error,Project.MSG_ERR); 188 throw new BuildException(error); 189 } 190 boolean continu = true; 191 if (other.getHaltIfMissingFlag()==Boolean.FALSE) { 192 Object referredTo = getProject().getReference(r.getRefId()); 193 if (referredTo==null) { 194 continu = false; 195 } 196 } 197 if (continu) { 198 Properties otherP = FixtureExaminer. 199 getReferencedProperties(getProject(),r.getRefId(),null); 200 Iterator itr = otherP.entrySet().iterator(); 201 while (itr.hasNext()) { 202 Map.Entry kv = (Map.Entry )itr.next(); 203 String key = (String )kv.getKey(); 204 if (get(key)==null) { 205 put(key, (String )kv.getValue()); 206 } 207 } 208 } 209 } 210 211 212 213 222 public Iterator readonlyStringIterator(Project theProject) 223 { 224 Map items = copyOfParameterObjects(); 225 List kvpairs = AntXFixture.newList(items.size()); 226 Iterator itr = items.values().iterator(); 227 while (itr.hasNext()) { 228 InnerNameValuePair e = (InnerNameValuePair)itr.next(); 229 String v = e.getValue(theProject,true); 230 if (v==null) { 231 kvpairs.add(""+e.getName()); 232 } else { 233 kvpairs.add(""+e.getName()+"="+v); 234 } 235 } 236 return kvpairs.iterator(); 237 } 238 239 240 241 246 public String stringFrom(Project theProject) 247 { 248 StringBuffer sb = AntXFixture.newLargeStringBuffer(); 249 final String theDelimiter = getDelim(); 250 251 int N=0; 252 Iterator itr= readonlyStringIterator(theProject); 253 254 while (itr.hasNext()) { 255 if (N>0) { 256 sb.append(theDelimiter); 257 } 258 sb.append(itr.next().toString()); 259 N++; 260 } 261 itr=null; 262 return sb.substring(0); 263 } 264 265 266 267 275 public void setResource(String rn) 276 { 277 AntX.require_(rn!=null,IAM_,"setRez- nonzro name"); 278 279 InputStream is = LocalTk.getSystemResourceAsStream(rn, getProject()); 280 if (is!=null) { 281 try { 282 Properties values = new Properties (); 283 values.load(is); 284 mergeOther(values); 285 } catch(IOException iox) { 286 String error = Iteration.uistrs().get("task.bad.configfile", 287 "resource("+rn+")", iox.getMessage()); 288 log(error,Project.MSG_ERR); 289 throw new BuildException(error,iox); 290 } 291 } else { 292 String warning = Iteration.uistrs().get("task.nostream.from.rez", rn); 293 log(warning,Project.MSG_WARN); 294 } 295 } 296 297 298 299 308 public void setFile(String filepath) 309 { 310 AntX.require_(filepath!=null,IAM_,"setFile- nonzro name"); 311 312 File f = getProject().resolveFile(filepath); 313 if (f.canRead()) { 314 try { 315 URL url = AntXFixture.fileUtils().getFileURL(f); 316 Properties values = InputFileLoader.loadProperties(url,null); 317 mergeOther(values); 318 } catch(IOException ioX) { 319 String error = Iteration.uistrs().get("task.bad.configfile", 320 filepath, ioX.getMessage()); 321 log(error,Project.MSG_ERR); 322 throw new BuildException(error,ioX); 323 } 324 } else { 325 String warning = Iteration.uistrs().get("task.err.filenotfound", filepath); 326 log(warning, Project.MSG_WARN); 327 } 328 } 329 330 331 332 340 public void setURL(String urlstring) 341 { 342 AntX.require_(urlstring!=null,IAM_,"setURL- nonzro URL"); 343 344 try { 345 URL url = new URL (urlstring); 346 Properties values = InputFileLoader.loadProperties(url,null); 347 mergeOther(values); 348 } catch(IOException ioX) { 349 String error = Iteration.uistrs().get("task.bad.configfile", 350 "URL("+urlstring+")", ioX.getMessage()); 351 log(error,Project.MSG_ERR); 352 throw new BuildException(error,ioX); 353 } 354 } 355 356 357 private String m_Id = "n/d"; 358 private String m_delim = DEFAULT_DELIMITER; 359 } 360 361 362 | Popular Tags |