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

XmlNamespaceManager.cs « System.Xml « System.XML « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 56dcaf5d88ca1535fd3d83ac29a7d37bde5fec6c (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//
// XmlNamespaceManager.cs
//
// Author:
//   Jason Diamond (jason@injektilo.org)
//
// (C) 2001 Jason Diamond  http://injektilo.org/
//

using System.Collections;

namespace System.Xml
{
	public class XmlNamespaceManager : IEnumerable
	{
		#region Fields

		private XmlNameTable nameTable;
		private NamespaceScope currentScope;

		#endregion

		#region Constructor

		public XmlNamespaceManager (XmlNameTable nameTable)
		{
			this.nameTable = nameTable;

			nameTable.Add ("xmlns");
			nameTable.Add ("xml");
			nameTable.Add (String.Empty);
			nameTable.Add ("http://www.w3.org/2000/xmlns/");
			nameTable.Add ("http://www.w3.org/XML/1998/namespace");

			PushScope ();
		}

		#endregion

		#region Properties

		public virtual string DefaultNamespace {
			get { return LookupNamespace (String.Empty); }
		}

		public XmlNameTable NameTable {
			get { return nameTable; }
		}

		#endregion

		#region Methods

		public virtual void AddNamespace (string prefix, string uri)
		{
			if (prefix == null)
				throw new ArgumentNullException ("prefix", "Value cannot be null.");

			if (uri == null)
				throw new ArgumentNullException ("uri", "Value cannot be null.");

			if (prefix.Length > 2 && prefix.Substring (0, 3).ToLower () == "xml")
				throw new ArgumentException ("Prefixes beginning with \"xml\" (regardless of whether the characters are uppercase, lowercase, or some combination thereof) are reserved for use by XML.", "prefix");

			if (currentScope.Namespaces == null)
				currentScope.Namespaces = new Hashtable ();

			currentScope.Namespaces.Add (nameTable.Add (prefix), nameTable.Add (uri));
		}

		[MonoTODO]
		public virtual IEnumerator GetEnumerator ()
		{
			throw new NotImplementedException ();
		}

		public virtual bool HasNamespace (string prefix)
		{
			return currentScope != null && currentScope.Namespaces != null && currentScope.Namespaces.Contains (prefix);
		}

		public virtual string LookupNamespace (string prefix)
		{
			NamespaceScope scope = currentScope;

			while (scope != null) {
				if (scope.Namespaces != null && scope.Namespaces.Contains (prefix))
					return scope.Namespaces[prefix] as string;
				scope = scope.Next;
			}

			switch (prefix) {
			case "xmlns":
				return nameTable.Get ("http://www.w3.org/2000/xmlns/");
			case "xml":
				return nameTable.Get ("http://www.w3.org/XML/1998/namespace");
			case "":
				return nameTable.Get (String.Empty);
			}

			return null;
		}

		public virtual string LookupPrefix (string uri)
		{
			if (uri == null)
				return null;

			NamespaceScope scope = currentScope;

			while (scope != null) 
			{
				if (scope.Namespaces != null && scope.Namespaces.ContainsValue (uri)) {
					foreach (DictionaryEntry entry in scope.Namespaces) {
						if (entry.Value.ToString() == uri)
							return nameTable.Get (entry.Key as string) as string;
					}
				}

				scope = scope.Next;
			}

			return String.Empty;
		}

		public virtual bool PopScope ()
		{
			if (currentScope != null)
				currentScope = currentScope.Next;

			return currentScope != null;
		}

		public virtual void PushScope ()
		{
			NamespaceScope newScope = new NamespaceScope ();
			newScope.Next = currentScope;
			currentScope = newScope;
		}

		[MonoTODO]
		public virtual void RemoveNamespace (string prefix, string uri)
		{
			throw new NotImplementedException ();
		}

		#endregion
	}

	internal class NamespaceScope
	{
		internal NamespaceScope Next;
		internal Hashtable Namespaces;
	}
}