aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Data/SourceLineNumber.cs
blob: 8ec65201234a6d86cc47c146aad53a72b30be8b6 (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
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

namespace WixToolset.Data
{
    using System;
    using System.IO;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    using SimpleJson;

    /// <summary>
    /// Represents file name and line number for source file
    /// </summary>
    public sealed class SourceLineNumber
    {
        /// <summary>
        /// Constructor for a source with no line information.
        /// </summary>
        /// <param name="fileName">File name of the source.</param>
        public SourceLineNumber(string fileName)
        {
            this.FileName = fileName;
        }

        /// <summary>
        /// Constructor for a source with line information.
        /// </summary>
        /// <param name="fileName">File name of the source.</param>
        /// <param name="lineNumber">Line number of the source.</param>
        public SourceLineNumber(string fileName, int lineNumber)
        {
            this.FileName = fileName;
            this.LineNumber = lineNumber;
        }

        /// <summary>
        /// Gets the file name of the source.
        /// </summary>
        /// <value>File name for the source.</value>
        public string FileName { get; private set; }

        /// <summary>
        /// Gets or sets the line number of the source.
        /// </summary>
        /// <value>Line number of the source.</value>
        public int? LineNumber { get; set; }

        /// <summary>
        /// Gets or sets the parent source line number that included this source line number.
        /// </summary>
        public SourceLineNumber Parent { get; set; }

        /// <summary>
        /// Gets the file name and line information.
        /// </summary>
        /// <value>File name and line information.</value>
        public string QualifiedFileName
        {
            get
            {
                return this.LineNumber.HasValue ? String.Concat(this.FileName, "*", this.LineNumber) : this.FileName;
            }
        }

        internal static SourceLineNumber Deserialize(JsonObject jsonObject)
        {
            var fileName = jsonObject.GetValueOrDefault<string>("file");
            var lineNumber = jsonObject.GetValueOrDefault("line", null);

            var parentJson = jsonObject.GetValueOrDefault<JsonObject>("parent");
            var parent = (parentJson == null) ? null : SourceLineNumber.Deserialize(parentJson);

            return new SourceLineNumber(fileName)
            {
                LineNumber = lineNumber,
                Parent = parent
            };
        }

        internal JsonObject Serialize()
        {
            var jsonObject = new JsonObject
            {
                { "file", this.FileName },
                { "line", this.LineNumber }
            };

            if (this.Parent != null)
            {
                var parentJson = this.Parent.Serialize();
                jsonObject.Add("parent", parentJson);
            }

            return jsonObject;
        }

        /// <summary>
        /// Creates a source line number from an encoded string.
        /// </summary>
        /// <param name="encodedSourceLineNumbers">Encoded string to parse.</param>
        public static SourceLineNumber CreateFromEncoded(string encodedSourceLineNumbers)
        {
            string[] linesSplit = encodedSourceLineNumbers.Split('|');

            SourceLineNumber first = null;
            SourceLineNumber parent = null;
            for (int i = 0; i < linesSplit.Length; ++i)
            {
                string[] filenameSplit = linesSplit[i].Split('*');
                SourceLineNumber source;

                if (2 == filenameSplit.Length)
                {
                    source = new SourceLineNumber(filenameSplit[0], Convert.ToInt32(filenameSplit[1]));
                }
                else
                {
                    source = new SourceLineNumber(filenameSplit[0]);
                }

                if (null != parent)
                {
                    parent.Parent = source;
                }

                parent = source;
                if (null == first)
                {
                    first = parent;
                }
            }

            return first;
        }

        /// <summary>
        /// Creates a source line number from a URI.
        /// </summary>
        /// <param name="uri">Uri to convert into source line number</param>
        public static SourceLineNumber CreateFromUri(string uri)
        {
            if (String.IsNullOrEmpty(uri))
            {
                return null;
            }

            // make the local path look like a normal local path
            var localPath = new Uri(uri).LocalPath;
            localPath = localPath.TrimStart(Path.AltDirectorySeparatorChar).Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);

            return new SourceLineNumber(localPath);
        }

        /// <summary>
        /// Creates a source line number from an XObject.
        /// </summary>
        /// <param name="node">XML node to create source line number from.</param>
        /// <param name="offset">Optional line number offset into XML file not already included in the line information.</param>
        public static SourceLineNumber CreateFromXObject(XObject node, int offset = 0)
        {
            string uri = node.BaseUri;
            IXmlLineInfo lineInfo = node as IXmlLineInfo;

            SourceLineNumber result = CreateFromUri(uri);
            if (null != result && null != lineInfo)
            {
                result.LineNumber = lineInfo.LineNumber + offset;
            }

            return result;
        }

        /// <summary>
        /// Get the source line information for the current element. Typically this information 
        /// is set by the precompiler for each element that it encounters.
        /// </summary>
        /// <param name="node">Element to get source line information for.</param>
        /// <returns>
        /// The source line number used to author the element being processed or
        /// null if the preprocessor did not process the element or the node is
        /// not an element.
        /// </returns>
        public static SourceLineNumber GetFromXAnnotation(XObject node)
        {
            return node.Annotation<SourceLineNumber>();
        }

        /// <summary>
        /// Returns the SourceLineNumber and parents encoded as a string.
        /// </summary>
        public string GetEncoded()
        {
            StringBuilder sb = new StringBuilder(this.QualifiedFileName);

            for (SourceLineNumber source = this.Parent; null != source; source = source.Parent)
            {
                sb.Append("|");
                sb.Append(source.QualifiedFileName);
            }

            return sb.ToString();
        }

        /// <summary>
        /// Determines if two SourceLineNumbers are equivalent.
        /// </summary>
        /// <param name="obj">Object to compare.</param>
        /// <returns>True if SourceLineNumbers are equivalent.</returns>
        public override bool Equals(object obj)
        {
            SourceLineNumber other = obj as SourceLineNumber;
            return null != other &&
                   this.LineNumber.HasValue == other.LineNumber.HasValue &&
                   (!this.LineNumber.HasValue || this.LineNumber == other.LineNumber) &&
                   this.FileName.Equals(other.FileName, StringComparison.OrdinalIgnoreCase) &&
                   (null == this.Parent && null == other.Parent || this.Parent.Equals(other.Parent));
        }

        /// <summary>
        /// Serves as a hash code for a particular type.
        /// </summary>
        /// <returns>The hash code.</returns>
        public override int GetHashCode()
        {
            return this.GetEncoded().GetHashCode();
        }

        /// <summary>
        /// Shows a string representation of a source line number.
        /// </summary>
        /// <returns>String representation of a source line number.</returns>
        public override string ToString()
        {
            return this.LineNumber.HasValue && !String.IsNullOrEmpty(this.FileName) ? String.Concat(this.FileName, "(", this.LineNumber, ")") : this.FileName ?? String.Empty;
        }
    }
}