Example #1
0
        /// <summary> Using an XmlNode creates a ComplexType using the appropriate info. </summary>
        /// <param name="xNode"> The XmlNode that was spotted to be a ComplexType. </param>
        private static void _createComplexType(XmlNode xNode)
        {
            //Information Structure
            //Name
            string name = XsdParser.GetName(xNode);
            Unit unit = null;
            string defaultUnit = "";
            ElementConstant constant = null;
            ElementVariable variable = null;
            ElementFunction function = null;
            ElementKeyword keyword = null;
            ElementReference reference = null;

            //Avoid unexpected situations
            if (!xNode.HasChildNodes)
                return;

            //Draw information
            foreach (XmlNode x in xNode.ChildNodes)
            {
                SimpleType sType;

                // Ignore comments.
                if (x.NodeType == XmlNodeType.Comment)
                    continue;

                // Crete elements.
                if (x.LocalName == "choice")
                {
                    foreach (XmlNode xc in x.ChildNodes)
                    {
                        string defaultValue;

                        // Check
                        if (xc.LocalName != "element") continue;
                        Debug.Assert(xc.Attributes != null, "xc.Attributes != null");

                        // Create Elements with their default values
                        switch (xc.Attributes["name"].Value.Trim())
                        {
                            case "constant":
                                sType = Structure.Structure.FindSimpleType(xc.Attributes["type"].Value.Trim());
                                defaultValue = xc.Attributes["default"] != null ?
                                                   xc.Attributes["default"].Value.Trim() : "";
                                constant = new ElementConstant(sType, defaultValue);
                                break;
                            case "variable":
                                sType = Structure.Structure.FindSimpleType(xc.Attributes["type"].Value.Trim());
                                defaultValue = xc.Attributes["default"] != null ?
                                                   xc.Attributes["default"].Value.Trim() : "";
                                variable = new ElementVariable(sType, defaultValue);
                                break;
                            case "function":
                                if (xc.HasChildNodes && xc.FirstChild.HasChildNodes && xc.FirstChild.FirstChild.HasChildNodes)
                                    if (xc.FirstChild.FirstChild.LocalName == "choice")
                                    {
                                        List<string> functionNames = new List<string>();
                                        foreach (XmlNode xcf in xc.FirstChild.FirstChild.ChildNodes)
                                            if (xcf.LocalName == "element")
                                            {
                                                Debug.Assert(xcf.Attributes != null, "xcf.Attributes != null");
                                                functionNames.Add(xcf.Attributes["ref"].Value.Trim());
                                            }
                                        function = new ElementFunction(functionNames);
                                    }
                                break;
                            case "keyword":
                                sType = Structure.Structure.FindSimpleType(xc.Attributes["type"].Value.Trim());
                                defaultValue = xc.Attributes["default"] != null ?
                                                   xc.Attributes["default"].Value.Trim() : "";
                                keyword = new ElementKeyword(sType, defaultValue);
                                break;
                        }
                    }
                }
                //Get Units & Default values
                else if (x.LocalName == "attribute")
                {
                    Debug.Assert(x.Attributes != null, "x.Attributes != null");
                    if (x.LocalName == "attribute" && x.Attributes["name"].Value.Trim() == "unit")
                    {
                        unit = Structure.Structure.FindUnit(x.Attributes["type"].Value.Trim());
                        defaultUnit = x.Attributes["default"].Value.Trim();
                        if (constant != null)
                            constant.SetUnit(unit, defaultUnit);
                        if (variable != null)
                            variable.SetUnit(unit, defaultUnit);
                    }
                    else if (x.LocalName == "attribute" && x.Attributes["name"].Value.Trim() == "x_unit")
                    {
                        if (variable == null) continue;
                        var xUnit = Structure.Structure.FindUnit(x.Attributes["type"].Value.Trim());
                        var defaultXUnit = x.Attributes["default"].Value.Trim();
                        variable.SetX_Unit(xUnit, defaultXUnit);
                    }
                        //Handle References
                    else if (x.LocalName == "attribute" && x.Attributes["name"].Value.Trim() == "reference")
                    {
                        sType = Structure.Structure.FindSimpleType(x.Attributes["type"].Value.Trim());
                        reference = new ElementReference(sType);
                    }
                }
                else
                {
                    Debug.WriteLine("Unhandled XSD Element named '{0}' inside '{1}'", x.LocalName, name);
                }
            }

            //Finish
            Structure.Structure.Add(new ComplexType(name, new List<ElementType> { constant, variable, function, keyword, reference }));
        }
Example #2
0
        public ElementFunction DuplicateStructure()
        {
            var newElement = new ElementFunction(new List<string>(FunctionNames));

            return newElement;
        }