2007-11-10 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / tools / mconfig / Mono.MonoConfig / DefaultNodeHandler.cs
1 //
2 // Authors:
3 //   Marek Habersack (mhabersack@novell.com)
4 //
5 // (C) 2007 Novell, Inc
6 //
7
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.
27 //
28 using System;
29 using System.Collections.Generic;
30 using System.Text;
31 using System.Xml;
32 using System.Xml.XPath;
33
34 namespace Mono.MonoConfig
35 {
36         class DefaultNode
37         {
38                 string contents;
39                 FeatureTarget target;
40
41                 public string Contents {
42                         get { return contents; }
43                 }
44
45                 public FeatureTarget Target {
46                         get { return target; }
47                 }
48
49                 public DefaultNode (string contents, FeatureTarget target)
50                 {
51                         this.contents = contents;
52                         this.target = target;
53                 }
54         }
55         
56         public class DefaultNodeHandler : IDocumentNodeHandler, IDefaultContainer, IStorageConsumer
57         {
58                 string section;
59                 string contents;
60                 FeatureTarget target;
61                 
62                 Dictionary <string, DefaultNode> storage;
63                 
64                 public void ReadConfiguration (XPathNavigator nav)
65                 {
66                         section = Helpers.GetRequiredNonEmptyAttribute (nav, "section");
67                         target = Helpers.ConvertEnum <FeatureTarget>  (Helpers.GetRequiredNonEmptyAttribute (nav, "target"), "target");
68                         
69                         XPathNodeIterator iter = nav.Select ("./text()");
70                         StringBuilder sb = new StringBuilder ();
71
72                         while (iter.MoveNext ())
73                                 sb.Append (iter.Current.Value);
74                         if (sb.Length > 0)
75                                 contents = sb.ToString ();
76                 }
77                 
78                 public void StoreConfiguration ()
79                 {
80                         AssertStorage ();
81
82                         DefaultNode dn = new DefaultNode (contents, target);
83                         
84                         if (storage.ContainsKey (section))
85                                 storage [section] = dn;
86                         else
87                                 storage.Add (section, dn);
88
89                         section = null;
90                         contents = null;
91                         storage = null;
92                 }
93
94                 public string FindDefault (string sectionName, FeatureTarget target)
95                 {
96                         AssertStorage ();
97
98                         if (storage.ContainsKey (sectionName)) {
99                                 DefaultNode dn = storage [sectionName];
100                                 
101                                 if (target == FeatureTarget.Any || dn.Target == FeatureTarget.Any || dn.Target == target)
102                                         return dn.Contents;
103                         }
104                         
105                         return null;
106                 }
107
108                 public void SetStorage (object storage)
109                 {
110                         this.storage = storage as Dictionary <string, DefaultNode>;
111                         if (this.storage == null)
112                                 throw new ApplicationException ("Invalid storage type");
113                 }
114                 
115                 void AssertStorage ()
116                 {
117                         if (storage == null)
118                                 throw new ApplicationException ("No storage attached");
119                 }
120         }
121 }