forked from axuno/SmartFormat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmartFormatter.cs
More file actions
274 lines (236 loc) · 10.5 KB
/
SmartFormatter.cs
File metadata and controls
274 lines (236 loc) · 10.5 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using SmartFormat.Core;
using SmartFormat.Core.Extensions;
using SmartFormat.Core.Output;
using SmartFormat.Core.Parsing;
using FormatException = SmartFormat.Core.FormatException;
namespace SmartFormat
{
/// <summary>
/// This class contains the Format method that constructs
/// the composite string by invoking each extension.
/// </summary>
public class SmartFormatter
{
#region: Constructor :
public SmartFormatter()
#if DEBUG
: this(Core.ErrorAction.ThrowError)
#else
: this(ErrorAction.Ignore)
#endif
{
}
public SmartFormatter(ErrorAction errorAction)
{
this.Parser = new Parser(errorAction);
this.ErrorAction = errorAction;
this.SourceExtensions = new List<ISource>();
this.FormatterExtensions = new List<IFormatter>();
}
#endregion
#region: Extension Registration :
public List<ISource> SourceExtensions { get; private set; }
public List<IFormatter> FormatterExtensions { get; private set; }
/// <summary>
/// Adds each extensions to this formatter.
/// Each extension must implement ISource, IFormatter, or both.
///
/// An exception will be thrown if the extension doesn't implement those interfaces.
/// </summary>
/// <param name="extensions"></param>
[Obsolete("Please use the specific overloads of AddExtensions().")]
public void AddExtensions(params object[] extensions)
{
foreach (var extension in extensions)
{
// We need to filter each extension to the correct list:
var source = extension as ISource;
var formatter = extension as IFormatter;
// If this object ISN'T a extension, throw an exception:
if (source == null && formatter == null)
throw new ArgumentException(string.Format("{0} does not implement ISource nor IFormatter.", extension.GetType().FullName), "extensions");
if (source != null)
SourceExtensions.Add(source);
if (formatter != null)
FormatterExtensions.Add(formatter);
}
}
/// <summary>
/// Adds each extensions to this formatter.
/// Each extension must implement ISource.
/// </summary>
/// <param name="sourceExtensions"></param>
public void AddExtensions(params ISource[] sourceExtensions)
{
SourceExtensions.AddRange(sourceExtensions);
}
/// <summary>
/// Adds each extensions to this formatter.
/// Each extension must implement IFormatter.
/// </summary>
/// <param name="formatterExtensions"></param>
public void AddExtensions(params IFormatter[] formatterExtensions)
{
FormatterExtensions.AddRange(formatterExtensions);
}
#endregion
#region: Properties :
public Parser Parser { get; set; }
public ErrorAction ErrorAction { get; set; }
#endregion
#region: Format Overloads :
public string Format(string format, params object[] args)
{
return Format(null, format, args);
}
public string Format(IFormatProvider provider, string format, params object[] args)
{
var output = new StringOutput(format.Length + args.Length * 8);
var formatParsed = Parser.ParseFormat(format);
object current = (args != null && args.Length > 0) ? args[0] : args; // The first item is the default.
var formatDetails = new FormatDetails(this, args, null, provider);
Format(output, formatParsed, current, formatDetails);
return output.ToString();
}
public void FormatInto(IOutput output, string format, params object[] args)
{
var formatParsed = Parser.ParseFormat(format);
object current = (args != null && args.Length > 0) ? args[0] : args; // The first item is the default.
var formatDetails = new FormatDetails(this, args, null, null);
Format(output, formatParsed, current, formatDetails);
}
public string FormatWithCache(ref FormatCache cache, string format, params object[] args)
{
var output = new StringOutput(format.Length + args.Length * 8);
if (cache == null) cache = new FormatCache(this.Parser.ParseFormat(format));
object current = (args != null && args.Length > 0) ? args[0] : args; // The first item is the default.
var formatDetails = new FormatDetails(this, args, cache, null);
Format(output, cache.Format, current, formatDetails);
return output.ToString();
}
public void FormatWithCacheInto(ref FormatCache cache, IOutput output, string format, params object[] args)
{
if (cache == null) cache = new FormatCache(this.Parser.ParseFormat(format));
object current = (args != null && args.Length > 0) ? args[0] : args; // The first item is the default.
var formatDetails = new FormatDetails(this, args, cache, null);
Format(output, cache.Format, current, formatDetails);
}
#endregion
#region: Format :
public void Format(IOutput output, Format format, object current, FormatDetails formatDetails)
{
// Before we start, make sure we have at least one source extension and one formatter extension:
CheckForExtensions();
Placeholder originalPlaceholder = formatDetails.Placeholder;
foreach (var item in format.Items)
{
var literalItem = item as LiteralText;
if (literalItem != null)
{
formatDetails.Placeholder = originalPlaceholder;
output.Write(literalItem.baseString, literalItem.startIndex, literalItem.endIndex - literalItem.startIndex, formatDetails);
continue;
} // Otherwise, the item is a placeholder.
var placeholder = (Placeholder)item;
object context = current;
formatDetails.Placeholder = placeholder;
bool handled;
// Evaluate the selectors:
foreach (var selector in placeholder.Selectors)
{
handled = false;
var result = context;
InvokeSourceExtensions(context, selector, ref handled, ref result, formatDetails);
if (!handled)
{
// The selector wasn't handled, which means it isn't valid
FormatError(selector, string.Format("Could not evaluate the selector \"{0}\"", selector.Text), selector.startIndex, output, formatDetails);
context = null;
break;
}
context = result;
}
// Evaluate the format:
handled = false;
try
{
InvokeFormatterExtensions(context, placeholder.Format, ref handled, output, formatDetails);
}
catch (Exception ex)
{
// An error occurred while formatting.
var errorIndex = placeholder.Format != null ? placeholder.Format.startIndex : placeholder.Selectors.Last().endIndex;
FormatError(item, ex, errorIndex, output, formatDetails);
continue;
}
}
}
private void CheckForExtensions()
{
if (this.SourceExtensions.Count == 0)
{
throw new InvalidOperationException("No source extensions are available. Please add at least one source extension, such as the DefaultSource.");
}
if (this.FormatterExtensions.Count == 0)
{
throw new InvalidOperationException("No formatter extensions are available. Please add at least one formatter extension, such as the DefaultFormatter.");
}
}
private void InvokeSourceExtensions(object current, Selector selector, ref bool handled, ref object result, FormatDetails formatDetails)
{
foreach (var sourceExtension in this.SourceExtensions)
{
sourceExtension.EvaluateSelector(current, selector, ref handled, ref result, formatDetails);
if (handled) break;
}
}
private void InvokeFormatterExtensions(object current, Format format, ref bool handled, IOutput output, FormatDetails formatDetails)
{
foreach (var formatterExtension in this.FormatterExtensions)
{
formatterExtension.EvaluateFormat(current, format, ref handled, output, formatDetails);
if (handled) break;
}
}
private void FormatError(FormatItem errorItem, string issue, int startIndex, IOutput output, FormatDetails formatDetails)
{
switch (this.ErrorAction)
{
case ErrorAction.Ignore:
return;
case ErrorAction.ThrowError:
throw new FormatException(errorItem, issue, startIndex);
case ErrorAction.OutputErrorInResult:
formatDetails.FormatError = new FormatException(errorItem, issue, startIndex);
output.Write(issue, formatDetails);
formatDetails.FormatError = null;
break;
case ErrorAction.MaintainTokens:
output.Write(formatDetails.Placeholder.Text, formatDetails);
break;
}
}
private void FormatError(FormatItem errorItem, Exception innerException, int startIndex, IOutput output, FormatDetails formatDetails)
{
switch (this.ErrorAction)
{
case ErrorAction.Ignore:
return;
case ErrorAction.ThrowError:
throw new FormatException(errorItem, innerException, startIndex);
case ErrorAction.OutputErrorInResult:
formatDetails.FormatError = new FormatException(errorItem, innerException, startIndex);
output.Write(innerException.Message, formatDetails);
formatDetails.FormatError = null;
break;
case ErrorAction.MaintainTokens:
output.Write(formatDetails.Placeholder.Text, formatDetails);
break;
}
}
#endregion
}
}