Skip to content

Commit d6e48cc

Browse files
committed
remove all generic addchild
1 parent a481da2 commit d6e48cc

File tree

5 files changed

+21
-40
lines changed

5 files changed

+21
-40
lines changed

src/DocumentFormat.OpenXml.Framework/AlternateContent.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ namespace DocumentFormat.OpenXml
1616
/// </summary>
1717
public class AlternateContent : OpenXmlCompositeElement
1818
{
19-
internal static readonly OpenXmlQualifiedName InternalQName = new(@"http://schemas.openxmlformats.org/markup-compatibility/2006", "AlternateContent");
19+
internal static readonly OpenXmlQualifiedName ElementQName = new(@"http://schemas.openxmlformats.org/markup-compatibility/2006", "AlternateContent");
20+
internal static readonly OpenXmlSchemaType ElementType = new(ElementQName, default);
2021

2122
/// <summary>
2223
/// Initializes a new instance of the AlternateContent
@@ -66,16 +67,16 @@ public AlternateContent(string outerXml)
6667
/// Gets a value that represents the markup compatibility
6768
/// namespace.
6869
/// </summary>
69-
public static string MarkupCompatibilityNamespace => InternalQName.Namespace.Uri;
70+
public static string MarkupCompatibilityNamespace => ElementQName.Namespace.Uri;
7071

7172
/// <summary>
7273
/// Gets a value that represents the tag name of the
7374
/// AlternateContent element.
7475
/// </summary>
75-
public static string TagName => InternalQName.Name;
76+
public static string TagName => ElementQName.Name;
7677

7778
/// <inheritdoc/>
78-
public override string LocalName => InternalQName.Name;
79+
public override string LocalName => ElementQName.Name;
7980

8081
/// <summary>
8182
/// Appends a new child of type T:DocumentFormat.OpenXml.AlternateContentChoice
@@ -113,10 +114,10 @@ internal override void ConfigureMetadata(ElementMetadata.Builder builder)
113114
{
114115
base.ConfigureMetadata(builder);
115116

116-
builder.SetSchema(new(InternalQName, default));
117+
builder.SetSchema(ElementType);
117118

118-
builder.AddChild<AlternateContentChoice>();
119-
builder.AddChild<AlternateContentFallback>();
119+
builder.AddChild(AlternateContentChoice.ElementType, () => new AlternateContentChoice());
120+
builder.AddChild(AlternateContentFallback.ElementType, () => new AlternateContentFallback());
120121
}
121122
}
122123
}

src/DocumentFormat.OpenXml.Framework/AlternateContentChoice.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ namespace DocumentFormat.OpenXml
1212
/// </summary>
1313
public class AlternateContentChoice : OpenXmlCompositeElement
1414
{
15-
internal static OpenXmlQualifiedName InternalQName => new(AlternateContent.InternalQName.Namespace.Uri, Name);
16-
1715
private const string Name = "Choice";
1816

17+
internal static readonly OpenXmlQualifiedName ElementQName = new(AlternateContent.ElementQName.Namespace.Uri, Name);
18+
internal static readonly OpenXmlSchemaType ElementType = new(ElementQName, default);
19+
1920
/// <summary>
2021
/// Initializes a new instance of the
2122
/// AlternateContentChoice class.
@@ -116,7 +117,7 @@ internal override void ConfigureMetadata(ElementMetadata.Builder builder)
116117
{
117118
base.ConfigureMetadata(builder);
118119

119-
builder.SetSchema(new(new(AlternateContent.InternalQName.Namespace.Uri, Name), default));
120+
builder.SetSchema(ElementType);
120121

121122
builder.AddElement<AlternateContentChoice>()
122123
.AddAttribute("Requires", a => a.Requires);

src/DocumentFormat.OpenXml.Framework/AlternateContentFallback.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ namespace DocumentFormat.OpenXml
1212
/// </summary>
1313
public class AlternateContentFallback : OpenXmlCompositeElement
1414
{
15-
internal static OpenXmlQualifiedName InternalQName => new(AlternateContent.InternalQName.Namespace.Uri, Name);
16-
1715
private const string Name = "Fallback";
1816

17+
internal static readonly OpenXmlQualifiedName ElementQName = new(AlternateContent.ElementQName.Namespace.Uri, Name);
18+
internal static readonly OpenXmlSchemaType ElementType = new(ElementQName, default);
19+
1920
/// <summary>
2021
/// Initializes a new instance of the AlternateContentFallback class.
2122
/// </summary>
@@ -98,7 +99,7 @@ internal override void ConfigureMetadata(ElementMetadata.Builder builder)
9899
{
99100
base.ConfigureMetadata(builder);
100101

101-
builder.SetSchema(new(new(AlternateContent.InternalQName.Namespace.Uri, Name), default));
102+
builder.SetSchema(ElementType);
102103
}
103104
}
104105
}

src/DocumentFormat.OpenXml.Framework/Framework/Metadata/ElementMetadata.cs

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class Builder : ValidatorBuilder
6060
private readonly IOpenXmlNamespaceResolver _resolver;
6161

6262
private List<IMetadataBuilder<AttributeMetadata>>? _attributes;
63-
private HashSet<IMetadataBuilder<ElementFactory>>? _children;
63+
private List<ElementFactory>? _children;
6464
private List<IValidator>? _constraints;
6565
private OpenXmlSchemaType _type;
6666

@@ -97,21 +97,10 @@ public void AddChild(in OpenXmlSchemaType type, Func<OpenXmlElement> activator)
9797
{
9898
if (_children is null)
9999
{
100-
_children = new HashSet<IMetadataBuilder<ElementFactory>>();
100+
_children = [];
101101
}
102102

103-
_children.Add(new KnownChild(type, activator));
104-
}
105-
106-
public void AddChild<T>()
107-
where T : OpenXmlElement, new()
108-
{
109-
if (_children is null)
110-
{
111-
_children = new HashSet<IMetadataBuilder<ElementFactory>>();
112-
}
113-
114-
_children.Add(new KnownChild<T>());
103+
_children.Add(new ElementFactory(type, activator));
115104
}
116105

117106
public FileFormatVersions Availability { get; set; } = FileFormatVersions.Office2007;
@@ -128,7 +117,7 @@ public void Add(IMetadataBuilder<AttributeMetadata> builder)
128117

129118
public ElementMetadata Build()
130119
{
131-
var lookup = _children is null ? _lazy : new Lazy<ElementFactoryCollection>(() => new ElementFactoryCollection(_children.Select(c => c.Build())), true);
120+
var lookup = _children is null ? _lazy : new Lazy<ElementFactoryCollection>(() => new ElementFactoryCollection(_children), true);
132121

133122
return new ElementMetadata(_type, BuildAttributes(), GetValidators(), _constraints?.ToArray(), Availability, Particle.Compile(), lookup);
134123
}
@@ -149,17 +138,6 @@ public ElementMetadata Build()
149138

150139
return attributes;
151140
}
152-
153-
private class KnownChild<T> : IMetadataBuilder<ElementFactory>
154-
where T : OpenXmlElement, new()
155-
{
156-
public ElementFactory Build() => ElementFactory.Create<T>();
157-
}
158-
159-
private class KnownChild(OpenXmlSchemaType type, Func<OpenXmlElement> factory) : IMetadataBuilder<ElementFactory>
160-
{
161-
public ElementFactory Build() => new ElementFactory(type, factory);
162-
}
163141
}
164142

165143
public class Builder<TElement> : ValidatorBuilder

src/DocumentFormat.OpenXml.Framework/OpenXmlElement.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1814,7 +1814,7 @@ internal OpenXmlElement CreateElement(in OpenXmlQualifiedName qname, string pref
18141814
newElement = ElementFactory(qname);
18151815

18161816
// try AlternateContent
1817-
if (newElement is null && AlternateContent.InternalQName.Equals(qname))
1817+
if (newElement is null && AlternateContent.ElementQName.Equals(qname))
18181818
{
18191819
newElement = new AlternateContent();
18201820
}

0 commit comments

Comments
 (0)