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

XmlSchemaXPath.cs « System.Xml.Schema « System.XML « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 58db106dbc8db98a9903d7077f2ffb6ce9e529dd (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Author: Dwivedi, Ajay kumar
//            Adwiv@Yahoo.com

//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.Xml.Serialization;
using System.ComponentModel;
using System.Xml;
using Mono.Xml;
using Mono.Xml.Schema;

namespace System.Xml.Schema
{
	/// <summary>
	/// Summary description for XmlSchemaXPath.
	/// </summary>
	public class XmlSchemaXPath : XmlSchemaAnnotated
	{
		private string xpath;
		XmlNamespaceManager nsmgr;
		internal bool isSelector;
		XsdIdentityPath [] compiledExpression;

		XsdIdentityPath currentPath;

		public XmlSchemaXPath()
		{
		}
		[DefaultValue("")]
		[System.Xml.Serialization.XmlAttribute("xpath")]
		public string XPath 
		{
			get{ return  xpath; } 
			set{ xpath = value; }
		}

		internal override int Compile(ValidationEventHandler h, XmlSchema schema)
		{
			// If this is already compiled this time, simply skip.
			if (CompilationId == schema.CompilationId)
				return 0;

			if (nsmgr == null) {
				nsmgr = new XmlNamespaceManager (new NameTable ());
				if (Namespaces != null)
					foreach (XmlQualifiedName qname in Namespaces.ToArray ())
						nsmgr.AddNamespace (qname.Name, qname.Namespace);
			}

			currentPath = new XsdIdentityPath ();
			ParseExpression (xpath, h, schema);

			XmlSchemaUtil.CompileID(Id, this, schema.IDCollection, h);
			this.CompilationId = schema.CompilationId;
			return errorCount;
		}

		internal XsdIdentityPath [] CompiledExpression {
			get { return compiledExpression; }
		}

		private void ParseExpression (string xpath, ValidationEventHandler h, XmlSchema schema)
		{
			ArrayList paths = new ArrayList ();
			ParsePath (xpath, 0, paths, h, schema);
			this.compiledExpression = (XsdIdentityPath []) paths.ToArray (typeof (XsdIdentityPath));
		}

		private void ParsePath (string xpath, int pos, ArrayList paths,
			ValidationEventHandler h, XmlSchema schema)
		{
			pos = SkipWhitespace (xpath, pos);
			if (xpath.Length >= pos + 3 && xpath [pos] == '.') {
				int tmp = pos;
				pos++;
				pos = SkipWhitespace (xpath, pos);
				if (xpath.Length > pos + 2 && xpath.IndexOf ("//", pos, 2) == pos) {
					currentPath.Descendants = true;
					pos += 2;
				}
				else
					pos = tmp;	// revert
			}
			ArrayList al = new ArrayList ();
			ParseStep (xpath, pos, al, paths, h, schema);
		}

		private void ParseStep (string xpath, int pos, ArrayList steps,
			ArrayList paths, ValidationEventHandler h, XmlSchema schema)
		{
			pos = SkipWhitespace (xpath, pos);
			if (xpath.Length == pos) {
				error (h, "Empty xpath expression is specified");
				return;
			}

			XsdIdentityStep step = new XsdIdentityStep ();
			switch (xpath [pos]) {
			case '@':
				if (isSelector) {
					error (h, "Selector cannot include attribute axes.");
					currentPath = null;
					return;
				}
				pos++;
				step.IsAttribute = true;
				pos = SkipWhitespace (xpath, pos);
				if (xpath.Length > pos && xpath [pos] == '*') {
					pos++;
					step.IsAnyName = true;
					break;
				}
				goto default;
			case '.':
				pos++;	// do nothing ;-)
				step.IsCurrent = true;
				break;
			case '*':
				pos++;
				step.IsAnyName = true;
				break;
			case 'c':
				if (xpath.Length > pos + 5 && xpath.IndexOf ("child", pos, 5) == pos) {
					int tmp = pos;
					pos += 5;
					pos = SkipWhitespace (xpath, pos);
					if (xpath.Length > pos && xpath [pos] == ':' && xpath [pos+1] == ':') {
						pos += 2;
						if (xpath.Length > pos && xpath [pos] == '*') {
							pos++;
							step.IsAnyName = true;
							break;
						}
						pos = SkipWhitespace (xpath, pos);
					}
					else
						pos = tmp;
				}
				goto default;
			case 'a':
				if (xpath.Length > pos + 9 && xpath.IndexOf ("attribute", pos, 9) == pos) {
					int tmp = pos;
					pos += 9;
					pos = SkipWhitespace (xpath, pos);
					if (xpath.Length > pos && xpath [pos] == ':' && xpath [pos+1] == ':') {
						if (isSelector) {
							error (h, "Selector cannot include attribute axes.");
							currentPath = null;
							return;
						}
						pos += 2;
						step.IsAttribute = true;
						if (xpath.Length > pos && xpath [pos] == '*') {
							pos++;
							step.IsAnyName = true;
							break;
						}
						pos = SkipWhitespace (xpath, pos);
					}
					else
						pos = tmp;
				}
				goto default;
			default:
				int nameStart = pos;
				while (xpath.Length > pos) {
					if (!XmlChar.IsNCNameChar (xpath [pos]))
						break;
					else
						pos++;
				}
				if (pos == nameStart) {
					error (h, "Invalid path format for a field.");
					this.currentPath = null;
					return;
				}
				if (xpath.Length == pos || xpath [pos] != ':')
					step.Name = xpath.Substring (nameStart, pos - nameStart);
				else {
					string prefix = xpath.Substring (nameStart, pos - nameStart);
					pos++;
					if (xpath.Length > pos && xpath [pos] == '*') {
						string ns = nsmgr.LookupNamespace (prefix, false);
						if (ns == null) {
							error (h, "Specified prefix '" + prefix + "' is not declared.");
							this.currentPath = null;
							return;
						}
						step.NsName = ns;
						pos++;
					} else {
						int localNameStart = pos;
						while (xpath.Length > pos) {
							if (!XmlChar.IsNCNameChar (xpath [pos]))
								break;
							else
								pos++;
						}
						step.Name = xpath.Substring (localNameStart, pos - localNameStart);
						string ns = nsmgr.LookupNamespace (prefix, false);
						if (ns == null) {
							error (h, "Specified prefix '" + prefix + "' is not declared.");
							this.currentPath = null;
							return;
						}
						step.Namespace = ns;
					}
				}
				break;
			}
			if (!step.IsCurrent)	// Current step is meaningless, other than its representation.
				steps.Add (step);
			pos = SkipWhitespace (xpath, pos);
			if (xpath.Length == pos) {
				currentPath.OrderedSteps = (XsdIdentityStep []) steps.ToArray (typeof (XsdIdentityStep));
				paths.Add (currentPath);
				return;
			}
			else if (xpath [pos] == '/') {
				pos++;
				if (step.IsAttribute) {
					error (h, "Unexpected xpath token after Attribute NameTest.");
					this.currentPath = null;
					return;
				}
				this.ParseStep (xpath, pos, steps, paths, h, schema);
				if (currentPath == null) // For ValidationEventHandler
					return;
				currentPath.OrderedSteps = (XsdIdentityStep []) steps.ToArray (typeof (XsdIdentityStep));
			} else if (xpath [pos] == '|') {
				pos++;
				currentPath.OrderedSteps = (XsdIdentityStep []) steps.ToArray (typeof (XsdIdentityStep));
				paths.Add (this.currentPath);
				this.currentPath = new XsdIdentityPath ();
				this.ParsePath (xpath, pos, paths, h, schema);
			} else {
				error (h, "Unexpected xpath token after NameTest.");
				this.currentPath = null;
				return;
			}
		}

		private int SkipWhitespace (string xpath, int pos)
		{
			bool loop = true;
			while (loop && xpath.Length > pos) {
				switch (xpath [pos]) {
				case ' ':
				case '\t':
				case '\r':
				case '\n':
					pos++;
					continue;
				default:
					loop = false;
					break;
				}
			}
			return pos;
		}

		//<selector 
		//  id = ID 
		//  xpath = a subset of XPath expression, see below 
		//  {any attributes with non-schema namespace . . .}>
		//  Content: (annotation?)
		//</selector>
		internal static XmlSchemaXPath Read(XmlSchemaReader reader, ValidationEventHandler h,string name)
		{
			XmlSchemaXPath path = new XmlSchemaXPath();
			reader.MoveToElement();

			if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != name)
			{
				error(h,"Should not happen :1: XmlSchemaComplexContentRestriction.Read, name="+reader.Name,null);
				reader.Skip();
				return null;
			}

			path.LineNumber = reader.LineNumber;
			path.LinePosition = reader.LinePosition;
			path.SourceUri = reader.BaseURI;

			XmlNamespaceManager currentMgr = XmlSchemaUtil.GetParserContext (reader.Reader).NamespaceManager;
			if (currentMgr != null) {
				path.nsmgr = new XmlNamespaceManager (reader.NameTable);
				IEnumerator e = currentMgr.GetEnumerator ();
				while (e.MoveNext ()) {
					string prefix = e.Current as string;
					switch (prefix) {
					case "xml":
					case "xmlns":
						continue;
					default:
						path.nsmgr.AddNamespace (prefix, currentMgr.LookupNamespace (prefix, false));
						break;
					}
				}
			}

			while(reader.MoveToNextAttribute())
			{
				if(reader.Name == "id")
				{
					path.Id = reader.Value;
				}
				else if(reader.Name == "xpath")
				{
					path.xpath = reader.Value;
				}
				else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
				{
					error(h,reader.Name + " is not a valid attribute for "+name,null);
				}
				else
				{
					XmlSchemaUtil.ReadUnhandledAttribute(reader,path);
				}
			}

			reader.MoveToElement();	
			if(reader.IsEmptyElement)
				return path;

			//  Content: (annotation?)
			int level = 1;
			while(reader.ReadNextElement())
			{
				if(reader.NodeType == XmlNodeType.EndElement)
				{
					if(reader.LocalName != name)
						error(h,"Should not happen :2: XmlSchemaXPath.Read, name="+reader.Name,null);
					break;
				}
				if(level <= 1 && reader.LocalName == "annotation")
				{
					level = 2;	//Only one annotation
					XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
					if(annotation != null)
						path.Annotation = annotation;
					continue;
				}
				reader.RaiseInvalidElementError();
			}
			return path;
		}

	}
}