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
|
// 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.
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Collections.Generic;
namespace Microsoft.Ddue.Tools {
public class FileCreatedEventArgs : EventArgs {
private string filePath;
private string hxfPath;
public FileCreatedEventArgs(string filePath, string hxfPath) {
this.filePath = filePath;
this.hxfPath = hxfPath;
}
public string FilePath {
get {
return(filePath);
}
}
public string HxfPath {
get {
return (hxfPath);
}
}
}
public class HxfGeneratorComponent : BuildComponent {
public HxfGeneratorComponent (BuildAssembler assembler, XPathNavigator configuration) : base(assembler, configuration) {
// get configuration data
inputValue = configuration.GetAttribute("input", String.Empty);
if (!String.IsNullOrEmpty(inputValue)) inputValue = Environment.ExpandEnvironmentVariables(inputValue);
outputValue = configuration.GetAttribute("output", String.Empty);
if (!String.IsNullOrEmpty(outputValue)) outputValue = Environment.ExpandEnvironmentVariables(outputValue);
// subscribe to component events
assembler.ComponentEvent += new EventHandler(FileCreatedHandler);
}
private string inputValue;
private string outputValue;
private XmlWriter writer;
private Dictionary<string, XmlWriter> writers = new Dictionary<string, XmlWriter>();
private void FileCreatedHandler (Object o, EventArgs e) {
FileCreatedEventArgs fe = e as FileCreatedEventArgs;
if (fe == null) return;
string path = Path.Combine(fe.HxfPath, outputValue).ToLower();
XmlWriter tempWriter;
if (!writers.TryGetValue(path, out tempWriter)) {
if (writer != null) {
writer.WriteEndDocument();
writer.Close();
}
WriteFile(path);
}
WriteFileElement(fe.FilePath);
}
private void WriteFileElement (string url) {
writer.WriteStartElement("File");
writer.WriteAttributeString("Url", url);
writer.WriteEndElement();
}
protected override void Dispose(bool disposing) {
if (disposing) {
writer.WriteEndDocument();
writer.Close();
}
base.Dispose(disposing);
}
public void WriteFile(string path) {
XmlWriterSettings writerSettings = new XmlWriterSettings();
writerSettings.Indent = true;
writer = XmlWriter.Create(path);
writer.WriteStartDocument();
writer.WriteStartElement("HelpFileList");
writer.WriteAttributeString("DTDVersion", "1.0");
// use the input to seed the output
if (!String.IsNullOrEmpty(inputValue)) {
try {
TextReader reader = File.OpenText(inputValue);
try {
while (true) {
string line = reader.ReadLine();
if (line == null) break;
WriteFileElement(line);
}
}
finally {
reader.Close();
}
}
catch (IOException ex) {
WriteMessage(MessageLevel.Error, String.Format("An access error occured while attempting to copy the input HxF data. The error message is:", ex.Message));
}
}
writers.Add(path, writer);
}
// don't do anything for individual files
public override void Apply (XmlDocument document, string key) {}
}
}
|