2 using System
.Collections
;
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
) {
30 public void AddNamespace(string uri
, string prefix
) {
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
);
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
);
49 public bool Normalize(string uri
, out string prefix
, out string localname
) {
50 int hash
= uri
.LastIndexOf('#');
52 prefix
= GetPrefix(uri
.Substring(0, hash
+1));
54 localname
= uri
.Substring(hash
+1);
59 hash
= uri
.LastIndexOf('/');
61 prefix
= GetPrefix(uri
.Substring(0, hash
+1));
63 localname
= uri
.Substring(hash
+1);
74 public string Normalize(string uri
) {
75 string prefix
, localname
;
76 if (Normalize(uri
, out prefix
, out localname
)) {
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
!= '_')
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
))
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
))
118 namespace SemWeb
.IO
{
121 internal class AutoPrefixNamespaceManager
: NamespaceManager
{
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)
136 else if (uri
== "http://www.w3.org/2000/01/rdf-schema#" && GetNamespace("rdfs") == null)
138 else if (uri
== "http://www.w3.org/2002/07/owl#" && GetNamespace("owl") == null)
140 else if (uri
== "http://purl.org/dc/elements/1.1/" && GetNamespace("dc") == null)
142 else if (uri
== "http://xmlns.com/foaf/0.1/" && GetNamespace("foaf") == null)
145 ret
= "autons" + (counter
++);
146 AddNamespace(uri
, ret
);