string configPath;
string locationConfigPath;
-
- internal Configuration (Configuration parent)
+ string locationSubPath;
+
+ internal Configuration (Configuration parent, string locationSubPath)
{
- Init (parent.system, null, parent);
+ this.parent = parent;
+ this.system = parent.system;
+ this.rootGroup = parent.rootGroup;
+ this.locationSubPath = locationSubPath;
}
internal Configuration (InternalConfigurationSystem system, string locationSubPath)
rootGroup.StreamName = streamName;
}
- if (configPath != null)
+ if (streamName != null)
Load ();
}
get { return locationConfigPath; }
}
+ internal string GetLocationSubPath ()
+ {
+ Configuration confg = parent;
+ string path = null;
+ while (confg != null) {
+ path = confg.locationSubPath;
+ if (!String.IsNullOrEmpty (path))
+ return path;
+ confg = confg.parent;
+ }
+ return path;
+ }
+
internal string ConfigPath {
get { return configPath; }
}
get { return (ConnectionStringsSection) GetSection ("connectionStrings"); }
}
+ // MSDN: If the value for this FilePath property represents a merged view and
+ // no actual file exists for the application, the path to the parent configuration
+ // file is returned.
public string FilePath {
- get { return streamName; }
+ get {
+ if (streamName == null && parent != null)
+ return parent.FilePath;
+ return streamName;
+ }
}
public bool HasFile {
}
}
- [MonoTODO ("HostingContext")]
ContextInformation evaluationContext;
public ContextInformation EvaluationContext {
get {
- if (evaluationContext == null)
- evaluationContext = new ContextInformation (this, null /* XXX */);
+ if (evaluationContext == null) {
+ object ctx = system.Host.CreateConfigurationContext (configPath, GetLocationSubPath() );
+ evaluationContext = new ContextInformation (this, ctx);
+ }
+
+
return evaluationContext;
}
}
rootSectionGroup.Initialize (this, rootGroup);
}
return rootSectionGroup;
- }
+ }
}
public ConfigurationSectionGroupCollection SectionGroups {
- get { return RootSectionGroup.SectionGroups; }
+ get { return RootSectionGroup.SectionGroups; }
}
public ConfigurationSectionCollection Sections {
ds.SectionHandler = secObj as IConfigurationSectionHandler;
sec = ds;
}
+ sec.Configuration = this;
- ConfigurationSection parentSection = parent != null ? parent.GetSectionInstance (config, true) : null;
- sec.RawXml = data as string;
+ ConfigurationSection parentSection = null;
+ if (parent != null) {
+ parentSection = parent.GetSectionInstance (config, true);
+ sec.SectionInformation.SetParentSection (parentSection);
+ }
+ sec.SectionInformation.ConfigFilePath = FilePath;
+
+ sec.ConfigContext = system.Host.CreateDeprecatedConfigContext(configPath);
+
+ string xml = data as string;
+ sec.RawXml = xml;
sec.Reset (parentSection);
- if (data is string) {
- XmlTextReader r = new XmlTextReader (new StringReader (data as string));
+ if (xml != null && xml == data) {
+ XmlTextReader r = new ConfigXmlTextReader (new StringReader (xml), FilePath);
sec.DeserializeSection (r);
r.Close ();
+
+ if (!String.IsNullOrEmpty (sec.SectionInformation.ConfigSource) && !String.IsNullOrEmpty (FilePath))
+ sec.DeserializeConfigSource (Path.GetDirectoryName (FilePath));
}
elementData [config] = sec;
object ctx = sec.SectionInformation.AllowExeDefinition != ConfigurationAllowExeDefinition.MachineToApplication ? (object) sec.SectionInformation.AllowExeDefinition : (object) sec.SectionInformation.AllowDefinition;
throw new ConfigurationErrorsException ("The section <" + name + "> can't be defined in this configuration file (the allowed definition context is '" + ctx + "').");
}
-
+
if (sec.SectionInformation.Type == null)
sec.SectionInformation.Type = system.Host.GetConfigTypeName (sec.GetType ());
-
+
SectionInfo section = new SectionInfo (name, sec.SectionInformation);
section.StreamName = streamName;
section.ConfigHost = system.Host;
section.ConfigHost = system.Host;
parentGroup.AddChild (section);
elementData [section] = sec;
+
+ sec.Initialize (this, section);
}
internal void RemoveConfigInfo (ConfigInfo config)
try {
Save (stream, mode, forceUpdateAll);
system.Host.WriteCompleted (streamName, true, ctx);
- } catch (Exception ex) {
+ } catch (Exception) {
system.Host.WriteCompleted (streamName, false, ctx);
throw;
} finally {
{
SaveAs (filename, mode, false);
}
-
- [MonoTODO ("Detect if file has changed")]
+
+ [MonoInternalNote ("Detect if file has changed")]
public void SaveAs (string filename, ConfigurationSaveMode mode, bool forceUpdateAll)
{
+ string dir = Path.GetDirectoryName (Path.GetFullPath (filename));
+ if (!Directory.Exists (dir))
+ Directory.CreateDirectory (dir);
Save (new FileStream (filename, FileMode.OpenOrCreate, FileAccess.Write), mode, forceUpdateAll);
}
bool Load ()
{
- if (streamName == null || streamName == "")
+ if (String.IsNullOrEmpty (streamName))
return true;
- XmlTextReader reader = null;
Stream stream = null;
+ // FIXME: we should remove this kind of hack that
+ // hides the actual error
try {
stream = system.Host.OpenStreamForRead (streamName);
- } catch (Exception e) {
+ } catch (Exception) {
return false;
}
- try {
- reader = new XmlTextReader (stream);
+ using (XmlTextReader reader = new ConfigXmlTextReader (stream, streamName)) {
ReadConfigFile (reader, streamName);
- } finally {
- if (reader != null)
- reader.Close();
}
return true;
}
-
- internal void ReadConfigFile (XmlTextReader reader, string fileName)
+ void ReadConfigFile (XmlReader reader, string fileName)
{
reader.MoveToContent ();
rootGroup.ReadRootData (reader, this, true);
}
-
- internal void ReadData (XmlTextReader reader, bool allowOverride)
+
+ internal void ReadData (XmlReader reader, bool allowOverride)
{
- rootGroup.ReadRootData (reader, this, allowOverride);
+ rootGroup.ReadData (this, reader, allowOverride);
}
- private void ThrowException (string text, XmlTextReader reader)
+ private void ThrowException (string text, XmlReader reader)
{
- throw new ConfigurationException (text, streamName, reader.LineNumber);
+ IXmlLineInfo li = reader as IXmlLineInfo;
+ throw new ConfigurationException (text, streamName, li != null ? li.LineNumber : 0);
}
}
}