示例#1
0
        public static TemplateList ReadFile(string templateListFilePath, FilterProcessor contentsFilter)
        {
            if (string.IsNullOrWhiteSpace(templateListFilePath))
            {
                return(null);
            }

            TemplateList listData = new TemplateList();

            try
            {
                using (FileStream fs = new FileStream(templateListFilePath, FileMode.Open, FileAccess.Read))
                    using (TextReader rd = new StreamReader(fs))
                    {
                        WorkSet currentTargetSet = listData.mainWorkSet;

                        string lineBuffer = rd.ReadLine();
                        while (lineBuffer != null)
                        {
                            if (string.IsNullOrWhiteSpace(lineBuffer) || lineBuffer.StartsWith('#'))
                            {
                                lineBuffer = rd.ReadLine();
                                continue;
                            }
                            lineBuffer = contentsFilter.Filter(lineBuffer).Trim();

                            string[] lineTokens = lineBuffer.Split(':', StringSplitOptions.TrimEntries);
                            if (lineTokens.Length > 0)
                            {
                                if (lineTokens[0].StartsWith('['))
                                {
                                    // Section Descriptor
                                    string sectionName = _PopSectionName(lineTokens[0]);
                                    if (sectionName != null)
                                    {
                                        sectionName = sectionName.Trim();
                                        if (sectionName == string.Empty)
                                        {
                                            currentTargetSet = listData.mainWorkSet;
                                        }
                                        else
                                        {
                                            if (!listData.conditionalWorkSets.ContainsKey(sectionName))
                                            {
                                                listData.conditionalWorkSets.Add(sectionName, new WorkSet());
                                            }
                                            currentTargetSet = listData.conditionalWorkSets[sectionName];
                                        }
                                    }
                                }
                                else
                                {
                                    switch (lineTokens[0])
                                    {
                                    case "C":   // Copy
                                    {
                                        if (lineTokens.Length > 1)
                                        {
                                            PathData srcPathData    = PathData.ParsePathString(lineTokens[1]);
                                            PathData targetPathData = null;
                                            if (lineTokens.Length > 2)
                                            {
                                                targetPathData = PathData.ParsePathString(lineTokens[2]);
                                            }
                                            else
                                            {
                                                targetPathData = srcPathData;
                                            }

                                            if (lineTokens[1].EndsWith("/*"))
                                            {
                                                currentTargetSet.copyAllEntries.Add(new Tuple <PathData, PathData>(srcPathData, targetPathData));
                                            }
                                            else
                                            {
                                                currentTargetSet.copyEntries.Add(new Tuple <PathData, PathData>(srcPathData, targetPathData));
                                            }
                                        }
                                    }
                                    break;

                                    case "F":   // Filter
                                    {
                                        if (lineTokens.Length > 1)
                                        {
                                            PathData srcPathData    = PathData.ParsePathString(lineTokens[1]);
                                            PathData targetPathData = null;
                                            if (lineTokens.Length > 2)
                                            {
                                                targetPathData = PathData.ParsePathString(lineTokens[2]);
                                            }
                                            else
                                            {
                                                targetPathData = srcPathData;
                                            }
                                            currentTargetSet.filterEntries.Add(new Tuple <PathData, PathData>(srcPathData, targetPathData));
                                        }
                                    }
                                    break;
                                    }
                                }
                            }

                            lineBuffer = rd.ReadLine();
                        }
                    }
            }
            catch
            {
                return(null);
            }

            return(listData);
        }