From 7dd5e65a056c490727f5edb12a5f58642c7fa269 Mon Sep 17 00:00:00 2001 From: Steve Loughran Date: Thu, 15 Jul 2004 16:41:12 +0000 Subject: [PATCH] A little extra for those people trying to debug inheritance of properties & save stuff in CVS. The XML output of is now *sorted*. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@276697 13f79535-47bb-0310-9956-ffa450edef68 --- .../ant/taskdefs/optional/EchoProperties.java | 56 +++++++++++++++++-- 1 file changed, 50 insertions(+), 6 deletions(-) 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 2e2a941ab..5da8b665e 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/EchoProperties.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/EchoProperties.java @@ -29,6 +29,10 @@ import java.util.Enumeration; import java.util.Hashtable; import java.util.Properties; import java.util.Vector; +import java.util.List; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.apache.tools.ant.BuildException; @@ -342,20 +346,60 @@ public class EchoProperties extends Task { } } + /** + * a tuple for the sort list. + */ + private static class Tuple implements Comparable{ + public String key; + public String value; + + public Tuple(String key, String value) { + this.key = key; + this.value = value; + } + + /** + * Compares this object with the specified object for order. + * @param o the Object to be compared. + * @return a negative integer, zero, or a positive integer as this object is + * less than, equal to, or greater than the specified object. + * @throws ClassCastException if the specified object's type prevents it + * from being compared to this Object. + */ + public int compareTo(Object o) { + Tuple that=(Tuple) o; + return key.compareTo(that.key); + } + } + + private List sortProperties(Properties props) { + //sort the list. Makes SCM and manual diffs easier. + List sorted = new ArrayList(props.size()); + Enumeration e = props.propertyNames(); + while (e.hasMoreElements()) { + String name = (String) e.nextElement(); + sorted.add(new Tuple(name, props.getProperty(name))); + } + Collections.sort(sorted); + return sorted; + } + protected void xmlSaveProperties(Properties props, OutputStream os) throws IOException { // create XML document Document doc = getDocumentBuilder().newDocument(); Element rootElement = doc.createElement(PROPERTIES); + List sorted=sortProperties(props); + + // output properties - String name; - Enumeration e = props.propertyNames(); - while (e.hasMoreElements()) { - name = (String) e.nextElement(); + Iterator iten = sorted.iterator(); + while (iten.hasNext()) { + Tuple tuple = (Tuple) iten.next(); Element propElement = doc.createElement(PROPERTY); - propElement.setAttribute(ATTR_NAME, name); - propElement.setAttribute(ATTR_VALUE, props.getProperty(name)); + propElement.setAttribute(ATTR_NAME, tuple.key); + propElement.setAttribute(ATTR_VALUE, tuple.value); rootElement.appendChild(propElement); }