NTP Analyzer  0.8.2
Analyze the operation of time servers
Ntp.Analyzer.Config.Compiler.Decompiler Class Reference

Public Member Functions

 Decompiler ()
 Initializes a new instance of the Decompiler class. More...
 
string Execute ()
 Execute the decompiler. More...
 

Properties

Configuration Configuration [get, set]
 Gets or sets the Configuration to decompile. More...
 
char IndentChar [get, set]
 Gets or sets the indent char. More...
 
int IndentSize [get, set]
 Gets or sets the size of the indent. More...
 
bool ShowDefaultValues [get, set]
 Gets or sets a value indicating whether this Decompiler show default values. More...
 

Private Member Functions

void AppendDateTimeKind (DateTimeKind value)
 
void AppendIndent (int size)
 Appends indent to output. More...
 
void AppendString (string value, Layout layout, int indent)
 Appends the string to output. More...
 
void DecompileComplexType (int indent, object node)
 Decompiles a complex node type (classes). More...
 
void DecompileConfiguration (int indent, string typeName, IConfigurationNode node)
 Decompiles a configuration section. More...
 
void DecompileReference (int indent, object node, PropertyInfo property)
 Decompiles a reference property if applicable. More...
 
bool DecompileReferenceCollection (int indent, object node, PropertyInfo property)
 Decompiles a property with a collection of references if applicable. More...
 
bool DecompileSetting (int indent, object node, PropertyInfo property)
 Decompiles a setting property if applicable. More...
 
bool DecompileSettingCollection (int indent, object node, PropertyInfo property)
 Decompiles a collection property if applicable. More...
 
void DecompileSimpleType (int indent, string name, object value, object defaultValue, Layout layout)
 Decompiles a simple node type (integers, strings, etc.) More...
 

Private Attributes

StringBuilder builder
 

Detailed Description

Definition at line 34 of file Decompiler.cs.

Constructor & Destructor Documentation

Ntp.Analyzer.Config.Compiler.Decompiler.Decompiler ( )
inline

Initializes a new instance of the Decompiler class.

Definition at line 39 of file Decompiler.cs.

40  {
41  IndentChar = ' ';
42  IndentSize = 8;
43  }
char IndentChar
Gets or sets the indent char.
Definition: Decompiler.cs:64
int IndentSize
Gets or sets the size of the indent.
Definition: Decompiler.cs:70

Member Function Documentation

void Ntp.Analyzer.Config.Compiler.Decompiler.AppendDateTimeKind ( DateTimeKind  value)
inlineprivate

Definition at line 83 of file Decompiler.cs.

References Ntp.Analyzer.Config.Table.Keyword.Find(), and Ntp.Analyzer.Config.Table.Keyword.Name.

84  {
85  switch (value)
86  {
87  case DateTimeKind.Local:
88  builder.Append(Keyword.Find(Symbol.KeywordTimeStampLocal).Name);
89  break;
90  case DateTimeKind.Utc:
91  case DateTimeKind.Unspecified:
92  builder.Append(Keyword.Find(Symbol.KeywordTimeStampUtc).Name);
93  break;
94  default:
95  builder.Append(Keyword.Find(Symbol.KeywordTimeStampUtc).Name);
96  break;
97  }
98  }
static Keyword Find(Symbol symbol)
Definition: Keyword.cs:216

Here is the call graph for this function:

void Ntp.Analyzer.Config.Compiler.Decompiler.AppendIndent ( int  size)
inlineprivate

Appends indent to output.

Parameters
sizeIndent size.

Definition at line 104 of file Decompiler.cs.

105  {
106  builder.Append(string.Empty.PadLeft(size*IndentSize, IndentChar));
107  }
char IndentChar
Gets or sets the indent char.
Definition: Decompiler.cs:64
int IndentSize
Gets or sets the size of the indent.
Definition: Decompiler.cs:70
void Ntp.Analyzer.Config.Compiler.Decompiler.AppendString ( string  value,
Layout  layout,
int  indent 
)
inlineprivate

Appends the string to output.

Parameters
valueValue of node.
layoutSpecial layout properties.
indentIndent count.

Definition at line 115 of file Decompiler.cs.

116  {
117  switch (layout)
118  {
119  case Layout.Block:
120  builder.AppendLine("{ ");
121  var lines = value.Trim().Split(
122  new[] {Environment.NewLine},
123  StringSplitOptions.None
124  );
125  foreach (var line in lines)
126  {
127  AppendIndent(indent + 1);
128  builder.AppendLine(line.Trim());
129  }
130  AppendIndent(indent);
131  builder.Append("}");
132  break;
133  case Layout.Quoted:
134  builder.Append("\"");
135  builder.Append(value);
136  builder.Append("\"");
137  break;
138  case Layout.Normal:
139  if (value.Contains(" ") || value.Contains("\t"))
140  {
141  builder.Append("\"");
142  builder.Append(value);
143  builder.Append("\"");
144  }
145  else
146  {
147  builder.Append(value);
148  }
149  break;
150  default:
151  builder.Append(value);
152  break;
153  }
154  }
void AppendIndent(int size)
Appends indent to output.
Definition: Decompiler.cs:104
void Ntp.Analyzer.Config.Compiler.Decompiler.DecompileComplexType ( int  indent,
object  node 
)
inlineprivate

Decompiles a complex node type (classes).

Parameters
indentIndent count.
nodeConfiguration node to decompile.

Definition at line 161 of file Decompiler.cs.

162  {
163  var properties = node.GetType().GetProperties().
164  OrderBy(
165  delegate(PropertyInfo info)
166  {
167  var attribute = System.Attribute.GetCustomAttribute(info, typeof(NtpaIndex));
168  return ((NtpaIndex) attribute)?.Index ?? int.MaxValue;
169  });
170 
171  foreach (var property in properties)
172  {
173  if (DecompileSetting(indent, node, property))
174  continue;
175 
176  if (DecompileSettingCollection(indent, node, property))
177  continue;
178 
179  if (DecompileReferenceCollection(indent, node, property))
180  continue;
181 
182  DecompileReference(indent, node, property);
183  }
184  }
bool DecompileReferenceCollection(int indent, object node, PropertyInfo property)
Decompiles a property with a collection of references if applicable.
Definition: Decompiler.cs:271
bool DecompileSettingCollection(int indent, object node, PropertyInfo property)
Decompiles a collection property if applicable.
Definition: Decompiler.cs:334
void DecompileReference(int indent, object node, PropertyInfo property)
Decompiles a reference property if applicable.
Definition: Decompiler.cs:245
bool DecompileSetting(int indent, object node, PropertyInfo property)
Decompiles a setting property if applicable.
Definition: Decompiler.cs:304
void Ntp.Analyzer.Config.Compiler.Decompiler.DecompileConfiguration ( int  indent,
string  typeName,
IConfigurationNode  node 
)
inlineprivate

Decompiles a configuration section.

Parameters
indentIndent count
typeNameName of configuration section.
nodeConfiguration node to decompile.

Definition at line 192 of file Decompiler.cs.

References Ntp.Analyzer.Interface.IConfigurationNode.ConfigName.

193  {
194  if (node == null)
195  return;
196 
197  // Swap builders
198  var temp = builder;
199  builder = new StringBuilder();
200 
201  DecompileComplexType(indent + 1, node);
202 
203  // Swap builder back
204  var res = builder;
205  builder = temp;
206 
207  // Skip empty nodes
208  if (res.ToString().Trim() == string.Empty)
209  return;
210 
211  if (indent >= 0)
212  {
213  // Make a blank line
214  AppendIndent(indent);
215  builder.AppendLine();
216 
217  // Write header
218  AppendIndent(indent);
219  builder.Append(typeName);
220 
221  if (node.ConfigName != null)
222  {
223  builder.Append(" ");
224  builder.Append(node.ConfigName);
225  }
226 
227  builder.AppendLine(" {");
228  }
229 
230  builder.Append(res);
231 
232  if (indent >= 0)
233  {
234  AppendIndent(indent);
235  builder.AppendLine("}");
236  }
237  }
void DecompileComplexType(int indent, object node)
Decompiles a complex node type (classes).
Definition: Decompiler.cs:161
void AppendIndent(int size)
Appends indent to output.
Definition: Decompiler.cs:104
void Ntp.Analyzer.Config.Compiler.Decompiler.DecompileReference ( int  indent,
object  node,
PropertyInfo  property 
)
inlineprivate

Decompiles a reference property if applicable.

Parameters
indentIndent count.
nodeConfiguration node to decompile.
propertyProperty to decompile.

Definition at line 245 of file Decompiler.cs.

References Ntp.Analyzer.Interface.IConfigurationNode.ConfigName, and Ntp.Analyzer.Config.Table.Keyword.Find().

246  {
247  var reference = (NtpaReference) System.Attribute.GetCustomAttribute(property, typeof(NtpaReference));
248  if (reference == null)
249  return;
250 
251  var value = property.GetValue(node, null);
252  if (value == null)
253  return;
254 
255  var keyword = Keyword.Find(reference.Keyword);
256  var configurationNode = value as IConfigurationNode;
257  if (configurationNode == null)
258  return;
259 
260  var name = configurationNode.ConfigName;
261  DecompileSimpleType(indent, keyword.Name, name, null, Layout.Normal);
262  }
static Keyword Find(Symbol symbol)
Definition: Keyword.cs:216
void DecompileSimpleType(int indent, string name, object value, object defaultValue, Layout layout)
Decompiles a simple node type (integers, strings, etc.)
Definition: Decompiler.cs:392

Here is the call graph for this function:

bool Ntp.Analyzer.Config.Compiler.Decompiler.DecompileReferenceCollection ( int  indent,
object  node,
PropertyInfo  property 
)
inlineprivate

Decompiles a property with a collection of references if applicable.

Parameters
indentIndent count.
nodeConfiguration node to decompile.
propertyProperty to decompile.
Returns
true if property is a collection of references; otherwise, false.

Definition at line 271 of file Decompiler.cs.

References Ntp.Analyzer.Interface.IConfigurationNode.ConfigName, and Ntp.Analyzer.Config.Table.Keyword.Find().

272  {
273  var attribute = System.Attribute.GetCustomAttribute(property, typeof(NtpaReferenceCollection));
274  if (attribute == null)
275  return false;
276 
277  var refCollection = (NtpaReferenceCollection) attribute;
278 
279  var value = property.GetValue(node, null);
280  if (!(value is IEnumerable))
281  return true;
282 
283  var keyword = Keyword.Find(refCollection.Keyword);
284  foreach (var subObject in value as IEnumerable)
285  {
286  var configurationNode = subObject as IConfigurationNode;
287  if (configurationNode != null)
288  {
289  var name = configurationNode.ConfigName;
290  DecompileSimpleType(indent, keyword.Name, name, null, Layout.Normal);
291  }
292  }
293 
294  return true;
295  }
static Keyword Find(Symbol symbol)
Definition: Keyword.cs:216
void DecompileSimpleType(int indent, string name, object value, object defaultValue, Layout layout)
Decompiles a simple node type (integers, strings, etc.)
Definition: Decompiler.cs:392

Here is the call graph for this function:

bool Ntp.Analyzer.Config.Compiler.Decompiler.DecompileSetting ( int  indent,
object  node,
PropertyInfo  property 
)
inlineprivate

Decompiles a setting property if applicable.

Parameters
indentIndent count.
nodeConfiguration node to decompile.
propertyProperty to decompile.
Returns
true if property is a setting; otherwise, false.

Definition at line 304 of file Decompiler.cs.

References Ntp.Analyzer.Config.Table.Keyword.Find().

305  {
306  var setting = (NtpaSetting) System.Attribute.GetCustomAttribute(property, typeof(NtpaSetting));
307  if (setting == null)
308  return false;
309 
310  var keyword = Keyword.Find(setting.Keyword);
311  var value = property.GetValue(node, null);
312  if (value == null)
313  return true;
314 
315  if (value is IConfigurationNode)
316  {
317  DecompileConfiguration(indent, keyword.Name, value as IConfigurationNode);
318  }
319  else
320  {
321  DecompileSimpleType(indent, keyword.Name, value, setting.Value, setting.Layout);
322  }
323 
324  return true;
325  }
void DecompileConfiguration(int indent, string typeName, IConfigurationNode node)
Decompiles a configuration section.
Definition: Decompiler.cs:192
static Keyword Find(Symbol symbol)
Definition: Keyword.cs:216
void DecompileSimpleType(int indent, string name, object value, object defaultValue, Layout layout)
Decompiles a simple node type (integers, strings, etc.)
Definition: Decompiler.cs:392

Here is the call graph for this function:

bool Ntp.Analyzer.Config.Compiler.Decompiler.DecompileSettingCollection ( int  indent,
object  node,
PropertyInfo  property 
)
inlineprivate

Decompiles a collection property if applicable.

Parameters
indentIndent count.
nodeConfiguration node to decompile.
propertyProperty to decompile.
Returns
true if property is a collection; otherwise, false.

Definition at line 334 of file Decompiler.cs.

References Ntp.Analyzer.Config.Table.Keyword.Find().

335  {
336  var attribute = System.Attribute.GetCustomAttribute(property, typeof(NtpaSettingsCollection));
337  if (attribute == null)
338  return false;
339 
340  var collection = (NtpaSettingsCollection) attribute;
341 
342  var value = property.GetValue(node, null);
343  if (value == null)
344  return true;
345 
346  // Decompile the collection in flat style
347  if (collection.Flatten && value is IEnumerable)
348  {
349  foreach (var subObject in value as IEnumerable)
350  {
351  DecompileComplexType(indent, subObject);
352  }
353  return true;
354  }
355 
356  if (!(value is IEnumerable))
357  return true;
358 
359  var raiseAdd = 0;
360  if (collection.Raise != Symbol.Undefined)
361  {
362  var raise = Keyword.Find(collection.Raise);
363  AppendIndent(indent);
364  builder.Append(raise.Name);
365  builder.AppendLine(" {");
366  raiseAdd = 1;
367  }
368 
369  var keyword = Keyword.Find(collection.Keyword);
370  foreach (var subObject in value as IEnumerable)
371  {
372  DecompileConfiguration(indent + raiseAdd, keyword.Name, subObject as IConfigurationNode);
373  }
374 
375  if (collection.Raise != Symbol.Undefined)
376  {
377  AppendIndent(indent);
378  builder.AppendLine("}");
379  }
380 
381  return true;
382  }
void DecompileComplexType(int indent, object node)
Decompiles a complex node type (classes).
Definition: Decompiler.cs:161
void DecompileConfiguration(int indent, string typeName, IConfigurationNode node)
Decompiles a configuration section.
Definition: Decompiler.cs:192
static Keyword Find(Symbol symbol)
Definition: Keyword.cs:216
void AppendIndent(int size)
Appends indent to output.
Definition: Decompiler.cs:104

Here is the call graph for this function:

void Ntp.Analyzer.Config.Compiler.Decompiler.DecompileSimpleType ( int  indent,
string  name,
object  value,
object  defaultValue,
Layout  layout 
)
inlineprivate

Decompiles a simple node type (integers, strings, etc.)

Parameters
indentIndent count.
nameThe name of the node.
valueThe value of the node.
defaultValueDefault value for this type.
layoutSpecial layout properties.

Definition at line 392 of file Decompiler.cs.

393  {
394  if (!ShowDefaultValues && value.Equals(defaultValue))
395  return;
396 
397  if (layout == Layout.Block)
398  {
399  AppendIndent(indent);
400  builder.AppendLine();
401  }
402 
403  AppendIndent(indent);
404  builder.Append(name);
405  builder.Append(" ");
406 
407  if (value is bool)
408  {
409  builder.Append((bool) value ? "Yes" : "No");
410  }
411  else if (value is string)
412  {
413  AppendString((string) value, layout, indent);
414  }
415  else if (value is DateTimeKind)
416  {
417  AppendDateTimeKind((DateTimeKind) value);
418  }
419  else if (value is Uri)
420  {
421  AppendString(((Uri) value).OriginalString, layout, indent);
422  }
423  else
424  {
425  builder.Append(value);
426  }
427 
428  builder.AppendLine();
429  }
void AppendDateTimeKind(DateTimeKind value)
Definition: Decompiler.cs:83
void AppendString(string value, Layout layout, int indent)
Appends the string to output.
Definition: Decompiler.cs:115
bool ShowDefaultValues
Gets or sets a value indicating whether this Decompiler show default values.
Definition: Decompiler.cs:58
void AppendIndent(int size)
Appends indent to output.
Definition: Decompiler.cs:104
string Ntp.Analyzer.Config.Compiler.Decompiler.Execute ( )
inline

Execute the decompiler.

Returns
Decompiled configuration as text.

Definition at line 76 of file Decompiler.cs.

77  {
78  builder = new StringBuilder();
80  return builder.ToString();
81  }
void DecompileConfiguration(int indent, string typeName, IConfigurationNode node)
Decompiles a configuration section.
Definition: Decompiler.cs:192

Member Data Documentation

StringBuilder Ntp.Analyzer.Config.Compiler.Decompiler.builder
private

Definition at line 45 of file Decompiler.cs.

Property Documentation

Configuration Ntp.Analyzer.Config.Compiler.Decompiler.Configuration
getset

Gets or sets the Configuration to decompile.

The configuration.

Definition at line 51 of file Decompiler.cs.

char Ntp.Analyzer.Config.Compiler.Decompiler.IndentChar
getset

Gets or sets the indent char.

The indent char.

Definition at line 64 of file Decompiler.cs.

Referenced by Ntp.Analyzer.Validate.Cli.Program.SetDecompilerParam().

int Ntp.Analyzer.Config.Compiler.Decompiler.IndentSize
getset

Gets or sets the size of the indent.

The size of the indent.

Definition at line 70 of file Decompiler.cs.

Referenced by Ntp.Analyzer.Validate.Cli.Program.SetDecompilerParam().

bool Ntp.Analyzer.Config.Compiler.Decompiler.ShowDefaultValues
getset

Gets or sets a value indicating whether this Decompiler show default values.

true if show default values; otherwise, false.

Definition at line 58 of file Decompiler.cs.


The documentation for this class was generated from the following file: