using System; using System.Collections.Generic; using System.Text; using System.Xml; using System.ComponentModel; namespace VEPROMS.CSLA.Library { #region vlnFormatDocument public class vlnFormatDocument:XmlDocument { private static XmlElement mydocele; public vlnFormatDocument(Format myFormat) { _MyFormat = myFormat; LoadXml(MyFormat.Data); mydocele = this.DocumentElement; } private Format _MyFormat; public Format MyFormat { get { return _MyFormat; } set { _MyFormat = value; } } public static XmlNode LookupSingleNode(XmlNode xmlNode, string path) { //original: //if (xmlNode == null) return null; //XmlNode xn = xmlNode.SelectSingleNode(path); //if (xn != null) return xn; //if (xmlNode.Name == "Step") xn = LookupSingleStepNode(xmlNode, path); //if (xn != null) return xn; //if (path.StartsWith("Font")) return LookupSingleFontNode(xmlNode, path); //return InheritLookup(xmlNode, path); if (path.StartsWith("Font")) { XmlNode xn = null; if (xmlNode != null) { xn = xmlNode.SelectSingleNode(path); if (xn != null) return xn; } else { xmlNode = LookupSingleNode(mydocele.FirstChild, "/PlantFormat/FormatData"); } return LookupSingleFontNode(xmlNode, path); } else { if (xmlNode == null) return null; XmlNode xn = xmlNode.SelectSingleNode(path); if (xn != null) return xn; if (xmlNode.Name == "Step") xn = LookupSingleStepNode(xmlNode, path); if (xn != null) return xn; if (path.StartsWith("Font")) return LookupSingleFontNode(xmlNode, path); return InheritLookup(xmlNode, path, false); } } public static XmlNode LookupSingleFontNode(XmlNode xmlNode, string path) { if (xmlNode == null) return null; XmlNode tmpNode = xmlNode; XmlNode xn = xmlNode.SelectSingleNode(path); while (xn == null)//Walk-up the format tree to find a font node { if (xmlNode.NodeType == XmlNodeType.Document) { return LookupSingleNode(tmpNode, "/PlantFormat/FormatData/" + path); } xmlNode = xmlNode.ParentNode; xn = xmlNode.SelectSingleNode(path); } return xn; } public static XmlNode LookupSingleStepNode(XmlNode xmlNode, string path) { if (xmlNode == null) return null; XmlNode xn = xmlNode.SelectSingleNode(path); XmlNode tmpNode = xmlNode; XmlNode tmpNode2 = xmlNode; while (xn == null) //Walk-up the format tree to find a step node { tmpNode = xmlNode; xmlNode = xmlNode.ParentNode.SelectSingleNode(string.Format("Step[@Type='{0}']",xmlNode.Attributes["ParentType"].InnerText)); if (xmlNode == null && path.StartsWith("Font")) return LookupSingleFontNode(tmpNode, path); if (xmlNode == null) return InheritLookup(tmpNode2, path, true); xn = xmlNode.SelectSingleNode(path); } return xn; } public static XmlNodeList LookupNodes(XmlNode xmlNode, string path) { if (xmlNode == null) return null; XmlNodeList xl = xmlNode.SelectNodes(path); if (xl == null||xl.Count==0) xl = InheritLookupNodes(xmlNode, path); if (xl == null) return null; return xl; } public static string Lookup(XmlNode xmlNode, string path) { //if (xmlNode == null) return null; XmlNode xn = LookupSingleNode(xmlNode, path); //if (xn == null) xn = InheritLookup(xmlNode, path); if (xn == null) return null; return xn.InnerText; } //public static string StepLookup(XmlNode xmlNode, string path) //{ // //if (xmlNode == null) return null; // XmlNode xn = LookupSingleStepNode(xmlNode, path); // //if (xn == null) xn = InheritLookup(xmlNode, path); // if (xn == null) return null; // return xn.InnerText; //} public static T EnumLookup(XmlNode xmlNode, string path) { string str = Lookup(xmlNode,path); if (str == null) return default(T); return (T)Enum.Parse(typeof(T), str); } public static int? IntLookup(XmlNode xmlNode, string path) { //if (xmlNode == null) return null; //XmlNode xn = LookupSingleNode(xmlNode, path); //if (xn == null) xn = InheritLookup(xmlNode, path); //if (xn == null) return null; //return int.Parse(xn.InnerText); string str = Lookup(xmlNode, path); if (str == null) return null; return int.Parse(str); } public static int SiblingCount(XmlNode xmlNode) { int retval = 0; string nodeName = xmlNode.Name; for (XmlNode xn = xmlNode; xn != null; xn = xn.PreviousSibling) { if (xn.Name == nodeName) retval++; } return retval; } public static string Path(XmlNode xmlNode) { // Walk the path string parentPath = (xmlNode.ParentNode == xmlNode.OwnerDocument ? "" : Path(xmlNode.ParentNode)); // Count Siblings with the Same Name int sibCount = SiblingCount(xmlNode); return string.Format("{0}/{1}[{2}]", parentPath, xmlNode.Name, sibCount); } public static XmlNode InheritLookup(XmlNode xmlNode, string path, bool stepLookup) { stepLookup = false; // 8/2 change for inheritance if (xmlNode == null) return null;// No path to match string xPath = Path(xmlNode);// Build xPath from xmlNode vlnFormatDocument fd = (vlnFormatDocument)(xmlNode.OwnerDocument);//First Get the vlnFormatDocument while(fd.MyFormat.MyParent != null) { fd = fd.MyFormat.MyParent.PlantFormat.XmlDoc;// Get the parents vlnFormatDocument if (fd != null) { XmlNode xp; if (path.StartsWith("/")) xp = fd.DocumentElement; else xp = fd.SelectSingleNode(xPath);// Get the related node if (xp != null) { XmlNode xn = null; if (stepLookup) xn = LookupSingleStepNode(xp, path); else xn = xp.SelectSingleNode(path); if(xn != null) return xn; } } } return null; } public static XmlNodeList InheritLookupNodes(XmlNode xmlNode, string path) { if (xmlNode == null) return null;// No path to match string xPath = Path(xmlNode);// Build xPath from xmlNode vlnFormatDocument fd = (vlnFormatDocument)(xmlNode.OwnerDocument);//First Get the vlnFormatDocument while (fd.MyFormat.MyParent != null) { fd = fd.MyFormat.MyParent.PlantFormat.XmlDoc;// Get the parents vlnFormatDocument if (fd != null) { XmlNode xp = fd.SelectSingleNode(xPath);// Get the related node if (xp != null) { XmlNodeList xl = xp.SelectNodes(path); if (xl != null) return xl; } } } return null; } } #endregion #region vlnFormatItem [TypeConverter(typeof(ExpandableObjectConverter))] public class vlnFormatItem { public vlnFormatItem(XmlNode xmlNode) { _XmlNode = xmlNode; } public vlnFormatItem() { } XmlNode _XmlNode; internal XmlNode XmlNode { get { return _XmlNode; } set { _XmlNode = value; } } public virtual string GetPDDisplayName() { return ToString(); } public virtual string GetPDName() { return ToString(); } public virtual string GetPDDescription() { return ToString(); } public virtual string GetPDCategory() { return ToString(); } public XmlNodeList SelectNodes(string path) { return vlnFormatDocument.LookupNodes(_XmlNode, path); } public XmlNode SelectSingleNode(string path) { return vlnFormatDocument.LookupSingleNode(_XmlNode, path); } public XmlNode SelectSingleFontNode(string path) { return vlnFormatDocument.LookupSingleFontNode(_XmlNode, path); } public string Lookup(string path) { return vlnFormatDocument.Lookup(_XmlNode, path); } public string Lookup(string path, ref string local) { return (local != null? local : local = Lookup(path)); } //public string StepLookup(string path) //{ // return vlnFormatDocument.StepLookup(_XmlNode, path); //} public int? IntLookup(string path) { return vlnFormatDocument.IntLookup(_XmlNode, path); } public int? IntLookup(string path, ref int? local) { return (local != null ? local : local = IntLookup(path)); } public T EnumLookup(string path) { return vlnFormatDocument.EnumLookup(_XmlNode, path); } public bool LazyLoad(ref LazyLoad ll, string xPath) { if (ll == null) { XmlNode xn = SelectSingleNode(xPath); string tstr = xn != null ? xn.InnerText : "FALSE"; ll = new LazyLoad(tstr.ToUpper() == "TRUE" ? true : false); } return ll.Value; } public string LazyLoad(ref LazyLoad ll, string xPath) { if (ll == null) { XmlNode xn = SelectSingleNode(xPath); ll = new LazyLoad(xn != null ? xn.InnerText : null); } return ll.Value; } public int? LazyLoad(ref LazyLoad ll, string xPath) { if (ll == null) { XmlNode xn = SelectSingleNode(xPath); ll = new LazyLoad(xn != null ? (int?)int.Parse(xn.InnerText) : null); } return ll.Value; } public Nullable LazyLoad(ref LazyLoad> ll, string xPath) where T:struct { if (ll == null) { XmlNode xn = SelectSingleNode(xPath); ll = new LazyLoad>(xn != null ? (Nullable)Enum.Parse(typeof(T), xn.InnerText) : null); } return ll.Value; } } #endregion #region LazyLoad public class LazyLoad { public LazyLoad(T value) { _Value = value; } private T _Value; public T Value { get { return _Value; } set { _Value = value; } } } #endregion #region vlnFormatList, new() public class vlnFormatList : List, ICustomTypeDescriptor where T : vlnFormatItem, new() { private XmlNodeList _XmlNodeList; internal XmlNodeList XmlNodeList { get { return _XmlNodeList; } set { _XmlNodeList = value; } } public vlnFormatList(XmlNodeList xmlNodeList) { if (xmlNodeList == null) return; _XmlNodeList = xmlNodeList; foreach (XmlNode xn in _XmlNodeList) { T tt = new T(); tt.XmlNode = xn; Add(tt); } } #region ICustomTypeDescriptor public String GetClassName() { return TypeDescriptor.GetClassName(this, true); } public AttributeCollection GetAttributes() { return TypeDescriptor.GetAttributes(this, true); } public String GetComponentName() { return TypeDescriptor.GetComponentName(this, true); } public TypeConverter GetConverter() { return TypeDescriptor.GetConverter(this, true); } public EventDescriptor GetDefaultEvent() { return TypeDescriptor.GetDefaultEvent(this, true); } public PropertyDescriptor GetDefaultProperty() { return TypeDescriptor.GetDefaultProperty(this, true); } public object GetEditor(Type editorBaseType) { return TypeDescriptor.GetEditor(this, editorBaseType, true); } public EventDescriptorCollection GetEvents(Attribute[] attributes) { return TypeDescriptor.GetEvents(this, attributes, true); } public EventDescriptorCollection GetEvents() { return TypeDescriptor.GetEvents(this, true); } public object GetPropertyOwner(PropertyDescriptor pd) { return this; } public PropertyDescriptorCollection GetProperties(Attribute[] attributes) { return GetProperties(); } public virtual PropertyDescriptorCollection GetProperties() { // Create a collection object to hold property descriptors PropertyDescriptorCollection pds = new PropertyDescriptorCollection(null); // Iterate the list int i = 0; for (i = 0; i < this.Count; i++) { // Create a property descriptor for the item and add to the property descriptor collection pds.Add(new vlnPropertyDescriptor, T>(this, i)); } // return the property descriptor collection return pds; } #endregion } #endregion #region vlnListConverter internal class vlnListConverter : ExpandableObjectConverter where T : vlnFormatList where C : vlnFormatItem, new() { private string Plural(string name) { if (name.EndsWith("y")) return name.Substring(0, name.Length - 1) + "ies"; if (name.EndsWith("ss")) return name + "es"; else return name + "s"; } public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destType) { if (destType == typeof(string) && value is T) { // Return department and department role separated by comma. return ((T)value).Count.ToString() + " " + (((T)value).Count == 1 ? typeof(C).Name : Plural(typeof(C).Name)); } return base.ConvertTo(context, culture, value, destType); } } #endregion #region vlnPropertyDescriptor public class vlnPropertyDescriptor : PropertyDescriptor where T : vlnFormatList where C : vlnFormatItem, new() { protected C _Item = null; public vlnPropertyDescriptor(T itemList, int index) : base("#" + index.ToString(), null) { _Item = itemList[index]; } public override bool CanResetValue(object component) { return true; } public override Type ComponentType { get { return _Item.GetType(); } } public override object GetValue(object component) { return _Item; } public override bool IsReadOnly { get { return true; } } public override Type PropertyType { get { return _Item.GetType(); } } public override void ResetValue(object component) { ;} public override bool ShouldSerializeValue(object component) { return true; } public override void SetValue(object component, object value) { /*_Item = value*/;} //public override AttributeCollection Attributes //{ get { return new AttributeCollection(null); } } public override string DisplayName { get { return _Item.GetPDDisplayName(); } } public override string Description { get { return _Item.GetPDDescription(); } } public override string Name { get { return _Item.GetPDName(); } } public override string Category { get { return _Item.GetPDCategory(); } } } // Class #endregion }