You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Zero/ZeroLevel/Services/Config/Implementation/AppWebConfigReader.cs

110 lines
3.8 KiB

6 years ago
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
namespace ZeroLevel.Services.Config.Implementation
{
internal sealed class AppWebConfigReader
{
private readonly string _configFilePath;
public bool ExistsAppConfigFile => string.IsNullOrWhiteSpace(_configFilePath) == false;
internal AppWebConfigReader(string configFilePath = null!)
6 years ago
{
7 months ago
if (configFilePath == null!)
6 years ago
{
6 years ago
var appConfig = Path.Combine(Configuration.BaseDirectory, $"{System.AppDomain.CurrentDomain.FriendlyName}.config");
6 years ago
if (File.Exists(appConfig))
{
_configFilePath = appConfig;
}
else
{
var webConfig = Path.Combine(Configuration.BaseDirectory, "web.config");
if (File.Exists(webConfig))
{
_configFilePath = webConfig;
}
else
{
_configFilePath = Directory.GetFiles(Configuration.BaseDirectory, "*.config").FirstOrDefault();
}
}
}
else
{
if (configFilePath.IndexOf(':') < 0)
{
this._configFilePath = Path.Combine(Configuration.BaseDirectory, configFilePath);
}
else
{
this._configFilePath = configFilePath;
}
}
}
internal IEnumerable<string> GetSections()
{
7 months ago
if (_configFilePath != null!)
6 years ago
{
var xdoc = XDocument.Load(_configFilePath);
var cs = xdoc.Descendants("connectionStrings").
Select(x => x.Name.LocalName);
return xdoc.Descendants("section")
.Select(x => x.Attribute("name").Value).Union(cs);
}
return Enumerable.Empty<string>();
}
internal IEnumerable<Tuple<string, string>> ReadSection(string sectionName)
{
7 months ago
if (_configFilePath != null!)
6 years ago
{
var xdoc = XDocument.Load(_configFilePath);
return xdoc.Descendants(sectionName).
SelectMany(x => x.Nodes().Where(n => null != (n as XElement)).Select(n =>
{
var xe = n as XElement;
return new Tuple<string, string>(FindName(xe!), FindValue(xe!));
}))
.Where(t => string.IsNullOrWhiteSpace(t.Item1) == false);
6 years ago
}
return Enumerable.Empty<Tuple<string, string>>();
}
private static string FindName(XElement n)
{
7 months ago
if (n == null!) return string.Empty;
6 years ago
var attributes = n.Attributes().
ToDictionary(i => i.Name.LocalName.ToLowerInvariant(), j => j.Value);
foreach (var v in new[] { "key", "name", "code", "id" })
{
if (attributes.ContainsKey(v))
return attributes[v];
}
return n.Name.LocalName;
}
private static string FindValue(XElement n)
{
7 months ago
if (n == null!) return string.Empty;
6 years ago
var attributes = n.Attributes().
ToDictionary(i => i.Name.LocalName.ToLowerInvariant(), j => j.Value);
foreach (var v in new[] { "value", "val", "file", "db", "connectionstring" })
{
if (attributes.ContainsKey(v))
return attributes[v];
}
return n.Value;
}
internal IEnumerable<Tuple<string, string>> ReadAppSettings()
{
return ReadSection("appSettings");
}
}
}

Powered by TurnKey Linux.