From 15ac873fb177768257f146f14bdcce1084730c73 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Fri, 21 Jan 2011 16:31:05 +0000 Subject: [PATCH] some refactoring triggered by PR 50611 git-svn-id: https://svn.apache.org/repos/asf/ant/antlibs/dotnet/trunk@1061884 13f79535-47bb-0310-9956-ffa450edef68 --- changes.xml | 2 +- src/main/org/apache/ant/dotnet/NUnitTask.java | 26 ++------ .../ant/dotnet/build/AbstractBuildTask.java | 7 +++ .../apache/ant/dotnet/build/MSBuildTask.java | 29 ++------- .../ant/dotnet/util/CollectionUtils.java | 62 +++++++++++++++++++ 5 files changed, 78 insertions(+), 48 deletions(-) create mode 100644 src/main/org/apache/ant/dotnet/util/CollectionUtils.java diff --git a/changes.xml b/changes.xml index bb4e4c2..6d40db6 100644 --- a/changes.xml +++ b/changes.xml @@ -52,7 +52,7 @@ The wix task has new nested elements that allow users to send addtional command line arguments to candle and light. - + The include and exclude arguments to NUnit were broken (they had a leading ","). diff --git a/src/main/org/apache/ant/dotnet/NUnitTask.java b/src/main/org/apache/ant/dotnet/NUnitTask.java index 07aa687..672bcc7 100644 --- a/src/main/org/apache/ant/dotnet/NUnitTask.java +++ b/src/main/org/apache/ant/dotnet/NUnitTask.java @@ -18,6 +18,7 @@ package org.apache.ant.dotnet; +import org.apache.ant.dotnet.util.CollectionUtils; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Task; import org.apache.tools.ant.types.Environment; @@ -307,32 +308,12 @@ public void execute() { if (includes.size() > 0) { StringBuffer sb = new StringBuffer("/include="); - iter = includes.iterator(); - boolean first = true; - while (iter.hasNext()) { - if (first) { - first = false; - } else { - sb.append(","); - } - NamedElement a = (NamedElement) iter.next(); - sb.append(a.getName()); - } + sb.append(CollectionUtils.flattenToString(includes)); exec.createArg().setValue(sb.toString()); } if (excludes.size() > 0) { StringBuffer sb = new StringBuffer("/exclude="); - iter = excludes.iterator(); - boolean first = true; - while (iter.hasNext()) { - if (first) { - first = false; - } else { - sb.append(","); - } - NamedElement a = (NamedElement) iter.next(); - sb.append(a.getName()); - } + sb.append(CollectionUtils.flattenToString(excludes)); exec.createArg().setValue(sb.toString()); } @@ -349,5 +330,6 @@ public static class NamedElement { private String name; public String getName() {return name;} public void setName(String s) {name = s;} + public String toString() {return getName();} } } \ No newline at end of file diff --git a/src/main/org/apache/ant/dotnet/build/AbstractBuildTask.java b/src/main/org/apache/ant/dotnet/build/AbstractBuildTask.java index 7cbcdaf..dd15337 100644 --- a/src/main/org/apache/ant/dotnet/build/AbstractBuildTask.java +++ b/src/main/org/apache/ant/dotnet/build/AbstractBuildTask.java @@ -126,6 +126,9 @@ public String getName() { return name; } + public String toString() { + return getName(); + } } /** @@ -159,6 +162,10 @@ public void setValue(String value) { public String getValue() { return value; } + + public String toString() { + return getName() + "=" + getValue(); + } } /** diff --git a/src/main/org/apache/ant/dotnet/build/MSBuildTask.java b/src/main/org/apache/ant/dotnet/build/MSBuildTask.java index 5b828ff..bd5cf6d 100644 --- a/src/main/org/apache/ant/dotnet/build/MSBuildTask.java +++ b/src/main/org/apache/ant/dotnet/build/MSBuildTask.java @@ -19,9 +19,10 @@ package org.apache.ant.dotnet.build; import java.io.File; -import java.util.Iterator; import java.util.List; +import org.apache.ant.dotnet.util.CollectionUtils; + import org.w3c.dom.DocumentFragment; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -58,18 +59,7 @@ protected String[] getBuildfileArguments(File buildFile) { protected String[] getTargetArguments(List targets) { if (targets.size() > 0) { StringBuffer sb = new StringBuffer("/target:"); - Iterator iter = targets.iterator(); - boolean first = true; - while (iter.hasNext()) { - AbstractBuildTask.Target t = - (AbstractBuildTask.Target) iter.next(); - if (!first) { - sb.append(";"); - } else { - first = false; - } - sb.append(t.getName()); - } + sb.append(CollectionUtils.flattenToString(targets, ";")); return new String[]{sb.toString()}; } else { return new String[0]; @@ -79,18 +69,7 @@ protected String[] getTargetArguments(List targets) { protected String[] getPropertyArguments(List properties) { if (properties.size() > 0) { StringBuffer sb = new StringBuffer("/property:"); - Iterator iter = properties.iterator(); - boolean first = true; - while (iter.hasNext()) { - AbstractBuildTask.Property p = - (AbstractBuildTask.Property) iter.next(); - if (!first) { - sb.append(";"); - } else { - first = false; - } - sb.append(p.getName()).append("=").append(p.getValue()); - } + sb.append(CollectionUtils.flattenToString(properties, ";")); return new String[]{sb.toString()}; } else { return new String[0]; diff --git a/src/main/org/apache/ant/dotnet/util/CollectionUtils.java b/src/main/org/apache/ant/dotnet/util/CollectionUtils.java new file mode 100644 index 0000000..6bddab2 --- /dev/null +++ b/src/main/org/apache/ant/dotnet/util/CollectionUtils.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.ant.dotnet.util; + +import java.util.Collection; +import java.util.Iterator; + +/** + * Helper methods related to collection manipulation. + * + *

This is a stripped down copy of Ant 1.8.2's version so the .NET + * Antlib can still be used with Ant 1.7.0.

+ * + * @since .NET Antlib 1.1 + */ +public class CollectionUtils { + + /** + * Creates a comma separated list of all values held in the given + * collection. + * + * @since .NET Antlib 1.1 + */ + public static String flattenToString(Collection c) { + return flattenToString(c, ","); + } + + /** + * Creates a list of all values held in the given collection + * separated by the given separator. + * + * @since .NET Antlib 1.1 + */ + public static String flattenToString(Collection c, String sep) { + Iterator iter = c.iterator(); + boolean first = true; + StringBuffer sb = new StringBuffer(); + while (iter.hasNext()) { + if (!first) { + sb.append(sep); + } + sb.append(String.valueOf(iter.next())); + first = false; + } + return sb.toString(); + } +}