//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
-#if NET_2_0
+
using System;
using System.Collections;
using System.Collections.Specialized;
+using System.Configuration.Internal;
+using System.ComponentModel;
using System.Reflection;
using System.Xml;
using System.IO;
-using System.Configuration.Internal;
namespace System.Configuration {
- public sealed class Configuration
+ // For configuration document, use this XmlDocument instead of the standard one. This ignores xmlns attribute for MS.
+ internal class ConfigurationXmlDocument : XmlDocument
{
+ public override XmlElement CreateElement (string prefix, string localName, string namespaceURI)
+ {
+ if (namespaceURI == "http://schemas.microsoft.com/.NetConfiguration/v2.0")
+ return base.CreateElement (String.Empty, localName, String.Empty);
+ else
+ return base.CreateElement (prefix, localName, namespaceURI);
+ }
+ }
+
+ public sealed class Configuration
+ {
Configuration parent;
Hashtable elementData = new Hashtable ();
string streamName;
string locationConfigPath;
string locationSubPath;
+ internal static event ConfigurationSaveEventHandler SaveStart;
+ internal static event ConfigurationSaveEventHandler SaveEnd;
+
internal Configuration (Configuration parent, string locationSubPath)
{
this.parent = parent;
this.system = parent.system;
this.rootGroup = parent.rootGroup;
this.locationSubPath = locationSubPath;
+ this.configPath = parent.ConfigPath;
}
internal Configuration (InternalConfigurationSystem system, string locationSubPath)
{
hasFile = true;
this.system = system;
-
+
system.InitForConfiguration (ref locationSubPath, out configPath, out locationConfigPath);
Configuration parent = null;
internal Configuration FindLocationConfiguration (string relativePath, Configuration defaultConfiguration)
{
- ConfigurationLocation loc = Locations.Find (relativePath);
-
Configuration parentConfig = defaultConfiguration;
-
- if (LocationConfigPath != null) {
+
+ if (!String.IsNullOrEmpty (LocationConfigPath)) {
Configuration parentFile = GetParentWithFile ();
if (parentFile != null) {
- string parentRelativePath = system.Host.GetConfigPathFromLocationSubPath (LocationConfigPath, relativePath);
+ string parentRelativePath = system.Host.GetConfigPathFromLocationSubPath (configPath, relativePath);
parentConfig = parentFile.FindLocationConfiguration (parentRelativePath, defaultConfiguration);
}
}
+ string relConfigPath = configPath.Substring (1) + "/";
+ if (relativePath.StartsWith (relConfigPath, StringComparison.Ordinal))
+ relativePath = relativePath.Substring (relConfigPath.Length);
+
+ ConfigurationLocation loc = Locations.FindBest (relativePath);
if (loc == null)
return parentConfig;
rootGroup.StreamName = streamName;
}
- if (streamName != null)
- Load ();
+ try {
+ if (streamName != null)
+ Load ();
+ } catch (XmlException ex) {
+ throw new ConfigurationErrorsException (ex.Message, ex, streamName, 0);
+ }
}
internal Configuration Parent {
get { return RootSectionGroup.Sections; }
}
- public ConfigurationSection GetSection (string path)
+ public ConfigurationSection GetSection (string sectionName)
{
- string[] parts = path.Split ('/');
+ string[] parts = sectionName.Split ('/');
if (parts.Length == 1)
return Sections [parts[0]];
return null;
}
- public ConfigurationSectionGroup GetSectionGroup (string path)
+ public ConfigurationSectionGroup GetSectionGroup (string sectionGroupName)
{
- string[] parts = path.Split ('/');
+ string[] parts = sectionGroupName.Split ('/');
ConfigurationSectionGroup group = SectionGroups [parts[0]];
for (int n=1; group != null && n<parts.Length; n++)
group = group.SectionGroups [parts [n]];
sec.RawXml = xml;
sec.Reset (parentSection);
- if (xml != null && xml == data) {
+ if (xml != null) {
XmlTextReader r = new ConfigXmlTextReader (new StringReader (xml), FilePath);
sec.DeserializeSection (r);
r.Close ();
internal void CreateSection (SectionGroupInfo group, string name, ConfigurationSection sec)
{
if (group.HasChild (name))
- throw new ConfigurationException ("Cannot add a ConfigurationSection. A section or section group already exists with the name '" + name + "'");
+ throw new ConfigurationErrorsException ("Cannot add a ConfigurationSection. A section or section group already exists with the name '" + name + "'");
if (!HasFile && !sec.SectionInformation.AllowLocation)
throw new ConfigurationErrorsException ("The configuration section <" + name + "> cannot be defined inside a <location> element.");
section.ConfigHost = system.Host;
group.AddChild (section);
elementData [section] = sec;
+ sec.Configuration = this;
}
internal void CreateSectionGroup (SectionGroupInfo parentGroup, string name, ConfigurationSectionGroup sec)
{
- if (parentGroup.HasChild (name)) throw new ConfigurationException ("Cannot add a ConfigurationSectionGroup. A section or section group already exists with the name '" + name + "'");
+ if (parentGroup.HasChild (name)) throw new ConfigurationErrorsException ("Cannot add a ConfigurationSectionGroup. A section or section group already exists with the name '" + name + "'");
if (sec.Type == null) sec.Type = system.Host.GetConfigTypeName (sec.GetType ());
sec.SetName (name);
Save (ConfigurationSaveMode.Modified, false);
}
- public void Save (ConfigurationSaveMode mode)
+ public void Save (ConfigurationSaveMode saveMode)
{
- Save (mode, false);
+ Save (saveMode, false);
}
- public void Save (ConfigurationSaveMode mode, bool forceUpdateAll)
+ public void Save (ConfigurationSaveMode saveMode, bool forceSaveAll)
{
+ if (!forceSaveAll && (saveMode != ConfigurationSaveMode.Full) && !HasValues (saveMode)) {
+ ResetModified ();
+ return;
+ }
+
+ ConfigurationSaveEventHandler saveStart = SaveStart;
+ ConfigurationSaveEventHandler saveEnd = SaveEnd;
+
object ctx = null;
+ Exception saveEx = null;
Stream stream = system.Host.OpenStreamForWrite (streamName, null, ref ctx);
try {
- Save (stream, mode, forceUpdateAll);
+ if (saveStart != null)
+ saveStart (this, new ConfigurationSaveEventArgs (streamName, true, null, ctx));
+
+ Save (stream, saveMode, forceSaveAll);
system.Host.WriteCompleted (streamName, true, ctx);
- } catch (Exception) {
+ } catch (Exception ex) {
+ saveEx = ex;
system.Host.WriteCompleted (streamName, false, ctx);
throw;
} finally {
stream.Close ();
+ if (saveEnd != null)
+ saveEnd (this, new ConfigurationSaveEventArgs (streamName, false, saveEx, ctx));
}
}
SaveAs (filename, ConfigurationSaveMode.Modified, false);
}
- public void SaveAs (string filename, ConfigurationSaveMode mode)
+ public void SaveAs (string filename, ConfigurationSaveMode saveMode)
{
- SaveAs (filename, mode, false);
+ SaveAs (filename, saveMode, false);
}
[MonoInternalNote ("Detect if file has changed")]
- public void SaveAs (string filename, ConfigurationSaveMode mode, bool forceUpdateAll)
+ public void SaveAs (string filename, ConfigurationSaveMode saveMode, bool forceSaveAll)
{
+ if (!forceSaveAll && (saveMode != ConfigurationSaveMode.Full) && !HasValues (saveMode)) {
+ ResetModified ();
+ return;
+ }
+
string dir = Path.GetDirectoryName (Path.GetFullPath (filename));
if (!Directory.Exists (dir))
Directory.CreateDirectory (dir);
- Save (new FileStream (filename, FileMode.OpenOrCreate, FileAccess.Write), mode, forceUpdateAll);
+ Save (new FileStream (filename, FileMode.OpenOrCreate, FileAccess.Write), saveMode, forceSaveAll);
}
void Save (Stream stream, ConfigurationSaveMode mode, bool forceUpdateAll)
SaveData (tw, mode, forceUpdateAll);
tw.WriteEndElement ();
+ ResetModified ();
}
finally {
+ tw.Flush ();
tw.Close ();
}
}
{
rootGroup.WriteRootData (tw, this, mode);
}
+
+ bool HasValues (ConfigurationSaveMode mode)
+ {
+ foreach (ConfigurationLocation loc in Locations) {
+ if (loc.OpenedConfiguration == null)
+ continue;
+ if (loc.OpenedConfiguration.HasValues (mode))
+ return true;
+ }
+
+ return rootGroup.HasValues (this, mode);
+ }
+
+ void ResetModified ()
+ {
+ foreach (ConfigurationLocation loc in Locations) {
+ if (loc.OpenedConfiguration == null)
+ continue;
+ loc.OpenedConfiguration.ResetModified ();
+ }
+
+ rootGroup.ResetModified (this);
+ }
bool Load ()
{
return true;
Stream stream = null;
-
- // FIXME: we should remove this kind of hack that
- // hides the actual error
try {
stream = system.Host.OpenStreamForRead (streamName);
- } catch (Exception) {
+ if (stream == null)
+ return false;
+ } catch {
return false;
}
using (XmlTextReader reader = new ConfigXmlTextReader (stream, streamName)) {
ReadConfigFile (reader, streamName);
}
+ ResetModified ();
return true;
}
private void ThrowException (string text, XmlReader reader)
{
IXmlLineInfo li = reader as IXmlLineInfo;
- throw new ConfigurationException (text, streamName, li != null ? li.LineNumber : 0);
+ throw new ConfigurationErrorsException (text, streamName, li != null ? li.LineNumber : 0);
}
}
}
-#endif