wasStitchNET – Diff between revs 7 and 8
?pathlinks?
Rev 7 | Rev 8 | |||
---|---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////// |
1 | /////////////////////////////////////////////////////////////////////////// |
|
2 | // Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3 // |
2 | // Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3 // |
|
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
|
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
|
5 | /////////////////////////////////////////////////////////////////////////// |
5 | /////////////////////////////////////////////////////////////////////////// |
|
6 | // Based on: https://seattlesoftware.wordpress.com/2009/03/13/get-the-xpath-to-an-xml-element-xelement/ |
6 | // Based on: https://seattlesoftware.wordpress.com/2009/03/13/get-the-xpath-to-an-xml-element-xelement/ |
|
- | 7 | |
||
7 | using System; |
8 | using System; |
|
8 | using System.Collections.Generic; |
- | ||
9 | using System.Linq; |
9 | using System.Linq; |
|
10 | using System.Text; |
- | ||
11 | using System.Reflection; |
- | ||
12 | using System.Xml.Linq; |
10 | using System.Xml.Linq; |
|
13 | |
11 | |
|
14 | namespace wasStitchNET.Patchers |
12 | namespace wasStitchNET.Patchers |
|
15 | { |
13 | { |
|
16 | public static class Utilities |
14 | public static class Utilities |
|
17 | { |
15 | { |
|
18 | |
- | ||
19 | /// <summary> |
16 | /// <summary> |
|
20 | /// Get the absolute XPath to a given XElement |
17 | /// Get the absolute XPath to a given XElement |
|
21 | /// (e.g. "/people/person[6]/name[1]/last[1]"). |
18 | /// (e.g. "/people/person[6]/name[1]/last[1]"). |
|
22 | /// </summary> |
19 | /// </summary> |
|
23 | /// <param name="element"> |
20 | /// <param name="element"> |
|
24 | /// The element to get the index of. |
21 | /// The element to get the index of. |
|
25 | /// </param> |
22 | /// </param> |
|
26 | public static string GetAbsoluteXPath(this XElement element) |
23 | public static string GetAbsoluteXPath(this XElement element) |
|
27 | { |
24 | { |
|
28 | if (element == null) |
25 | if (element == null) |
|
29 | throw new ArgumentNullException("element"); |
26 | throw new ArgumentNullException("element"); |
|
30 | |
27 | |
|
31 | Func<XElement, string> relativeXPath = e => |
28 | Func<XElement, string> relativeXPath = e => |
|
32 | { |
29 | { |
|
33 | var index = e.IndexPosition(); |
30 | var index = e.IndexPosition(); |
|
34 | var name = e.Name.LocalName; |
31 | var name = e.Name.LocalName; |
|
35 | |
32 | |
|
36 | // If the element is the root, no index is required |
33 | // If the element is the root, no index is required |
|
- | 34 | |
||
- | 35 | return index == -1 |
||
37 | |
36 | ? "/" + name |
|
38 | return (index == -1) ? "/" + name : string.Format |
37 | : string.Format |
|
39 | ( |
38 | ( |
|
40 | "/{0}[{1}]", |
39 | "/{0}[{1}]", |
|
41 | name, |
40 | name, |
|
42 | index.ToString() |
41 | index.ToString() |
|
43 | ); |
42 | ); |
|
44 | }; |
43 | }; |
|
45 | |
44 | |
|
46 | var ancestors = from e in element.Ancestors() |
45 | var ancestors = from e in element.Ancestors() |
|
47 | select relativeXPath(e); |
46 | select relativeXPath(e); |
|
48 | |
47 | |
|
49 | return string.Concat(ancestors.Reverse().ToArray()) + |
48 | return string.Concat(ancestors.Reverse().ToArray()) + |
|
50 | relativeXPath(element); |
49 | relativeXPath(element); |
|
51 | } |
50 | } |
|
52 | |
51 | |
|
53 | /// <summary> |
52 | /// <summary> |
|
54 | /// Get the index of the given XElement relative to its |
53 | /// Get the index of the given XElement relative to its |
|
55 | /// siblings with identical names. If the given element is |
54 | /// siblings with identical names. If the given element is |
|
56 | /// the root, -1 is returned. |
55 | /// the root, -1 is returned. |
|
57 | /// </summary> |
56 | /// </summary> |
|
58 | /// <param name="element"> |
57 | /// <param name="element"> |
|
59 | /// The element to get the index of. |
58 | /// The element to get the index of. |
|
60 | /// </param> |
59 | /// </param> |
|
61 | public static int IndexPosition(this XElement element) |
60 | public static int IndexPosition(this XElement element) |
|
62 | { |
61 | { |
|
63 | if (element == null) |
62 | if (element == null) |
|
64 | { |
- | ||
65 | throw new ArgumentNullException("element"); |
63 | throw new ArgumentNullException("element"); |
|
66 | } |
- | ||
67 | |
64 | |
|
68 | if (element.Parent == null) |
- | ||
69 | { |
65 | if (element.Parent == null) |
|
70 | return -1; |
- | ||
71 | } |
66 | return -1; |
|
72 | |
67 | |
|
73 | var i = 1; // Indexes for nodes start at 1, not 0 |
68 | var i = 1; // Indexes for nodes start at 1, not 0 |
|
74 | |
69 | |
|
75 | foreach (var sibling in element.Parent.Elements(element.Name)) |
70 | foreach (var sibling in element.Parent.Elements(element.Name)) |
|
76 | { |
71 | { |
|
77 | if (sibling == element) |
72 | if (sibling == element) |
|
78 | { |
- | ||
79 | return i; |
73 | return i; |
|
80 | } |
- | ||
81 | |
74 | |
|
82 | i++; |
75 | i++; |
|
83 | } |
76 | } |
|
84 | |
77 | |
|
85 | throw new InvalidOperationException |
78 | throw new InvalidOperationException |
|
86 | ("element has been removed from its parent."); |
79 | ("element has been removed from its parent."); |
|
87 | } |
80 | } |
|
88 | |
- | ||
89 | } |
81 | } |
|
90 | } |
82 | } |
|
91 | |
83 | |
|
- | 84 |
|
||
- | 85 | |
||
- | 86 | |
||
- | 87 | |