IdentifierExtractor Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;
namespace IdentifierExtractor
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> identifierList = new List<string>();
            //Extract the raw identifiers from config files
            string sourceDirectory = @"C:\Users\saikatd\SSTE";
            string outputFileName = "IdentifierList.txt";
            List<string> fileList = GetFileList(sourceDirectory, @"*.config", 0);
            fileList.AddRange(GetFileList(sourceDirectory, @"*.utf8", 0));
            for (int i = 0; i < fileList.Count; i++)
            {
                StreamReader sr = new StreamReader(fileList[i]);
                for (string str = sr.ReadLine(); str != null; str = sr.ReadLine())
                {
                    if (GetLeftIdentifier(str).EndsWith("_identifier"))
                    {
                        identifierList.Add(str);
                    }
                }
                sr.Close();
            }
            //Resolve the cascading dependency of identifiers
            for (int i = 0; i < identifierList.Count; i++)
            {
                Console.WriteLine(i.ToString() + "/" + identifierList.Count);
                while (identifierList[i].Contains(@"@"))
                {
                    string nestedIdentifier = "";
                    //check for expressions like @{attrBoundary}@{nonAttrBoundary}
                    if (identifierList[i].Contains(@"@{"))
                    {
                        //extract the nested attribute
                        int startPos = identifierList[i].IndexOf(@"@{") + 2;
                        int endPos = identifierList[i].IndexOf(@"}", startPos);
                        nestedIdentifier = identifierList[i].Substring(startPos, endPos - startPos);
                        List<string> declarationList = new List<string>();
                        declarationList = GetDeclarations(nestedIdentifier, fileList);
                        string tempStr = identifierList[i].ToString();
                        for (int k = 0; k < declarationList.Count; k++)
                        {
                            if (k == 0)
                            {
                                if (declarationList[k].StartsWith(@"@") && !declarationList[k].StartsWith(@"@{"))
                                {
                                    identifierList[i] = identifierList[i].Replace(nestedIdentifier, declarationList[k].TrimStart('@'));
                                }
                                else
                                {
                                    identifierList[i] = identifierList[i].Replace(@"@{" + nestedIdentifier + @"}", declarationList[k]);
                                }
                            }
                            else
                            {
                                if (declarationList[k].StartsWith(@"@") && !declarationList[k].StartsWith(@"@{"))
                                {
                                    identifierList.Insert(i + k, tempStr.Replace(nestedIdentifier, declarationList[k].TrimStart('@')));
                                }
                                else
                                {
                                    identifierList.Insert(i + k, tempStr.Replace(@"@{" + nestedIdentifier + @"}", declarationList[k]));
                                }   
                            }
                        }
                    }
                    //check for expressions like @PrimeLandingPagePageFrameworkFallback_identifier
                    else
                    {
                        //extract the nested attribute                      
                        nestedIdentifier = GetRightIdentifier(identifierList[i]).TrimStart('@');
                        List<string> declarationList = new List<string>();
                        declarationList = GetDeclarations(nestedIdentifier, fileList);
                        string tempStr = identifierList[i].ToString();
                        for (int k = 0; k < declarationList.Count; k++)
                        {
                            if (k == 0)
                            {
                                identifierList[i] = identifierList[i].Replace(@"@" + nestedIdentifier, declarationList[k]);
                            }
                            else
                            {
                                identifierList.Insert(i + k, tempStr.Replace(@"@" + nestedIdentifier, declarationList[k]));
                            }
                        }
                    }
                }
            }
            StreamWriter sw = new StreamWriter(sourceDirectory + @"\" + outputFileName, true);
            for (int i = 0; i < identifierList.Count; i++)
            {
                sw.WriteLine(identifierList[i]);
            }
            sw.Close();
        }
        public static List<string> GetDeclarations(string tag, List<string> fileList)
        {
            List<string> declarationList = new List<string>();
            for (int i = 0; i < fileList.Count; i++)
            {
                StreamReader sr = new StreamReader(fileList[i]);
                for (string str = sr.ReadLine(); str != null; str = sr.ReadLine())
                {
                    if (GetLeftIdentifier(str).ToString() == tag)
                    {
                        declarationList.Add(GetRightIdentifier(str));
                    }
                }
                sr.Close();
            }
            return declarationList;
        }
        
        public static List<string> GetFileList(string sourceDirectory, string fileExtension, int sizeInBytes)
        {
            List<string> fileList = new List<string>();
            DirectoryInfo di = new DirectoryInfo(sourceDirectory);
            FileInfo[] fiArr = di.GetFiles(fileExtension, SearchOption.AllDirectories);
            foreach (FileInfo file in fiArr)
            {
                if (file.Length > sizeInBytes)
                {
                    fileList.Add(file.FullName);
                }
            }
            return fileList;
        }
        public static string GetLeftIdentifier(string text)
        {
            string leftIdentifier = "";
            Regex reg = new Regex("=");
            string[] stringArray = reg.Split(text, 2);
            if (stringArray.Length < 2 || text.StartsWith("#"))
            {
                return text;
            }
            leftIdentifier = stringArray[0];
            leftIdentifier = leftIdentifier.Trim();
            return leftIdentifier;
        }
        public static string GetRightIdentifier(string text)
        {
            string rightIdentifier = "";
            Regex reg = new Regex("=");
            string[] stringArray = reg.Split(text, 2);
            if (stringArray.Length < 2 || text.StartsWith("#"))
            {
                return text;
            }
            rightIdentifier = stringArray[1];
            rightIdentifier = rightIdentifier.Trim();
            return rightIdentifier;
        }
    }
}
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s