wasStitchNET – Diff between revs 7 and 8
?pathlinks?
Rev 7 | Rev 8 | |||
---|---|---|---|---|
Line 2... | Line 2... | |||
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; |
|
Line 13... | Line 11... | |||
13 | |
11 | |
|
14 | namespace wasStitchNET.Patchers |
12 | namespace wasStitchNET.Patchers |
|
15 | { |
13 | { |
|
16 | public static class Utilities |
14 | public static class Utilities |
|
17 | { |
- | ||
18 | |
15 | { |
|
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) |
|
Line 33... | Line 30... | |||
33 | var index = e.IndexPosition(); |
30 | var index = e.IndexPosition(); |
|
34 | var name = e.Name.LocalName; |
31 | var name = e.Name.LocalName; |
|
Line 35... | Line 32... | |||
35 | |
32 | |
|
Line -... | Line 33... | |||
- | 33 | // If the element is the root, no index is required |
||
- | 34 | |
||
36 | // If the element is the root, no index is required |
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() |
|
Line 43... | Line 42... | |||
43 | ); |
42 | ); |
|
44 | }; |
43 | }; |
|
Line 45... | Line 44... | |||
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); |
|
Line 48... | Line 47... | |||
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. |
- | ||
60 | /// </param> |
58 | /// The element to get the index of. |
|
61 | public static int IndexPosition(this XElement element) |
- | ||
Line 62... | Line 59... | |||
62 | { |
59 | /// </param> |
|
63 | if (element == null) |
- | ||
64 | { |
60 | public static int IndexPosition(this XElement element) |
|
65 | throw new ArgumentNullException("element"); |
- | ||
Line 66... | Line 61... | |||
66 | } |
61 | { |
|
Line 67... | Line 62... | |||
67 | |
62 | if (element == null) |
|
68 | if (element.Parent == null) |
63 | throw new ArgumentNullException("element"); |
|
69 | { |
64 | |
|
70 | return -1; |
- | ||
71 | } |
65 | if (element.Parent == null) |
|
72 | |
- | ||
Line 73... | Line 66... | |||
73 | var i = 1; // Indexes for nodes start at 1, not 0 |
66 | return -1; |
|
74 | |
67 | |
|
Line 75... | Line 68... | |||
75 | foreach (var sibling in element.Parent.Elements(element.Name)) |
68 | var i = 1; // Indexes for nodes start at 1, not 0 |
|
76 | { |
69 | |
|
77 | if (sibling == element) |
70 | foreach (var sibling in element.Parent.Elements(element.Name)) |
|
78 | { |
- | ||
79 | return i; |
71 | { |
|
80 | } |
72 | if (sibling == element) |
|
81 | |
73 | return i; |