File: XPathSemWebNavigator.cs

package info (click to toggle)
beagle 0.2.12-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 12,204 kB
  • ctags: 16,188
  • sloc: cs: 91,628; sh: 27,627; ansic: 10,646; makefile: 2,248; xml: 15
file content (353 lines) | stat: -rw-r--r-- 10,067 bytes parent folder | download
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
using System;
using System.Collections;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;

using SemWeb;

namespace SemWeb.Util {
	public class XPathSemWebNavigator : XPathNavigator {
		Store model;
		NamespaceManager nsmgr;
		NSWrap nswrap;
		
		ArrayList stack = new ArrayList();
		Position current;
		
		private static Entity ExpandEntitiesOfType = new Entity(null);
		
		Entity rdfType = NS.RDF+"type";
		
		private class Position {
			public int Index;
			public bool FirstChild, LastChild;
			
			public Entity Predicate;
			public Resource Object;
			
			public Position[] Children;
		}
		
		private class NSWrap : XsltContext {
			NamespaceManager nsmgr;
			
			public NSWrap(NamespaceManager nsmgr) : base(new NameTable()) { this.nsmgr = nsmgr; }
			
			public override bool HasNamespace (string prefix) {
				return LookupNamespace(prefix) != null;
			}
			
			public override string LookupNamespace (string prefix) {
				return nsmgr.GetNamespace(prefix);
			}
			public override string LookupPrefix (string uri) {
				return nsmgr.GetPrefix(uri);
			}
			
			// These don't really do anything (yet?).
			
			public override bool Whitespace {
				get { return false; }
			} 
			
			public override int CompareDocument (string baseUri, string nextbaseUri) {
				return baseUri.CompareTo(nextbaseUri);
			}
			
			public override bool PreserveWhitespace (System.Xml.XPath.XPathNavigator node) {
				return false;
			}
			
			public override IXsltContextFunction ResolveFunction (string prefix, string name, System.Xml.XPath.XPathResultType[] ArgTypes) {
				return null;
			}

			public override IXsltContextVariable ResolveVariable (string prefix, string name) {
				return null;
			}
		}

		public XPathSemWebNavigator(Store model, NamespaceManager namespaces) : this(ExpandEntitiesOfType, model, namespaces, null) { }

		public XPathSemWebNavigator(Entity root, Store model, NamespaceManager namespaces) : this(root, model, namespaces, null) { }
		
		private XPathSemWebNavigator(Entity root, Store model, NamespaceManager namespaces, string exapandThisPredicate) {
			this.model = model;
			
			if (!(namespaces is SemWeb.IO.AutoPrefixNamespaceManager))
				namespaces = new SemWeb.IO.AutoPrefixNamespaceManager(namespaces);
			this.nsmgr = namespaces;
			nswrap = new NSWrap(nsmgr);
			
			Position start = new Position();
			start.FirstChild = true;
			start.LastChild = true;
			start.Predicate = root; // a trick to make sure the URI info for the root reflects the root
			start.Object = root;
			if (exapandThisPredicate != null)
				Expand(start, exapandThisPredicate);
			current = start;
		}
		
		private XPathSemWebNavigator(XPathSemWebNavigator clone) {
			MoveTo(clone);
		}
		
		private void Expand(Position p) {
			Expand(p, null);
		}
		
		private void Expand(Position p, string expandOnlyThisPredicate) {
			if (!(p.Object is Entity)) {
				p.Children = new Position[0];
				return;
			}
			
			ArrayList children = new ArrayList();
			int ctr = 0;
			
			if (p.Object == ExpandEntitiesOfType) {
				if (expandOnlyThisPredicate == null) {
					// Get a list of entities and their types.
					foreach (Statement s in model.Select(new Statement(null, rdfType, null))) {
						if (!(s.Object is Entity)) continue;
						Position c = new Position();
						c.Index = ctr++;
						c.Predicate = (Entity)s.Object;
						c.Object = s.Subject;
						children.Add(c);
					}
				} else {
					foreach (Entity e in model.GetEntitiesOfType(expandOnlyThisPredicate)) {
						Position c = new Position();
						c.Predicate = expandOnlyThisPredicate;
						c.Index = ctr++;
						c.Object = e;
						children.Add(c);
					}
				}
			} else {

			if (expandOnlyThisPredicate == null || !expandOnlyThisPredicate.StartsWith("!")) {
				Statement q = new Statement(
					(Entity)p.Object,
					expandOnlyThisPredicate == null ? (Entity)null : (Entity)expandOnlyThisPredicate,
					null);
				
				foreach (Statement s in model.Select(q)) {
					Position c = new Position();
					c.Index = ctr++;
					c.Predicate = s.Predicate;
					c.Object = s.Object;
					children.Add(c);
				}
			}
			
			if (expandOnlyThisPredicate == null || expandOnlyThisPredicate.StartsWith("!")) {
				Statement q = new Statement(
					null,
					expandOnlyThisPredicate == null ? (Entity)null : (Entity)expandOnlyThisPredicate.Substring(1),
					p.Object);
				
				foreach (Statement s in model.Select(q)) {
					Position c = new Position();
					c.Index = ctr++;
					c.Predicate = "!" + s.Predicate;
					c.Object = s.Subject;
					children.Add(c);
				}
			}
			
			}
			
			p.Children = (Position[])children.ToArray(typeof(Position));
			
			if (p.Children.Length > 0) {
				p.Children[0].FirstChild = true;
				p.Children[p.Children.Length-1].LastChild = true;
			}
		}

		public override string BaseURI { get { return ""; } }

		public override bool HasAttributes { get { return false; } }

		public override bool HasChildren { get { return true; } }

		public override bool IsEmptyElement { get { return false; } }

		public override string LocalName {
			get {
				string p, l;
				if (current.Predicate == ExpandEntitiesOfType)
					return "root";
				if (current.Predicate.Uri == null)
					return "anonymous";
				if (nsmgr.Normalize(current.Predicate.Uri, out p, out l))
					return l;
				throw new InvalidOperationException("The local name of " + current.Predicate.Uri + " could not be determined.");
			}
		}

		public override string Name {
			get {
				if (current.Predicate == ExpandEntitiesOfType)
					return "root";
				if (current.Predicate.Uri == null)
					return "anonymous";
				return nsmgr.Normalize(current.Predicate.Uri);
			}
		}

		public override string NamespaceURI {
			get {
				string p, l;
				if (current.Predicate.Uri == null)
					return "anonymous";
				if (nsmgr.Normalize(current.Predicate.Uri, out p, out l))
					return nsmgr.GetNamespace(p);
				throw new InvalidOperationException("The namespace URI of " + current.Predicate.Uri + " could not be determined.");
			}
		}

		public override XmlNameTable NameTable {
			get {
				return null;
			}
		}

		public override XPathNodeType NodeType {
			get {
				if (stack.Count == 0)
					return XPathNodeType.Root;
				return XPathNodeType.Element;
			}
		}

		public override string Prefix {
			get {
				string p, l;
				if (nsmgr.Normalize(current.Predicate.Uri, out p, out l))
					return p;
				throw new InvalidOperationException("The prefix of " + current.Predicate.Uri + " could not be determined.");
			}
		}

		public override string Value {
			get {
				if (current.Predicate == ExpandEntitiesOfType)
					return "root";
				if (current.Object is Literal)
					return ((Literal)current.Object).Value;
				if (current.Object.Uri == null)
					return "";
				return current.Object.Uri;
			}
		}

		public override string XmlLang { get { return ""; } }

		public override XPathNavigator Clone () {
			return new XPathSemWebNavigator(this);
		}

		//public virtual XmlNodeOrder ComparePosition (XPathNavigator nav)
		
		public override string GetAttribute (string localName, string namespaceURI) {
			return "";
		}

		public override string GetNamespace (string name) {
			return nsmgr.GetNamespace(name);
		}
		
		public override bool IsSamePosition (XPathNavigator other) {
			if (!(other is XPathSemWebNavigator)) return false;
			XPathSemWebNavigator o = (XPathSemWebNavigator)other;
			return (o.current == current);
		}

		public override bool MoveTo (XPathNavigator other) {
			XPathSemWebNavigator clone = other as XPathSemWebNavigator;
			if (clone == null) return false;
			this.model = clone.model;
			this.nsmgr = clone.nsmgr;
			this.nswrap = clone.nswrap;
			this.stack = (ArrayList)clone.stack.Clone();
			this.current = clone.current;
			return true;
		}

		public override bool MoveToAttribute (string localName, string namespaceURI) { return false; }

		public override bool MoveToNamespace (string name) { return false; }

		public override bool MoveToFirst () {
			return MoveToFirstChild();
		}

		public override void MoveToRoot () {
			if (stack.Count == 0) return;
			current = (Position)stack[0];
			stack.Clear();
		}

		public override bool MoveToFirstAttribute () { return false; }

		public override bool MoveToFirstChild () {
			if (current.Children == null) Expand(current);
			if (current.Children.Length == 0) return false;
			stack.Add(current);
			current = current.Children[0];
			return true;
		}

		public override bool MoveToFirstNamespace (XPathNamespaceScope namespaceScope) { return false; }

		public override bool MoveToId (string id) { return false; }

		public override bool MoveToNext () {
			if (current.LastChild) return false;
			current = ((Position)stack[stack.Count-1]).Children[current.Index+1];
			return true;
		}

		public override bool MoveToNextAttribute () { return false; }

		public override bool MoveToNextNamespace (XPathNamespaceScope namespaceScope) { return false; }

		public override bool MoveToParent () {
			if (stack.Count == 0) return false;
			current = (Position)stack[stack.Count-1];
			stack.RemoveAt(stack.Count-1);
			return true;
		}

		public override bool MoveToPrevious () {
			if (current.FirstChild) return false;
			current = ((Position)stack[stack.Count-1]).Children[current.Index-1];
			return true;
		}
		
		public override XPathNodeIterator SelectChildren (string name, string namespaceURI) {
			if (current.Object is Literal) throw new InvalidOperationException("The navigator is positioned on a literal element.");
			return new XPathSemWebNavigator((Entity)current.Object, model, nsmgr, namespaceURI + name).SelectChildren(XPathNodeType.All);
		}
		
		public override XPathNodeIterator Select (XPathExpression expr) {
			expr.SetContext(nswrap);
			return base.Select(expr);
		}
		
		public override object Evaluate (XPathExpression expr) {
			expr.SetContext(nswrap);
			return base.Evaluate(expr);
		}
		
		public override object Evaluate (XPathExpression expr, XPathNodeIterator context) {
			expr.SetContext(nswrap);
			return base.Evaluate(expr, context);
		}
	}
}