From: <bo...@us...> - 2013-04-14 18:21:38
|
Revision: 528 http://sourceforge.net/p/xmlunit/code/528 Author: bodewig Date: 2013-04-14 18:21:34 +0000 (Sun, 14 Apr 2013) Log Message: ----------- caching of (partial) XPaths to speed up comparisions, submitted by David Rees - fixes #61 Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/XPathContext.java trunk/xmlunit/src/main/net-core/diff/XPathContext.cs Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/XPathContext.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/XPathContext.java 2013-04-14 16:29:49 UTC (rev 527) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/XPathContext.java 2013-04-14 18:21:34 UTC (rev 528) @@ -16,11 +16,15 @@ import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; + import javax.xml.namespace.QName; + import net.sf.xmlunit.util.Nodes; + import org.w3c.dom.Node; public class XPathContext { @@ -131,13 +135,22 @@ } public String getXPath() { - StringBuilder sb = new StringBuilder(); - for (Level l : path) { - sb.append(SEP).append(l.expression); + String xpath = getXPath(path.descendingIterator()); + return xpath.replace(SEP + SEP, SEP); + } + + public String getXPath(Iterator<Level> dIterator) { + if (!dIterator.hasNext()) { + return ""; } - return sb.toString().replace(SEP + SEP, SEP); + Level l = dIterator.next(); + if (null == l.xpath) { + l.xpath = getXPath(dIterator) + SEP + l.expression; + } + return l.xpath; } + private String getName(QName name) { String ns = name.getNamespaceURI(); String p = null; @@ -164,6 +177,7 @@ private final String expression; private List<Level> children = new ArrayList<Level>(); private Map<QName, Level> attributes = new HashMap<QName, Level>(); + private String xpath; private Level(String expression) { this.expression = expression; } Modified: trunk/xmlunit/src/main/net-core/diff/XPathContext.cs =================================================================== --- trunk/xmlunit/src/main/net-core/diff/XPathContext.cs 2013-04-14 16:29:49 UTC (rev 527) +++ trunk/xmlunit/src/main/net-core/diff/XPathContext.cs 2013-04-14 18:21:34 UTC (rev 528) @@ -128,14 +128,23 @@ public string XPath { get { - StringBuilder sb = new StringBuilder(); - foreach (Level l in path) { - sb.AppendFormat(SEP + "{0}", l.Expression); - } - return sb.Replace(SEP + SEP, SEP).ToString(); + String xpath = EnsureXPathsAreSetOnLevels(path.Last); + return xpath.Replace(SEP + SEP, SEP); } } + private static String EnsureXPathsAreSetOnLevels(LinkedListNode<Level> l) { + if (l == null) { + return string.Empty; + } + Level level = l.Value; + if (null == level.XPath) { + level.XPath = EnsureXPathsAreSetOnLevels(l.Previous) + + SEP + level.Expression; + } + return level.XPath; + } + private string GetName(XmlQualifiedName name) { string ns = name.Namespace; string p = null; @@ -159,10 +168,15 @@ } internal class Level { + private string xpath; internal readonly string Expression; internal readonly IList<Level> Children = new List<Level>(); internal readonly IDictionary<XmlQualifiedName, Level> Attributes = new Dictionary<XmlQualifiedName, Level>(); + internal string XPath { + get { return xpath; } + set { xpath = value; } + } internal Level(string expression) { this.Expression = expression; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |