blob: 37755abdd9a6c42b0a29996e08b089ded0338759 (
plain)
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
|
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Xml;
using System.Xml.XPath;
namespace Microsoft.Ddue.Tools {
internal class HashComputation {
public HashComputation (string input, string output) {
Input = XPathExpression.Compile(input);
Output = XPathExpression.Compile(output);
}
public XPathExpression Input;
public XPathExpression Output;
}
public class ComputeHashComponent : BuildComponent {
public ComputeHashComponent (XPathNavigator configuration) : base(configuration) {
if (configuration == null) throw new ArgumentNullException("configuraton");
XPathNodeIterator hash_nodes = configuration.Select("hash");
foreach (XPathNavigator hash_node in hash_nodes) {
string input_xpath = hash_node.GetAttribute("input", String.Empty);
string output_xpath = hash_node.GetAttribute("output", String.Empty);
computations.Add( new HashComputation(input_xpath, output_xpath) );
}
}
// A list of the hash computations to do
private List<HashComputation> computations = new List<HashComputation>();
// Logic to compute a unique hash of a comment id string
private static Guid ComputeHash (string key) {
byte[] input = Encoding.UTF8.GetBytes(key);
byte[] output = md5.ComputeHash(input);
return( new Guid(output) );
}
private static HashAlgorithm md5 = new MD5CryptoServiceProvider();
// The actual action of the component
public override void Apply (XmlDocument document, string key) {
Guid id = ComputeHash(key);
foreach (HashComputation computation in computations) {
XPathNavigator output = document.CreateNavigator().SelectSingleNode(computation.Output);
if (output == null) continue;
output.SetValue(id.ToString());
}
}
}
}
|