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
;
30 using System
.ComponentModel
;
33 using System
.Xml
.XPath
;
35 namespace Mono
.MonoConfig
37 public static class Helpers
39 public const string FakeRootName
= "F_a_K_e_R_o_O_t_M_o_N_o_C_o_N_f_I_g_N_o_D_e";
41 public static EnumType ConvertEnum
<EnumType
> (string value, string attrName
)
44 EnumConverter cvt
= new EnumConverter (typeof (EnumType
));
45 return (EnumType
) cvt
.ConvertFromInvariantString (value);
47 throw new ApplicationException (
48 String
.Format ("Failed to parse the '{0}' attribute '{1}'", attrName
, value));
52 public static string BreakLongLine (string line
, string indent
, int maxLineWidth
)
54 StringBuilder sb
= new StringBuilder ();
56 int lineLen
= line
.Length
;
57 int segments
= lineLen
/ maxLineWidth
;
59 int segmentLen
= maxLineWidth
- 1;
62 while (segments
-- >= 0) {
63 idx
= line
.LastIndexOf (' ', segmentStart
+ segmentLen
);
65 segmentLen
= idx
- segmentStart
;
69 sb
.AppendFormat ("{0}{1}\n", indent
, line
.Substring (segmentStart
, segmentLen
));
70 segmentStart
= idx
+ 1;
71 if (lineLen
- segmentStart
> maxLineWidth
)
72 segmentLen
= maxLineWidth
;
74 segmentLen
= lineLen
- segmentStart
- 1;
77 return sb
.ToString ();
80 public static string GetRequiredNonEmptyAttribute (XPathNavigator node
, string name
)
82 string val
= GetOptionalAttribute (node
, name
);
84 if (String
.IsNullOrEmpty (val
))
85 ThrowMissingRequiredAttribute (node
, name
);
89 public static string GetOptionalAttribute (XPathNavigator node
, string name
)
91 return node
.GetAttribute (name
, String
.Empty
);
94 static void ThrowMissingRequiredAttribute (XPathNavigator node
, string name
)
96 throw new ApplicationException (String
.Format ("Element '{0}' is missing required attribute '{1}'",
97 node
.LocalName
, name
));
100 public static void BuildSectionTree (XPathNodeIterator iter
, Section section
)
102 XPathNavigator nav
, tmp
;
103 XPathNodeIterator children
;
104 List
<Section
> sectionChildren
= section
.Children
;
105 Section newSection
, curSection
;
107 while (iter
.MoveNext ()) {
109 children
= nav
.Select ("section[string-length(@name) > 0]");
110 curSection
= new Section (nav
);
112 while (children
.MoveNext ()) {
113 tmp
= children
.Current
;
114 newSection
= new Section (tmp
);
115 BuildSectionTree (tmp
.Select ("section[string-length(@name) > 0]"), newSection
);
116 curSection
.Children
.Add (newSection
);
118 sectionChildren
.Add (curSection
);
122 public static XmlDocument
FindDefault (IDefaultContainer
[] defaults
, string name
, FeatureTarget target
)
126 if (defaults
== null || (len
= defaults
.Length
) == 0)
129 IDefaultContainer cur
;
132 for (int i
= 0; i
< len
; i
++) {
134 text
= cur
.FindDefault (name
, target
);
142 XmlDocument ret
= new XmlDocument ();
143 ret
.LoadXml (String
.Format ("<{0}>{1}</{0}>", FakeRootName
, text
));
148 public static ConfigBlockBlock
FindConfigBlock (IConfigBlockContainer
[] configBlocks
, string name
)
152 if (configBlocks
== null || (len
= configBlocks
.Length
) == 0)
155 IConfigBlockContainer cur
;
156 ConfigBlockBlock ret
= null;
158 for (int i
= 0; i
< len
; i
++) {
159 cur
= configBlocks
[i
];
160 ret
= cur
.FindConfigBlock (name
);
168 public static void SaveXml (XmlDocument doc
, string targetFile
)
170 XmlWriterSettings settings
= new XmlWriterSettings ();
171 settings
.CloseOutput
= true;
172 settings
.CheckCharacters
= true;
173 settings
.Indent
= true;
174 settings
.Encoding
= Encoding
.UTF8
;
175 settings
.IndentChars
= "\t";
176 settings
.NewLineHandling
= NewLineHandling
.Replace
;
178 XmlWriter writer
= null;
180 writer
= XmlWriter
.Create (targetFile
, settings
);
183 } catch (Exception ex
) {
184 throw new ApplicationException (
185 String
.Format ("Failed to write XML file {1}", targetFile
), ex
);