-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAppController.cs
599 lines (446 loc) · 21.3 KB
/
AppController.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
// Copyright (c) Microsoft Corporation. All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Globalization;
using System.Xml;
using System.IO;
using System.Web;
using System.Windows.Forms;
using System.Xml.Xsl;
using MSHelpCompiler;
using ContentServiceLibrary;
// Version variables in this code are collection + "." + version, but ContentItem requires a
// version and collection, eg. version="10", collection="MSDN"
namespace PackageThis
{
public class MtpsNode
{
public string navAssetId;
public string navLocale;
public string navVersion;
public string targetAssetId;
public string targetContentId;
public string targetLocale;
public string targetVersion;
public string title;
public bool external;
public MtpsNode(string navAssetId, string navLocale, string navVersion,
string targetContentId, string targetAssetId, string targetLocale,
string targetVersion, string title)
{
this.navAssetId = navAssetId;
this.navLocale = navLocale;
this.navVersion = navVersion;
this.targetContentId = targetContentId;
this.targetAssetId = targetAssetId.ToLower().StartsWith("assetid:") == true ?
targetAssetId.Remove(0,8) : targetAssetId;
this.targetLocale = targetLocale;
this.targetVersion = targetVersion;
this.title = title;
this.external = targetAssetId.ToLower().Contains("http:");
}
}
public class AppController
{
private string application = "PackageThisGui";
private string topNode;
private string locale;
private string version;
private TreeView tocControl;
private string workingDir;
private string withinHxsDir;
private string rawDir;
private string hxsDir;
private string hxsSubDir = "html";
public Dictionary<string, string> links = new Dictionary<string, string>();
static private Stream resourceStream = typeof(AppController).Assembly.GetManifestResourceStream(
"PackageThis.Extra.hxs.xslt");
static private XmlReader transformFile = XmlReader.Create(resourceStream);
static private XslCompiledTransform xform = null;
// static private StreamWriter sw;
public AppController(string topNode, string locale, string version, TreeView tocControl, string workingDir)
{
this.topNode = topNode;
this.locale = locale;
this.version = version;
this.tocControl = tocControl;
this.workingDir = workingDir;
this.rawDir = Path.Combine(workingDir, "raw");
this.hxsDir = Path.Combine(workingDir, "hxs");
this.withinHxsDir = Path.Combine(hxsDir, hxsSubDir);
if (xform == null)
{
xform = new XslCompiledTransform(true);
xform.Load(transformFile);
}
Directory.CreateDirectory(rawDir);
ContentItem contentItem = lookupTOCNode(topNode, locale, version);
processNodeList(contentItem, tocControl.Nodes);
}
ContentItem lookupTOCNode(string contentIdentifier, string locale, string version)
{
string[] splitVersion = version.Split(new char[] {'.'});
ContentItem contentItem = new ContentItem(contentIdentifier, locale, splitVersion[1],
splitVersion[0], application);
contentItem.Load(false, false);
return contentItem;
}
void processNodeList(ContentItem contentItem, TreeNodeCollection tnCollection)
{
XmlDocument xmlDocument = new XmlDocument();
XmlNamespaceManager nsm = new XmlNamespaceManager(xmlDocument.NameTable);
nsm.AddNamespace("toc", "urn:mtpg-com:mtps/2004/1/toc");
nsm.AddNamespace("mtps", "http://msdn2.microsoft.com/mtps");
nsm.AddNamespace("asp", "http://msdn2.microsoft.com/asp");
nsm.AddNamespace("mshelp", "http:/msdn.microsoft.com/mshelp");
if(string.IsNullOrEmpty(contentItem.toc) == true)
return;
xmlDocument.LoadXml(contentItem.toc);
XmlNodeList nodes = xmlDocument.SelectNodes("/toc:Node/toc:Node", nsm);
foreach(XmlNode node in nodes)
{
string title = GetAttribute(node.Attributes["toc:Title"]);
string target = HttpUtility.UrlDecode(GetAttribute(node.Attributes["toc:Target"]));
string targetLocale = GetAttribute(node.Attributes["toc:TargetLocale"]);
string targetVersion = GetAttribute(node.Attributes["toc:TargetVersion"]);
string subTree = HttpUtility.UrlDecode(GetAttribute(node.Attributes["toc:SubTree"]));
string subTreeVersion = GetAttribute(node.Attributes["toc:SubTreeVersion"]);
string subTreeLocale = GetAttribute(node.Attributes["toc:SubTreeLocale"]);
string isPhantom = GetAttribute(node.Attributes["toc:IsPhantom"]);
if (isPhantom != "true" &&
title != "@PhantomNode" &&
title != "@NoTitle" &&
string.IsNullOrEmpty(title) != true &&
string.IsNullOrEmpty(target) != true)
{
TreeNode treeNode = tnCollection.Add(title);
MtpsNode mtpsNode = new MtpsNode(subTree, subTreeLocale, subTreeVersion,
contentItem.contentId, target, targetLocale, targetVersion, title);
treeNode.Tag = mtpsNode;
// Mark nodes red that point outside this server
if (mtpsNode.external == true)
{
treeNode.ForeColor = System.Drawing.Color.Red;
//treeNode.NodeFont = new System.Drawing.Font(tocControl.Font, System.Drawing.FontStyle.Italic);
}
if (subTree != null)
{
// Add a + as the title so any node with subnodes is expandable.
// Only load the subnodes when user expands this node.
// We rely on Tag == null rather than Text == "+" in case
// there really is a node with a title of "+".
treeNode.Nodes.Add("+");
}
}
else
{
if (subTree != null)
{
// TODO: add a ContentItem constructor that takes a combined
// version string: MSDN.10, Office.12, etc.
string[] splitVersion = subTreeVersion.Split(new char[] {'.'});
ContentItem childContentItem = new ContentItem(subTree, subTreeLocale,
splitVersion[1], splitVersion[0], application);
childContentItem.Load(false);
processNodeList(childContentItem, tnCollection);
}
}
}
}
private string GetAttribute(XmlAttribute attribute)
{
return (attribute == null ? null : attribute.Value);
}
public void ExpandNode(TreeNode node)
{
if (node.Nodes[0].Tag == null)
{
ContentItem contentItem;
MtpsNode mtpsNode = node.Tag as MtpsNode;
try
{
contentItem = lookupTOCNode(mtpsNode.navAssetId, mtpsNode.navLocale,
mtpsNode.navVersion);
processNodeList(contentItem, node.Nodes);
}
catch
{
// mtpsNode.external = true;
// node.ForeColor = System.Drawing.Color.Red;
}
node.Nodes.Remove(node.Nodes[0]); // This removes the node labeled "+"
}
}
public void UncheckNodes(TreeNode node)
{
// Events are created even if the checked state doesn't change.
// That confuses the event handler because it assumes that the
// event is only fired on a state change.
if(node.Checked == true)
node.Checked = false;
foreach (TreeNode currentNode in node.Nodes)
{
if(currentNode.Checked == true)
currentNode.Checked = false;
if (currentNode.Nodes != null)
UncheckNodes(currentNode);
}
}
public bool WriteContent(TreeNode node, Content contentDataSet)
{
DataRow row;
MtpsNode mtpsNode = node.Tag as MtpsNode;
string[] splitVersion = mtpsNode.targetVersion.Split(new char[] {'.'});
ContentItem contentItem = new ContentItem("AssetId:" + mtpsNode.targetAssetId, mtpsNode.targetLocale,
splitVersion[1], splitVersion[0], application);
try
{
contentItem.Load(true);
}
catch
{
node.ForeColor = System.Drawing.Color.Red;
return false; // tell the event handler to reject the click.
}
if (contentDataSet.Tables["Item"].Rows.Find(mtpsNode.targetAssetId) == null)
{
row = contentDataSet.Tables["Item"].NewRow();
row["ContentId"] = contentItem.contentId;
row["Title"] = mtpsNode.title;
row["VersionId"] = mtpsNode.targetVersion;
row["AssetId"] = mtpsNode.targetAssetId;
row["Pictures"] = contentItem.numImages;
row["Size"] = contentItem.xml == null ? 0 : contentItem.xml.Length;
row["Metadata"] = contentItem.metadata;
row["Annotations"] = contentItem.annotations;
contentDataSet.Tables["Item"].Rows.Add(row);
}
if (contentDataSet.Tables["ItemInstance"].Rows.Find(node.FullPath) == null)
{
row = contentDataSet.Tables["ItemInstance"].NewRow();
row["ContentId"] = contentItem.contentId;
row["FullPath"] = node.FullPath;
contentDataSet.Tables["ItemInstance"].Rows.Add(row);
}
foreach (string imageFilename in contentItem.ImageFilenames)
{
row = contentDataSet.Tables["Picture"].NewRow();
row["ContentId"] = contentItem.contentId;
row["Filename"] = imageFilename;
}
if (string.IsNullOrEmpty(contentItem.links) == false)
{
XmlDocument linkDoc = new XmlDocument();
XmlNamespaceManager nsm = new XmlNamespaceManager(linkDoc.NameTable);
nsm.AddNamespace("k", "urn:mtpg-com:mtps/2004/1/key");
nsm.AddNamespace("mtps", "urn:msdn-com:public-content-syndication");
linkDoc.LoadXml(contentItem.links);
XmlNodeList nodes = linkDoc.SelectNodes("//mtps:link", nsm);
foreach (XmlNode xmlNode in nodes)
{
XmlNode assetIdNode = xmlNode.SelectSingleNode("mtps:assetId", nsm);
XmlNode contentIdNode = xmlNode.SelectSingleNode("k:contentId", nsm);
if (assetIdNode == null || contentIdNode == null)
continue;
string assetId = assetIdNode.InnerText;
string contentId = contentIdNode.InnerText;
if (string.IsNullOrEmpty(assetId) == false)
{
// Remove "assetId:" from front
assetId = HttpUtility.UrlDecode(assetIdNode.InnerText.Remove(0, "assetId:".Length));
if (links.ContainsKey(assetId) == false)
{
links.Add(assetId, contentId);
}
}
}
}
contentItem.Write(rawDir);
return true;
}
public void RemoveContent(TreeNode node, Content contentDataSet)
{
if (node.Tag != null)
{
MtpsNode mtpsNode = node.Tag as MtpsNode;
DataRow row = contentDataSet.Tables["ItemInstance"].Rows.Find(node.FullPath);
if (row != null)
{
DataRow parentRow = row.GetParentRow("FK_Item_ItemInstance");
contentDataSet.Tables["ItemInstance"].Rows.Remove(row);
int count = parentRow.GetChildRows("FK_Item_ItemInstance").Length;
if (count == 0) // If there are no refs to this item, delete it and its files
{
foreach (string file in Directory.GetFiles(rawDir,
parentRow["ContentId"].ToString() + "*"))
{
File.Delete(file);
}
contentDataSet.Tables["Item"].Rows.Remove(parentRow);
}
}
}
}
public void CreateChm(string chmFile, string title, string locale, Content contentDataSet)
{
Chm chm = new Chm(workingDir, title,
chmFile, locale, tocControl.Nodes, contentDataSet, links);
chm.Create();
ExportProgressForm progressForm = new ExportProgressForm(chm, chm.expectedLines);
progressForm.ShowDialog();
}
public void CreateHxs(string hxsFile, string title, string copyright, string locale,
Content contentDataSet)
{
if (Directory.Exists(hxsDir) == true)
{
Directory.Delete(hxsDir, true);
}
Directory.CreateDirectory(hxsDir);
Directory.CreateDirectory(withinHxsDir);
foreach (string file in Directory.GetFiles(rawDir))
{
File.Copy(file, Path.Combine(withinHxsDir, Path.GetFileName(file)), true);
}
// This will be used as a base name for forming all of the MSHelp files.
string baseFilename = Path.GetFileNameWithoutExtension(hxsFile);
foreach (DataRow row in contentDataSet.Tables["Item"].Rows)
{
if (Int32.Parse(row["Size"].ToString()) != 0)
{
Transform(row["ContentId"].ToString(),
row["Metadata"].ToString(),
row["Annotations"].ToString(),
row["VersionId"].ToString(),
contentDataSet);
}
}
// Create TOC
Hxt hxt = new Hxt(Path.Combine(hxsDir, baseFilename + ".hxt"), Encoding.UTF8);
CreateHxt(tocControl.Nodes, hxt, contentDataSet);
hxt.Close();
CreateHxks(baseFilename);
WriteExtraFiles();
Hxf hxf = new Hxf(Path.Combine(hxsDir, baseFilename + ".hxf"), Encoding.UTF8);
string[] files = Directory.GetFiles(hxsDir, "*", SearchOption.AllDirectories);
foreach(string file in files)
{
hxf.WriteLine(file.Replace(hxsDir, ""));
}
hxf.Close();
string lcid = new CultureInfo(locale).LCID.ToString();
Hxc hxc = new Hxc(baseFilename, title, lcid, "1.0", copyright, hxsDir, Encoding.UTF8);
int numHtmlFiles = Directory.GetFiles(hxsDir, "*.htm", SearchOption.AllDirectories).Length;
int numFiles = Directory.GetFiles(hxsDir, "*", SearchOption.AllDirectories).Length;
// This gives the number of information lines output by the compiler. It
// was determined experimentally, and should give some means of making an
// accurate progress bar during a compile.
// Actual equation is numInfoLines = 2*numHtmlFiles + (numFiles - numHtmlFiles) + 6
// After factoring, we get this equation
int expectedLines = numHtmlFiles + numFiles + 6;
Hxs hxs = new Hxs(Path.Combine(Path.GetFullPath(hxsDir), baseFilename + ".hxc"),
Path.GetFullPath(hxsDir),
Path.GetFullPath(hxsFile));
ExportProgressForm hxsProgressForm = new ExportProgressForm(hxs, expectedLines);
hxsProgressForm.ShowDialog();
}
public void CreateHxt(TreeNodeCollection nodeCollection, Hxt hxt, Content contentDataSet)
{
bool opened = false; // Keep track of opening or closing of TOC entries in the .hxt
foreach (TreeNode node in nodeCollection)
{
if (node.Checked == true)
{
MtpsNode mtpsNode = node.Tag as MtpsNode;
DataRow row = contentDataSet.Tables["Item"].Rows.Find(mtpsNode.targetAssetId);
string Url;
if (Int32.Parse(row["Size"].ToString()) == 0)
Url = null;
else
Url = Path.Combine(hxsSubDir,
row["ContentId"].ToString() + ".htm");
hxt.WriteStartNode(mtpsNode.title, Url);
opened = true;
}
if (node.Nodes.Count != 0 || node.Tag != null)
{
CreateHxt(node.Nodes, hxt, contentDataSet);
}
if (opened)
{
opened = false;
hxt.WriteEndNode();
}
}
}
void CreateHxks(string baseFileName)
{
Hxk hxk = new Hxk(baseFileName, "A", hxsDir);
hxk = new Hxk(baseFileName, "B", hxsDir);
hxk = new Hxk(baseFileName, "F", hxsDir);
hxk = new Hxk(baseFileName, "K", hxsDir);
hxk = new Hxk(baseFileName, "N", hxsDir);
hxk = new Hxk(baseFileName, "S", hxsDir);
}
// Includes stoplist and stylesheet
void WriteExtraFiles()
{
WriteExtraFile("Classic.css");
// TODO: Locate stop lists for other locales and add them to the project.
WriteExtraFile("msdnFTSstop_Unicode.stp");
WriteExtraFile("green-left.jpg");
WriteExtraFile("green-middle.jpg");
WriteExtraFile("green-right.jpg");
}
void WriteExtraFile(string filename)
{
Stream resourceStream;
resourceStream = typeof(Program).Assembly.GetManifestResourceStream(
"PackageThis.Extra." + filename);
FileStream fs = new FileStream(Path.Combine(hxsDir, filename),
FileMode.Create, FileAccess.Write);
int b;
while ((b = resourceStream.ReadByte()) != -1)
{
fs.WriteByte((byte)b);
}
resourceStream.Close();
fs.Close();
}
public void Transform(string contentId, string metadataXml, string annotationsXml,
string versionId, Content contentDataSet)
{
XsltArgumentList arguments = new XsltArgumentList();
Link link = new Link(contentDataSet, links);
XmlDocument metadata = new XmlDocument();
XmlDocument annotations = new XmlDocument();
string filename = Path.Combine(withinHxsDir, contentId + ".htm");
StreamReader sr = new StreamReader(filename);
string xml = sr.ReadToEnd();
sr.Close();
metadata.LoadXml(metadataXml);
annotations.LoadXml(annotationsXml);
arguments.AddParam("metadata", "", metadata.CreateNavigator());
arguments.AddParam("annotations", "", annotations.CreateNavigator());
arguments.AddParam("version", "", versionId);
arguments.AddParam("locale", "", locale);
arguments.AddExtensionObject("urn:Link", link);
TextReader tr = new StringReader(xml);
XmlReader xr = XmlReader.Create(tr);
using (StreamWriter sw = new StreamWriter(filename, false, Encoding.UTF8))
{
try
{
xform.Transform(xr, arguments, sw);
}
catch (Exception ex)
{
return;
}
}
}
}
}