protected ProfileItem(ProfileSection owner, int lineNumber)
		{
			if(owner == null)
				throw new ArgumentNullException("owner");

			_owner = owner;
			_lineNumber = Math.Max(lineNumber, -1);
		}
Beispiel #2
0
        public static Profile Load(Stream stream, Encoding encoding = null)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            Profile        profile = new Profile(stream is FileStream ? ((FileStream)stream).Name : string.Empty);
            string         text, content;
            ProfileSection section = null;

            using (var reader = new StreamReader(stream, encoding ?? Encoding.UTF8))
            {
                int lineNumber = 0;

                while ((text = reader.ReadLine()) != null)
                {
                    //解析读取到的行文本
                    switch (ParseLine(text, out content))
                    {
                    case LineType.Section:
                        var parts    = content.Split(' ', '\t');
                        var sections = profile.Sections;

                        foreach (string part in parts)
                        {
                            if (!sections.TryGet(part, out section))
                            {
                                section = sections.Add(part, lineNumber);
                            }

                            sections = section.Sections;
                        }

                        break;

                    case LineType.Entry:
                        //if(section == null)
                        //	throw new ProfileException("Invalid format of the profile.");

                        var index = content.IndexOf('=');

                        if (section == null)
                        {
                            if (index < 0)
                            {
                                profile.Items.Add(new ProfileEntry(lineNumber, content));
                            }
                            else
                            {
                                profile.Items.Add(new ProfileEntry(lineNumber, content.Substring(0, index), content.Substring(index + 1)));
                            }
                        }
                        else
                        {
                            if (index < 0)
                            {
                                section.Entries.Add(lineNumber, content);
                            }
                            else
                            {
                                section.Entries.Add(lineNumber, content.Substring(0, index), content.Substring(index + 1));
                            }
                        }

                        break;

                    case LineType.Comment:
                        if (section == null)
                        {
                            profile._items.Add(new ProfileComment(content, lineNumber));
                        }
                        else
                        {
                            section.Items.Add(new ProfileComment(content, lineNumber));
                        }
                        break;
                    }

                    //递增行号
                    lineNumber++;
                }
            }

            return(profile);
        }
		private static void WriteSection(TextWriter writer, ProfileSection section)
		{
			if(section == null)
				return;

			var sections = new List<ProfileSection>();

			if(section.Entries.Count > 0 || section.Comments.Count > 0)
			{
				writer.WriteLine();
				writer.WriteLine("[{0}]", section.FullName);
			}

			foreach(var item in section.Items)
			{
				switch(item.ItemType)
				{
					case ProfileItemType.Section:
						sections.Add((ProfileSection)item);
						break;
					case ProfileItemType.Entry:
						var entry = (ProfileEntry)item;

						if(string.IsNullOrWhiteSpace(entry.Value))
							writer.WriteLine(entry.Name);
						else
							writer.WriteLine(entry.Name + "=" + entry.Value);

						break;
					case ProfileItemType.Comment:
						foreach(var line in ((ProfileComment)item).Lines)
						{
							writer.WriteLine("#" + line);
						}
						break;
				}
			}

			if(sections.Count > 0)
			{
				foreach(var child in sections)
					WriteSection(writer, child);
			}
		}
		private void UpdateEntries(ProfileSection section, object value)
		{
			if(section == null || value == null)
				return;

			var entries = value as IEnumerable<ProfileEntry>;

			if(entries != null)
			{
				foreach(var entry in entries)
				{
					section.SetEntryValue(entry.Name, entry.Value);
				}

				return;
			}

			var dictionary = value as IDictionary;

			if(dictionary != null)
			{
				foreach(DictionaryEntry entry in dictionary)
				{
					section.SetEntryValue(
						Zongsoft.Common.Convert.ConvertValue<string>(entry.Key),
						Zongsoft.Common.Convert.ConvertValue<string>(entry.Value));
				}

				return;
			}

			if(Zongsoft.Common.TypeExtension.IsAssignableFrom(typeof(IDictionary<,>), value.GetType()))
			{
				foreach(var entry in (IEnumerable)value)
				{
					var entryKey = entry.GetType().GetProperty("Key", BindingFlags.Public | BindingFlags.Instance).GetValue(entry);
					var entryValue = entry.GetType().GetProperty("Value", BindingFlags.Public | BindingFlags.Instance).GetValue(entry);

					section.SetEntryValue(
						Zongsoft.Common.Convert.ConvertValue<string>(entryKey),
						Zongsoft.Common.Convert.ConvertValue<string>(entryValue));
				}
			}
		}
		private bool ParsePath(string path, bool createSectionOnNotExists, out ProfileSection section, out string name, out bool isSectionPath)
		{
			section = null;
			name = null;
			isSectionPath = false;

			if(string.IsNullOrWhiteSpace(path))
				throw new ArgumentNullException("path");

			var parts = path.Split('/');
			int index = -1;

			for(int i = parts.Length - 1; i >= 0; i--)
			{
				if(!string.IsNullOrWhiteSpace(parts[i]))
				{
					index = i;
					break;
				}
			}

			if(index < 0)
				return false;

			name = parts[index];
			isSectionPath = string.IsNullOrWhiteSpace(parts[parts.Length - 1]);

			var sections = this.Sections;

			for(int i = 0; i < index; i++)
			{
				if(string.IsNullOrWhiteSpace(parts[i]))
					continue;

				section = sections[parts[i]];

				if(section == null)
				{
					if(!createSectionOnNotExists)
						return false;

					section = sections.Add(parts[i]);
				}

				sections = section.Sections;
			}

			return true;
		}