forked from dotnet/Open-XML-SDK
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenXmlPartRootElement.cs
More file actions
371 lines (317 loc) · 14.2 KB
/
OpenXmlPartRootElement.cs
File metadata and controls
371 lines (317 loc) · 14.2 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// 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.Features;
using DocumentFormat.OpenXml.Framework;
using DocumentFormat.OpenXml.Packaging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
namespace DocumentFormat.OpenXml
{
/// <summary>
/// Represents a base class for all root elements.
/// </summary>
public abstract class OpenXmlPartRootElement : OpenXmlCompositeElement
{
private OpenXmlElementContext? _context;
private bool? _standaloneDeclaration;
/// <summary>
/// Initializes a new instance of the OpenXmlPartRootElement class.
/// </summary>
protected OpenXmlPartRootElement()
{
}
/// <summary>
/// Initializes a new instance of the OpenXmlPartRootElement class using the supplied OpenXmlPart.
/// </summary>
/// <param name="openXmlPart">The OpenXmlPart class.</param>
protected OpenXmlPartRootElement(OpenXmlPart openXmlPart)
{
if (openXmlPart is null)
{
throw new ArgumentNullException(nameof(openXmlPart));
}
LoadFromPart(openXmlPart);
}
/// <summary>
/// Initializes a new instance of the OpenXmlPartRootElement class using the supplied outer XML.
/// </summary>
/// <param name="outerXml">The outer XML of the element.</param>
protected OpenXmlPartRootElement(string outerXml)
: base(outerXml)
{
}
/// <summary>
/// Initializes a new instance of the OpenXmlPartRootElement class using the supplied list of child elements.
/// </summary>
/// <param name="childElements">All child elements.</param>
protected OpenXmlPartRootElement(IEnumerable<OpenXmlElement> childElements)
: base(childElements)
{
}
/// <summary>
/// Initializes a new instance of the OpenXmlPartRootElement class using the supplied array of child elements.
/// </summary>
/// <param name="childElements">All child elements</param>
protected OpenXmlPartRootElement(params OpenXmlElement[] childElements)
: base(childElements)
{
}
/// <summary>
/// Gets the OpenXmlElementContext.
/// </summary>
internal override OpenXmlElementContext RootElementContext => _context ??= new(Features.GetNamespaceResolver());
/// <summary>
/// Load the DOM tree from the Open XML part.
/// </summary>
/// <param name="openXmlPart">The part this root element to be loaded from.</param>
/// <exception cref="InvalidDataException">Thrown when the part contains an incorrect root element.</exception>
internal void LoadFromPart(OpenXmlPart openXmlPart)
{
if (openXmlPart is null)
{
throw new ArgumentNullException(nameof(openXmlPart));
}
// Accessed before stream as it may cause the stream to reload
var strictRelationshipFound = openXmlPart.OpenXmlPackage.StrictRelationshipFound;
using (Stream partStream = openXmlPart.GetStream(FileMode.Open))
{
LoadFromPart(openXmlPart, partStream, strictRelationshipFound);
}
}
/// <summary>
/// Load the DOM tree from the Open XML part.
/// </summary>
/// <param name="openXmlPart">The part this root element to be loaded from.</param>
/// <param name="partStream">The stream of the part.</param>
/// <param name="strictRelationshipFound">Whether a strict relationship was found.</param>
/// <returns>
/// Returns true when the part stream is loaded successfully into this root element.
/// Returns false when the part stream does not contain any xml element.
/// </returns>
/// <exception cref="InvalidDataException">Thrown when the part stream contains an incorrect root element.</exception>
internal bool LoadFromPart(OpenXmlPart openXmlPart, Stream partStream, bool strictRelationshipFound)
{
if (partStream.Length < 4)
{
// The XmlReader.Read() method requires at least four bytes from the data stream in order to begin parsing.
return false;
}
var events = openXmlPart.Features.Get<IPartRootEventsFeature>();
events?.OnChange(EventType.Reloading, openXmlPart);
var context = RootElementContext;
// set MaxCharactersInDocument to limit the part size on loading DOM.
context.XmlReaderSettings.MaxCharactersInDocument = openXmlPart.MaxCharactersInPart;
context.XmlReaderSettings.DtdProcessing = DtdProcessing.Prohibit; // set to prohibit explicitly for security fix
using (var xmlReader = XmlConvertingReaderFactory.Create(partStream, Features.GetNamespaceResolver(), context.XmlReaderSettings, strictRelationshipFound))
{
context.MCSettings = openXmlPart.MCSettings;
xmlReader.Read();
if (xmlReader.NodeType == XmlNodeType.XmlDeclaration)
{
var standaloneAttribute = xmlReader.GetAttribute("standalone");
if (standaloneAttribute is not null)
{
_standaloneDeclaration = standaloneAttribute.Equals("yes", StringComparison.OrdinalIgnoreCase);
}
}
if (!xmlReader.EOF)
{
xmlReader.MoveToContent();
}
if (xmlReader.EOF
|| xmlReader.NodeType != XmlNodeType.Element
|| !xmlReader.IsStartElement())
{
// the stream does NOT contains any xml element.
return false;
}
var resolver = Features.GetNamespaceResolver();
var qname = new OpenXmlQualifiedName(xmlReader.NamespaceURI, xmlReader.LocalName);
if (!resolver.IsKnown(qname.Namespace) || !QName.Equals(qname))
{
var elementQName = new XmlQualifiedName(xmlReader.LocalName, xmlReader.NamespaceURI).ToString();
var msg = SR.Format(ExceptionMessages.Fmt_PartRootIsInvalid, elementQName, XmlQualifiedName.ToString());
throw new InvalidDataException(msg);
}
// remove all children and clear all attributes
OuterXml = string.Empty;
var mcContextPushed = PushMcContext(xmlReader);
Load(xmlReader, context.LoadMode);
if (mcContextPushed)
{
PopMcContext();
}
}
events?.OnChange(EventType.Reloaded, openXmlPart);
return true;
}
/// <summary>
/// Save the DOM into the OpenXML part.
/// </summary>
internal void SaveToPart(OpenXmlPart openXmlPart)
{
if (openXmlPart is null)
{
throw new ArgumentNullException(nameof(openXmlPart));
}
// If we're saving the existing root to the the part, we don't need to unload the root as they're already equal
using var partStream = openXmlPart.GetStream(FileMode.Create, unloadRootOnChange: !ReferenceEquals(this, openXmlPart.PartRootElement));
Save(partStream);
}
/// <summary>
/// Saves the DOM tree to the specified stream.
/// </summary>
/// <param name="stream">
/// The stream to which to save the XML.
/// </param>
public void Save(Stream stream)
{
var settings = new XmlWriterSettings
{
CloseOutput = true,
// We use UTF8 with no BOM as some viewers that consume documents cannot handle the BOM
Encoding = new UTF8Encoding(false),
};
var events = Features.Get<IPartRootEventsFeature>();
events?.OnChange(EventType.Saving, OpenXmlPart);
using (var xmlWriter = new XmlDOMTextWriter(stream, settings))
{
if (_standaloneDeclaration is not null)
{
xmlWriter.WriteStartDocument(_standaloneDeclaration.Value);
}
WriteTo(xmlWriter);
// Do not call WriteEndDocument if this root element is not parsed.
// In that case, the WriteTo() will just call WriteRaw() with the raw xml,
// so no WriteStartElement() needs to be called. Since the XmlWriter will
// still on document start state. Call WriteEndDocument() will cause exception.
if (XmlParsed)
{
xmlWriter.WriteEndDocument();
}
}
events?.OnChange(EventType.Saved, OpenXmlPart);
}
/// <summary>
/// Gets the part that is associated with the DOM tree.
/// It returns null when the DOM tree is not associated with a part.
/// </summary>
public OpenXmlPart? OpenXmlPart { get; internal set; }
/// <summary>
/// Saves the data in the DOM tree back to the part. This method can
/// be called multiple times and each time it is called, the stream
/// will be flushed.
/// </summary>
/// <remarks>
/// Call this method explicitly to save the changes in the DOM tree.
/// </remarks>
/// <exception cref="InvalidOperationException">Thrown when the tree is not associated with a part.</exception>
public void Save()
{
if (OpenXmlPart is null)
{
throw new InvalidOperationException(ExceptionMessages.CannotSaveDomTreeWithoutAssociatedPart);
}
SaveToPart(OpenXmlPart);
}
/// <summary>
/// Reloads the part content into an Open XML DOM tree. This method can
/// be called multiple times and each time it is called, the tree will
/// be reloaded and previous changes on the tree are abandoned.
/// </summary>
/// <exception cref="InvalidOperationException">Thrown when the tree is not associated with a part.</exception>
public void Reload()
{
if (OpenXmlPart is null)
{
throw new InvalidOperationException(ExceptionMessages.CannotReloadDomTreeWithoutAssociatedPart);
}
LoadFromPart(OpenXmlPart);
}
/// <summary>
/// Saves the current node to the specified XmlWriter.
/// </summary>
/// <param name="xmlWriter">
/// The XmlWriter to which to save the current node.
/// </param>
public override void WriteTo(XmlWriter xmlWriter)
{
if (xmlWriter is null)
{
throw new ArgumentNullException(nameof(xmlWriter));
}
if (XmlParsed)
{
// check the namespace mapping defined in this node first. because till now xmlWriter don't know the mapping defined in the current node.
var prefix = LookupNamespaceLocal(NamespaceUri);
// if not defined in the current node, try the xmlWriter
if (Parent is not null && prefix.IsNullOrEmpty())
{
prefix = xmlWriter.LookupPrefix(NamespaceUri);
}
// if xmlWriter didn't find it, it means the node is constructed by user and is not in the tree yet
// in this case, we use the predefined prefix
if (prefix.IsNullOrEmpty())
{
prefix = Features.GetNamespaceResolver().LookupPrefix(QName.Namespace.Uri);
}
xmlWriter.WriteStartElement(prefix, LocalName, NamespaceUri);
// fix bug #225919, write out all namespace into to root
WriteNamespaceAtributes(xmlWriter);
WriteAttributesTo(xmlWriter);
if (HasChildren || !string.IsNullOrEmpty(InnerText))
{
WriteContentTo(xmlWriter);
xmlWriter.WriteFullEndElement();
}
else
{
xmlWriter.WriteEndElement();
}
}
else
{
xmlWriter.WriteRaw(RawOuterXml);
}
}
private void WriteNamespaceAtributes(XmlWriter xmlWrite)
{
if (WriteAllNamespaceOnRoot)
{
var namespaces = new Dictionary<string, string>();
foreach (OpenXmlElement element in Descendants())
{
if (element.NamespaceDeclField is not null)
{
foreach (var item in element.NamespaceDeclField)
{
if (!namespaces.ContainsKey(item.Key))
{
namespaces.Add(item.Key, item.Value);
}
}
}
}
foreach (var namespacePair in namespaces)
{
if (!namespacePair.Key.IsNullOrEmpty())
{
if (NamespaceDeclField is not null &&
string.IsNullOrEmpty(LookupPrefixLocal(namespacePair.Value)) &&
string.IsNullOrEmpty(LookupNamespaceLocal(namespacePair.Key)))
{
xmlWrite.WriteAttributeString(OpenXmlElementContext.XmlnsPrefix, namespacePair.Key, OpenXmlElementContext.XmlnsUri, namespacePair.Value);
}
}
}
}
}
/// <summary>
/// Gets a value indicating whether the Save method will try write all namespace declaration on the root element.
/// </summary>
internal virtual bool WriteAllNamespaceOnRoot => true;
}
}