-
Notifications
You must be signed in to change notification settings - Fork 365
/
CloneComponent.cs
134 lines (110 loc) · 4.67 KB
/
CloneComponent.cs
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
// Copyright © Microsoft Corporation.
// This source file is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.
// Change History
// 12/23/2012 - EFW - Added a dispose method to properly dispose of all components in each branch
// 12/23/2013 - EFW - Updated the build component to be discoverable via MEF
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Xml;
using System.Xml.XPath;
using Sandcastle.Core.BuildAssembler;
using Sandcastle.Core.BuildAssembler.BuildComponent;
namespace Sandcastle.Tools.BuildComponents
{
/// <summary>
/// This build component is used to clone the topic for each set of build components and execute them
/// on the cloned topic.
/// </summary>
public class CloneComponent : BuildComponentCore
{
#region Build component factory for MEF
//=====================================================================
/// <summary>
/// This is used to create a new instance of the build component
/// </summary>
[BuildComponentExport("Clone Component")]
public sealed class Factory : BuildComponentFactory
{
/// <inheritdoc />
public override BuildComponentCore Create()
{
return new CloneComponent(this.BuildAssembler);
}
}
#endregion
#region Private data members
//=====================================================================
private readonly List<IEnumerable<BuildComponentCore>> branches = new List<IEnumerable<BuildComponentCore>>();
#endregion
#region Constructor
//=====================================================================
/// <summary>
/// Constructor
/// </summary>
/// <param name="buildAssembler">A reference to the build assembler</param>
protected CloneComponent(IBuildAssembler buildAssembler) : base(buildAssembler)
{
}
#endregion
#region Method overrides
//=====================================================================
/// <inheritdoc />
/// <remarks>This sets a unique group ID for each branch</remarks>
public override string GroupId
{
get => base.GroupId;
set
{
base.GroupId = value;
int branchId = 1;
foreach(var branch in branches)
{
string branchGroupId = value + "/" + branchId.ToString(CultureInfo.InvariantCulture);
branchId++;
foreach(var component in branch)
component.GroupId = branchGroupId;
}
}
}
/// <inheritdoc />
/// <remarks>Multiple <c>branch</c> elements are specified as the configuration. Each <c>branch</c>
/// element can contain one or more <c>component</c> definitions that will be created and executed when
/// this component is applied. Each branch receives a clone of the document. This may be useful for
/// generating multiple help output formats in one build configuration.</remarks>
public override void Initialize(XPathNavigator configuration)
{
if(configuration == null)
throw new ArgumentNullException(nameof(configuration));
XPathNodeIterator branchNodes = configuration.Select("branch");
foreach(XPathNavigator branchNode in branchNodes)
branches.Add(this.BuildAssembler.LoadComponents(branchNode));
// Set a default group ID
this.GroupId = null;
}
/// <inheritdoc />
public override void Apply(XmlDocument document, string key)
{
if(document == null)
throw new ArgumentNullException(nameof(document));
foreach(var branch in branches)
{
XmlDocument subdocument = (XmlDocument)document.Clone();
foreach(var component in branch)
component.Apply(subdocument, key);
}
}
/// <inheritdoc />
protected override void Dispose(bool disposing)
{
if(disposing)
foreach(var branch in branches)
foreach(var component in branch)
component.Dispose();
base.Dispose(disposing);
}
#endregion
}
}