3 // Marek Habersack (mhabersack@novell.com)
5 // (C) 2007 Novell, Inc
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System
.Collections
.Generic
;
32 using System
.Xml
.XPath
;
34 namespace Mono
.MonoConfig
41 public string Contents
{
42 get { return contents; }
45 public FeatureTarget Target
{
46 get { return target; }
49 public DefaultNode (string contents
, FeatureTarget target
)
51 this.contents
= contents
;
56 public class DefaultNodeHandler
: IDocumentNodeHandler
, IDefaultContainer
, IStorageConsumer
62 Dictionary
<string, DefaultNode
> storage
;
64 public void ReadConfiguration (XPathNavigator nav
)
66 section
= Helpers
.GetRequiredNonEmptyAttribute (nav
, "section");
67 target
= Helpers
.ConvertEnum
<FeatureTarget
> (Helpers
.GetRequiredNonEmptyAttribute (nav
, "target"), "target");
69 XPathNodeIterator iter
= nav
.Select ("./text()");
70 StringBuilder sb
= new StringBuilder ();
72 while (iter
.MoveNext ())
73 sb
.Append (iter
.Current
.Value
);
75 contents
= sb
.ToString ();
78 public void StoreConfiguration ()
82 DefaultNode dn
= new DefaultNode (contents
, target
);
84 if (storage
.ContainsKey (section
))
85 storage
[section
] = dn
;
87 storage
.Add (section
, dn
);
94 public string FindDefault (string sectionName
, FeatureTarget target
)
98 if (storage
.ContainsKey (sectionName
)) {
99 DefaultNode dn
= storage
[sectionName
];
101 if (target
== FeatureTarget
.Any
|| dn
.Target
== FeatureTarget
.Any
|| dn
.Target
== target
)
108 public void SetStorage (object storage
)
110 this.storage
= storage
as Dictionary
<string, DefaultNode
>;
111 if (this.storage
== null)
112 throw new ApplicationException ("Invalid storage type");
115 void AssertStorage ()
118 throw new ApplicationException ("No storage attached");