-
Notifications
You must be signed in to change notification settings - Fork 595
Expand file tree
/
Copy pathXmlPath.cs
More file actions
188 lines (156 loc) · 5.71 KB
/
XmlPath.cs
File metadata and controls
188 lines (156 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using DocumentFormat.OpenXml.Framework;
using DocumentFormat.OpenXml.Packaging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Xml;
namespace DocumentFormat.OpenXml
{
/// <summary>
/// Defines XPath like information for OpenXmlElement.
/// </summary>
[DebuggerDisplay("{XPath,nq}")]
public class XmlPath
{
/// <summary>
/// Initializes a new instance of the <see cref="XmlPath"/> from
/// the specified <see cref="OpenXmlElement"/>.
/// </summary>
/// <param name="element">
/// The <see cref="OpenXmlElement"/>.
/// </param>
public XmlPath(OpenXmlElement element)
{
if (element is null)
{
throw new ArgumentNullException(nameof(element));
}
PartUri = element.GetPartUri();
XPath = TryBuildXPath(GetElements(element), out var namespaces);
if (namespaces is null)
{
Namespaces = ReadOnlyWrapper.Instance;
}
else
{
Namespaces = new ReadOnlyWrapper(namespaces);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="XmlPath"/> from
/// the specified <see cref="OpenXmlPart"/>.
/// </summary>
/// <param name="part">The <see cref="OpenXmlPart"/>.</param>
public XmlPath(OpenXmlPart part)
{
if (part is null)
{
throw new ArgumentNullException(nameof(part));
}
PartUri = part.Uri;
XPath = string.Empty;
Namespaces = ReadOnlyWrapper.Instance;
}
/// <summary>
/// Gets the namespace definitions used in <see cref="XPath"/>
/// </summary>
public IXmlNamespaceResolver Namespaces { get; }
/// <summary>
/// Gets the XPath string.
/// </summary>
public string XPath { get; }
/// <summary>
/// Gets the internal URI of the part relative to the package root.
/// </summary>
public Uri? PartUri { get; }
/// <summary>
/// Gets XmlPath information of the specified OpenXmlElement.
/// </summary>
/// <param name="element">The OpenXmlElement.</param>
/// <returns>XmlPath to this element from root element.</returns>
internal static XmlPath? GetXPath(OpenXmlElement? element)
{
if (element is null)
{
return null;
}
return new XmlPath(element);
}
internal static XmlPath? GetXPath(OpenXmlPart? part)
{
if (part is null)
{
return null;
}
return new XmlPath(part);
}
private static string TryBuildXPath(Stack<OpenXmlElement> elements, out XmlNamespaceManager? namespaces)
{
if (elements.Count == 0)
{
namespaces = null;
return string.Empty;
}
var xpath = StringBuilderPool.Acquire();
namespaces = new XmlNamespaceManager(new NameTable());
foreach (var element in elements)
{
xpath.Append('/');
if (element is OpenXmlMiscNode)
{
xpath.Append(element.OuterXml);
}
else
{
Debug.Assert(!string.IsNullOrEmpty(element.LocalName));
if (!string.IsNullOrEmpty(element.Prefix))
{
if (!namespaces.HasNamespace(element.Prefix))
{
namespaces.AddNamespace(element.Prefix, element.NamespaceUri);
}
xpath.Append(element.Prefix);
xpath.Append(':');
}
else if (!string.IsNullOrEmpty(element.NamespaceUri))
{
xpath.Append(element.NamespaceUri);
xpath.Append(':');
}
xpath.Append(element.LocalName);
xpath.Append('[');
xpath.Append(element.GetXPathIndex());
xpath.Append(']');
}
}
return StringBuilderPool.GetValueAndRelease(xpath);
}
private static Stack<OpenXmlElement> GetElements(OpenXmlElement? element)
{
var elements = new Stack<OpenXmlElement>();
while (element is not null)
{
elements.Push(element);
element = element.Parent;
}
return elements;
}
private class ReadOnlyWrapper : IXmlNamespaceResolver
{
private readonly IXmlNamespaceResolver _other;
public static IXmlNamespaceResolver Instance { get; } = new ReadOnlyWrapper(new XmlNamespaceManager(new NameTable()));
public ReadOnlyWrapper(IXmlNamespaceResolver other)
{
_other = other;
}
public IDictionary<string, string> GetNamespacesInScope(XmlNamespaceScope scope) => _other.GetNamespacesInScope(scope);
public string? LookupNamespace(string prefix) => _other.LookupNamespace(prefix);
public string? LookupPrefix(string namespaceName) => _other.LookupPrefix(namespaceName);
}
}
}