2 using System
.Collections
;
3 using System
.Collections
.Specialized
;
5 namespace SemWeb
.Util
{
6 internal class UriMap
: IDictionary
{
7 private static char[] splitchars
= { '\\', '/', '#' }
;
16 bool ICollection
.IsSynchronized { get { return false; }
}
17 object ICollection
.SyncRoot { get { return null; }
}
19 bool IDictionary
.IsFixedSize { get { return false; }
}
20 bool IDictionary
.IsReadOnly { get { return false; }
}
22 object IDictionary
.this[object key
] {
24 return this[(string)key
];
27 this[(string)key
] = value;
31 void IDictionary
.Add(object key
, object value) {
32 this[(string)key
] = value;
35 bool IDictionary
.Contains(object key
) {
36 return Contains((string)key
);
39 ICollection IDictionary
.Keys { get { throw new NotImplementedException(); }
}
40 ICollection IDictionary
.Values { get { throw new NotImplementedException(); }
}
41 IDictionaryEnumerator IDictionary
.GetEnumerator() { throw new NotImplementedException(); }
43 void IDictionary
.Remove(object key
) {
47 void ICollection
.CopyTo(Array dest
, int index
) { throw new NotImplementedException(); }
49 IEnumerator IEnumerable
.GetEnumerator() { throw new NotImplementedException(); }
51 public object this[string uri
] {
53 Node node
= FindNode(uri
, false);
54 if (node
== null) return null;
58 Node node
= FindNode(uri
, true);
68 public bool Contains(string uri
) {
69 Node node
= FindNode(uri
, false);
73 public int Count { get { return count; }
}
75 Node
FindNode(string uri
, bool create
) {
77 int pos
; string piece
;
78 NextPieceStart(out pos
);
79 while ((piece
= NextPiece(uri
, ref pos
)) != null) {
81 node
= node
.FindChild(piece
, create
, out created
);
83 if (node
== null) return null;
89 public void Remove(string uri
) {
93 NextPieceStart(out pos
);
94 string piece
= NextPiece(uri
, ref pos
);
95 string next
= NextPiece(uri
, ref pos
);
96 while (piece
!= null) {
99 node
= node
.FindChild(piece
, false, out created
);
100 if (node
== null) return;
102 if (node
.RemoveChild(piece
))
106 next
= NextPiece(uri
, ref pos
);
110 void NextPieceStart(out int position
) {
114 string NextPiece(string uri
, ref int position
) {
115 if (position
== uri
.Length
) return null;
117 // Get the next stopping position
119 if (position
== 0 && uri
.Length
> 15) look
= 15;
120 int next
= uri
.IndexOfAny(splitchars
, look
);
122 string r
= uri
.Substring(position
);
123 position
= uri
.Length
;
127 // Grab sequences of the same character in a row.
128 while (next
!= uri
.Length
-1 && uri
[next
] == uri
[next
+1])
131 string ret
= uri
.Substring(position
, next
-position
+1);
138 public IDictionary Children
;
141 public Node
FindChild(string name
, bool create
, out bool created
) {
142 if (Children
== null && !create
) {
147 if (Children
== null)
148 Children
= new HybridDictionary();
150 Node ret
= (Node
)Children
[name
];
151 if (ret
!= null || !create
) {
157 Children
[name
] = ret
;
162 public bool RemoveChild(string name
) {
163 if (Children
!= null && Children
.Contains(name
)) {
164 Children
.Remove(name
);