/// <summary> /// This method helps recursivly retrieve propeties. /// </summary> /// <param name="o"></param> /// <param name="name"></param> /// <returns></returns> ObjectItem Get_Value(object o, string name) { ObjectItem result; string left; int index; string parseError = "Parser error: the command line can't be evaluated!"; result = new ObjectItem(null, null); if (/*o == null ||*/ name == null || name == string.Empty) { return(result); } index = name.IndexOf('.'); index = (index > 0) ? index : name.Length; left = name.Substring(0, index); result = GetProperty(o, left); if (result.Error != null && result.Error.Length > 0) { result.Error = parseError; } if (left != name) { if (result.Value == null) { result.Error = parseError; } else { result = Get_Value(result.Value, name.Substring(index + 1)); } } return(result); }
/// <summary> /// Recursively parse the command line content /// </summary> /// <param name="commandLine"></param> void DoParse(string commandLine) { string strLeft; string strRight; object left; string str = commandLine.Replace(" ", " "); str = str.Trim(); //Asignment if (str.Contains("=")) { string[] strs = str.Split(new char[] { '=' }); if (strs.Length != 2 || strs[0].Length == 0 || str[1] == 0) { _result.Error = "syntax error: Don't know how to do Assignment!"; return; } strLeft = strRight = string.Empty; strLeft = strs[0]; strRight = strs[1]; strLeft = strLeft.Trim(); strRight = strRight.Trim(); _result = new CommandLine(strRight, _table).Result; //Set a value to a variable in the Table. if (_table.Contains(strLeft)) { _table.Remove(strLeft); _table.Add(strLeft, Result.Value); } //Assign a value to a property. else if (strLeft.Contains(".")) { left = new CommandLine(strLeft.Substring(0, strLeft.LastIndexOf(".")), _table).Result.Value; if (left != null) { ReflectionUtils.SetProperty(left, strLeft.Substring(strLeft.LastIndexOf(".") + 1), Result.Value); } else { Result.Error = "Failed: can't set value to " + strLeft + "!"; } } //Make a declaration else { _table.Add(strLeft, Result.Value); } } //Invoke a method, Creating a object. else if (str.EndsWith(")")) { if (str.StartsWith("new ")) { str = str.Substring(4).Trim(); } left = null; strRight = strLeft = str.Substring(0, str.IndexOf("(")); //If there is a dot, invoke a method from an object if (strLeft.LastIndexOf(".") >= 0) { strLeft = strLeft.Substring(0, strLeft.LastIndexOf(".")); left = new CommandLine(strLeft, _table).Result.Value; } //When left is null, invoke a constructor, otherwise invoke a method. if (left != null) { //Get the command line start from the method name //this will get the argument list. str = str.Substring(strRight.LastIndexOf(".") + 1); } Result.Value = InvokMethod(str, left); } //Retrive data else { if (_table.Contains(str)) { _result.Value = _table[str]; return; } left = null; strRight = str; if (str.Contains(".")) { strLeft = str.Substring(0, str.IndexOf(".")); left = _table[strLeft]; //If an object is found in a Table, the property name is needed in order to retrieve the value. if (left != null && str.Length - (strLeft.Length + 1) > 0) { strRight = str.Substring(str.IndexOf(".") + 1, str.Length - (strLeft.Length + 1)); } } _result = Get_Value(left, strRight); if (Result.Error != null && Result.Error.Length > 0) { //If the expression is just for a static member of a class _result = GetProperty(null, strRight); } } }
/// <summary> /// constructor /// </summary> /// <param name="commandLine"></param> /// <param name="table"></param> public CommandLine(string commandLine, Hashtable table) { _table = table; _result = new ObjectItem(null, null); DoParse(commandLine); }
/// <summary> /// This method helps recursivly retrieve propeties. /// </summary> /// <param name="o"></param> /// <param name="name"></param> /// <returns></returns> ObjectItem Get_Value(object o, string name) { ObjectItem result; string left; int index; string parseError = "Parser error: the command line can't be evaluated!"; result = new ObjectItem(null, null); if (/*o == null ||*/ name == null || name == string.Empty) { return result; } index = name.IndexOf('.'); index = (index > 0) ? index : name.Length; left = name.Substring(0, index); result = GetProperty(o, left); if (result.Error != null && result.Error.Length > 0) { result.Error = parseError; } if (left != name) { if (result.Value == null) { result.Error = parseError; } else { result = Get_Value(result.Value, name.Substring(index + 1)); } } return result; }