summaryrefslogtreecommitdiffstats
path: root/tools/Sandcastle/Source/BuildAssembler/BuildComponents/ResolveConceptualLinksComponent.cs
blob: 2531e2380c32103ab908b70ad787bcca44bb6911 (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
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
// 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.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Text.RegularExpressions;
using System.Web;


namespace Microsoft.Ddue.Tools {

	public class ResolveConceptualLinksComponent : BuildComponent {

        // Instantiation logic

		public ResolveConceptualLinksComponent (BuildAssembler assembler, XPathNavigator configuration) : base(assembler, configuration) {

            string showBrokenLinkTextValue = configuration.GetAttribute("showBrokenLinkText", String.Empty);
            if (!String.IsNullOrEmpty(showBrokenLinkTextValue)) showBrokenLinkText = Convert.ToBoolean(showBrokenLinkTextValue);

			XPathNodeIterator targetsNodes = configuration.Select("targets");
			foreach (XPathNavigator targetsNode in targetsNodes) {

                // the base directory containing target; required
                string baseValue = targetsNode.GetAttribute("base", String.Empty);
                if (String.IsNullOrEmpty(baseValue)) WriteMessage(MessageLevel.Error, "Every targets element must have a base attribute that specifies the path to a directory of target metadata files.");
                baseValue = Environment.ExpandEnvironmentVariables(baseValue);
                if (!Directory.Exists(baseValue)) WriteMessage(MessageLevel.Error, String.Format("The specified target metadata directory '{0}' does not exist.", baseValue));

                // an xpath expression to construct a file name
                // (not currently used; pattern is hard-coded to $target.cmp.xml
                string filesValue = targetsNode.GetAttribute("files", String.Empty);

                // an xpath expression to construct a url
                string urlValue = targetsNode.GetAttribute("url", String.Empty);
                XPathExpression urlExpression;
                if (String.IsNullOrEmpty(urlValue)) {
                    urlExpression = XPathExpression.Compile("concat(/metadata/topic/@id,'.htm')");
                } else {
                    urlExpression = CompileXPathExpression(urlValue);
                }
                
                // an xpath expression to construct link text
                string textValue = targetsNode.GetAttribute("text", String.Empty);
                XPathExpression textExpression;
                if (String.IsNullOrEmpty(textValue)) {
                    textExpression = XPathExpression.Compile("string(/metadata/topic/title)");
                } else {
                    textExpression = CompileXPathExpression(textValue);
                }

                // the type of link to create to targets found in the directory; required
                string typeValue = targetsNode.GetAttribute("type", String.Empty);
                if (String.IsNullOrEmpty(typeValue)) WriteMessage(MessageLevel.Error, "Every targets element must have a type attribute that specifies what kind of link to create to targets found in that directory.");
                
                // convert the link type to an enumeration member
                LinkType type = LinkType.None;
                try {
                    type = (LinkType) Enum.Parse(typeof(LinkType), typeValue, true);
                } catch (ArgumentException) {
                    WriteMessage(MessageLevel.Error, String.Format("'{0}' is not a valid link type.", typeValue));
                }

                // we have all the required information; create a TargetDirectory and add it to our collection
                TargetDirectory targetDirectory = new TargetDirectory(baseValue, urlExpression, textExpression, type);
                targetDirectories.Add(targetDirectory);

            }

            WriteMessage(MessageLevel.Info, String.Format("Collected {0} targets directories.", targetDirectories.Count));	

		}

        private XPathExpression CompileXPathExpression (string xpath) {
            XPathExpression expression = null;
            try {
                expression = XPathExpression.Compile(xpath);
            } catch (ArgumentException e) {
                WriteMessage(MessageLevel.Error, String.Format("'{0}' is not a valid XPath expression. The error message is: {1}", xpath, e.Message));
            } catch (XPathException e) {
                WriteMessage(MessageLevel.Error, String.Format("'{0}' is not a valid XPath expression. The error message is: {1}", xpath, e.Message));
            }
            return (expression);
        }

        // Conceptual link resolution logic

		public override void Apply (XmlDocument document, string key) {
			ResolveConceptualLinks(document, key);
		}

        private bool showBrokenLinkText = false;

        private string BrokenLinkDisplayText (string target, string text) {
            if (showBrokenLinkText) {
                return(String.Format("{0}", text));
            } else {
                return(String.Format("[{0}]", target));
            }
        }

        private TargetDirectoryCollection targetDirectories = new TargetDirectoryCollection();

        private static XPathExpression conceptualLinks = XPathExpression.Compile("//conceptualLink");

        private static Regex validGuid = new Regex(@"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$", RegexOptions.Compiled);

		private void ResolveConceptualLinks (XmlDocument document, string key) {

			// find links
			XPathNodeIterator linkIterator = document.CreateNavigator().Select(conceptualLinks);

			// copy them to an array, because enumerating through an XPathNodeIterator
			// fails when the nodes in it are altered
			XPathNavigator[] linkNodes = BuildComponentUtilities.ConvertNodeIteratorToArray(linkIterator);
			
			foreach (XPathNavigator linkNode in linkNodes) {

                ConceptualLinkInfo link = ConceptualLinkInfo.Create(linkNode);

                // determine url, text, and link type
                string url = null;
                string text = null;
                LinkType type = LinkType.None;
                bool isValidLink = validGuid.IsMatch(link.Target);
                if (isValidLink) {
                    // a valid link; try to fetch target info
                    TargetInfo target = GetTargetInfoFromCache(link.Target.ToLower());
                    if (target == null) {
                        // no target found; issue warning, set link style to none, and text to in-source fall-back
                        //type = LinkType.None;
                        type = LinkType.Index;
                        text = BrokenLinkDisplayText(link.Target, link.Text);
                        WriteMessage(MessageLevel.Warn, String.Format("Unknown conceptual link target '{0}'.", link.Target));
                    } else {
                        // found target; get url, text, and type from stored info
                        url = target.Url;
                        text = target.Text;
                        type = target.Type;
                    }
                } else {
                    // not a valid link; issue warning, set link style to none, and text to invalid target
                    //type = LinkType.None;
                    type = LinkType.Index;
                    text = BrokenLinkDisplayText(link.Target, link.Text);
                    WriteMessage(MessageLevel.Warn, String.Format("Invalid conceptual link target '{0}'.", link.Target));
                }

                // write opening link tag and target info
                XmlWriter writer = linkNode.InsertAfter();
                switch (type) {
                    case LinkType.None:
                        writer.WriteStartElement("span");
                        writer.WriteAttributeString("class", "nolink");
                        break;
                    case LinkType.Local:
                        writer.WriteStartElement("a");
                        writer.WriteAttributeString("href", url);
                        break;
                    case LinkType.Index:
                        writer.WriteStartElement("mshelp", "link", "http://msdn.microsoft.com/mshelp");
                        writer.WriteAttributeString("keywords", link.Target.ToLower());
                        writer.WriteAttributeString("tabindex", "0");
                        break;
                }

                // write the link text
                writer.WriteString(text);

                // write the closing link tag
                writer.WriteEndElement();
                writer.Close();

                // delete the original tag
                linkNode.DeleteSelf();
            }
		}




		// a simple caching system for target names

		private TargetInfo GetTargetInfoFromCache (string target) {

			TargetInfo info;
			if (!cache.TryGetValue(target, out info)) {
				info = targetDirectories.GetTargetInfo(target + ".cmp.xml");

				if (cache.Count >= cacheSize) cache.Clear();

				cache.Add(target, info);
			}

            return(info);

		}

		private static int cacheSize = 1000;

		private Dictionary<string,TargetInfo> cache = new Dictionary<string,TargetInfo>(cacheSize);

        //private CustomContext context = new CustomContext();

	}

	// different types of links

	internal enum LinkType {
		None,		// not active
		Local,		// a href
		Index  	// mshelp:link keyword
        //Regex       // regular expression with match/replace
	}

    // a representation of a targets directory, along with all the assoicated expressions used to
    // find target metadat files in it, and extract urls and link text from those files

    internal class TargetDirectory {

        private string directory;

        private XPathExpression fileExpression = XPathExpression.Compile("concat($target,'.cmp.htm')");

        private XPathExpression urlExpression = XPathExpression.Compile("concat(/metadata/topic/@id,'.htm')");

        private XPathExpression textExpression = XPathExpression.Compile("string(/metadata/topic/title)");

        private LinkType type;

        public string Directory {
            get {
                return (directory);
            }
        }

        public XPathExpression UrlExpression {
            get {
                return (urlExpression);
            }
        }

        public XPathExpression TextExpression {
            get {
                return (textExpression);
            }
        }


        public LinkType LinkType {
            get {
                return (type);
            }
        }

        public TargetDirectory (string directory, LinkType type) {
            if (directory == null) throw new ArgumentNullException("directory");
            this.directory = directory;
            this.type = type;
        }

        public TargetDirectory (string directory, XPathExpression urlExpression, XPathExpression textExpression, LinkType type) {
            if (directory == null) throw new ArgumentNullException("directory");
            if (urlExpression == null) throw new ArgumentNullException("urlExpression");
            if (textExpression == null) throw new ArgumentNullException("textExpression");
            this.directory = directory;
            this.urlExpression = urlExpression;
            this.textExpression = textExpression;
            this.type = type;
        }

        private XPathDocument GetDocument (string file) {
            string path = Path.Combine(directory, file);
            if (File.Exists(path)) {
                XPathDocument document = new XPathDocument(path);
                return (document);
            } else {
                return (null);
            }
        }

        public TargetInfo GetTargetInfo (string file) {
            XPathDocument document = GetDocument(file);
            if (document == null) {
                return(null);
            } else {
                XPathNavigator context = document.CreateNavigator();

                string url = context.Evaluate(urlExpression).ToString();
                string text = context.Evaluate(textExpression).ToString();
                TargetInfo info = new TargetInfo(url, text, type);
                return(info);
            }
        }

        public TargetInfo GetTargetInfo (XPathNavigator linkNode, CustomContext context) {

            // compute the metadata file name to open
            XPathExpression localFileExpression = fileExpression.Clone();
            localFileExpression.SetContext(context);
            string file = linkNode.Evaluate(localFileExpression).ToString();
            if (String.IsNullOrEmpty(file)) return (null);

            // load the metadata file
            XPathDocument metadataDocument = GetDocument(file);
            if (metadataDocument == null) return (null);

            // querry the metadata file for the target url and link text
            XPathNavigator metadataNode = metadataDocument.CreateNavigator();
            XPathExpression localUrlExpression = urlExpression.Clone();
            localUrlExpression.SetContext(context);
            string url = metadataNode.Evaluate(localUrlExpression).ToString();
            XPathExpression localTextExpression = textExpression.Clone();
            localTextExpression.SetContext(context);
            string text = metadataNode.Evaluate(localTextExpression).ToString();

            // return this information
            TargetInfo info = new TargetInfo(url, text, type);
            return (info);
        }



    }

    // our collection of targets directories

    internal class TargetDirectoryCollection {

        public TargetDirectoryCollection() {}

        private List<TargetDirectory> targetDirectories = new List<TargetDirectory>();

        public int Count {
            get {
                return(targetDirectories.Count);
            }
        }

        public void Add (TargetDirectory targetDirectory) {
            targetDirectories.Add(targetDirectory);
        }

        public TargetInfo GetTargetInfo (string file) {
            foreach (TargetDirectory targetDirectory in targetDirectories) {
                TargetInfo info = targetDirectory.GetTargetInfo(file);
                if (info != null) return (info);
            }
            return (null);
        }

        public TargetInfo GetTargetInfo (XPathNavigator linkNode, CustomContext context) {
            foreach (TargetDirectory targetDirectory in targetDirectories) {
                TargetInfo info = targetDirectory.GetTargetInfo(linkNode, context);
                if (info != null) return (info);
            }
            return (null);
        }

    }

    // a representation of a resolved target, containing all the information necessary to actually write out the link

    internal class TargetInfo {

        private string url;

        private string text;

        private LinkType type;

        public string Url {
            get {
                return(url);
            }
        }

        public string Text {
            get {
                return(text);
            }
        }

        public LinkType Type {
            get {
                return(type);
            }
        }

        internal TargetInfo (string url, string text, LinkType type) {
            if (url == null) throw new ArgumentNullException("url");
            if (text == null) throw new ArgumentNullException("url");
            this.url = url;
            this.text = text;
            this.type = type;
        }
    }

    // a representation of a conceptual link

    internal class ConceptualLinkInfo {

        private string target;

        private string text;

        private bool showText = false;

        public string Target {
            get {
                return (target);
            }
        }

        public string Text {
            get {
                return (text);
            }
        }

        public bool ShowText {
            get {
                return (showText);
            }
        }

        private ConceptualLinkInfo () { }

        public static ConceptualLinkInfo Create (XPathNavigator node) {
            if (node == null) throw new ArgumentNullException("node");
            
            ConceptualLinkInfo info = new ConceptualLinkInfo();
            
            info.target = node.GetAttribute("target", String.Empty);
            info.text = node.ToString();
            
            return(info);
        }

    }

}