5 using System.Diagnostics;
6 using System.Collections.Generic;
9 using Lucene.Net.Index;
13 public enum SortType {
19 // The HelpSource class keeps track of the archived data, and its
31 // The unique ID for this HelpSource.
35 // The name of the HelpSource, used by all the file (.tree, .zip, ...) used by it
37 // The full directory path where the HelpSource files are located
40 // The tree of this help source
48 public HelpSource (string base_filename, bool create)
50 this.name = Path.GetFileName (base_filename);
51 this.basePath = Path.GetDirectoryName (base_filename);
52 this.treeFilePath = base_filename + ".tree";
53 this.storage = new Monodoc.Storage.ZipStorage (base_filename + ".zip");
54 this.cache = DocCacheHelper.GetDefaultCache (Name);
56 tree = create ? new Tree (this, string.Empty, string.Empty) : new Tree (this, treeFilePath);
63 tree = new Tree (this, "Blah", "Blah");
65 this.cache = new Caches.NullCache ();
80 /* This gives the full path of the source/ directory */
81 public string BaseFilePath {
87 public TraceLevel TraceLevel {
92 public string BaseDir {
104 public RootTree RootTree {
113 public IDocCache Cache {
119 public IDocStorage Storage {
128 // A HelpSource may have a common prefix to its URL, give it here
129 protected virtual string UriPrefix {
135 public virtual SortType SortType {
137 return SortType.Caption;
142 /// Returns a stream from the packaged help source archive
144 public virtual Stream GetHelpStream (string id)
146 return storage.Retrieve (id);
149 public virtual Stream GetCachedHelpStream (string id)
151 if (string.IsNullOrEmpty (id))
152 throw new ArgumentNullException ("id");
153 if (!cache.CanCache (DocEntity.Text))
154 return GetHelpStream (id);
155 if (!cache.IsCached (id))
156 cache.CacheText (id, GetHelpStream (id));
157 return cache.GetCachedStream (id);
160 public XmlReader GetHelpXml (string id)
162 var url = "monodoc:///" + SourceID + "@" + Uri.EscapeDataString (id) + "@";
163 var stream = cache.IsCached (id) ? cache.GetCachedStream (id) : storage.Retrieve (id);
165 return stream == null ? null : new XmlTextReader (url, stream);
168 public virtual XmlDocument GetHelpXmlWithChanges (string id)
170 XmlDocument doc = new XmlDocument ();
171 if (!storage.SupportRevision) {
172 doc.Load (GetHelpXml (id));
174 var revManager = storage.RevisionManager;
175 doc.Load (revManager.RetrieveLatestRevision (id));
180 public virtual string GetCachedText (string id)
182 if (!cache.CanCache (DocEntity.Text))
184 if (!cache.IsCached (id))
185 cache.CacheText (id, GetText (id));
186 return cache.GetCachedString (id);
189 public virtual string GetText (string id)
191 return new StreamReader (GetHelpStream (id)).ReadToEnd ();
194 // Tells if the result for the provided id is generated dynamically
195 // by the help source
196 public virtual bool IsGeneratedContent (string id)
201 // Tells if the content of the provided id is meant to be returned raw
202 public virtual bool IsRawContent (string id)
207 // Tells if provided id refers to a multi-content-type document if it's case
208 // tells the ids it's formed of
209 public virtual bool IsMultiPart (string id, out IEnumerable<string> parts)
216 /// Saves the tree and the archive
220 tree.Save (treeFilePath);
224 public virtual void RenderPreviewDocs (XmlNode newNode, XmlWriter writer)
226 throw new NotImplementedException ();
229 public virtual string GetPublicUrl (Node node)
231 return node.GetInternalUrl ();
234 public virtual bool CanHandleUrl (string url)
236 return url.StartsWith (UriPrefix, StringComparison.OrdinalIgnoreCase);
239 public virtual string GetInternalIdForUrl (string url, out Node node, out Dictionary<string, string> context)
242 node = MatchNode (url);
243 return node == null ? null : url.Substring (UriPrefix.Length);
246 public virtual Node MatchNode (string url)
250 var matchCache = LRUCache<string, Node>.Default;
251 if ((current = matchCache.Get (url)) != null)
254 current = Tree.RootNode;
255 var strippedUrl = url.StartsWith (UriPrefix, StringComparison.OrdinalIgnoreCase) ? url.Substring (UriPrefix.Length) : url;
256 var searchNode = new Node () { Element = strippedUrl };
259 int index = current.ChildNodes.BinarySearch (searchNode, NodeElementComparer.Instance);
261 Node n = current.ChildNodes[index];
262 matchCache.Put (url, n);
266 if (index == current.ChildNodes.Count) {
267 return SlowMatchNode (Tree.RootNode, matchCache, strippedUrl);
273 current = current.ChildNodes [index - 1];
279 /* That slow path is mainly here to handle ecmaspec type of url which are composed of hard to sort numbers
280 * because they don't have the same amount of digit. We could use a regex to harmonise the various number
281 * parts but then it would be quite specific. Since in the case of ecmaspec the tree is well-formed enough
282 * the "Slow" match should still be fast enough
284 Node SlowMatchNode (Node current, LRUCache<string, Node> matchCache, string url)
286 //Console.WriteLine ("Entering slow path for {0} starting from {1}", url, current.Element);
287 while (current != null) {
289 foreach (Node n in current.ChildNodes) {
290 var element = n.Element.StartsWith (UriPrefix, StringComparison.OrdinalIgnoreCase) ? n.Element.Substring (UriPrefix.Length) : n.Element;
291 if (url.Equals (element, StringComparison.Ordinal)) {
292 matchCache.Put (url, n);
294 } else if (url.StartsWith (element + ".", StringComparison.OrdinalIgnoreCase) && !n.IsLeaf) {
307 class NodeElementComparer : IComparer<Node>
309 public static NodeElementComparer Instance = new NodeElementComparer ();
311 public int Compare (Node n1, Node n2)
313 return string.Compare (Cleanup (n1), Cleanup (n2), StringComparison.Ordinal);
316 string Cleanup (Node n)
318 var prefix = n.Tree != null && n.Tree.HelpSource != null ? n.Tree.HelpSource.UriPrefix : string.Empty;
319 var element = n.Element.StartsWith (prefix, StringComparison.OrdinalIgnoreCase) ? n.Element.Substring (prefix.Length) : n.Element;
320 if (char.IsDigit (element, 0)) {
321 var count = element.TakeWhile (char.IsDigit).Count ();
322 element = element.PadLeft (Math.Max (0, 3 - count) + element.Length, '0');
324 //Console.WriteLine ("Cleaned up {0} to {1}", n.Element, element);
329 public virtual DocumentType GetDocumentTypeForId (string id)
331 return DocumentType.PlainText;
334 public virtual Stream GetImage (string url)
336 Stream result = null;
337 storage.TryRetrieve (url, out result);
342 // Populates the index.
344 public virtual void PopulateIndex (IndexMaker index_maker)
349 // Create different Documents for adding to Lucene search index
350 // The default action is do nothing. Subclasses should add the docs
352 public virtual void PopulateSearchableIndex (IndexWriter writer)