1
using System;
2
using System.Collections;
3
using System.Collections.Specialized;
4
using System.Text.RegularExpressions;
5
using System.Web;
6
using System.Web.Caching;
7
using System.Xml;
8
using System.IO;
9
using System.Text;
10
using NetFocus.Web.Core;
11
12
namespace NetFocus.Web.Applications.PersonalBlog
13
...{
14
public class UrlsData
15
...{
16
private members#region private members
17
18
private static string cacheKey = "UrlsData";
19
private static UrlsData instance = null;
20
private NameValueCollection _paths = null;
21
private NameValueCollection _reversePaths = null;
22
private ArrayList _tabUrls = null;
23
private string _locationFilter = null;
24
private LocationSet _locationSet = null;
25
26
#endregion
27
28
Instance#region Instance
29
30
public UrlsData(string siteUrlsXmlFile)
31
...{
32
_paths = new NameValueCollection();
33
_reversePaths = new NameValueCollection();
34
_tabUrls = new ArrayList();
35
36
Initialize(siteUrlsXmlFile);
37
}
38
public static UrlsData Instance()
39
...{
40
if (Caches.Get(cacheKey) == null)
41
...{
42
string file = WebContext.Current.PhysicalPath(Configuration.Instance.UrlsFile);
43
instance = new UrlsData(file);
44
Caches.Max(cacheKey, instance, new CacheDependency(file));
45
}
46
return instance;
47
}
48
49
#endregion
50
51
Public Properties#region Public Properties
52
53
public LocationSet Locations
54
...{
55
get ...{ return _locationSet; }
56
}
57
public string LocationFilter
58
...{
59
get
60
...{
61
return _locationFilter;
62
}
63
}
64
public NameValueCollection Paths
65
...{
66
get ...{ return this._paths; }
67
}
68
public NameValueCollection ReversePaths
69
...{
70
get ...{ return this._reversePaths; }
71
}
72
public ArrayList TabUrls
73
...{
74
get
75
...{
76
return _tabUrls;
77
}
78
}
79
80
#endregion
81
82
Private Methods#region Private Methods
83
84
private void Initialize(string siteUrlsXmlFile)
85
...{
86
string globalPath = Globals.ApplicationPath;
87
if (globalPath != null)
88
...{
89
globalPath = globalPath.Trim();
90
}
91
XmlDocument doc = new XmlDocument();
92
doc.Load(siteUrlsXmlFile);
93
94
XmlNode basePaths = doc.SelectSingleNode("SiteUrls/locations");
95
_locationSet = CreateLocationSet(basePaths, globalPath);
96
_locationFilter = _locationSet.Filter;
97
98
XmlNode transformers = doc.SelectSingleNode("SiteUrls/transformers");
99
ListDictionary transforms = CreateTransformers(transformers);
100
101
XmlNode urls = doc.SelectSingleNode("SiteUrls/urls");
102
CreateUrls(urls, transforms);
103
104
}
105
private static ListDictionary CreateTransformers(XmlNode transformers)
106
...{
107
ListDictionary transforms = new ListDictionary();
108
foreach (XmlNode n in transformers.ChildNodes)
109
...{
110
if (n.NodeType != XmlNodeType.Comment)
111
...{
112
string k = n.Attributes["key"].Value;
113
string v = n.Attributes["value"].Value;
114
115
if (!string.IsNullOrEmpty(k))
116
...{
117
transforms[k] = v;
118
}
119
}
120
}
121
122
return transforms;
123
}
124
private void CreateUrls(XmlNode urls, ListDictionary transforms)
125
...{
126
foreach (XmlNode n in urls.ChildNodes)
127
...{
128
if (n.NodeType != XmlNodeType.Comment)
129
...{
130
string name = n.Attributes["name"].Value;
131
132
XmlAttribute direct = n.Attributes["navigateUrl"];
133
if (direct != null)
134
...{
135
_paths.Add(name, direct.Value);
136
}
137
else
138
...{
139
string path = n.Attributes["path"].Value;
140
141
foreach (string key in transforms.Keys)
142
...{
143
path = path.Replace(key, transforms[key].ToString());
144
}
145
146
string location = null;
147
XmlAttribute l = n.Attributes["location"];
148
if (l != null)
149
location = l.Value;
150
151
Location loc = _locationSet.FindLocationByName(location);
152
153
_paths.Add(name, loc.Path + path);
154
155
// Store the full path to avoid having key duplicates
156
//
157
string keyPath = loc.Path + path;
158
if (Globals.ApplicationPath.Length > 0)
159
keyPath = keyPath.Replace(Globals.ApplicationPath, "").ToLower();
160
_reversePaths.Add(keyPath, name);
161
162
XmlAttribute realpath = n.Attributes["realpath"];
163
XmlAttribute pattern = n.Attributes["pattern"];
164
165
//Store full paths like regular urls.
166
if (realpath != null && pattern != null)
167
...{
168
string p = (!pattern.Value.StartsWith("/") ? loc.Path : String.Empty) + pattern.Value;
169
string rp = (!realpath.Value.StartsWith("/") ? loc.PhysicalPath : String.Empty) + realpath.Value;
170
171
foreach (string key in transforms.Keys)
172
...{
173
p = p.Replace(key, transforms[key].ToString());
174
rp = rp.Replace(key, transforms[key].ToString());
175
}
176
177
loc.Add(new ReWrittenUrl(location + "." + name, p, rp));
178
}
179
}
180
181
}
182
183
}
184
}
185
private LocationSet CreateLocationSet(XmlNode basePaths, string globalPath)
186
...{
187
LocationSet ls = new LocationSet();
188
foreach (XmlNode n in basePaths.ChildNodes)
189
...{
190
if (n.NodeType != XmlNodeType.Comment)
191
...{
192
XmlAttribute name = n.Attributes["name"];
193
XmlAttribute path = n.Attributes["path"];
194
XmlAttribute physicalPath = n.Attributes["physicalPath"];
195
XmlAttribute exclude = n.Attributes["exclude"];
196
XmlAttribute ltype = n.Attributes["type"];
197
198
if (name != null && path != null)
199
...{
200
string pp = null;
201
if (physicalPath != null)
202
...{
203
pp = globalPath + physicalPath.Value;
204
}
205
206
bool isExeclude = (exclude != null && bool.Parse(exclude.Value));
207
Location l = null;
208
string vp = globalPath + path.Value;
209
if (ltype == null)
210
...{
211
l = new Location(vp, pp, isExeclude);
212
}
213
else
214
...{
215
Type t = Type.GetType(ltype.Value);
216
if (t == null)
217
...{
218
//CSException csEx = new CSException(CSExceptionType.UnknownError, "Type for custom location could not be loaded " + ltype.Value);
219
//csEx.Log();
220
//throw csEx;
221
}
222
223
l = Activator.CreateInstance(t, new object[] ...{ vp, pp, isExeclude }) as Location;
224
if (l == null)
225
...{
226
//CSException csEx = new CSException(CSExceptionType.UnknownError, "Type for custom location could not be loaded as an instance of Location" + ltype.Value);
227
//csEx.Log();
228
//throw csEx;
229
}
230
}
231
ls.Add(name.Value, l);
232
}
233
}
234
}
235
236
return ls;
237
}
238
239
#endregion //Private Methods
240
241
public string FormatUrl(string name)
242
...{
243
return FormatUrl(name, null);
244
}
245
public virtual string FormatUrl(string name, params object[] parameters)
246
...{
247
if (parameters == null)
248
...{
249
return this.Paths[name];
250
}
251
else
252
...{
253
try
254
...{
255
return string.Format(Paths[name], parameters);
256
}
257
catch
258
...{ }
259
return string.Empty;
260
}
261
}
262
}
263
}