Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public IEnumerable<DsnReference> GetDsnReferences(string directory)
- {
- var references = new LinkedList<DsnReference>();
- var cfqueryRegex = new Regex(@"<cfquery(.*?)>", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Multiline);
- var attributeRegex = new Regex(@"(\w+)\s*=\s*[""'](.*?)[""']", RegexOptions.Compiled | RegexOptions.Multiline);
- VisitFiles(
- directory, "",
- (abs, relative) =>
- {
- if (!relative.EndsWith(".cfm") && !relative.EndsWith(".cfc"))
- return;
- string contents;
- using (var sr = new StreamReader(abs))
- contents = sr.ReadToEnd();
- var matches = cfqueryRegex.Matches(contents);
- foreach (Match match in matches)
- {
- var attributes = (
- from attributeMatch in
- attributeRegex.Matches(match.Result("$1")).OfType<Match>()
- select new
- {
- Key = attributeMatch.Result("$1").ToLower(),
- Value = attributeMatch.Result("$2").ToLower()
- }).ToDictionary(k => k.Key, v => v.Value);
- references.AddLast(new DsnReference
- {
- Dsn = attributes.ContainsKey("datasource") ? attributes["datasource"] : null,
- Username = attributes.ContainsKey("username") ? attributes["username"] : null,
- Password = attributes.ContainsKey("password") ? attributes["password"] : null,
- RelativePath = relative
- });
- }
- });
- return references;
- }
- private static void VisitFiles(string path, string relative, Action<string, string> visit)
- {
- var files = Directory.GetFiles(path);
- var directories = Directory.GetDirectories(path).Where(d => !d.EndsWith("\\.svn"));
- foreach (var di in directories.Select(dir => new DirectoryInfo(dir)))
- {
- VisitFiles(di.FullName, relative + "\\" + di.Name, visit);
- }
- foreach (var fi in files.Select(file => new FileInfo(file)))
- {
- visit(fi.FullName, relative + "\\" + fi.Name);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement