コード例 #1
0
ファイル: PermissionData.cs プロジェクト: ic4f/oldcode
        private void loadPermissionCats()
        {
            FileStream         fs = new FileStream(ConfigurationHelper.PermissionCategoryFile, FileMode.Open);
            BinaryReader       r  = new BinaryReader(fs);
            int                id;
            int                parentId;
            string             name;
            PermissionCategory cat;

            while (fs.Position < fs.Length)
            {
                id       = r.ReadInt32();
                parentId = r.ReadInt32();
                name     = r.ReadString();
                cat      = new PermissionCategory(id, parentId, name);
                cats.Add(id, cat);
                if (parentId == NO_PARENT)
                {
                    catArrays.Add(id, new ArrayList());
                    topCats.Add(cat);
                }
                else
                {
                    ArrayList catList = (ArrayList)catArrays[parentId];
                    catList.Add(cat);
                    permArrays.Add(id, new ArrayList());
                }
            }
            r.Close();
            fs.Close();
        }
コード例 #2
0
        private void loadPermissions()
        {
            FileStream   fs = File.OpenRead(ConfigurationHelper.PermissionSourceFile);
            StreamReader r  = new StreamReader(fs, System.Text.Encoding.ASCII);

            string patternCat  = @"\s*(?<id>c\d+)\s+(?<parentid>c-?\d+)\s+""(?<name>[^""]+)""";
            string patternPerm = @"\s*(?<id>\d+)\s+(?<parentid>c\d+)\s+(?<type>(and)|(or))\s+""(?<name>[^""]+)""\s+""(?<code>\w+)""";

            Regex regexPerm = new Regex(patternPerm, RegexOptions.IgnoreCase | RegexOptions.Compiled);
            Regex regexCat  = new Regex(patternCat, RegexOptions.IgnoreCase | RegexOptions.Compiled);

            MatchCollection mc;

            string line;
            int    id;
            int    parentId;
            string type;
            string name;
            string code;

            b.Permission         perm;
            b.PermissionCategory cat;
            b.PermissionCategory parentCat;
            int counter = 0;

            while ((line = r.ReadLine()) != null)
            {
                counter++;
                if (line != "" && !line.StartsWith("//"))
                {
                    if (line.StartsWith("c"))
                    {
                        mc = regexCat.Matches(line);
                        if (mc.Count != 1)
                        {
                            throw new Exception("Invalid format in permissions file -- line " + counter);
                        }

                        id       = Convert.ToInt32(mc[0].Groups["id"].ToString().Substring(1));
                        parentId = Convert.ToInt32(mc[0].Groups["parentid"].ToString().Substring(1));
                        name     = mc[0].Groups["name"].ToString();

                        if (parentId != b.PermissionData.NO_PARENT)
                        {
                            if (!catsHash.ContainsKey(parentId))
                            {
                                throw new Exception("Nonexistant category as parentId -- line " + counter);
                            }

                            parentCat = (b.PermissionCategory)catsHash[parentId];
                            if (parentCat.ParentId != b.PermissionData.NO_PARENT)
                            {
                                throw new Exception("A category's parent must be a first-level category -- line " + counter);
                            }
                        }
                        cat = new b.PermissionCategory(id, parentId, name);
                        catsHash.Add(id, cat);
                        orderedCats.Add(cat);
                    }
                    else
                    {
                        mc = regexPerm.Matches(line);
                        if (mc.Count != 1)
                        {
                            throw new Exception("Invalid format in permissions file -- line " + counter);
                        }

                        id       = Convert.ToInt32(mc[0].Groups["id"].ToString());
                        parentId = Convert.ToInt32(mc[0].Groups["parentid"].ToString().Substring(1));
                        type     = mc[0].Groups["type"].ToString();
                        name     = mc[0].Groups["name"].ToString();
                        code     = mc[0].Groups["code"].ToString();

                        if (!catsHash.ContainsKey(parentId))
                        {
                            throw new Exception("Nonexistant category as parentId -- line " + counter);
                        }

                        parentCat = (b.PermissionCategory)catsHash[parentId];
                        if (parentCat.ParentId == b.PermissionData.NO_PARENT)
                        {
                            throw new Exception("A permission's parent must be a second-level category -- line " + counter);
                        }

                        perm = new b.Permission(id, parentId, type, name, code);
                        permsHash.Add(id, perm);
                        orderedPerms.Add(perm);
                    }
                }
            }
            r.Close();
            fs.Close();
        }