/// <summary>ValueType factory method using enumeration.</summary>
        public static ValueType NewValueType(ValueTypeName type)
        {
            ValueType op = null;

            switch (type)
            {
            case ValueTypeName.Boolean:
                op = new BoolValueType();
                break;

            case ValueTypeName.Enum:
                op = new EnumValueType();
                break;

            case ValueTypeName.Number:
                op = new NumberValueType();
                break;

            case ValueTypeName.DateTime:
                op = new DateTimeValueType();
                break;

            case ValueTypeName.String:
            default:
                op = new StringValueType();
                break;
            }

            return(op);
        }
Exemplo n.º 2
0
        /// <summary>Deserializes XML into a SchemaTree instance.</summary>
        public void ReadXml(XmlReader reader)
        {
            // stack to hold parent nodes
            Stack <Entity> stack = new Stack <Entity>();

            if (reader.MoveToContent() != XmlNodeType.Element && reader.Name != "schema")
            {
                return;
            }

            this.Application = reader.GetAttribute("application");

            // move to root entity node and init
            bool   results = reader.ReadToDescendant("entity");
            string name    = reader.GetAttribute("name");
            string field   = reader.GetAttribute("table");

            this._root = new Entity(name, field);

            Entity current = this._root;

            // recurse tree from the root
            while (reader.Read())
            {
                // process start elements
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name == "entity")
                    {
                        // create a new entity
                        name  = reader.GetAttribute("name");
                        field = reader.GetAttribute("table");

                        // push the current entity on to a stack
                        stack.Push(current);
                        current = new Entity(name, field);

                        #region [old code]
                        //current.FkField = reader.GetAttribute( "fkField" );
                        //current.ParentIdField = reader.GetAttribute( "parentIdField" );
                        #endregion [old code]

                        #region [new code]
                        // split the parent and child fields by ',' - yea I know this is bad XML design
                        current.ParentIdFields = reader.GetAttribute("parentIdField").Replace(" ", null).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        current.ChildIdFields  = reader.GetAttribute("fkField").Replace(" ", null).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        //current.FkField = current.ChildIdFields[0];
                        //current.ParentIdField = current.ParentIdFields[0];
                        #endregion [new code]

                        // get the optional join attribute
                        string joinValue = reader.GetAttribute("join");
                        if (joinValue != null)
                        {
                            current.Join = (JoinType)Enum.Parse(typeof(JoinType), joinValue, true);
                        }
                    }
                    else if (reader.Name == "attribute")
                    {
                        // get the attribute's properties
                        name  = reader.GetAttribute("name");
                        field = reader.GetAttribute("field");
                        string flagsStr = reader.GetAttribute("flags");

                        // move to the value type node
                        do
                        {
                            reader.Read();
                        } while (reader.NodeType != XmlNodeType.Element);

                        // reference to a new value type
                        ValueType vt = null;

                        // deserialize the value type information
                        #region [process each value type]
                        switch (reader.Name)
                        {
                        // boolean operand value type
                        case "boolean":
                            vt = new BoolValueType();
                            break;

                        // number operand value type
                        case "number":
                            vt = new NumberValueType();
                            if (reader.HasAttributes)
                            {
                                // set min/max
                            }
                            break;

                        // string operand value type
                        case "string":
                            vt = new StringValueType();
                            if (reader.HasAttributes)
                            {
                                ((StringValueType)vt).Filter = reader.GetAttribute("filter");
                            }
                            break;

                        // datetime operand value type
                        case "dateTime":
                            vt = new DateTimeValueType();
                            break;

                        // enum operand value type
                        case "enum":
                            vt = new EnumValueType();
                            bool stringItems = Convert.ToBoolean(reader.GetAttribute("stringitems"));
                            ((EnumValueType)vt).IsItemsString = stringItems;
                            ((EnumValueType)vt).Items         = new List <EnumItem>();

                            // read each item
                            while (reader.NodeType != XmlNodeType.EndElement || reader.Name != "enum")
                            {
                                reader.Read();

                                if (reader.Name == "item")
                                {
                                    EnumItem item = new EnumItem(reader.GetAttribute("text"),
                                                                 reader.GetAttribute("value"));
                                    string order = reader.GetAttribute("order");
                                    if (!String.IsNullOrEmpty(order))
                                    {
                                        item.Order = Convert.ToInt32(order);
                                    }
                                    ((EnumValueType)vt).Items.Add(item);
                                }
                            }
                            break;

                        default:
                            break;
                        }
                        #endregion

                        // add this attribute to the current entity
                        Attribute attr = new Attribute(name, field, vt);
                        if (!String.IsNullOrEmpty(flagsStr))
                        {
                            attr.Options = (ElementFlag)Enum.Parse(typeof(ElementFlag), flagsStr, true);
                        }
                        current.AddChild(attr);

                        // move to end element
                        reader.ReadToNextSibling("dummy");
                    }
                    else
                    {
                        continue;
                    }
                }
                // process end elements
                else if (reader.NodeType == XmlNodeType.EndElement)
                {
                    if (reader.Name == "entity")
                    {
                        if (current.Equals(this._root))
                        {
                            continue;
                        }

                        // add current as a child of the previous entity
                        Entity parent = stack.Pop();
                        parent.AddChild(current);
                        current = parent;
                    }
                }
                else
                {
                    continue;
                }
            }
        }