| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- //
- // System.Configuration.ConfigurationLocation.cs
- //
- // Authors:
- // Duncan Mak ([email protected])
- // Lluis Sanchez Gual ([email protected])
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
- //
- #if NET_2_0 && XML_DEP
- using System;
- using System.Collections;
- using System.Collections.Specialized;
- using System.Xml;
- using System.IO;
- namespace System.Configuration {
- public sealed class Configuration
- {
- Configuration parent;
- Hashtable elementData = new Hashtable ();
- string fileName;
- ConfigurationSectionGroup rootSectionGroup;
- ConfigurationLocationCollection locations;
- SectionGroupInfo rootGroup;
-
- internal Configuration (): this (null, null)
- {
- }
-
- internal Configuration (string file): this (file, null)
- {
- }
-
- internal Configuration (Configuration parent): this (null, parent)
- {
- }
-
- internal Configuration (string file, Configuration parent)
- {
- fileName = file;
- this.parent = parent;
- if (parent != null)
- rootGroup = parent.rootGroup;
- else {
- rootGroup = new SectionGroupInfo ();
- rootGroup.FileName = file;
- }
-
- if (file != null) Load (file);
- }
-
- internal Configuration Parent {
- get { return parent; }
- }
-
- internal string FileName {
- get { return fileName; }
- }
- [MonoTODO]
- public AppSettingsSection AppSettings {
- get { throw new NotImplementedException (); }
- }
- [MonoTODO]
- public PathLevel ConfigurationPathLevel {
- get { throw new NotImplementedException (); }
- }
- [MonoTODO]
- public ConnectionStringsSection ConnectionStrings {
- get { throw new NotImplementedException (); }
- }
- [MonoTODO]
- public string FilePath {
- get { throw new NotImplementedException (); }
- }
- [MonoTODO]
- public bool HasFile {
- get { throw new NotImplementedException (); }
- }
- public ConfigurationLocationCollection Locations {
- get {
- if (locations == null) locations = new ConfigurationLocationCollection ();
- return locations;
- }
- }
- [MonoTODO]
- public string Path {
- get { throw new NotImplementedException (); }
- }
- public ConfigurationSectionGroup RootSectionGroup {
- get {
- if (rootSectionGroup == null) {
- rootSectionGroup = new ConfigurationSectionGroup ();
- rootSectionGroup.Initialize (this, rootGroup);
- }
- return rootSectionGroup;
- }
- }
- public ConfigurationSectionGroupCollection SectionGroups {
- get { return RootSectionGroup.SectionGroups; }
- }
- public ConfigurationSectionCollection Sections {
- get { return RootSectionGroup.Sections; }
- }
- public static Configuration GetExeConfiguration (string path, ConfigurationUserLevel level)
- {
- return new Configuration (path + ".config", GetMachineConfiguration ());
- }
- public static Configuration GetMachineConfiguration ()
- {
- return GetMachineConfiguration (System.Runtime.InteropServices.RuntimeEnvironment.SystemConfigurationFile);
- }
- public static Configuration GetMachineConfiguration (string path)
- {
- return new Configuration (path);
- }
-
- [MonoTODO]
- public static Configuration GetMachineConfiguration (string path, string server)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public static Configuration GetMachineConfiguration (
- string path, string server, IntPtr user_token)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public static Configuration GetMachineConfiguration (
- string path, string server, string username, string password)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public static Configuration GetWebConfiguration ()
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public static Configuration GetWebConfiguration (string path)
- {
- return new Configuration (path, GetMachineConfiguration ());
- }
- [MonoTODO]
- public static Configuration GetWebConfiguration (string path, string site)
- {
- throw new NotImplementedException ();
- }
-
- [MonoTODO]
- public static Configuration GetWebConfiguration (string path, string site, string subpath)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public static Configuration GetWebConfiguration (
- string path, string site, string subpath, string server)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public static Configuration GetWebConfiguration (
- string path, string site, string subpath, string server, IntPtr user_token)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public static Configuration GetWebConfiguration (
- string path, string site, string subpath, string server, string username, string password)
- {
- throw new NotImplementedException ();
- }
-
- internal ConfigurationSection GetSectionInstance (SectionInfo config, bool createDefaultInstance)
- {
- object data = elementData [config];
- ConfigurationSection sec = data as ConfigurationSection;
- if (sec != null || !createDefaultInstance) return sec;
-
- object secObj = config.CreateInstance () as ConfigurationSection;
- if (!(secObj is ConfigurationSection))
- sec = new RuntimeOnlySection ();
- else
- sec = (ConfigurationSection) secObj;
-
- ConfigurationSection parentSection = parent != null ? parent.GetSectionInstance (config, true) : null;
- sec.RawXml = data as string;
- sec.Reset (parentSection, this);
-
- if (data != null) {
- XmlTextReader r = new XmlTextReader (new StringReader (data as string));
- sec.ReadXml (r, this);
- r.Close ();
- }
-
- elementData [config] = sec;
- return sec;
- }
-
- internal ConfigurationSectionGroup GetSectionGroupInstance (SectionGroupInfo group)
- {
- ConfigurationSectionGroup gr = group.CreateInstance () as ConfigurationSectionGroup;
- if (gr != null) gr.Initialize (this, group);
- return gr;
- }
-
- internal void SetConfigurationSection (SectionInfo config, ConfigurationSection sec)
- {
- elementData [config] = sec;
- }
-
- internal void SetSectionData (SectionInfo config, string data)
- {
- elementData [config] = data;
- }
-
- 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 + "'");
- if (sec.TypeName == null) sec.TypeName = sec.GetType ().AssemblyQualifiedName;
- sec.SetName (name);
- SectionInfo section = new SectionInfo (name, sec.TypeName, sec.AllowLocation, sec.AllowDefinition);
- section.FileName = FileName;
- group.AddChild (section);
- elementData [section] = sec;
- }
-
- 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 (sec.TypeName == null) sec.TypeName = sec.GetType ().AssemblyQualifiedName;
- sec.SetName (name);
- SectionGroupInfo section = new SectionGroupInfo (name, sec.TypeName);
- section.FileName = FileName;
- parentGroup.AddChild (section);
- elementData [section] = sec;
- }
-
- internal void RemoveConfigInfo (ConfigInfo config)
- {
- elementData.Remove (config);
- }
-
- public void Update ()
- {
- Update (ConfigurationUpdateMode.Full, true);
- }
-
- public void Update (ConfigurationUpdateMode mode)
- {
- Update (mode, false);
- }
-
- [MonoTODO]
- public void Update (ConfigurationUpdateMode mode, bool forceUpdateAll)
- {
- XmlTextWriter tw = new XmlTextWriter (new StreamWriter (fileName));
- tw.Formatting = Formatting.Indented;
- try {
- tw.WriteStartElement ("configuration");
- if (rootGroup.HasConfigContent (this)) {
- rootGroup.WriteConfig (this, tw, mode);
- }
- rootGroup.WriteRootData (tw, this, mode);
- tw.WriteEndElement ();
- }
- finally {
- tw.Close ();
- }
- }
-
- internal bool Load (string fileName)
- {
- this.fileName = fileName;
- if (!File.Exists (fileName))
- throw new ConfigurationException ("File '" + fileName + "' not found");
- #if (XML_DEP)
- XmlTextReader reader = null;
- try {
- FileStream fs = new FileStream (fileName, FileMode.Open, FileAccess.Read);
- reader = new XmlTextReader (fs);
- ReadConfigFile (reader, fileName);
- /* } catch (ConfigurationException) {
- throw;
- } catch (Exception e) {
- throw new ConfigurationException ("Error reading " + fileName, e);
- */ } finally {
- if (reader != null)
- reader.Close();
- }
- #endif
- return true;
- }
- #if (XML_DEP)
- internal void ReadConfigFile (XmlTextReader reader, string fileName)
- {
- reader.MoveToContent ();
- if (reader.NodeType != XmlNodeType.Element || reader.Name != "configuration")
- ThrowException ("Configuration file does not have a valid root element", reader);
- if (reader.HasAttributes)
- ThrowException ("Unrecognized attribute in root element", reader);
- if (reader.IsEmptyElement) {
- reader.Skip ();
- return;
- }
-
- reader.ReadStartElement ();
- reader.MoveToContent ();
- if (reader.LocalName == "configSections") {
- if (reader.HasAttributes)
- ThrowException ("Unrecognized attribute in <configSections>.", reader);
-
- rootGroup.ReadConfig (this, reader);
- }
-
- rootGroup.ReadRootData (reader, this);
- }
-
- #endif
- private void ThrowException (string text, XmlTextReader reader)
- {
- throw new ConfigurationException (text, fileName, reader.LineNumber);
- }
- }
- }
- #endif
|