2010-05-19 Jb Evain <jbevain@novell.com>
[mcs.git] / tools / mconfig / Mono.MonoConfig / DefaultNodeHandler.cs
blob9a2058555889237cc002c449ec14e7d25887cb32
1 //
2 // Authors:
3 // Marek Habersack (mhabersack@novell.com)
4 //
5 // (C) 2007 Novell, Inc
6 //
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
28 using System;
29 using System.Collections.Generic;
30 using System.Text;
31 using System.Xml;
32 using System.Xml.XPath;
34 namespace Mono.MonoConfig
36 class DefaultNode
38 string contents;
39 FeatureTarget target;
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;
52 this.target = target;
56 public class DefaultNodeHandler : IDocumentNodeHandler, IDefaultContainer, IStorageConsumer
58 string section;
59 string contents;
60 FeatureTarget target;
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);
74 if (sb.Length > 0)
75 contents = sb.ToString ();
78 public void StoreConfiguration ()
80 AssertStorage ();
82 DefaultNode dn = new DefaultNode (contents, target);
84 if (storage.ContainsKey (section))
85 storage [section] = dn;
86 else
87 storage.Add (section, dn);
89 section = null;
90 contents = null;
91 storage = null;
94 public string FindDefault (string sectionName, FeatureTarget target)
96 AssertStorage ();
98 if (storage.ContainsKey (sectionName)) {
99 DefaultNode dn = storage [sectionName];
101 if (target == FeatureTarget.Any || dn.Target == FeatureTarget.Any || dn.Target == target)
102 return dn.Contents;
105 return null;
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 ()
117 if (storage == null)
118 throw new ApplicationException ("No storage attached");