/// <summary>Get a <see cref="HostFile"/> object for the host file with the specified <paramref name="hostFilePath"/>.</summary> /// <param name="hostFilePath">The host file path.</param> /// <returns>An initialized <see cref="HostFile"/> object.</returns> private HostFile GetHostfile(string hostFilePath) { List<string> lines = this.DataAccess.GetHostfileContent(hostFilePath); HostFile hostFile = HostFile.CreateHostFile(hostFilePath); HostfileLineByLineNavigator navigator = new HostfileLineByLineNavigator(lines); HostfileEntryCollection collection = new HostfileEntryCollection(hostFile.PropertyChangedCallBack); foreach (HostfileLine line in navigator) { if (line.IsMultiLineComment) { /* multi line comment */ if (collection.GetElementBy(HostfileEntryType.Comment, line.MultiLineCommentText) == null) { collection.Add(new Comment(line.MultiLineCommentText, hostFile.PropertyChangedCallBack)); } } else if (line.IsToggleModeOption) { string value = line.Values.FirstOrDefault(); if (value != null) { /* group toggle-mode */ if (value.Equals("group", StringComparison.OrdinalIgnoreCase)) { hostFile.ExclusiveGroupToggleModeIsEnabled = true; } /* host toggle-mode */ if (value.Equals("host", StringComparison.OrdinalIgnoreCase)) { hostFile.ExclusiveHostToggleModeIsEnabled = true; } } } else if (line.IsGlobalComment) { /* single comment */ collection.Add(new Comment(line.Values.FirstOrDefault(), hostFile.PropertyChangedCallBack)); } else if (line.IsGroupStart) { /* host group */ HostGroup group = new HostGroup(line.GroupName, line.DescriptionText, hostFile.PropertyChangedCallBack); collection.Add(group); } else if (line.IsHost) { /* host */ IHostfileEntry parentItem = line.IsMemberOfHostGroup ? collection.GetElementBy( HostfileEntryType.HostGroup, line.GroupName) : null; HostGroup parentGroup = parentItem != null ? parentItem as HostGroup : null; Host host = new Host(parentGroup, line.Values.FirstOrDefault(), line.DescriptionText, hostFile.PropertyChangedCallBack) { IsActive = line.IsActive }; /* domains */ string domainString = line.Values.LastOrDefault(); if (string.IsNullOrEmpty(domainString) == false) { List<string> domainNames = domainString.Split(' ').Select(domain => domain.Trim()).ToList(); foreach (string domainName in domainNames) { host.Childs.Add(new Domain(host, domainName, hostFile.PropertyChangedCallBack)); } } if (parentGroup != null) { parentGroup.Childs.Add(host); } else { collection.Add(host); } } } /* attach entry collection to host file */ hostFile.Childs = collection; return hostFile; }
/// <summary>Initializes a new instance of the <see cref="HostfileLine"/> class.</summary> /// <param name="lineByLineNavigator">The line by line navigator.</param> /// <param name="lineNumber">The line number.</param> /// <param name="lineText">The line text.</param> public HostfileLine(HostfileLineByLineNavigator lineByLineNavigator, int lineNumber, string lineText) { this.lineByLineNavigator = lineByLineNavigator; this.lineNumber = lineNumber; this.lineText = lineText; }