ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From <Jan.Mate...@rzf.fin-nrw.de>
Subject checking task configuration
Date Mon, 28 Jan 2008 14:07:27 GMT
I prefer collecting all configuration errors and reporting them at once
over throwing a BuildException for each error. Ok Ant is not a GUI ....
I implemented a helper class for that - is it worth to introduce that
into Ant Core?


Jan



/**
 * @see <a
href="http://martinfowler.com/eaaDev/Notification.html">Notification
Pattern</a>
 */
public class AntConfigurationChecker {
	List errors = new ArrayList();
	Task task;

	public AntConfigurationChecker(Task task) {
		this.task = task;
	}
	
	public void assertConfig(boolean bedingung, String
fehlermeldung) {
		if (bedingung) {
			errors.add(fehlermeldung);
		}
	}
	
	public void fail(String fehlermeldung) {
		errors.add(fehlermeldung);
	}
	
	public void checkErrors() throws BuildException {
		if (!errors.isEmpty()) {
			StringBuffer sb = new StringBuffer();
			sb.append("Configuration error on <");
			sb.append(task.getTaskName());
			sb.append(">:");
			sb.append(System.getProperty("line.separator"));
			for (Iterator it = errors.iterator();
it.hasNext();) {
				String msg = (String) it.next();
				sb.append("- ");
				sb.append(msg);
	
sb.append(System.getProperty("line.separator"));
			}
			throw new BuildException(sb.toString());
		}		
	}
}



public class MyTask {
	private void checkConfiguration() {
		AntConfigurationChecker checker = new
AntConfigurationChecker(this);
		checker.assertConfig(
			xmlfile == null && txtfile == null,
			"You have to specify one of 'txtfile' or
'xmlfile'.");
		checker.assertConfig(
			release == null,
			"You have to specify the 'release'.");
		checker.checkErrors();
	}
	public void execute() throws BuildException {
		checkConfiguration();
		...
	}
}

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message