1 | // Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team |
---|
2 | // |
---|
3 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this |
---|
4 | // software and associated documentation files (the "Software"), to deal in the Software |
---|
5 | // without restriction, including without limitation the rights to use, copy, modify, merge, |
---|
6 | // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
---|
7 | // to whom the Software is furnished to do so, subject to the following conditions: |
---|
8 | // |
---|
9 | // The above copyright notice and this permission notice shall be included in all copies or |
---|
10 | // substantial portions of the Software. |
---|
11 | // |
---|
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
---|
13 | // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
---|
14 | // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
---|
15 | // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
---|
16 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
17 | // DEALINGS IN THE SOFTWARE. |
---|
18 | |
---|
19 | using System; |
---|
20 | using ICSharpCode.NRefactory.Editor; |
---|
21 | using ICSharpCode.NRefactory.TypeSystem; |
---|
22 | |
---|
23 | namespace ICSharpCode.NRefactory.Documentation |
---|
24 | { |
---|
25 | /// <summary> |
---|
26 | /// Represents a documentation comment. |
---|
27 | /// </summary> |
---|
28 | public class DocumentationComment |
---|
29 | { |
---|
30 | ITextSource xml; |
---|
31 | protected readonly ITypeResolveContext context; |
---|
32 | |
---|
33 | /// <summary> |
---|
34 | /// Gets the XML code for this documentation comment. |
---|
35 | /// </summary> |
---|
36 | public ITextSource Xml { |
---|
37 | get { return xml; } |
---|
38 | } |
---|
39 | |
---|
40 | /// <summary> |
---|
41 | /// Creates a new DocumentationComment. |
---|
42 | /// </summary> |
---|
43 | /// <param name="xml">The XML text.</param> |
---|
44 | /// <param name="context">Context for resolving cref attributes.</param> |
---|
45 | public DocumentationComment(ITextSource xml, ITypeResolveContext context) |
---|
46 | { |
---|
47 | if (xml == null) |
---|
48 | throw new ArgumentNullException("xml"); |
---|
49 | if (context == null) |
---|
50 | throw new ArgumentNullException("context"); |
---|
51 | this.xml = xml; |
---|
52 | this.context = context; |
---|
53 | } |
---|
54 | |
---|
55 | /// <summary> |
---|
56 | /// Creates a new DocumentationComment. |
---|
57 | /// </summary> |
---|
58 | /// <param name="xml">The XML text.</param> |
---|
59 | /// <param name="context">Context for resolving cref attributes.</param> |
---|
60 | public DocumentationComment(string xml, ITypeResolveContext context) |
---|
61 | { |
---|
62 | if (xml == null) |
---|
63 | throw new ArgumentNullException("xml"); |
---|
64 | if (context == null) |
---|
65 | throw new ArgumentNullException("context"); |
---|
66 | this.xml = new StringTextSource(xml); |
---|
67 | this.context = context; |
---|
68 | } |
---|
69 | |
---|
70 | /// <summary> |
---|
71 | /// Resolves the given cref value to an entity. |
---|
72 | /// Returns null if the entity is not found, or if the cref attribute is syntactically invalid. |
---|
73 | /// </summary> |
---|
74 | public virtual IEntity ResolveCref(string cref) |
---|
75 | { |
---|
76 | try { |
---|
77 | return IdStringProvider.FindEntity(cref, context); |
---|
78 | } catch (ReflectionNameParseException) { |
---|
79 | return null; |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | public override string ToString () |
---|
84 | { |
---|
85 | return Xml.Text; |
---|
86 | } |
---|
87 | |
---|
88 | public static implicit operator string (DocumentationComment documentationComment) |
---|
89 | { |
---|
90 | if (documentationComment != null) |
---|
91 | return documentationComment.ToString (); |
---|
92 | return null; |
---|
93 | } |
---|
94 | } |
---|
95 | } |
---|