| 1 28 29 package com.idaremedia.antx.starters; 30 31 import java.util.Iterator ; 32 33 import org.apache.tools.ant.BuildException; 34 import org.apache.tools.ant.Project; 35 import org.apache.tools.ant.types.Reference; 36 37 import com.idaremedia.antx.AntX; 38 import com.idaremedia.antx.AssertableProjectComponent; 39 import com.idaremedia.antx.apis.FlexStringFriendly; 40 import com.idaremedia.antx.helpers.Strings; 41 import com.idaremedia.antx.ownhelpers.LocalTk; 42 43 81 82 public class StringItemListHandle extends AssertableProjectComponent 83 implements Cloneable , ListFriendly 84 { 85 89 public StringItemListHandle() 90 { 91 super(AntX.starters+"ItemListHandle:"); 92 } 93 94 95 96 101 public StringItemListHandle(Reference ref) 102 { 103 super(AntX.starters+"ItemListHandle:"); 104 require_(ref!=null && ref.getRefId()!=null,"ctor- nonzro refId"); 105 setListRef(LocalTk.referenceFor(ref)); 106 } 107 108 109 110 114 public final Object clone() 115 { 116 try { 117 StringItemListHandle cloned = (StringItemListHandle)super.clone(); 118 if (m_listReference!=null) { 119 cloned.m_listReference = 120 LocalTk.referenceFor(m_listReference.getRefId(),getProject()); 121 } 122 return cloned; 123 } catch(CloneNotSupportedException clnx) { 124 throw new Error (uistrs().get(AntX.CLONE_BROKEN_MSGID)); 125 } 126 } 127 128 129 130 134 public final void setListRef(Reference r) 135 { 136 require_(r!=null,"setRef- nonzro ref"); 137 m_listReference = r; 138 } 139 140 141 142 146 public final Reference getListRef() 147 { 148 return m_listReference; 149 } 150 151 152 153 159 public final void setRaw(Boolean raw) 160 { 161 if (StringItemList.RAW.equals(raw)) { 162 m_how = StringItemList.RAW; 163 } else { 164 m_how = StringItemList.PROCESSED; 165 } 166 } 167 168 169 170 179 public final StringItemList getList(Project theProject) 180 { 181 String error = null; 182 183 if (getListRef()==null) { 184 error = uistrs().get("type.needs.this.attr", 185 getUsedForName(),"listref"); 186 log(error,Project.MSG_ERR); 187 throw new BuildException(error); 188 } 189 190 if (theProject==null) { 191 theProject= getProject(); 192 verify_(theProject!=null,"getRefObj- hav project"); 193 } 194 195 String refid = getListRef().getRefId(); 196 Object o = theProject.getReference(refid); 197 198 if (o==null) { 199 error = uistrs().get("task.missing.refid", refid); 200 } 201 else if (!StringItemList.class.isAssignableFrom(o.getClass())) { 202 error = uistrs().get("task.bad.refid", refid, 203 StringItemList.class.getName(), 204 o.getClass().getName()); 205 } 206 if (error!=null) { 207 log(error,Project.MSG_ERR); 208 throw new BuildException(error); 209 } 210 return (StringItemList)o; 211 } 212 213 214 215 222 public final Iterator readonlyStringIterator(Project theProject) 223 { 224 return getList(theProject).readonlyStringIterator(theProject,m_how); 225 } 226 227 228 229 233 public final boolean isEmpty() 234 { 235 if (getListRef()==null) { 236 return true; 237 } 238 verifyInProject_("isEmpty"); 239 StringItemList list = getList(getProject()); 240 return list.isEmpty(); 241 } 242 243 244 245 246 250 public final int size() 251 { 252 if (getListRef()==null) { 253 return 0; 254 } 255 verifyInProject_("isEmpty"); 256 return getList(getProject()).size(); 257 } 258 259 260 261 268 public static final String byLineStringFrom(StringItemList il, Project P) 269 { 270 if (il==null) { 271 return ""; 272 } 273 int i=0; 274 StringBuffer sb = new StringBuffer (100); 275 Iterator itr= il.readonlyStringIterator(P); 276 while (itr.hasNext()) { 277 if (i>0) { 278 sb.append(Strings.NL); 279 } 280 sb.append(itr.next()); 281 i++; 282 } 283 return sb.substring(0); 284 } 285 286 287 288 296 public final String byLineStringFrom(Project P) 297 { 298 if (getListRef()==null) { 299 return ""; 300 } 301 return byLineStringFrom(getList(P), P); 302 } 303 304 305 306 315 public String stringFrom(Project P) 316 { 317 if (getListRef()==null) { 318 return ""; 319 } 320 StringItemList il = getList(P); 321 if (il instanceof FlexStringFriendly) { 322 return ((FlexStringFriendly)il).stringFrom(P); 323 } 324 return byLineStringFrom(il,P); 325 } 326 327 328 329 333 public String toString() 334 { 335 if (getListRef()==null) { 336 return ""; 337 } 338 verifyInProject_("toString"); 339 return getList(getProject()).toString(); 340 } 341 342 343 344 348 public final String getUsedForName() 349 { 350 return m_typicalName; 351 } 352 353 354 355 359 public final void setUsedForName(String name) 360 { 361 require_(name!=null,"setUsedForNam- nonzro nam"); 362 m_typicalName = name; 363 } 364 365 366 private Reference m_listReference; 367 private String m_typicalName= "items"; 368 private Boolean m_how = StringItemList.PROCESSED; 369 } 370 371 372 | Popular Tags |