using System; using System.Xml; using System.Collections.Specialized; namespace IBatisNet.Common.Xml { /// /// Summary description for NodeUtils. /// public class NodeUtils { /// /// Searches for the attribute with the specified name in this attributes list. /// /// /// The key /// public static string GetStringAttribute(NameValueCollection attributes, string name) { string value = attributes[name]; if (value == null) { return string.Empty; } else { return value; } } /// /// Searches for the attribute with the specified name in this attributes list. /// /// /// The key /// The default value to be returned if the attribute is not found. /// public static string GetStringAttribute(NameValueCollection attributes, string name, string def) { string value = attributes[name]; if (value == null) { return def; } else { return value; } } /// /// Searches for the attribute with the specified name in this attributes list. /// /// /// The key /// The default value to be returned if the attribute is not found. /// public static byte GetByteAttribute(NameValueCollection attributes, string name, byte def) { string value = attributes[name]; if (value == null) { return def; } else { return XmlConvert.ToByte(value); } } /// /// Searches for the attribute with the specified name in this attributes list. /// /// /// The key /// The default value to be returned if the attribute is not found. /// public static int GetIntAttribute(NameValueCollection attributes, string name, int def) { string value = attributes[name]; if (value == null) { return def; } else { return XmlConvert.ToInt32(value); } } /// /// Searches for the attribute with the specified name in this attributes list. /// /// /// The key /// The default value to be returned if the attribute is not found. /// public static bool GetBooleanAttribute(NameValueCollection attributes, string name, bool def) { string value = attributes[name]; if (value == null) { return def; } else { return XmlConvert.ToBoolean(value); } } /// /// /// /// /// public static NameValueCollection ParseAttributes(XmlNode node) { return ParseAttributes(node, null); } /// /// /// /// /// /// public static NameValueCollection ParseAttributes(XmlNode node, NameValueCollection variables) { NameValueCollection attributes = new NameValueCollection(); for (int i = 0; i < node.Attributes.Count; i++) { XmlAttribute attribute = node.Attributes[i]; String value = ParsePropertyTokens(attribute.Value, variables); attributes.Add(attribute.Name, value); } return attributes; } /// /// Replace properties by their values in the given string /// /// /// /// public static string ParsePropertyTokens(string str, NameValueCollection properties) { string OPEN = "${"; string CLOSE = "}"; string newString = str; if (newString != null && properties != null) { int start = newString.IndexOf(OPEN); int end = newString.IndexOf(CLOSE); while (start > -1 && end > start) { string prepend = newString.Substring(0, start); string append = newString.Substring(end + CLOSE.Length); int index = start + OPEN.Length; string propName = newString.Substring(index, end-index); string propValue = properties.Get(propName); if (propValue == null) { newString = prepend + propName + append; } else { newString = prepend + propValue + append; } start = newString.IndexOf(OPEN); end = newString.IndexOf(CLOSE); } } return newString; } } }