Skip to content

Commit 0097991

Browse files
committed
Replace typed layer
1 parent 8017be9 commit 0097991

39 files changed

+365
-56
lines changed

gen/DocumentFormat.OpenXml.Generator.Models/Generators/Elements/DataModelWriterExtensions.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ namespace DocumentFormat.OpenXml.Generator.Generators.Elements;
1111

1212
public static class DataModelWriterExtensions
1313
{
14+
private static HashSet<string> _typedBasedClasses = new()
15+
{
16+
"OpenXmlElement",
17+
"OpenXmlCompositeElement",
18+
"OpenXmlLeafElement",
19+
"OpenXmlLeafTextElement",
20+
"OpenXmlPartRootElement",
21+
};
22+
1423
public static bool GetDataModelSyntax(this IndentedTextWriter writer, OpenXmlGeneratorServices services, SchemaNamespace model)
1524
{
1625
foreach (var ns in GetNamespaces(model, services).Distinct().OrderBy(n => n))
@@ -53,6 +62,21 @@ public static bool GetDataModelSyntax(this IndentedTextWriter writer, OpenXmlGen
5362
return delimiter.Count > 0;
5463
}
5564

65+
private static string GetBaseName(SchemaType type)
66+
{
67+
if (type.IsPart)
68+
{
69+
return "TypedOpenXmlPartRootElement";
70+
}
71+
72+
if (_typedBasedClasses.Contains(type.BaseClass))
73+
{
74+
return $"Typed{type.BaseClass}";
75+
}
76+
77+
return type.BaseClass;
78+
}
79+
5680
private static void WriteType(this IndentedTextWriter writer, OpenXmlGeneratorServices services, SchemaType element)
5781
{
5882
writer.WriteDocumentationComment(BuildTypeComments(services, element));
@@ -69,7 +93,7 @@ private static void WriteType(this IndentedTextWriter writer, OpenXmlGeneratorSe
6993
writer.Write("partial class ");
7094
writer.Write(className);
7195
writer.Write(" : ");
72-
writer.WriteLine(element.IsPart ? "OpenXmlPartRootElement" : element.BaseClass);
96+
writer.WriteLine(GetBaseName(element));
7397

7498
using (writer.AddBlock(new() { AddNewLineBeforeClosing = true, IncludeTrailingNewline = false }))
7599
{

gen/DocumentFormat.OpenXml.Generator.Models/Generators/Parts/PartWriter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public static void WritePart(this IndentedTextWriter writer, OpenXmlGeneratorSer
4949
writer.Write("public partial class ");
5050
writer.Write(type.Name);
5151
writer.Write(" : ");
52+
5253
writer.Write(type.Base);
5354

5455
if (type.HasFixedContent)

gen/DocumentFormat.OpenXml.Generator.Models/Models/Part.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@ namespace DocumentFormat.OpenXml.Generator.Models;
55

66
public class Part
77
{
8+
private string _base = null!;
9+
810
public string Name { get; set; } = null!;
911

10-
public string Base { get; set; } = null!;
12+
public string Base
13+
{
14+
get => _base;
15+
set => _base = value.Equals("OpenXmlPart") ? "TypedOpenXmlPart" : value;
16+
}
1117

1218
public bool HasFixedContent => ContentType is not null;
1319

src/DocumentFormat.OpenXml/AlternateContent.cs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,44 @@ public override OpenXmlElement CloneNode(bool deep)
117117
return CloneImp<AlternateContent>(deep);
118118
}
119119

120-
internal override void ConfigureMetadata(ElementMetadata.Builder builder)
120+
private protected override IFeatureCollection CreateFeatures() => new AlternateContentFeatures(this);
121+
122+
private class AlternateContentFeatures : ElementFeatureCollection, IElementMetadata
121123
{
122-
base.ConfigureMetadata(builder);
124+
public AlternateContentFeatures(OpenXmlElement owner)
125+
: base(owner)
126+
{
127+
}
128+
129+
public override TFeature Get<TFeature>()
130+
{
131+
if (typeof(TFeature) == typeof(IElementMetadata))
132+
{
133+
return (TFeature)(object)this;
134+
}
135+
136+
return base.Get<TFeature>()!;
137+
}
138+
139+
Type IElementMetadata.Type => typeof(AlternateContent);
140+
141+
ReadOnlyArray<AttributeMetadata> IElementMetadata.Attributes => default;
142+
143+
FileFormatVersions IElementMetadata.Availability => FileFormatVersions.Office2007;
144+
145+
ElementFactoryCollection IElementMetadata.Children { get; } = new(new[]
146+
{
147+
new ElementFactory(typeof(AlternateContentChoice), AlternateContentChoice.InternalQName, static () => new AlternateContentChoice()),
148+
new ElementFactory(typeof(AlternateContentFallback), AlternateContentFallback.InternalQName, static () => new AlternateContentFallback()),
149+
});
150+
151+
ReadOnlyArray<IValidator> IElementMetadata.Constraints => default;
152+
153+
CompiledParticle? IElementMetadata.Particle => new(null);
123154

124-
builder.SetSchema(InternalQName);
155+
OpenXmlQualifiedName IElementMetadata.QName => InternalQName;
125156

126-
builder.AddChild<AlternateContentChoice>();
127-
builder.AddChild<AlternateContentFallback>();
157+
ReadOnlyArray<IValidator> IElementMetadata.Validators => default;
128158
}
129159
}
130160
}

src/DocumentFormat.OpenXml/AlternateContentChoice.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ namespace DocumentFormat.OpenXml
1010
/// <summary>
1111
/// Defines an mc:Choice element in mc:AlternateContent.
1212
/// </summary>
13-
public class AlternateContentChoice : OpenXmlCompositeElement
13+
public class AlternateContentChoice : TypedOpenXmlCompositeElement
1414
{
15+
internal static OpenXmlQualifiedName InternalQName => new(AlternateContent.InternalQName.Namespace.Uri, Name);
16+
1517
private const string Name = "Choice";
1618

1719
/// <summary>

src/DocumentFormat.OpenXml/AlternateContentFallback.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ namespace DocumentFormat.OpenXml
1010
/// <summary>
1111
/// Defines a mc:Fallback element in mc:AlternateContent.
1212
/// </summary>
13-
public class AlternateContentFallback : OpenXmlCompositeElement
13+
public class AlternateContentFallback : TypedOpenXmlCompositeElement
1414
{
15+
internal static OpenXmlQualifiedName InternalQName => new(AlternateContent.InternalQName.Namespace.Uri, Name);
16+
1517
private const string Name = "Fallback";
1618

1719
/// <summary>

src/DocumentFormat.OpenXml/OpenXmlCompositeElement.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ namespace DocumentFormat.OpenXml
1919
/// </summary>
2020
public abstract class OpenXmlCompositeElement : OpenXmlElement
2121
{
22+
private protected const string UseGenericVersion = "Should use the generic version of this. This overload will be removed in a future version.";
23+
2224
private OpenXmlElement? _lastChild;
2325

2426
/// <summary>
@@ -42,7 +44,7 @@ protected OpenXmlCompositeElement(string outerXml)
4244
/// Initializes a new instance of the OpenXmlCompositeElement class using the supplied collection of elements.
4345
/// </summary>
4446
/// <param name="childrenElements">A collection of elements.</param>
45-
[Obsolete("Should use the generic version of this. This overload will be removed in a future version.")]
47+
[Obsolete(UseGenericVersion)]
4648
protected OpenXmlCompositeElement(IEnumerable childrenElements)
4749
: this()
4850
{

src/DocumentFormat.OpenXml/OpenXmlElement.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using DocumentFormat.OpenXml.Framework.Metadata;
77
using DocumentFormat.OpenXml.Packaging;
88
using System;
9+
using System.Collections;
910
using System.Collections.Generic;
1011
using System.Diagnostics;
1112
using System.Diagnostics.CodeAnalysis;
@@ -67,13 +68,15 @@ public IFeatureCollection Features
6768
{
6869
if (_features is null)
6970
{
70-
_features = new ElementFeatureCollection(this);
71+
_features = CreateFeatures();
7172
}
7273

7374
return _features;
7475
}
7576
}
7677

78+
private protected virtual IFeatureCollection CreateFeatures() => new ElementFeatureCollection(this);
79+
7780
private MarkupCompatibilityAttributes? McAttributesFiled
7881
{
7982
get
@@ -2606,7 +2609,7 @@ internal void RemoveAttributesBasedonMC()
26062609
return root as OpenXmlPartRootElement;
26072610
}
26082611

2609-
private sealed partial class ElementFeatureCollection : IFeatureCollection
2612+
private protected partial class ElementFeatureCollection : IFeatureCollection
26102613
{
26112614
private readonly OpenXmlElement _owner;
26122615

@@ -2617,15 +2620,27 @@ public ElementFeatureCollection(OpenXmlElement owner)
26172620

26182621
public bool IsReadOnly => true;
26192622

2620-
public int Revision => GetPartFeatures()?.Revision ?? 0;
2623+
public int Revision => GetParentFeatures()?.Revision ?? 0;
2624+
2625+
public virtual IFeatureCollection Default => FeatureCollection.Default;
26212626

26222627
[KnownFeature(typeof(AnnotationsFeature))]
26232628
[KnownFeature(typeof(IElementMetadata), Factory = nameof(CreateMetadata))]
2624-
[DelegatedFeature(nameof(GetPartFeatures))]
2625-
[DelegatedFeature(nameof(FeatureCollection.TypedOrDefault), typeof(FeatureCollection))]
2626-
public partial TFeature? Get<TFeature>();
2629+
[DelegatedFeature(nameof(GetParentFeatures))]
2630+
[DelegatedFeature(nameof(Default))]
2631+
private partial TFeature? GetBuiltIn<TFeature>();
2632+
2633+
public virtual TFeature? Get<TFeature>() => GetBuiltIn<TFeature>();
2634+
2635+
private IFeatureCollection? GetParentFeatures()
2636+
{
2637+
if (_owner is OpenXmlPartRootElement root)
2638+
{
2639+
return root.OpenXmlPart?.Features;
2640+
}
26272641

2628-
public IFeatureCollection? GetPartFeatures() => _owner.GetPart()?.Features;
2642+
return _owner.Parent?.Features;
2643+
}
26292644

26302645
private IElementMetadata CreateMetadata() => this.GetRequired<IElementMetadataFactoryFeature>().GetMetadata(_owner);
26312646

src/DocumentFormat.OpenXml/OpenXmlMiscNode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace DocumentFormat.OpenXml
1313
/// <summary>
1414
/// Represents an Open XML non element node (i.e. PT, Comments, Entity, Notation, XmlDeclaration).
1515
/// </summary>
16-
public class OpenXmlMiscNode : OpenXmlElement
16+
public class OpenXmlMiscNode : TypedOpenXmlElement
1717
{
1818
private const string StrCDataSectionName = "#cdata-section";
1919
private const string StrCommentName = "#comment";

src/DocumentFormat.OpenXml/OpenXmlUnknownElement.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace DocumentFormat.OpenXml
1414
/// Represents elements that are not defined in the Office Open XML ECMA standard.
1515
/// </summary>
1616
[OfficeAvailability(FileFormatVersions.None)]
17-
public class OpenXmlUnknownElement : OpenXmlCompositeElement
17+
public class OpenXmlUnknownElement : TypedOpenXmlCompositeElement
1818
{
1919
private string _namespaceUri;
2020
private string _tagName;

0 commit comments

Comments
 (0)