File: NamespaceManager.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 (150 lines) | stat: -rw-r--r-- 4,316 bytes parent folder | download | duplicates (4)
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
using System;
using System.Collections;

namespace SemWeb {
	
	internal class NS {
		public const string RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
		public const string RDFS = "http://www.w3.org/2000/01/rdf-schema#";
	
		/*Entity entRDFTYPE = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type";
		Entity entRDFFIRST = "http://www.w3.org/1999/02/22-rdf-syntax-ns#first";
		Entity entRDFREST = "http://www.w3.org/1999/02/22-rdf-syntax-ns#rest";
		Entity entRDFNIL = "http://www.w3.org/1999/02/22-rdf-syntax-ns#nil";
		Entity entOWLSAMEAS = "http://www.w3.org/2002/07/owl#sameAs";
		Entity entLOGIMPLIES = "http://www.w3.org/2000/10/swap/log#implies";*/
	}
	
	public class NamespaceManager {
		NamespaceManager parent;
		Hashtable atob = new Hashtable();
		Hashtable btoa = new Hashtable();
		
		public NamespaceManager() : this (null) {
		}
		
		public NamespaceManager(NamespaceManager parent) {
			this.parent = parent;
		}
		
		public void AddNamespace(string uri, string prefix) {
			atob[uri] = prefix;
			btoa[prefix] = uri;
		}
		
		public virtual string GetNamespace(string prefix) {
			string ret = (string)btoa[prefix];
			if (ret != null) return ret;
			if (parent != null) return parent.GetNamespace(prefix);
			return null;
		}
		
		public virtual string GetPrefix(string uri) {
			string ret = (string)atob[uri];
			if (ret != null) return ret;
			if (parent != null) return parent.GetPrefix(uri);
			return null;
		}
		
		public bool Normalize(string uri, out string prefix, out string localname) {
			int hash = uri.LastIndexOf('#');
			if (hash > 0) {
				prefix = GetPrefix(uri.Substring(0, hash+1));
				if (prefix != null) {
					localname = uri.Substring(hash+1);
					return true;
				}
			}
			
			hash = uri.LastIndexOf('/');
			if (hash > 0) {
				prefix = GetPrefix(uri.Substring(0, hash+1));
				if (prefix != null) {
					localname = uri.Substring(hash+1);
					return true;
				}
			}
			
			prefix = null;
			localname = null;
			
			return false;
		}
		
		public string Normalize(string uri) {
			string prefix, localname;
			if (Normalize(uri, out prefix, out localname)) {
				bool ok = true;
				if (localname.Length == 0) ok = false;
				else if (!char.IsLetter(localname[0])) ok = false;
				foreach (char c in localname)
					if (!char.IsLetterOrDigit(c) && c != '-' && c != '_')
						ok = false;
				if (ok)
					return prefix + ":" + localname;
			}
			return "<" + uri + ">";
		}
		
		public string Resolve(string qname) {
			int colon = qname.IndexOf(':');
			if (colon == -1) throw new ArgumentException("Invalid qualified name.");
			string prefix = qname.Substring(0, colon);
			string ns = GetNamespace(prefix);
			if (ns == null) throw new ArgumentException("The prefix " + prefix + " is not declared.");
			return ns + qname.Substring(colon+1);
		}
		
		public ICollection GetNamespaces() {
			if (parent == null) return atob.Keys;
			ArrayList items = new ArrayList(atob.Keys);
			foreach (string ns in parent.GetNamespaces())
				if (!items.Contains(ns))
					items.Add(ns);
			return items;
		}

		public ICollection GetPrefixes() {
			if (parent == null) return atob.Values;
			ArrayList items = new ArrayList(atob.Values);
			foreach (string ns in parent.GetPrefixes())
				if (!items.Contains(ns))
					items.Add(ns);
			return items;
		}
	}
}

namespace SemWeb.IO {
	using SemWeb;
	
	internal class AutoPrefixNamespaceManager : NamespaceManager {
		int counter = 0;
		
		public AutoPrefixNamespaceManager() : this (null) {
		}
		
		public AutoPrefixNamespaceManager(NamespaceManager parent) : base(parent) {
		}
		
		public override string GetPrefix(string uri) {
			string ret = base.GetPrefix(uri);
			if (ret != null) return ret;
			
			if (uri == "http://www.w3.org/1999/02/22-rdf-syntax-ns#" && GetNamespace("rdf") == null)
				ret = "rdf";
			else if (uri == "http://www.w3.org/2000/01/rdf-schema#" && GetNamespace("rdfs") == null)
				ret = "rdfs";
			else if (uri == "http://www.w3.org/2002/07/owl#" && GetNamespace("owl") == null)
				ret = "owl";
			else if (uri == "http://purl.org/dc/elements/1.1/" && GetNamespace("dc") == null)
				ret = "dc";
			else if (uri == "http://xmlns.com/foaf/0.1/" && GetNamespace("foaf") == null)
				ret = "foaf";
			else			
				ret = "autons" + (counter++);
			AddNamespace(uri, ret);
			return ret;
		}
	}
}