1 18 package org.apache.tools.ant.types; 19 20 import java.io.File ; 21 import java.util.Stack ; 22 import java.util.Vector ; 23 import java.util.Iterator ; 24 import java.util.ArrayList ; 25 26 import org.apache.tools.ant.Project; 27 import org.apache.tools.ant.BuildException; 28 import org.apache.tools.ant.taskdefs.Redirector; 29 30 34 public class RedirectorElement extends DataType { 35 36 39 private boolean usingInput = false; 40 41 44 private boolean usingOutput = false; 45 46 49 private boolean usingError = false; 50 51 56 private Boolean logError; 57 58 59 private String outputProperty; 60 61 62 private String errorProperty; 63 64 65 private String inputString; 66 67 68 private Boolean append; 69 70 71 private Boolean alwaysLog; 72 73 74 private Boolean createEmptyFiles; 75 76 77 private Mapper inputMapper; 78 79 80 private Mapper outputMapper; 81 82 83 private Mapper errorMapper; 84 85 86 private Vector inputFilterChains = new Vector (); 87 88 89 private Vector outputFilterChains = new Vector (); 90 91 92 private Vector errorFilterChains = new Vector (); 93 94 95 private String outputEncoding; 96 97 98 private String errorEncoding; 99 100 101 private String inputEncoding; 102 103 104 private Boolean logInputString; 105 106 110 public void addConfiguredInputMapper(Mapper inputMapper) { 111 if (isReference()) { 112 throw noChildrenAllowed(); 113 } 114 if (this.inputMapper != null) { 115 if (usingInput) { 116 throw new BuildException("attribute \"input\"" 117 + " cannot coexist with a nested <inputmapper>"); 118 } else { 119 throw new BuildException("Cannot have > 1 <inputmapper>"); 120 } 121 } 122 this.inputMapper = inputMapper; 123 } 124 125 129 public void addConfiguredOutputMapper(Mapper outputMapper) { 130 if (isReference()) { 131 throw noChildrenAllowed(); 132 } 133 if (this.outputMapper != null) { 134 if (usingOutput) { 135 throw new BuildException("attribute \"output\"" 136 + " cannot coexist with a nested <outputmapper>"); 137 } else { 138 throw new BuildException("Cannot have > 1 <outputmapper>"); 139 } 140 } 141 this.outputMapper = outputMapper; 142 } 143 144 148 public void addConfiguredErrorMapper(Mapper errorMapper) { 149 if (isReference()) { 150 throw noChildrenAllowed(); 151 } 152 if (this.errorMapper != null) { 153 if (usingError) { 154 throw new BuildException("attribute \"error\"" 155 + " cannot coexist with a nested <errormapper>"); 156 } else { 157 throw new BuildException("Cannot have > 1 <errormapper>"); 158 } 159 } 160 this.errorMapper = errorMapper; 161 } 162 163 171 public void setRefid(Reference r) throws BuildException { 172 if (usingInput 173 || usingOutput 174 || usingError 175 || inputString != null 176 || logError != null 177 || append != null 178 || createEmptyFiles != null 179 || inputEncoding != null 180 || outputEncoding != null 181 || errorEncoding != null 182 || outputProperty != null 183 || errorProperty != null 184 || logInputString != null) { 185 throw tooManyAttributes(); 186 } 187 super.setRefid(r); 188 } 189 190 194 public void setInput(File input) { 195 if (isReference()) { 196 throw tooManyAttributes(); 197 } 198 if (inputString != null) { 199 throw new BuildException("The \"input\" and \"inputstring\" " 200 + "attributes cannot both be specified"); 201 } 202 usingInput = true; 203 inputMapper = createMergeMapper(input); 204 } 205 206 210 public void setInputString(String inputString) { 211 if (isReference()) { 212 throw tooManyAttributes(); 213 } 214 if (usingInput) { 215 throw new BuildException("The \"input\" and \"inputstring\" " 216 + "attributes cannot both be specified"); 217 } 218 this.inputString = inputString; 219 } 220 221 227 public void setLogInputString(boolean logInputString) { 228 if (isReference()) { 229 throw tooManyAttributes(); 230 } 231 this.logInputString = logInputString ? Boolean.TRUE : Boolean.FALSE; 232 } 233 234 240 public void setOutput(File out) { 241 if (isReference()) { 242 throw tooManyAttributes(); 243 } 244 if (out == null) { 245 throw new IllegalArgumentException ("output file specified as null"); 246 } 247 usingOutput = true; 248 outputMapper = createMergeMapper(out); 249 } 250 251 255 public void setOutputEncoding(String outputEncoding) { 256 if (isReference()) { 257 throw tooManyAttributes(); 258 } 259 this.outputEncoding = outputEncoding; 260 } 261 262 267 public void setErrorEncoding(String errorEncoding) { 268 if (isReference()) { 269 throw tooManyAttributes(); 270 } 271 this.errorEncoding = errorEncoding; 272 } 273 274 278 public void setInputEncoding(String inputEncoding) { 279 if (isReference()) { 280 throw tooManyAttributes(); 281 } 282 this.inputEncoding = inputEncoding; 283 } 284 285 292 public void setLogError(boolean logError) { 293 if (isReference()) { 294 throw tooManyAttributes(); 295 } 296 this.logError = ((logError) ? Boolean.TRUE : Boolean.FALSE); 297 } 298 299 303 public void setError(File error) { 304 if (isReference()) { 305 throw tooManyAttributes(); 306 } 307 if (error == null) { 308 throw new IllegalArgumentException ("error file specified as null"); 309 } 310 usingError = true; 311 errorMapper = createMergeMapper(error); 312 } 313 314 320 public void setOutputProperty(String outputProperty) { 321 if (isReference()) { 322 throw tooManyAttributes(); 323 } 324 this.outputProperty = outputProperty; 325 } 326 327 333 public void setAppend(boolean append) { 334 if (isReference()) { 335 throw tooManyAttributes(); 336 } 337 this.append = ((append) ? Boolean.TRUE : Boolean.FALSE); 338 } 339 340 347 public void setAlwaysLog(boolean alwaysLog) { 348 if (isReference()) { 349 throw tooManyAttributes(); 350 } 351 this.alwaysLog = ((alwaysLog) ? Boolean.TRUE : Boolean.FALSE); 352 } 353 354 359 public void setCreateEmptyFiles(boolean createEmptyFiles) { 360 if (isReference()) { 361 throw tooManyAttributes(); 362 } 363 this.createEmptyFiles = ((createEmptyFiles) 364 ? Boolean.TRUE : Boolean.FALSE); 365 } 366 367 373 public void setErrorProperty(String errorProperty) { 374 if (isReference()) { 375 throw tooManyAttributes(); 376 } 377 this.errorProperty = errorProperty; 378 } 379 380 384 public FilterChain createInputFilterChain() { 385 if (isReference()) { 386 throw noChildrenAllowed(); 387 } 388 FilterChain result = new FilterChain(); 389 result.setProject(getProject()); 390 inputFilterChains.add(result); 391 return result; 392 } 393 394 398 public FilterChain createOutputFilterChain() { 399 if (isReference()) { 400 throw noChildrenAllowed(); 401 } 402 FilterChain result = new FilterChain(); 403 result.setProject(getProject()); 404 outputFilterChains.add(result); 405 return result; 406 } 407 408 412 public FilterChain createErrorFilterChain() { 413 if (isReference()) { 414 throw noChildrenAllowed(); 415 } 416 FilterChain result = new FilterChain(); 417 result.setProject(getProject()); 418 errorFilterChains.add(result); 419 return result; 420 } 421 422 426 public void configure(Redirector redirector) { 427 configure(redirector, null); 428 } 429 430 436 public void configure(Redirector redirector, String sourcefile) { 437 if (isReference()) { 438 getRef().configure(redirector, sourcefile); 439 return; 440 } 441 if (alwaysLog != null) { 442 redirector.setAlwaysLog(alwaysLog.booleanValue()); 443 } 444 if (logError != null) { 445 redirector.setLogError(logError.booleanValue()); 446 } 447 if (append != null) { 448 redirector.setAppend(append.booleanValue()); 449 } 450 if (createEmptyFiles != null) { 451 redirector.setCreateEmptyFiles(createEmptyFiles.booleanValue()); 452 } 453 if (outputProperty != null) { 454 redirector.setOutputProperty(outputProperty); 455 } 456 if (errorProperty != null) { 457 redirector.setErrorProperty(errorProperty); 458 } 459 if (inputString != null) { 460 redirector.setInputString(inputString); 461 } 462 if (logInputString != null) { 463 redirector.setLogInputString(logInputString.booleanValue()); 464 } 465 if (inputMapper != null) { 466 String [] inputTargets = null; 467 try { 468 inputTargets = 469 inputMapper.getImplementation().mapFileName(sourcefile); 470 } catch (NullPointerException enPeaEx) { 471 if (sourcefile != null) { 472 throw enPeaEx; 473 } 474 } 475 if (inputTargets != null && inputTargets.length > 0) { 476 redirector.setInput(toFileArray(inputTargets)); 477 } 478 } 479 if (outputMapper != null) { 480 String [] outputTargets = null; 481 try { 482 outputTargets = 483 outputMapper.getImplementation().mapFileName(sourcefile); 484 } catch (NullPointerException enPeaEx) { 485 if (sourcefile != null) { 486 throw enPeaEx; 487 } 488 } 489 if (outputTargets != null && outputTargets.length > 0) { 490 redirector.setOutput(toFileArray(outputTargets)); 491 } 492 } 493 if (errorMapper != null) { 494 String [] errorTargets = null; 495 try { 496 errorTargets = 497 errorMapper.getImplementation().mapFileName(sourcefile); 498 } catch (NullPointerException enPeaEx) { 499 if (sourcefile != null) { 500 throw enPeaEx; 501 } 502 } 503 if (errorTargets != null && errorTargets.length > 0) { 504 redirector.setError(toFileArray(errorTargets)); 505 } 506 } 507 if (inputFilterChains.size() > 0) { 508 redirector.setInputFilterChains(inputFilterChains); 509 } 510 if (outputFilterChains.size() > 0) { 511 redirector.setOutputFilterChains(outputFilterChains); 512 } 513 if (errorFilterChains.size() > 0) { 514 redirector.setErrorFilterChains(errorFilterChains); 515 } 516 if (inputEncoding != null) { 517 redirector.setInputEncoding(inputEncoding); 518 } 519 if (outputEncoding != null) { 520 redirector.setOutputEncoding(outputEncoding); 521 } 522 if (errorEncoding != null) { 523 redirector.setErrorEncoding(errorEncoding); 524 } 525 } 526 527 532 protected Mapper createMergeMapper(File destfile) { 533 Mapper result = new Mapper(getProject()); 534 result.setClassname( 535 org.apache.tools.ant.util.MergingMapper.class.getName()); 536 result.setTo(destfile.getAbsolutePath()); 537 return result; 538 } 539 540 545 protected File [] toFileArray(String [] name) { 546 if (name == null) { 547 return null; 548 } 549 ArrayList list = new ArrayList (name.length); 551 for (int i = 0; i < name.length; i++) { 552 if (name[i] != null) { 553 list.add(getProject().resolveFile(name[i])); 554 } 555 } 556 return (File []) (list.toArray(new File [list.size()])); 557 } 558 559 566 protected void dieOnCircularReference(Stack stk, Project p) 567 throws BuildException { 568 if (isChecked()) { 569 return; 570 } 571 if (isReference()) { 572 super.dieOnCircularReference(stk, p); 573 } else { 574 Mapper[] m = new Mapper[] {inputMapper, outputMapper, errorMapper}; 575 for (int i = 0; i < m.length; i++) { 576 if (m[i] != null) { 577 stk.push(m[i]); 578 m[i].dieOnCircularReference(stk, p); 579 stk.pop(); 580 } 581 } 582 Vector [] v = new Vector [] 583 {inputFilterChains, outputFilterChains, errorFilterChains}; 584 for (int i = 0; i < v.length; i++) { 585 if (v[i] != null) { 586 for (Iterator fci = v[i].iterator(); fci.hasNext();) { 587 FilterChain fc = (FilterChain) fci.next(); 588 stk.push(fc); 589 fc.dieOnCircularReference(stk, p); 590 stk.pop(); 591 } 592 } 593 } 594 setChecked(true); 595 } 596 } 597 598 603 private RedirectorElement getRef() { 604 return (RedirectorElement) getCheckedRef(); 605 } 606 607 } 608 | Popular Tags |