-
Notifications
You must be signed in to change notification settings - Fork 0
/
InvokeXdtTransform.cs
151 lines (117 loc) · 5.1 KB
/
InvokeXdtTransform.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Diagnostics;
using System.IO;
using System.Management.Automation;
using System.Management.Automation.Tracing;
using System.Xml;
using System.Xml.Schema;
using Microsoft.Web.XmlTransform;
using Xdt.Powershell.Properties;
namespace Xdt.Powershell
{
/// <inheritdoc />
/// <summary>
/// A simple Cmdlet that outputs a greeting to the pipeline.
/// </summary>
[Cmdlet(VerbsLifecycle.Invoke, "XdtTransform")]
[CmdletBinding]
public class InvokeXdtTransform : Cmdlet
{
private IXmlTransformationLogger _logger;
public InvokeXdtTransform()
{
Logger = new PowershellTransformationLogger(this);
}
/// <summary>
/// XDT Transform Xml
/// </summary>
[Parameter(Position = 0, HelpMessage = "XDT Transform XML Text")]
public XmlDocument TransformXML { get; set; }
/// <summary>
/// Target XML To Apply XDT Transform
/// </summary>
[Parameter(Position = 0, HelpMessage = "The name of the person to greet", ValueFromPipeline = true)]
public XmlDocument TargetXML { get; set; }
/// <summary>
/// Provide Custom Logging Implementation for XML Transforms, Defaults to Powerhell Console Logging
/// </summary>
[Parameter(Position = 0, HelpMessage = "The name of the person to greet", ValueFromPipeline = true)]
public IXmlTransformationLogger Logger { get; set; }
/// <summary>
/// Path to XDT Transform file
/// </summary>
[ValidatePathExists]
[Parameter(Position = 0, HelpMessage = "Path to XDT Transform file", ValueFromPipeline = true)]
public FileInfo TransformPath { get; set; }
/// <summary>
/// Target XML To Apply XDT Transform
/// </summary>
[Parameter(Position = 0, HelpMessage = "Path to Output result file. This will leave the original target xml file intact and output the result to a separate fie.", ValueFromPipelineByPropertyName = true)]
public FileInfo OutputPath { get; set; }
/// <inheritdoc />
/// <summary>
/// Perform Cmdlet processing.
/// </summary>
protected override void ProcessRecord()
{
if (_logger != null)
{
var exception = new InvalidOperationException("Logger configuration provided is null or invalid.");
var error = new ErrorRecord(exception, "InvalidOperationException", ErrorCategory.InvalidOperation, _logger);
WriteError(error);
}
if (TransformPath != null)
{
WriteVerbose($"Loading XDT Transform from {TransformPath}");
TransformXML = new XmlDocument();
TransformXML.Load(TransformPath.FullName);
WriteVerbose("done.");
WriteVerbose($"{TransformXML.OuterXml}");
}
WriteVerbose("Validating Transform is valid XML Document Transform");
var xdtSchema = XmlSchema.Read(new StringReader(Resources.XMLDocumentTransform), ValidationEventHandler);
TransformXML.Schemas.Add(xdtSchema);
TransformXML.Validate(ValidationEventHandler);
WriteVerbose("done.");
WriteVerbose("Convert Transform XML Text to XMLTransformation");
var xdtTransform = new XmlTransformation( TransformXML.OuterXml , false, _logger);
WriteVerbose("done.");
WriteVerbose("Convert XML Document to convertable format.");
var targetXml = new XmlTransformableDocument();
var targetXmlReader = XmlReader.Create(new StringReader(TargetXML.OuterXml));
targetXml.Load(targetXmlReader);
WriteVerbose("done.");
WriteVerbose("Apply XDT Transform to Target XML");
WriteVerbose(TransformXML.ToString());
WriteVerbose(TransformXML.OuterXml);
xdtTransform.Apply(targetXml);
WriteVerbose("done.");
TargetXML = targetXml;
}
private void ValidationEventHandler(object sender, ValidationEventArgs args)
{
var exception = new ParseException($"XML Validation Failed. - { args.Message }", args.Exception);
switch (args.Severity)
{
case XmlSeverityType.Error:
WriteError(new ErrorRecord(exception, "ParserValidationError", ErrorCategory.ParserError, args.Message));
break;
case XmlSeverityType.Warning:
WriteWarning(args.Message);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
protected override void EndProcessing()
{
WriteObject(TargetXML);
if (OutputPath != null)
{
WriteVerbose($"OutputPath {OutputPath} specified. Saving Transformed XML to this location.");
TargetXML.Save(OutputPath.FullName);
}
base.EndProcessing();
}
}
}