1 28 29 package com.idaremedia.antx.ownhelpers; 30 31 import java.util.Iterator ; 32 import java.util.Map ; 33 import java.util.NoSuchElementException ; 34 35 import org.apache.tools.ant.Project; 36 import org.apache.tools.ant.types.RegularExpression; 37 import org.apache.tools.ant.util.regexp.Regexp; 38 39 import com.idaremedia.antx.AntX; 40 import com.idaremedia.antx.FixtureExaminer; 41 import com.idaremedia.antx.parameters.IgnoreCaseEnabled; 42 import com.idaremedia.antx.parameters.PropertySource; 43 44 62 63 public final class PropertiesIterator extends ProjectDependentSkeleton 64 implements Iterator , IgnoreCaseEnabled 65 { 66 private static final String IAM_ = AntX.utilities+"PropertiesIterator:"; 67 68 69 75 public PropertiesIterator() 76 { 77 } 78 79 80 86 public PropertiesIterator(Project P, PropertySource domain) 87 { 88 setProject(P); 89 setDomain(domain); 90 } 91 92 93 97 public PropertiesIterator(InnerProperties bean) 98 { 99 setProject(bean.getProject()); 100 setDomain(bean.getDomain()); 101 setIgnoreCase(bean.isIgnoreCase()); 102 setPrefix(bean.getPrefixFilterPattern()); 103 String pattern = bean.getLikeFilterPattern(); 104 if (pattern==null) { 105 pattern = bean.getNotLikeFilterPattern(); 106 if (pattern!=null) { 107 setExclude(true); 108 } 109 } 110 if (pattern!=null) { 111 RegularExpression re = new RegularExpression(); 112 re.setPattern(pattern); 113 setRE(re); 114 } 115 } 116 117 118 122 public void dispose() 123 { 124 m_entryItr = null; 125 m_re = null; 126 m_nextEntry = null; 127 m_formCheck = null; 128 m_prefix = null; 129 } 130 131 132 137 public void setProject(Project P) 138 { 139 AntX.require_(P!=null,IAM_,"setProject- nonzro project"); 140 super.setProject(P); 141 } 142 143 144 149 public void setDomain(PropertySource domain) 150 { 151 AntX.require_(domain!=null,IAM_,"setDomain- nonzro domain"); 152 m_domain = domain; 153 } 154 155 156 161 public PropertySource getDomain() 162 { 163 return m_domain; 164 } 165 166 167 172 public void setRE(RegularExpression re) 173 { 174 m_re = re; 175 } 176 177 178 183 public void setRE(Regexp re) 184 { 185 m_re = re; 186 } 187 188 189 194 public boolean willCheckRE() 195 { 196 return m_re!=null; 197 } 198 199 200 205 public void setPrefix(String prefix) 206 { 207 m_prefix = prefix; 208 m_prefixLocal = null; 209 } 210 211 212 216 public String getPrefix() 217 { 218 return m_prefix; 219 } 220 221 222 227 public boolean willCheckPrefix() 228 { 229 return m_prefix!=null; 230 } 231 232 233 238 public void setIgnoreCase(boolean ignore) 239 { 240 m_reOptions = ignore ? Regexp.MATCH_CASE_INSENSITIVE : 0; 241 } 242 243 244 248 public boolean isIgnoreCase() 249 { 250 return m_reOptions!=0; 251 } 252 253 254 255 262 public final void setExclude(boolean exclude) 263 { 264 m_exclude = exclude; 265 } 266 267 268 269 273 public final boolean isExclusion() 274 { 275 return m_exclude; 276 } 277 278 279 285 public void setCheckSimpleBrokenSubstitution() 286 { 287 getFormCheck().setCheckSimpleBrokenSubstitution(); 288 } 289 290 291 297 public void setCheckBrokenSubstitution() 298 { 299 getFormCheck().setCheckBrokenSubstitution(); 300 } 301 302 303 312 public void setBrokenSubstitutionProxy(String valueProxy) 313 { 314 AntX.require_(valueProxy!=null,IAM_,"setProxy- nonzro valu"); 315 getFormCheck().setBrokenSubstitutionProxy(valueProxy); 316 } 317 318 319 323 public boolean willCheckForm() 324 { 325 return m_formCheck!=null; 326 } 327 328 329 334 private void prepareToIterate() 335 { 336 Project P = getProjectNoNull(); 337 338 Map mp = FixtureExaminer.copyOfProperties(getDomain(),P); 339 m_itemCount = mp.size(); 340 m_entryItr = mp.entrySet().iterator(); 341 mp = null; 342 343 if (m_re instanceof RegularExpression) { 344 m_re = ((RegularExpression)m_re).getRegexp(P); 345 } 346 if (isIgnoreCase() && m_prefix!=null) { 347 m_prefixLocal= m_prefix.toLowerCase(); 348 } 349 } 350 351 352 358 public int candidateCount() 359 { 360 return m_itemCount; 361 } 362 363 364 368 public boolean hasNext() 369 { 370 if (m_entryItr==null) { 371 prepareToIterate(); 372 } 373 374 boolean gotOne = (m_nextEntry!=null); 375 376 if (!gotOne) { 377 while (m_entryItr.hasNext()) { 378 Map.Entry candidate = (Map.Entry )m_entryItr.next(); 379 boolean match; 380 381 if (willCheckPrefix()) { 382 String name = (String )candidate.getKey(); 383 if (m_prefixLocal!=null) { 384 name = name.toLowerCase(); 385 match = name.startsWith(m_prefixLocal); 386 } else { 387 match = name.startsWith(m_prefix); 388 } 389 if (!match) { 391 continue; 392 } 393 } 394 395 if (willCheckRE()) { 396 String name = (String )candidate.getKey(); 397 match = ((Regexp)m_re).matches(name, m_reOptions); 398 if (m_exclude) { 399 if (match) { 400 continue; 401 } 402 } else if (!match) { 403 continue; 404 } 405 } 406 407 if (willCheckForm()) { 408 candidate = m_formCheck.verifiedPropertyValue 409 (candidate, getProject()); 410 } 411 412 if (candidate!=null) { 413 m_nextEntry = candidate; 414 gotOne = true; 415 break; 416 } 417 } 418 } 419 420 return gotOne; 421 } 422 423 424 429 public Object next() 430 { 431 if (m_nextEntry==null) { 432 throw new NoSuchElementException (); 433 } 434 Object result = m_nextEntry; 435 m_nextEntry = null; 436 return result; 437 } 438 439 440 441 446 public final void remove() 447 { 448 throw new UnsupportedOperationException (); 449 } 450 451 452 453 458 public void reset() 459 { 460 m_entryItr = null; 461 m_nextEntry = null; 462 m_itemCount = 0; 463 m_prefixLocal = null; 464 } 465 466 467 468 472 private PropertyExaminer getFormCheck() 473 { 474 if (m_formCheck==null) { 475 m_formCheck = new PropertyExaminer(); 476 } 477 return m_formCheck; 478 } 479 480 481 private Iterator m_entryItr; 482 private Map.Entry m_nextEntry; 483 private int m_itemCount; 484 485 private PropertySource m_domain= PropertySource.ALL; 486 private String m_prefix, m_prefixLocal; 487 private Object m_re; 488 private int m_reOptions; private PropertyExaminer m_formCheck; private boolean m_exclude;} 492 493 494 | Popular Tags |