aboutsummaryrefslogtreecommitdiff
path: root/src/api/wix/WixToolset.Data/SourceLineNumber.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/wix/WixToolset.Data/SourceLineNumber.cs')
-rw-r--r--src/api/wix/WixToolset.Data/SourceLineNumber.cs275
1 files changed, 275 insertions, 0 deletions
diff --git a/src/api/wix/WixToolset.Data/SourceLineNumber.cs b/src/api/wix/WixToolset.Data/SourceLineNumber.cs
new file mode 100644
index 00000000..970f17e7
--- /dev/null
+++ b/src/api/wix/WixToolset.Data/SourceLineNumber.cs
@@ -0,0 +1,275 @@
1// 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.
2
3namespace WixToolset.Data
4{
5 using System;
6 using System.IO;
7 using System.Text;
8 using System.Xml;
9 using System.Xml.Linq;
10 using SimpleJson;
11
12 /// <summary>
13 /// Represents file name and line number for source file
14 /// </summary>
15 public sealed class SourceLineNumber
16 {
17 /// <summary>
18 /// Constructor for a source with no line information.
19 /// </summary>
20 /// <param name="fileName">File name of the source.</param>
21 public SourceLineNumber(string fileName)
22 {
23 this.FileName = fileName;
24 }
25
26 /// <summary>
27 /// Constructor for a source with line information.
28 /// </summary>
29 /// <param name="fileName">File name of the source.</param>
30 /// <param name="lineNumber">Line number of the source.</param>
31 public SourceLineNumber(string fileName, int lineNumber)
32 {
33 this.FileName = fileName;
34 this.LineNumber = lineNumber;
35 }
36
37 /// <summary>
38 /// Constructor for a source with a parent and no line information.
39 /// </summary>
40 /// <param name="fileName">File name of the source.</param>
41 /// <param name="parent">Parent of this source line number</param>
42 public SourceLineNumber(string fileName, SourceLineNumber parent)
43 {
44 this.FileName = fileName;
45 this.Parent = parent;
46 }
47
48 /// <summary>
49 /// Constructor for a source with a parent and line information.
50 /// </summary>
51 /// <param name="fileName">File name of the source.</param>
52 /// <param name="parent">Parent of this source line number</param>
53 /// <param name="lineNumber">Line number of the source.</param>
54 public SourceLineNumber(string fileName, SourceLineNumber parent, int lineNumber)
55 {
56 this.FileName = fileName;
57 this.Parent = parent;
58 this.LineNumber = lineNumber;
59 }
60
61 /// <summary>
62 /// Gets the file name of the source.
63 /// </summary>
64 /// <value>File name for the source.</value>
65 public string FileName { get; }
66
67 /// <summary>
68 /// Gets or sets the line number of the source.
69 /// </summary>
70 /// <value>Line number of the source.</value>
71 public int? LineNumber { get; set; }
72
73 /// <summary>
74 /// Gets or sets the parent source line number that included this source line number.
75 /// </summary>
76 public SourceLineNumber Parent { get; private set; }
77
78 /// <summary>
79 /// Gets the file name and line information.
80 /// </summary>
81 /// <value>File name and line information.</value>
82 public string QualifiedFileName => this.LineNumber.HasValue ? String.Concat(this.FileName, "*", this.LineNumber) : this.FileName;
83
84 internal static SourceLineNumber Deserialize(JsonObject jsonObject)
85 {
86 var fileName = jsonObject.GetValueOrDefault<string>("file");
87 var lineNumber = jsonObject.GetValueOrDefault("line", null);
88
89 var parentJson = jsonObject.GetValueOrDefault<JsonObject>("parent");
90 var parent = (parentJson == null) ? null : SourceLineNumber.Deserialize(parentJson);
91
92 return new SourceLineNumber(fileName)
93 {
94 LineNumber = lineNumber,
95 Parent = parent
96 };
97 }
98
99 internal JsonObject Serialize()
100 {
101 var jsonObject = new JsonObject
102 {
103 { "file", this.FileName },
104 { "line", this.LineNumber }
105 };
106
107 if (this.Parent != null)
108 {
109 var parentJson = this.Parent.Serialize();
110 jsonObject.Add("parent", parentJson);
111 }
112
113 return jsonObject;
114 }
115
116 /// <summary>
117 /// Creates a source line number from an encoded string.
118 /// </summary>
119 /// <param name="encodedSourceLineNumbers">Encoded string to parse.</param>
120 public static SourceLineNumber CreateFromEncoded(string encodedSourceLineNumbers)
121 {
122 var linesSplitIndex = encodedSourceLineNumbers.IndexOf('|');
123
124 // The most common case is that there is a single encoded line,
125 // so optimize for that case.
126 if (linesSplitIndex < 0)
127 {
128 return DecodeSourceLineNumber(encodedSourceLineNumbers, 0, -1);
129 }
130 else // decode the multiple lines.
131 {
132 var startLine = 0;
133
134 SourceLineNumber first = null;
135 SourceLineNumber parent = null;
136 while (startLine < encodedSourceLineNumbers.Length)
137 {
138 var source = DecodeSourceLineNumber(encodedSourceLineNumbers, startLine, linesSplitIndex - 1);
139
140 if (null != parent)
141 {
142 parent.Parent = source;
143 }
144
145 parent = source;
146 if (null == first)
147 {
148 first = parent;
149 }
150
151 if (linesSplitIndex < 0)
152 {
153 break;
154 }
155
156 startLine = linesSplitIndex + 1;
157 linesSplitIndex = encodedSourceLineNumbers.IndexOf('|', startLine);
158 }
159
160 return first;
161 }
162 }
163
164 /// <summary>
165 /// Creates a source line number from a URI.
166 /// </summary>
167 /// <param name="uri">Uri to convert into source line number</param>
168 public static SourceLineNumber CreateFromUri(string uri)
169 {
170 if (String.IsNullOrEmpty(uri))
171 {
172 return null;
173 }
174
175 // make the local path look like a normal local path
176 var localPath = new Uri(uri).LocalPath;
177 localPath = localPath.TrimStart(Path.AltDirectorySeparatorChar).Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
178
179 return new SourceLineNumber(localPath);
180 }
181
182 /// <summary>
183 /// Creates a source line number from an XObject.
184 /// </summary>
185 /// <param name="node">XML node to create source line number from.</param>
186 /// <param name="offset">Optional line number offset into XML file not already included in the line information.</param>
187 public static SourceLineNumber CreateFromXObject(XObject node, int offset = 0)
188 {
189 var result = CreateFromUri(node.BaseUri);
190 if (null != result && node is IXmlLineInfo lineInfo)
191 {
192 result.LineNumber = lineInfo.LineNumber + offset;
193 }
194
195 return result;
196 }
197
198 /// <summary>
199 /// Get the source line information for the current element. Typically this information
200 /// is set by the precompiler for each element that it encounters.
201 /// </summary>
202 /// <param name="node">Element to get source line information for.</param>
203 /// <returns>
204 /// The source line number used to author the element being processed or
205 /// null if the preprocessor did not process the element or the node is
206 /// not an element.
207 /// </returns>
208 public static SourceLineNumber GetFromXAnnotation(XObject node)
209 {
210 return node.Annotation<SourceLineNumber>();
211 }
212
213 /// <summary>
214 /// Returns the SourceLineNumber and parents encoded as a string.
215 /// </summary>
216 public string GetEncoded()
217 {
218 var sb = new StringBuilder(this.QualifiedFileName);
219
220 for (var parent = this.Parent; null != parent; parent = parent.Parent)
221 {
222 sb.Append("|");
223 sb.Append(parent.QualifiedFileName);
224 }
225
226 return sb.ToString();
227 }
228
229 /// <summary>
230 /// Determines if two SourceLineNumbers are equivalent.
231 /// </summary>
232 /// <param name="obj">Object to compare.</param>
233 /// <returns>True if SourceLineNumbers are equivalent.</returns>
234 public override bool Equals(object obj)
235 {
236 return obj is SourceLineNumber other &&
237 this.LineNumber.HasValue == other.LineNumber.HasValue &&
238 (!this.LineNumber.HasValue || this.LineNumber == other.LineNumber) &&
239 this.FileName.Equals(other.FileName, StringComparison.OrdinalIgnoreCase) &&
240 (null == this.Parent && null == other.Parent || this.Parent.Equals(other.Parent));
241 }
242
243 /// <summary>
244 /// Serves as a hash code for a particular type.
245 /// </summary>
246 /// <returns>The hash code.</returns>
247 public override int GetHashCode()
248 {
249 return this.GetEncoded().GetHashCode();
250 }
251
252 /// <summary>
253 /// Shows a string representation of a source line number.
254 /// </summary>
255 /// <returns>String representation of a source line number.</returns>
256 public override string ToString()
257 {
258 return this.LineNumber.HasValue && !String.IsNullOrEmpty(this.FileName) ? String.Concat(this.FileName, "(", this.LineNumber, ")") : this.FileName ?? String.Empty;
259 }
260
261 private static SourceLineNumber DecodeSourceLineNumber(string encoded, int startIndex, int endIndex)
262 {
263 if (endIndex < 0)
264 {
265 endIndex = encoded.Length - 1;
266 }
267
268 var count = endIndex - startIndex;
269 var filenameSplitIndex = encoded.LastIndexOf('*', endIndex - 1, count);
270 return (filenameSplitIndex < 0) ? new SourceLineNumber(encoded) :
271 new SourceLineNumber(encoded.Substring(startIndex, filenameSplitIndex - startIndex),
272 Convert.ToInt32(encoded.Substring(filenameSplitIndex + 1, endIndex - filenameSplitIndex)));
273 }
274 }
275}