The StaDyn Programming Language
Source code documentation of StaDyn, a hybrid static and dynamic typing language.
DynVarManager.cs
Go to the documentation of this file.
1 using System;
2 using System.Xml;
3 using System.IO;
4 using System.Collections.Generic;
5 
6 namespace DynVarManagement {
15  public class DynVarManager {
16  #region Fields
17 
18  private XmlDocument document;
19  private string fileName;
20 
21  #endregion
22 
23  #region ".dyn" XML constants: Extension, tags and attributes
24 
28  public static readonly string DynVarFileExt = ".dyn";
29 
30  public static readonly string ApplicationTag = "application";
31  public static readonly string NamespaceTag = "namespace";
32  public static readonly string ClassTag = "class";
33  public static readonly string InterfaceTag = "interface";
34  public static readonly string MethodTag = "method";
35  public static readonly string DynVarTag = "dynvar";
36 
37  public static readonly string NameAtt = "name";
38  public static readonly string DynReturnAtt = "dynreturn";
39 
40  #endregion
41 
42  #region Properties
43 
44  public string FileName {
45  get { return fileName; }
46  }
47 
48  #endregion
49 
50  #region LoadOrCreate
51 
58  public bool LoadOrCreate(string fileName) {
59  this.fileName = fileName;
60  document = new XmlDocument();
61  try {
62  document.Load(fileName);
63  //Compatibility:
64  this.ready = true;
65  return true;
66  } catch (Exception) {
67  XmlElement el = document.CreateElement(ApplicationTag);
68  el.SetAttribute(NameAtt, Path.GetFileNameWithoutExtension(fileName));
69  document.AppendChild(el);
70  //Compatibility:
71  this.ready = true;
72  return false;
73  }
74  }
75 
76  #endregion
77 
78  #region Save
79 
80  public void Save() {
81  if (document != null) {
82  document.Save(FileName);
83  }
84  }
85 
86  #endregion
87 
88  #region SetDynamic
89 
90  public void SetDynamic(VarPath path) {
91  //Get/Create nodes until parent element is reached
92  XmlNode node = getOrCreateUntilParent(path);
93  //Set dynamic (field or variable)
94  if (path.VarName != null)
95  getOrCreateNode(node, DynVarTag, NameAtt, path.VarName);
96  //Set dynamic (method return)
97  else if (path.MethodName != null && node.Name.Equals(MethodTag) && node is XmlElement)
98  (node as XmlElement).SetAttribute(DynReturnAtt, true.ToString());
99  }
100 
101  #endregion
102 
103  #region SetStatic
104 
105  public void SetStatic(VarPath path) {
106  //Get/Create nodes until parent element is reached
107  XmlNode node = getOrCreateUntilParent(path);
108  //Set static (field or variable)
109  if (path.VarName != null) {
110  XmlNode varNode = node.SelectSingleNode(
111  DynVarTag + "[@" + NameAtt + "= '" + path.VarName + "']");
112  node.RemoveChild(varNode);
113  }
114  //Set static (method return)
115  else if (path.MethodName != null) {
116  XmlNode methodNode = node.SelectSingleNode(
117  MethodTag + "[@" + NameAtt + "= '" + path.MethodName + "']");
118  XmlElement methodElement = methodNode as XmlElement;
119  if (methodElement != null)
120  methodElement.SetAttribute(DynReturnAtt, false.ToString());
121  }
122  }
123 
124  #endregion
125 
126  #region IsDynamic
127 
128  public bool IsDynamic(VarPath path) {
129  //Get/Create nodes until parent element is reached
130  XmlNode node = getOrCreateUntilParent(path);
131  //Check dynamic (field or variable)
132  if (path.VarName != null) {
133  node = node.SelectSingleNode(
134  DynVarTag + "[@" + NameAtt + "= '" + path.VarName + "']");
135  //Var is dynamic if and only if node exists
136  return node != null;
137  }
138  //Check dynamic (method return)
139  else if (path.MethodName != null && node.Name.Equals(MethodTag) && node is XmlElement) {
140  XmlElement methodElement = node as XmlElement;
141  return true.ToString().Equals(methodElement.GetAttribute(DynReturnAtt));
142  }
143  else
144  return false;
145  }
146 
147  #endregion
148 
149  #region Private methods
150 
151  private XmlNode getOrCreateNode(XmlNode node, string name, string attribute, string value) {
152  XmlNode newNode = node.SelectSingleNode(name + "[@" + attribute + " = '" + value + "']");
153  if (newNode == null) {
154  newNode = document.CreateElement(name);
155  ((XmlElement)newNode).SetAttribute(attribute, value);
156  node.AppendChild(newNode);
157  }
158  return newNode;
159  }
160 
161  private XmlNode getOrCreateUntilParent(VarPath path) {
162  XmlNode node = document.DocumentElement;
163  if (path.NamespaceName != null) {
164  node = getOrCreateNode(node, NamespaceTag, NameAtt, path.NamespaceName);
165  }
166  if (path.ClassName != null) {
167  node = getOrCreateNode(node, ClassTag, NameAtt, path.ClassName);
168  }
169  else if (path.InterfaceName != null) {
170  node = getOrCreateNode(node, InterfaceTag, NameAtt, path.InterfaceName);
171  }
172  if (path.MethodName != null) {
173  node = getOrCreateNode(node, MethodTag, NameAtt, path.MethodName);
174  }
175  // * "block" element is not used anymore.
176  // *
177  // *
178  //if (path.Blocks.Count > 0)
179  //{
180  // XmlNodeList nodeList;
181  // XmlNode blockNode;
182  // for (int blockLevel = 0; blockLevel < path.Blocks.Count; blockLevel++)
183  // {
184  // nodeList = node.SelectNodes("block");
185  // blockNode = null;
186  // for (int i = 0; i <= path.Blocks[blockLevel] - nodeList.Count; i++)
187  // {
188  // blockNode = document.CreateElement("block");
189  // node.AppendChild(blockNode);
190  // }
191  // node = blockNode == null ? nodeList[path.Blocks[blockLevel]] : blockNode;
192  // }
193  //}
194  return node;
195  }
196 
197  #endregion
198 
199  //COMPATIBILITY
200 
201 
202  //* These members provide compatibility with the old DynVarManagement implementation, using the same interface.
203  //* - Instead of Load, LoadOrCreate can be used.
204  //* - Instead of SearchDynVar, IsDynamic can be used (VarPath argument instead of various string arguments)
205  //* - Constructor and Ready property added.
206 
207 
208  #region Fields
209 
213  private bool ready;
214 
215  #endregion
216 
217  #region Properties
218 
222  public bool Ready {
223  get { return this.ready; }
224  }
225 
226  #endregion
227 
228  #region Constructor
229 
233  public DynVarManager() {
234  this.ready = false;
235  }
236 
237  #endregion
238 
239  #region Load
240 
244  public void Load(string name) {
245  this.fileName = name;
246  document = new XmlDocument();
247  try {
248  document.Load(name);
249  this.ready = true;
250  } catch (Exception e) {
251  throw new DynVarException(e.Message);
252  }
253  }
254 
255  #endregion
256 
257  #region SearchDynVar
258 
259  public bool SearchDynVar(string ns, string classId, string varId) {
260  VarPath path = new VarPath();
261  path.NamespaceName = ns;
262  path.ClassName = classId;
263  path.VarName = varId;
264  return IsDynamic(path);
265  }
266 
267  public bool SearchDynVar(string ns, string classId, string methodId, string varId) {
268  VarPath path = new VarPath();
269  path.NamespaceName = ns;
270  path.ClassName = classId;
271  path.MethodName = methodId;
272  path.VarName = varId;
273  return IsDynamic(path);
274  }
275 
276  public bool SearchDynVar(string ns, string classId, string methodId, List<int> blockList, string varId) {
277  //Blocks not used any more
278  return SearchDynVar(ns, classId, methodId, varId);
279  }
280 
281  #endregion
282 
283  //*
284  // * Extended interface with new SearchDynVar overloading (unnecessary since IsDynamic accepts VarPath)
285  // * - Dynamic method return managing
286  // * - Interface members managing
287  // *
288 
289  #region SearchDynVar
290 
291  public bool SearchDynVar(string ns, string classOrInterfaceId, string methodId, bool isClassId) {
292  VarPath path = new VarPath();
293  path.NamespaceName = ns;
294  if (isClassId)
295  path.ClassName = classOrInterfaceId;
296  else
297  path.InterfaceName = classOrInterfaceId;
298  path.MethodName = methodId;
299  return IsDynamic(path);
300  }
301 
302  #endregion
303 
304  }
305 
306  #region class VarPath
307 
308  public class VarPath {
309  #region Fields
310 
311  private string namespaceName;
312  private string className;
313  private string interfaceName;
314  private string methodName;
315  //Not used anymore
316  //private List<int> blocks;
317  private string varName;
318 
319  #endregion
320 
321  #region Properties
322  public string NamespaceName {
323  get { return namespaceName; }
324  set { namespaceName = value; }
325  }
326  public string ClassName {
327  get { return className; }
328  set { className = value; }
329  }
330  public string InterfaceName {
331  get { return interfaceName; }
332  set { interfaceName = value; }
333  }
334  public string MethodName {
335  get { return methodName; }
336  set { methodName = value; }
337  }
338 
339  //* Not used anymore
340  //public List<int> Blocks
341  //{
342  // get { return blocks; }
343  //}
344 
345  public string VarName {
346  get { return varName; }
347  set { varName = value; }
348  }
349 
350  #endregion
351 
352  #region Constructor
353 
354  public VarPath() {
355  //Not used anymore
356  //blocks = new List<int>();
357  }
358 
359  #endregion
360  }
361 
362  #endregion
363 }
364 
365 
366 /*
367 // * Old implementation
368 
369 using System;
370 using System.Collections.Generic;
371 using System.Collections;
372 using System.Text;
373 using System.Xml;
374 using System.IO;
375 
376 namespace DynVarManagement
377 {
382  public class DynVarManager
383  {
384  #region Fields
385 
389  private string fileName;
390 
394  private XmlDocument document;
395 
399  private bool ready;
400 
404  public const string DynVarFileExt = ".dyn";
405 
406  #endregion
407 
408  #region Properties
409 
413  public bool Ready
414  {
415  get { return this.ready; }
416  }
417 
418  #endregion
419 
420  #region Constructor
421 
425  public DynVarManager()
426  {
427  this.ready = false;
428  }
429 
430  #endregion
431 
432  #region Load()
433 
437  public void Load(string name)
438  {
439  this.fileName = name;
440  document = new XmlDocument();
441  try
442  {
443  document.Load(name);
444  this.ready = true;
445  }
446  catch (Exception e)
447  {
448  throw new DynVarException(e.Message);
449  }
450  }
451 
452  #endregion
453 
454  #region SearchDynVar
455 
456  public bool SearchDynVar(string ns, string classId, string varId)
457  {
458  XmlNode node = document.DocumentElement.SelectSingleNode("namespace[@name = '" + ns + "']");
459  if (node != null)
460  {
461  node = node.SelectSingleNode("class[@name = '" + classId + "']");
462  if (node != null)
463  {
464  node = node.SelectSingleNode("dynvar[@name = '" + varId + "']");
465  if (node != null)
466  return true;
467  }
468  }
469  return false;
470  }
471 
472  public bool SearchDynVar(string ns, string classId, string methodId, string varId)
473  {
474  XmlNode node = document.DocumentElement.SelectSingleNode("namespace[@name = '" + ns + "']");
475  if (node != null)
476  {
477  node = node.SelectSingleNode("class[@name = '" + classId + "']");
478  if (node != null)
479  {
480  node = node.SelectSingleNode("method[@name = '" + methodId + "']");
481  if (node != null)
482  {
483  node = node.SelectSingleNode("dynvar[@name = '" + varId + "']");
484  if (node != null)
485  return true;
486  }
487  }
488  }
489  return false;
490  }
491 
492  public bool SearchDynVar(string ns, string classId, string methodId, List<int> blockList, string varId)
493  {
494  XmlNode node = document.DocumentElement.SelectSingleNode("namespace[@name = '" + ns + "']");
495  if (node != null)
496  {
497  node = node.SelectSingleNode("class[@name = '" + classId + "']");
498  if (node != null)
499  {
500  node = node.SelectSingleNode("method[@name = '" + methodId + "']");
501  if (node != null)
502  {
503  XmlNodeList nodeList = node.SelectNodes("block");
504  for (int i = 0; i < blockList.Count; i++)
505  {
506  if (blockList[i] < nodeList.Count)
507  {
508  node = nodeList.Item(blockList[i]);
509  nodeList = node.SelectNodes("block");
510  }
511  else
512  return false;
513  }
514  node = node.SelectSingleNode("dynvar[@name = '" + varId + "']");
515  if (node != null)
516  return true;
517  }
518  }
519  }
520  return false;
521  }
522 
523  #endregion
524  }
525 }
526 */
static readonly string ClassTag
DynVarManager()
Constructor of DynVarManager
static readonly string NamespaceTag
bool LoadOrCreate(string fileName)
Attemps to load the specified &quot;.dyn&quot; file. If the file does not exist or an exception is thrown...
bool SearchDynVar(string ns, string classId, string methodId, string varId)
bool IsDynamic(VarPath path)
static readonly string DynVarFileExt
File extension
static readonly string MethodTag
New implementation of DynVarManagement: -Ignores &quot;block&quot; elements: not used anymore. -Provides wider interface: -Dynamic method return (dynreturn attribute in method elements) -Interface elements Includes set of old fields, properties and methods for retro-compatibility reasons.
bool Ready
Gets true if the xml document is loaded. Otherwise false.
static readonly string ApplicationTag
bool SearchDynVar(string ns, string classId, string varId)
static readonly string InterfaceTag
static readonly string DynVarTag
The parent class for all exceptions related to DynVars
void SetDynamic(VarPath path)
bool SearchDynVar(string ns, string classOrInterfaceId, string methodId, bool isClassId)
void SetStatic(VarPath path)
bool SearchDynVar(string ns, string classId, string methodId, List< int > blockList, string varId)
void Load(string name)
Loads the xml file associated to the source code file
static readonly string DynReturnAtt
static readonly string NameAtt