From 2f35469477b9358250cb3f5d7778b06f6ff58e2f Mon Sep 17 00:00:00 2001 From: Matthew Jason Benson Date: Thu, 10 Mar 2005 21:16:08 +0000 Subject: [PATCH] Sort echoproperties output in text mode as well as XML. PR: 18976 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@277886 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 2 +- .../ant/taskdefs/optional/EchoProperties.java | 18 +++++---- .../tools/ant/util/CollectionUtils.java | 38 ++++++++++++++++++- 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index f1b25daaa..f2ea3d05b 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -67,7 +67,7 @@ Other changes: -------------- * now (alphanumerically) sorts the property list - before echoing, when you request XML output (format="xml"). + before echoing. Bugzilla report 18976. * A new base class DispatchTask has been added to facilitate elegant creation of tasks with multiple actions. diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/EchoProperties.java b/src/main/org/apache/tools/ant/taskdefs/optional/EchoProperties.java index 2aa07f983..6cfd4467d 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/EchoProperties.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/EchoProperties.java @@ -325,15 +325,19 @@ public class EchoProperties extends Task { *@exception IOException trouble */ protected void saveProperties(Hashtable allProps, OutputStream os) - throws IOException, BuildException { - Properties props = new Properties(); - Enumeration e = allProps.keys(); - while (e.hasMoreElements()) { - String name = e.nextElement().toString(); + throws IOException, BuildException { + final List keyList = new ArrayList(allProps.keySet()); + Collections.sort(keyList); + Properties props = new Properties() { + public Enumeration keys() { + return CollectionUtils.asEnumeration(keyList.iterator()); + } + }; + for (int i = 0; i < keyList.size(); i++) { + String name = keyList.get(i).toString(); String value = allProps.get(name).toString(); - props.put(name, value); + props.setProperty(name, value); } - if ("text".equals(format)) { jdkSaveProperties(props, os, "Ant properties"); } else if ("xml".equals(format)) { diff --git a/src/main/org/apache/tools/ant/util/CollectionUtils.java b/src/main/org/apache/tools/ant/util/CollectionUtils.java index f2971c0b7..a575da37d 100644 --- a/src/main/org/apache/tools/ant/util/CollectionUtils.java +++ b/src/main/org/apache/tools/ant/util/CollectionUtils.java @@ -16,10 +16,11 @@ */ package org.apache.tools.ant.util; +import java.util.Vector; +import java.util.Iterator; import java.util.Dictionary; import java.util.Enumeration; import java.util.NoSuchElementException; -import java.util.Vector; /** * A set of helper methods related to collection manipulation. @@ -139,6 +140,41 @@ public class CollectionUtils { return new CompoundEnumeration(e1, e2); } + /** + * Adapt the specified Iterator to the Enumeration interface. + * @param iter the Iterator to adapt. + * @return an Enumeration. + */ + public static Enumeration asEnumeration(final Iterator iter) { + return new Enumeration() { + public boolean hasMoreElements() { + return iter.hasNext(); + } + public Object nextElement() { + return iter.next(); + } + }; + } + + /** + * Adapt the specified Enumeration to the Iterator interface. + * @param enum the Enumeration to adapt. + * @return an Iterator. + */ + public static Iterator asIterator(final Enumeration enum) { + return new Iterator() { + public boolean hasNext() { + return enum.hasMoreElements(); + } + public Object next() { + return enum.nextElement(); + } + public void remove() { + throw new UnsupportedOperationException(); + } + }; + } + private static final class CompoundEnumeration implements Enumeration { private final Enumeration e1, e2;