OSDN Git Service

4c228df45a8d17c24f6fecc6875264864beee62a
[wptscs/wpts.git] / HmLib / Parsers / XmlElement.cs
1 // ================================================================================================
2 // <summary>
3 //      ページのXML要素をあらわすモデルクラスソース</summary>
4 //
5 // <copyright file="XmlElement.cs" company="honeplusのメモ帳">
6 //      Copyright (C) 2011 Honeplus. All rights reserved.</copyright>
7 // <author>
8 //      Honeplus</author>
9 // ================================================================================================
10
11 namespace Honememo.Parsers
12 {
13     using System;
14     using System.Collections.Generic;
15     using System.Text;
16     using System.Xml;
17     using Honememo.Utilities;
18
19     /// <summary>
20     /// ページのXML要素をあらわすモデルクラスです。
21     /// </summary>
22     /// <remarks>解析処理は複雑なため、<see cref="XmlParser"/>として別途実装。</remarks>
23     public class XmlElement : ListElement
24     {
25         #region private変数
26
27         /// <summary>
28         /// タグ名。
29         /// </summary>
30         private string name;
31
32         #endregion
33
34         #region コンストラクタ
35
36         /// <summary>
37         /// 指定されたタグ名・属性・値からXML要素を生成する。
38         /// </summary>
39         /// <param name="name">タグ名。</param>
40         /// <param name="attributes">属性。</param>
41         /// <param name="innerElements">値。</param>
42         /// <param name="parsedString">Parse解析時の元の文字列。</param>
43         public XmlElement(
44             string name,
45             IDictionary<string, string> attributes,
46             ICollection<IElement> innerElements,
47             string parsedString = null)
48         {
49             this.Name = name;
50             this.ParsedString = parsedString;
51             if (attributes != null)
52             {
53                 this.Attributes = new Dictionary<string, string>(attributes);
54             }
55             else
56             {
57                 this.Attributes = new Dictionary<string, string>();
58             }
59
60             if (innerElements != null)
61             {
62                 this.AddRange(innerElements);
63             }
64         }
65
66         /// <summary>
67         /// 指定されたタグ名・値からXML要素を生成する。
68         /// </summary>
69         /// <param name="name">タグ名。</param>
70         /// <param name="value">値。未指定時は<c>null</c>。</param>
71         public XmlElement(string name, string value = null)
72         {
73             this.Name = name;
74             this.Attributes = new Dictionary<string, string>();
75             if (!string.IsNullOrEmpty(value))
76             {
77                 this.Add(new TextElement(value));
78             }
79         }
80
81         #endregion
82         
83         #region プロパティ
84
85         /// <summary>
86         /// タグ名。
87         /// </summary>
88         /// <exception cref="ArgumentNullException">タグ名がnullの場合。</exception>
89         /// <exception cref="ArgumentException">タグ名が空の場合。</exception>
90         public virtual string Name
91         {
92             get
93             {
94                 return this.name;
95             }
96
97             set
98             {
99                 this.name = Validate.NotBlank(value);
100             }
101         }
102
103         /// <summary>
104         /// タグに含まれる属性情報。
105         /// </summary>
106         public virtual IDictionary<string, string> Attributes
107         {
108             get;
109             protected set;
110         }
111
112         #endregion
113
114         #region 内部実装メソッド
115
116         /// <summary>
117         /// このXML要素を表す文字列を返す。
118         /// </summary>
119         /// <returns>このXML要素を表す文字列。</returns>
120         protected override string ToStringImpl()
121         {
122             StringBuilder b = new StringBuilder();
123             XmlWriterSettings s = new XmlWriterSettings();
124             s.CheckCharacters = false;
125             s.ConformanceLevel = ConformanceLevel.Fragment;
126             using (XmlWriter w = XmlWriter.Create(b, s))
127             {
128                 w.WriteStartElement(this.Name);
129                 foreach (KeyValuePair<string, string> attr in this.Attributes)
130                 {
131                     w.WriteAttributeString(attr.Key, attr.Value);
132                 }
133
134                 foreach (IElement element in this)
135                 {
136                     // エンコードする/しないは中身の責任として、ここではエンコードしない
137                     // ※ エンコードするテキストを用意したい場合はXmlTextElementを使うなど
138                     w.WriteRaw(element.ToString());
139                 }
140
141                 w.WriteEndElement();
142             }
143
144             return b.ToString();
145         }
146
147         #endregion
148     }
149 }