Welcome to mirror list, hosted at ThFree Co, Russian Federation.

XmlDeclaration.cs « System.Xml « System.XML « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c34df121227c358b30325be58be625753af47d47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//
// System.Xml.XmlDeclaration
//
// Author:
//	Duncan Mak  (duncan@ximian.com)
//
// (C) Ximian, Inc.

using System;
using System.Xml;
using System.Text.RegularExpressions;

namespace System.Xml
{
	public class XmlDeclaration : XmlLinkedNode
	{
		string encoding = "UTF-8"; // defaults to UTF-8
		string standalone;
		string version;

		protected internal XmlDeclaration (string version, string encoding,
						   string standalone, XmlDocument doc)
			: base (doc)
		{
			if (encoding == null)
				encoding = "";

			if (standalone == null)
				standalone = "";

			this.version = version;
			this.encoding = encoding;
			this.standalone = standalone;
		}

		public string Encoding  {
			get { return encoding; } 
			set { encoding = (value == null) ? String.Empty : value; }
		}

		public override string InnerText {
			get { return Value; }
			set { ParseInput (value); }
		}
		
		public override string LocalName {
			get { return "xml"; }
		}

		public override string Name {
			get { return "xml"; }
		}

		public override XmlNodeType NodeType {
			get { return XmlNodeType.XmlDeclaration; }
		}

		public string Standalone {
			get { return standalone; }
			set {
				if(value != null)
				{
					if (value.ToUpper() == "YES")
						standalone = "yes";
					if (value.ToUpper() == "NO")
						standalone = "no";
				}
				else
					standalone = String.Empty;
			}
		}

		public override string Value {
			get {
				string formatEncoding = "";
				string formatStandalone = "";

				if (encoding != String.Empty)
					formatEncoding = String.Format (" encoding=\"{0}\"", encoding);

				if (standalone != String.Empty)
					formatStandalone = String.Format (" standalone=\"{0}\"", standalone);

				return String.Format ("version=\"{0}\"{1}{2}", Version, formatEncoding, formatStandalone);
			}
			set { ParseInput (value); }
		}

		public string Version {
			get { return version; }
		}

		public override XmlNode CloneNode (bool deep)
		{
			return new XmlDeclaration (Version, Encoding, standalone, OwnerDocument);
		}

		public override void WriteContentTo (XmlWriter w) {}

		public override void WriteTo (XmlWriter w)
		{
			// This doesn't seem to match up very well with w.WriteStartDocument()
			// so writing out custom here.
			w.WriteRaw (String.Format ("<?xml {0}?>", Value));
		}

		void ParseInput (string input)
		{
//			Encoding = input.Split (new char [] { ' ' }) [1].Split (new char [] { '=' }) [1];
//			Standalone = input.Split (new char [] { ' ' }) [2].Split (new char [] { '=' }) [1];
			Match m = XmlDeclRegex.Match(input);
			if(!m.Success)
				throw new XmlException("illegal XML declaration format.");
//			Version = m.Result("${ver}");
			Encoding = m.Result("${enc}");
			Standalone = m.Result("${sta}");
		}

		// This regular expression matches XMLDecl of XML specification BNF[23]
		static Regex xmlDeclRegex;
		Regex XmlDeclRegex 
		{
			get 
			{
				if(xmlDeclRegex == null) xmlDeclRegex = new Regex(allMatch, RegexOptions.Compiled);
				return xmlDeclRegex;
			}
		}

		// This code makes some loss, but you may understand a bit easily.
		const string verMatch = "\\s*version\\s*=\\s*(\\'(?<ver>.*?)\\'|\\\"(?<ver>.*?)\")";
		const string encMatch = "\\s*encoding\\s*=\\s*(\\'(?<enc>.*?)\\'|\\\"(?<enc>.*?)\")";
		const string staMatch = "\\s*standalone\\s*=\\s*(\\'(?<sta>.*?)\\'|\\\"(?<sta>.*?)\")";
		const string allMatch = verMatch + "(" + encMatch + ")?(" + staMatch + ")?";
	}
}