wasStitchNET – Diff between revs 8 and 9

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 8 Rev 9
Line 11... Line 11...
11   11  
12 namespace wasStitchNET.Patchers 12 namespace wasStitchNET.Patchers
13 { 13 {
14 public static class XML 14 public static class XML
-   15 {
15 { 16 public static XDocument PatchXDocument(XDocument cfg, XDocument nfg, HashSet<string> forceXPaths,
16 public static HashSet<string> GetFileDelta(string cfg, string nfg) 17 HashSet<string> excludeXPaths)
17 { -  
18 var configuredTags = new HashSet<string>(); -  
19   -  
20 var configuration = XDocument.Load(cfg); -  
21 foreach (var e in XDocument.Load(nfg).Descendants()) -  
22 { 18 {
23 var cfgElement = configuration.XPathSelectElement(e.GetAbsoluteXPath()); 19 // Select all distinct paths in the new configuration without paths to exclude and with paths to force.
24 if (cfgElement == null) 20 foreach (var e in nfg
25 { 21 .Descendants()
26 configuredTags.Add(e.Name.LocalName); 22 .Where(e => !excludeXPaths.Any(o => e.GetAbsoluteXPath().Contains(o)))
27 continue; 23 .Concat(
28 } -  
29   24 nfg
30 if (e.Descendants().Any()) -  
31 continue; -  
32   25 .Descendants()
33 if (!cfgElement.Value.Equals(e.Value)) 26 .Where(e => forceXPaths.Any(o => e.GetAbsoluteXPath().Contains(o))))
34 continue; -  
35   -  
36 configuredTags.Add(e.Name.LocalName); -  
37 } -  
38   -  
39 return configuredTags; -  
40 } -  
41   -  
42 public static XDocument PatchXDocument(XDocument cfg, XDocument nfg, HashSet<string> configuredTags) -  
43 { -  
44 foreach (var e in nfg.Descendants() -  
45 .Where(e => configuredTags.Contains(e.Name.LocalName))) 27 .Distinct())
-   28 {
-   29 // Get the XPath to this element.
46 { 30 var xpath = e.GetAbsoluteXPath();
47 // Select the element in the current configuration that is found in the default configuration. 31 // Select the element in the current configuration that is found in the default configuration.
48 var cfgElement = cfg.XPathSelectElement(e.GetAbsoluteXPath()); 32 var cfgElement = cfg.XPathSelectElement(xpath);
49 switch (cfgElement != null) 33 switch (cfgElement != null)
50 { 34 {
51 // Element found in current configuration. 35 // Element found in current configuration.
52 case true: 36 case true:
53 // Only set the element value if it has no descendants. 37 // Only set the element value if it has no descendants and it contained in the force paths.
54 if (!cfgElement.Descendants().Any()) 38 if (!cfgElement.Descendants().Any() && forceXPaths.Any(o => xpath.Contains(o)))
55 cfgElement.Value = e.Value; 39 cfgElement.Value = e.Value;
56 break; 40 break;
57 // Element not found in the current configuration. 41 // Element not found in the current configuration.
58 default: 42 default:
Line 62... Line 46...
62 { 46 {
63 var cfgParentElement = cfg.XPathSelectElement(parent.GetAbsoluteXPath()); 47 var cfgParentElement = cfg.XPathSelectElement(parent.GetAbsoluteXPath());
64 if (cfgParentElement != null) 48 if (cfgParentElement != null)
65 { 49 {
66 // Add the default configuration parent to the current configuration. 50 // Add the default configuration parent to the current configuration.
67 cfgParentElement.ReplaceWith( 51 cfgParentElement.Add(
68 nfg.XPathSelectElement(cfgParentElement.GetAbsoluteXPath())); 52 nfg.XPathSelectElement(cfgParentElement.GetAbsoluteXPath()));
69 break; 53 break;
70 } 54 }
71 parent = parent.Parent; 55 parent = parent.Parent;
72 } while (parent != null); 56 } while (parent != null);