/// <summary>
        ///  Format post data
        /// </summary>
        /// <param name="model"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public string postFormat(RequestBaseModel model, params string[] param)
        {
            string data = string.Empty;

            if (model == null)
            {
                return(data);
            }

            if (model is PostModel)
            {
                string formatString = ((PostModel)model).FormatString;
                return(string.Format(formatString, param));

                //if (formula.Length > 2) return data;
                //data = formula[0] + param;

                //if (formula.Length == 2) data += formula[1];
            }
            return(data);
        }
Exemplo n.º 2
0
        /// <summary>
        ///  Read test only
        /// </summary>
        public static void initDictionary(string resName, ref Dictionary <string, SiteModel> modelDictionary, ref List <string> menuList, ref List <string> postList)
        {
            Dictionary <string, SiteModel> cacheModelDict = new Dictionary <string, SiteModel>();
            List <string> cachePostList = new List <string>();
            List <string> cacheMenuList = new List <string>();

            if (majorList == null)
            {
                updateMajorList(saveDir + "/MajorList.txt");
            }
            if (courseProperties == null)
            {
                updateClassProperties(saveDir + "/CourseProperties.txt");
            }

            #region Read xml and initalize dictionary
            try
            {
                System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
                using (Stream stream = asm.GetManifestResourceStream(resName))
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(stream);
                    XmlNodeList rootNodeList = doc.SelectSingleNode("Requestitem").ChildNodes;
                    foreach (XmlNode childNode in rootNodeList)
                    {
                        //Get basic attribute of site name and encoding name
                        XmlAttribute siteNameAttribute = childNode.Attributes["siteName"];
                        XmlAttribute encodingAttribute = childNode.Attributes["encodingName"];
                        string       siteName          = siteNameAttribute == null ? "Common" : siteNameAttribute.Value;
                        string       encodingName      = encodingAttribute == null ? "utf-8" : encodingAttribute.Value;

                        //Initilize request dictionary
                        Dictionary <string, RequestBaseModel> requestDictionary = new Dictionary <string, RequestBaseModel>();
                        List <string> headerTmp;

                        foreach (XmlNode item in childNode.ChildNodes)
                        {
                            if (item.Name == "#comment")
                            {
                                continue;
                            }
                            string keyTmp = string.Empty, ruleTmp = string.Empty, urlTmp = string.Empty,
                                   handHeader = string.Empty, getHandRule = string.Empty,
                                   formatString = string.Empty, nextKey = string.Empty, nomenu = string.Empty, itemName = item.Name;

                            System.Diagnostics.Debug.WriteLine(siteName + " -->" + itemName);
                            ////Get and set basic attributes value
                            foreach (XmlAttribute attribute in item.Attributes)
                            {
                                string value = attribute.Value;
                                switch (attribute.Name)
                                {
                                case "key": { keyTmp = value; } break;

                                case "parsingRules": { ruleTmp = value; } break;

                                case "url": { urlTmp = value; } break;

                                case "nextKey": { nextKey = value; } break;

                                case "nomenu": { nomenu = value; } break;

                                case "formatString": { formatString = value; } break;

                                case "handHeader": { handHeader = value; } break;

                                case "getHandRule": { getHandRule = value; } break;
                                }
                            }

                            //initialize helper array and list
                            //Get child node value
                            //Assignment postparamArray and headerArray
                            string[] headers = null;
                            headerTmp = new List <string>();
                            foreach (XmlNode childitem in item.ChildNodes)
                            {
                                if (childitem.Name == "header")
                                {
                                    headerTmp.Add(childitem.Attributes["data"].Value);
                                }
                            }
                            if (headerTmp.Count > 0)
                            {
                                headers = headerTmp.ToArray();
                            }

                            //Create model with item name
                            if (itemName == "getitem" || itemName == "checkitem")
                            {
                                RequestBaseModel rbm = new RequestBaseModel(urlTmp, ruleTmp, headers);
                                requestDictionary.Add(keyTmp, rbm);
                            }
                            else if (itemName == "postitem")
                            {
                                //If itemName is postitem
                                string optionalValue = string.Empty;
                                if (item.Attributes["optional"] != null)
                                {
                                    optionalValue = item.Attributes["optional"].Value;
                                }

                                BindingList <OptionalItem <string, string>[]> selectorsTmp;
                                linkMuliOptional(optionalValue, '|', out selectorsTmp);

                                //Add item to postlist
                                PostModel pm = new PostModel(urlTmp, ruleTmp, headers, selectorsTmp, formatString, handHeader, getHandRule);
                                pm.NextKey = nextKey;
                                requestDictionary.Add(keyTmp, pm);
                                cachePostList.Add(keyTmp);
                            }
                            //Add item to allItem collecion
                            if (!string.IsNullOrEmpty(nomenu))
                            {
                                nomenu = string.Empty;
                            }
                            else
                            {
                                cacheMenuList.Add(keyTmp);
                            }
                        }
                        RequestBaseModel loginModel = requestDictionary.ContainsKey("Login") ? requestDictionary["Login"] : null;
                        string           logoutUrl  = requestDictionary.ContainsKey("Logout") ? requestDictionary["Logout"].Url : string.Empty;
                        Encoding         encoding   = Encoding.GetEncoding(encodingName);

                        //Initilize siteModel
                        SiteModel siteModel = new SiteModel()
                        {
                            SiteName          = siteName,
                            LoginModel        = loginModel,
                            LogoutUrl         = logoutUrl,
                            RequestEncoding   = encoding,
                            RequestDictionary = requestDictionary
                        };
                        //Add model to dictionary
                        cacheModelDict.Add(siteName, siteModel);
                    }
                }
                modelDictionary = cacheModelDict;
                postList        = cachePostList;
                menuList        = cacheMenuList;
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Initialize fail with error (ModelLoader.cs, Line:213)\n " + e.Message + e.StackTrace);
            }
            #endregion
        }