Dont reindex already indexed files. Yet another bug uncovered by the DateTime fixes.
[beagle.git] / Util / SemWeb / NamespaceManager.cs
blob9184a8386e33229cfcb705ca772f204527e4f60f
1 using System;
2 using System.Collections;
4 namespace SemWeb {
6 internal class NS {
7 public const string RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
8 public const string RDFS = "http://www.w3.org/2000/01/rdf-schema#";
10 /*Entity entRDFTYPE = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type";
11 Entity entRDFFIRST = "http://www.w3.org/1999/02/22-rdf-syntax-ns#first";
12 Entity entRDFREST = "http://www.w3.org/1999/02/22-rdf-syntax-ns#rest";
13 Entity entRDFNIL = "http://www.w3.org/1999/02/22-rdf-syntax-ns#nil";
14 Entity entOWLSAMEAS = "http://www.w3.org/2002/07/owl#sameAs";
15 Entity entLOGIMPLIES = "http://www.w3.org/2000/10/swap/log#implies";*/
18 public class NamespaceManager {
19 NamespaceManager parent;
20 Hashtable atob = new Hashtable();
21 Hashtable btoa = new Hashtable();
23 public NamespaceManager() : this (null) {
26 public NamespaceManager(NamespaceManager parent) {
27 this.parent = parent;
30 public void AddNamespace(string uri, string prefix) {
31 atob[uri] = prefix;
32 btoa[prefix] = uri;
35 public virtual string GetNamespace(string prefix) {
36 string ret = (string)btoa[prefix];
37 if (ret != null) return ret;
38 if (parent != null) return parent.GetNamespace(prefix);
39 return null;
42 public virtual string GetPrefix(string uri) {
43 string ret = (string)atob[uri];
44 if (ret != null) return ret;
45 if (parent != null) return parent.GetPrefix(uri);
46 return null;
49 public bool Normalize(string uri, out string prefix, out string localname) {
50 int hash = uri.LastIndexOf('#');
51 if (hash > 0) {
52 prefix = GetPrefix(uri.Substring(0, hash+1));
53 if (prefix != null) {
54 localname = uri.Substring(hash+1);
55 return true;
59 hash = uri.LastIndexOf('/');
60 if (hash > 0) {
61 prefix = GetPrefix(uri.Substring(0, hash+1));
62 if (prefix != null) {
63 localname = uri.Substring(hash+1);
64 return true;
68 prefix = null;
69 localname = null;
71 return false;
74 public string Normalize(string uri) {
75 string prefix, localname;
76 if (Normalize(uri, out prefix, out localname)) {
77 bool ok = true;
78 if (localname.Length == 0) ok = false;
79 else if (!char.IsLetter(localname[0])) ok = false;
80 foreach (char c in localname)
81 if (!char.IsLetterOrDigit(c) && c != '-' && c != '_')
82 ok = false;
83 if (ok)
84 return prefix + ":" + localname;
86 return "<" + uri + ">";
89 public string Resolve(string qname) {
90 int colon = qname.IndexOf(':');
91 if (colon == -1) throw new ArgumentException("Invalid qualified name.");
92 string prefix = qname.Substring(0, colon);
93 string ns = GetNamespace(prefix);
94 if (ns == null) throw new ArgumentException("The prefix " + prefix + " is not declared.");
95 return ns + qname.Substring(colon+1);
98 public ICollection GetNamespaces() {
99 if (parent == null) return atob.Keys;
100 ArrayList items = new ArrayList(atob.Keys);
101 foreach (string ns in parent.GetNamespaces())
102 if (!items.Contains(ns))
103 items.Add(ns);
104 return items;
107 public ICollection GetPrefixes() {
108 if (parent == null) return atob.Values;
109 ArrayList items = new ArrayList(atob.Values);
110 foreach (string ns in parent.GetPrefixes())
111 if (!items.Contains(ns))
112 items.Add(ns);
113 return items;
118 namespace SemWeb.IO {
119 using SemWeb;
121 internal class AutoPrefixNamespaceManager : NamespaceManager {
122 int counter = 0;
124 public AutoPrefixNamespaceManager() : this (null) {
127 public AutoPrefixNamespaceManager(NamespaceManager parent) : base(parent) {
130 public override string GetPrefix(string uri) {
131 string ret = base.GetPrefix(uri);
132 if (ret != null) return ret;
134 if (uri == "http://www.w3.org/1999/02/22-rdf-syntax-ns#" && GetNamespace("rdf") == null)
135 ret = "rdf";
136 else if (uri == "http://www.w3.org/2000/01/rdf-schema#" && GetNamespace("rdfs") == null)
137 ret = "rdfs";
138 else if (uri == "http://www.w3.org/2002/07/owl#" && GetNamespace("owl") == null)
139 ret = "owl";
140 else if (uri == "http://purl.org/dc/elements/1.1/" && GetNamespace("dc") == null)
141 ret = "dc";
142 else if (uri == "http://xmlns.com/foaf/0.1/" && GetNamespace("foaf") == null)
143 ret = "foaf";
144 else
145 ret = "autons" + (counter++);
146 AddNamespace(uri, ret);
147 return ret;