-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScriptItem.cs
464 lines (412 loc) · 13 KB
/
ScriptItem.cs
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
* Copyright (c) 2008, Anthony James McCreath
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1 Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2 Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3 Neither the name of the project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Anthony James McCreath "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Anthony James McCreath BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
using System;
using System.Data;
using System.Configuration;
using System.IO;
using System.Text;
namespace ClockWork.ScriptBuilder
{
/// <summary>
/// Base class for objects in a script that can be structured and know how to render themselves to a ScriptWriter
/// </summary>
public class ScriptItem : IScriptItem, IScriptIfCondition
{
#region Constructors
/// <summary>
/// Creates an empty script
/// </summary>
public ScriptItem()
: this(ScriptLayout.None)
{
}
/// <summary>
/// Create an empty script with a custom layout
/// </summary>
/// <param name="layout">override the default layout</param>
public ScriptItem(ScriptLayout layout)
{
this.TrySetLayout(layout, false); // construct layout should always be able to set the layout, hence false
this.OnInitialise();
}
#endregion
#region Initialisation
/// <summary>
/// A good place to set default values for an item
/// Called by the constructor
/// </summary>
protected virtual void OnInitialise()
{
}
#endregion
#region Layout Control
/// <summary>
/// Defines the default layout that this item with use
/// </summary>
public virtual ScriptLayout DefaultLayout
{
get { return ScriptLayout.Inline; }
}
private ScriptLayout _Layout = ScriptLayout.NotAlreadyEstablished;
/// <summary>
/// How an item is going to be layed out.
/// Inline = render without newlines
/// InlineBlock = start on same line but each following internal item should be on a new line
/// Block = start on a new line, all internal items should be on a new line
///
/// Note: internal items may cause the content to render over several lines even though this item is Inline
/// </summary>
public ScriptLayout Layout
{
get
{
// will only return a real style
switch (_Layout)
{
// virtual styles return the default
case ScriptLayout.None:
throw new Exception("Invalid Layout Value: " + _Layout);
case ScriptLayout.NotAlreadyEstablished:
case ScriptLayout.Default:
{
ScriptLayout defaultLayout = this.DefaultLayout;
switch (defaultLayout)
{
case ScriptLayout.None:
case ScriptLayout.Default:
case ScriptLayout.NotAlreadyEstablished:
throw new Exception("Invalid Default Layout: " + defaultLayout);
case ScriptLayout.Inline:
case ScriptLayout.InlineBlock:
case ScriptLayout.Block:
default:
return defaultLayout;
}
}
// real styles
case ScriptLayout.Inline:
case ScriptLayout.InlineBlock:
case ScriptLayout.Block:
default:
return _Layout;
};
}
}
/// <summary>
/// Attempt to set the layout.
/// General policy is to only set the layout if it has not been set yet.
/// The presedence of layout control is:
/// Constructor Based Layout if supplied directly by user (e.g. specifically created in script structure with layout)
/// Constructor Based Layout if supplied by parent item (e.g. created by parent item with a layout)
/// Set by Parent after contruction (e.g. parent has a preferred layout)
/// The items Default layout
///
/// Initially the layout is set to Auto which indicates
///
/// Default: use this if you want to
/// </summary>
/// <param name="layout">the layout preferred</param>
/// <param name="onlyIfNotAlreadyEstablished">Use false if you wish to override natural presidence and for a layout change</param>
public void TrySetLayout(ScriptLayout layout, bool onlyIfNotAlreadyEstablished)
{
if (!onlyIfNotAlreadyEstablished || !IsLayoutEstablished)
{
if (_Layout != layout)
{
switch (layout)
{
case ScriptLayout.None: // do nothing, cant set to None!
break;
case ScriptLayout.Default:
case ScriptLayout.NotAlreadyEstablished: // a reset?
case ScriptLayout.Inline:
case ScriptLayout.InlineBlock:
case ScriptLayout.Block:
default:
_Layout = layout;
OnLayoutChanged(new LayoutChangedEventArgs(_Layout));
break;
}
}
}
}
/// <summary>
/// Updates the layout if it has not already been set
/// </summary>
/// <param name="layout"></param>
public void TrySetLayout(ScriptLayout layout)
{
this.TrySetLayout(layout, true);
}
/// <summary>
/// Helper to try and set a layout for an item
/// First checks if the sent object is an IScriptItem
/// </summary>
/// <param name="item">object to check</param>
/// <param name="layout">preferred layout</param>
public static void ObjectTrySetLayout(object item, ScriptLayout layout)
{
if (item is IScriptItem)
((IScriptItem)item).TrySetLayout(layout);
}
/// <summary>
/// Has a layout been requested
/// This could be via the constructor or using the TrySetLayout method
/// </summary>
public bool IsLayoutEstablished
{
get
{
return _Layout != ScriptLayout.NotAlreadyEstablished;
}
}
/// <summary>
/// Triggered when the itmes layout is changed
/// </summary>
public event EventHandler<LayoutChangedEventArgs> LayoutChanged;
/// <summary>
/// Triggered when the items layout is changed
/// subclasses may alter internal layouts when this is striggered
/// </summary>
/// <param name="e">includes the new layout</param>
protected virtual void OnLayoutChanged(LayoutChangedEventArgs e)
{
if (LayoutChanged!=null)
LayoutChanged(this, e);
}
#endregion
#region Indentation
private int _Indents = 0;
/// <summary>
/// How much this item should be indented by
/// </summary>
public int Indents
{
get { return _Indents; }
set { _Indents = value; }
}
#endregion
#region Rendering
/// <summary>
/// Render straight to string using the default script writer
/// </summary>
/// <returns></returns>
public string Render()
{
TextWriter writer = new StringWriter();
Render(writer);
return writer.ToString();
}
/// <summary>
/// Rnder straight to a string using the specified format provider
/// </summary>
/// <param name="formatProvider">used when rendering</param>
/// <returns></returns>
public string Render(IFormatProvider formatProvider)
{
TextWriter textWriter = new StringWriter();
ScriptWriter scriptWriter = new ScriptWriter(textWriter, formatProvider);
Render(scriptWriter);
return textWriter.ToString();
}
/// <summary>
/// Render to a stream
/// </summary>
/// <param name="stream"></param>
public void Render(Stream stream)
{
StreamWriter writer = new StreamWriter(stream);
Render(writer);
}
/// <summary>
/// Render to a stream using the specified format provider
/// </summary>
/// <param name="stream"></param>
/// <param name="formatProvider"></param>
public void Render(Stream stream, IFormatProvider formatProvider)
{
StreamWriter writer = new StreamWriter(stream);
Render(writer, formatProvider);
}
/// <summary>
/// Render to a string builder
/// </summary>
/// <param name="stringBuilder"></param>
public void Render(StringBuilder stringBuilder)
{
StringWriter writer = new StringWriter(stringBuilder);
Render(writer);
}
/// <summary>
/// Render to a string builder using the specified format provider
/// </summary>
/// <param name="stringBuilder"></param>
/// <param name="formatProvider"></param>
public void Render(StringBuilder stringBuilder, IFormatProvider formatProvider)
{
StringWriter writer = new StringWriter(stringBuilder);
Render(writer, formatProvider);
}
/// <summary>
/// Render to a stream writer
/// </summary>
/// <param name="writer"></param>
public void Render(StreamWriter writer)
{
IScriptWriter scriptWriter = new ScriptWriter(writer);
Render(scriptWriter);
}
/// <summary>
/// Render to a stream writer using the specified format provider
/// </summary>
/// <param name="writer"></param>
/// <param name="formatProvider"></param>
public void Render(StreamWriter writer, IFormatProvider formatProvider)
{
IScriptWriter scriptWriter = new ScriptWriter(writer, formatProvider);
Render(scriptWriter);
}
/// <summary>
/// Render to a text writer
/// </summary>
/// <param name="writer"></param>
public void Render(TextWriter writer)
{
IScriptWriter scriptWriter = new ScriptWriter(writer);
Render(scriptWriter);
}
/// <summary>
/// Render to a text writer using the specified format provider
/// </summary>
/// <param name="writer"></param>
/// <param name="formatProvider"></param>
public void Render(TextWriter writer, IFormatProvider formatProvider)
{
IScriptWriter scriptWriter = new ScriptWriter(writer,formatProvider);
Render(scriptWriter);
}
/// <summary>
/// Render the content to a script writer
/// </summary>
/// <param name="writer"></param>
public void Render(IScriptWriter writer)
{
Render(writer, 0);
}
/// <summary>
/// Render the content to a script writer indenting the content as specified
/// </summary>
/// <param name="writer">the script writer to render to</param>
/// <param name="indents">how many indents to add. These indents are added to the item own Indent level</param>
public void Render(IScriptWriter writer, int indents)
{
try
{
int totalIndents = 0;
if (this.Layout == ScriptLayout.Block || this.Layout == ScriptLayout.InlineBlock) // don't indent if not multiline
totalIndents = Indents + indents;
try
{
writer.BeginIndent(totalIndents);
OnRender(new RenderingEventArgs(writer));
}
finally
{
writer.EndIndent(totalIndents);
}
}
finally
{
writer.Flush();
}
}
/// <summary>
/// Triggered when the content is to being rendered
/// You can use this with a baisc ScriptItem to create a rendeing proxy
/// i.e. place a script item where you want to render and listen to this event.
/// when triggered render to the writer in the arguments
/// </summary>
public event EventHandler<RenderingEventArgs> Rendering;
/// <summary>
/// This is called when the content is to be rendered
/// Subclasses will do what has to be done!
/// </summary>
/// <param name="e">includes the script writer to render content to</param>
protected virtual void OnRender(RenderingEventArgs e)
{
if (Rendering != null)
Rendering(this, e);
}
/// <summary>
/// Will this item rendered to more than en empty string
/// Subclasses should override this if nned be
/// Used as the default result for ScriptIf testing
/// </summary>
/// <returns></returns>
public virtual bool HasRenderContent
{
get
{
return true;
}
}
/// <summary>
/// Helper to test if an object will render as more than an empty string
/// </summary>
/// <param name="item">object to check</param>
/// <returns></returns>
public static bool ObjectHasRenderContent(object item)
{
if (item == null)
return false;
if (item is IScriptItem)
return ((IScriptItem)item).HasRenderContent;
if (item is string)
return !String.IsNullOrEmpty((string)item);
return true;
}
#endregion
#region IScriptIfCondition
/// <summary>
/// For example a list with no values may return false
/// Used in Sb.If testing
/// A good use is to indicate if an item needs to be included
/// default behavior is to return HasRenderContent
/// can be overriden by subclasses
/// </summary>
/// <returns></returns>
public virtual bool ScriptIfResult
{
get
{
return this.HasRenderContent;
}
}
#endregion
}
}