07 March 2011

Place holder in c# .net - Parser

look on it,

Lets define a place holder as follow :  identified string with some meaning.

Identification :

          {placeHolder:property}

e.g.

          Data Source={connection_string:server}

place holder represented by concrete classes, produce by factory, parsed by engine.

lets look on the engine,




public string Parse(string command)
        {
            char[] commandChArray = command.ToCharArray(0, command.Length);


            int index = 0;


            StringBuilder outputMainCommand = new StringBuilder();


            // each place holder command may consist of place holder (aka command name) and property    
            // (aka command value).
            StringBuilder executeCommandName = new StringBuilder();
            StringBuilder executeCommandData = new StringBuilder();


         
            bool isCommandContext = false;
            bool isCommandData = false;
            bool isCommandName = false;


            int closeBracket = 0;
            while (index < commandChArray.Count())
            {
                switch (commandChArray[index])
                {
                    // encounter with {
                    case START_COMMAND_IDENTIFIER:
                        
                        closeBracket++;


                        if (!isCommandContext)
                        {
                            closeBracket = 1;


                            executeCommandName.Clear();
                            executeCommandData.Clear();


                            isCommandContext = true;
                            isCommandName = true;
                            isCommandData = false;
                        }
                        else
                        {
                            if (isCommandName)
                            {
                                executeCommandName.Append(commandChArray[index]);
                            }
                            else if (isCommandData)
                            {
                                executeCommandData.Append(commandChArray[index]);
                            }
                        }
                        break;
                   
                   // encounter with }
                   case END_COMMAND_IDENTIFIER:
                        
                       closeBracket--;


                       if (isCommandContext && closeBracket == 0)
                        {                            
                            try
                            {                                
                                string commandName = string.Empty;


                                if (executeCommandName != null)
                                {
                                    commandName = executeCommandName.ToString();
                                }


                                string commandData = string.Empty;
                                if (executeCommandData != null)
                                {
                                    commandData = executeCommandData.ToString();
                                }


                                if (!string.IsNullOrEmpty(commandName))
                                {
                                    string output = GetPlaceHolderHandler(commandName).FromPlaceHolder(commandData);


                                    outputMainCommand.Append(output);
                                }
                                else
                                    // common
                                {
                                    outputMainCommand.Append(commandChArray[index]);
                                }
                            }
                            finally
                            {
                                executeCommandName.Clear();
                                executeCommandData.Clear();


                                isCommandContext = false;
                                isCommandName = true;
                                isCommandData = false;    
                            }
                        }
                        if (isCommandContext)
                        {
                            if( isCommandName )
                            {
                                executeCommandName.Append(commandChArray[index]);
                            }
                            else if( isCommandData)
                            {
                                executeCommandData.Append(commandChArray[index]);
                            }
                        }
                                             
                        break;


                    case COMMAND_SEPERATOR_IDENTIFIER:


                        if (isCommandContext)
                        {
                            isCommandName = false;


                            if (isCommandData)
                            {
                                executeCommandData.Append(commandChArray[index]);
                            }


                            isCommandData = true;


                        }
                        else
                            // common
                        {
                            outputMainCommand.Append(commandChArray[index]);
                        }


                        break;


                    default:
                        if (isCommandContext)
                        {
                            if( isCommandName )
                            {
                                executeCommandName.Append(commandChArray[index]);
                            }
                            else if( isCommandData)
                            {
                                executeCommandData.Append(commandChArray[index]);
                            }
                        }
                        else
                        {
                            outputMainCommand.Append(commandChArray[index]);
                        }                       
                        break;
                }


                index++;
            }


            return outputMainCommand.ToString();
        }


Parse(string command) input example :  Data Source={connection_string:server}.

How to improve it ?
Add parallel to the parser, split the data to be parsed into command chunks and streaming the command value to the required command place holder, think on command defined from many place holder, all should be parsed.

No comments: