Beispiel #1
0
        private void ScanPropertyNode(XElement propertyElement, LoadContext lc)
        {
            string propertyName = propertyElement.Attribute("name").Value;
            string type         = propertyElement.Attribute("type").Value;
            string defaultValue = propertyElement.Attribute("default").Value;

            PropertyType propertyType;

            switch (type.ToLowerInvariant())
            {
            case "double":
                propertyType = PropertyType.Decimal;
                break;

            case "decimal":
                propertyType = PropertyType.Decimal;
                break;

            case "int":
                propertyType = PropertyType.Integer;
                break;

            case "bool":
                propertyType = PropertyType.Boolean;
                break;

            default:
                propertyType = PropertyType.String;
                break;
            }

            PropertyUnion propertyDefaultValue = new PropertyUnion(defaultValue, propertyType);

            lc.ParseContext.PropertyTypes.Add(propertyName, propertyDefaultValue.InternalType);
        }
Beispiel #2
0
        private Conditional <FlagOptions> ReadFlagOptionNode(XElement flagElement, LoadContext lc)
        {
            IConditionTreeItem conditions = ConditionTree.Empty;
            var conditionsAttribute       = flagElement.Attribute("conditions");

            if (conditionsAttribute != null)
            {
                IXmlLineInfo line = conditionsAttribute as IXmlLineInfo;

                try
                {
                    conditions = lc.ConditionParser.Parse(conditionsAttribute.Value, lc.ParseContext);
                }
                catch (ConditionFormatException ex)
                {
                    lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition + conditionsAttribute.Name.LocalName.Length + 2 + ex.PositionStart, LoadErrorCategory.Error, ex.Message));
                    return(null);
                }
            }

            FlagOptions theOptions = FlagOptions.None;

            foreach (var node in flagElement.Descendants(lc.NS + "option"))
            {
                theOptions |= (FlagOptions)Enum.Parse(typeof(FlagOptions), node.Value, true);
            }

            return(new Conditional <FlagOptions>(theOptions, conditions));
        }
        public static void LoadFromXml(this RenderPath command, XElement element, LoadContext lc)
        {
            if (element.Attribute("thickness") != null)
            {
                command.Thickness = double.Parse(element.Attribute("thickness").Value);
            }

            string start;

            if (element.GetAttribute("start", lc, out start))
            {
                command.Start = new ComponentPoint(start);
            }

            var fill = element.Attribute("fill");

            if (fill != null && fill.Value.ToLowerInvariant() != "false")
            {
                command.Fill = true;
            }

            string data;

            if (element.GetAttribute("data", lc, out data))
            {
                command.Commands = PathHelper.ParseCommands(data);
            }
        }
        public static void LoadFromXml(this Ellipse command, XElement element, LoadContext lc)
        {
            if (element.Attribute("thickness") != null)
                command.Thickness = double.Parse(element.Attribute("thickness").Value);

            var fill = element.Attribute("fill");
            if (fill != null && fill.Value.ToLowerInvariant() != "false")
                command.Fill = true;

            if (element.Attribute("centre") != null)
                command.Centre = new ComponentPoint(element.Attribute("centre").Value);
            else
            {
                string x = element.Attribute("x").Value;
                string y = element.Attribute("y").Value;
                command.Centre = new ComponentPoint(x, y);
            }

            string radius = "r";
            if (lc.FormatVersion <= new Version(1, 1))
                radius = "radius";

            string rx;
            if (element.GetAttribute(radius + "x", lc, out rx))
                command.RadiusX = double.Parse(rx);

            string ry;
            if (element.GetAttribute(radius + "y", lc, out ry))
                command.RadiusY = double.Parse(ry);
        }
Beispiel #5
0
        private void ReadDeclarationSection(XElement declaration, XNamespace ns, LoadContext lc, ComponentDescription description)
        {
            IXmlLineInfo line = declaration as IXmlLineInfo;

            // Read meta nodes
            foreach (var metaElement in declaration.Elements(ns + "meta"))
            {
                ReadMetaNode(metaElement, lc, description);
            }

            // Check all required metadata was set
            if (String.IsNullOrEmpty(description.ComponentName))
            {
                lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition, LoadErrorCategory.Error,
                                            "Component name is required"));
            }

            //Read properties
            List <ComponentProperty> properties = new List <ComponentProperty>();

            foreach (var propertyElement in declaration.Elements(ns + "property"))
            {
                ScanPropertyNode(propertyElement, lc);
            }
            foreach (var propertyElement in declaration.Elements(ns + "property"))
            {
                ComponentProperty property = ReadPropertyNode(propertyElement, lc);
                properties.Add(property);
            }
            description.Properties = properties.ToArray();

            // Read flags
            List <Conditional <FlagOptions> > flagOptions = new List <Conditional <FlagOptions> >();

            foreach (var flagGroup in declaration.Elements(ns + "flags"))
            {
                var flags = ReadFlagOptionNode(flagGroup, lc);
                if (flags != null)
                {
                    flagOptions.Add(flags);
                }
            }
            description.Flags = flagOptions.ToArray();

            // Read configurations
            var componentConfigurations = new List <ComponentConfiguration>();
            var configurations          = declaration.Element(ns + "configurations");

            if (configurations != null)
            {
                foreach (var node in configurations.Elements(ns + "configuration"))
                {
                    ComponentConfiguration newConfiguration = ReadConfigurationNode(node, description);

                    componentConfigurations.Add(newConfiguration);
                }
            }
            description.Metadata.Configurations.AddRange(componentConfigurations);
        }
Beispiel #6
0
        private void ReadConnectionsSection(XElement declaration, LoadContext lc, ComponentDescription description)
        {
            List <ConnectionGroup> parsedConnectionGroups = new List <ConnectionGroup>();
            var connectionGroupNodes = declaration.XPathSelectElements("/cd:connections/cd:group", lc.NamespaceManager);

            foreach (var connectionGroupNode in connectionGroupNodes)
            {
                IConditionTreeItem           conditionCollection = ConditionTree.Empty;
                List <ConnectionDescription> connections         = new List <ConnectionDescription>();

                var conditionsAttribute = connectionGroupNode.Attribute("conditions");
                if (conditionsAttribute != null)
                {
                    IXmlLineInfo line = conditionsAttribute as IXmlLineInfo;

                    try
                    {
                        conditionCollection = lc.ConditionParser.Parse(connectionGroupNode.Attribute("conditions").Value, lc.ParseContext);
                    }
                    catch (ConditionFormatException ex)
                    {
                        lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition + conditionsAttribute.Name.LocalName.Length + 2 + ex.PositionStart, LoadErrorCategory.Error, ex.Message));
                        continue;
                    }
                }

                foreach (var connectionNode in connectionGroupNode.Elements(ns + "connection"))
                {
                    ConnectionEdge edge = ConnectionEdge.None;
                    if (connectionNode.Attribute("edge") != null)
                    {
                        string edgeText = connectionNode.Attribute("edge").Value.ToLowerInvariant();
                        if (edgeText == "start")
                        {
                            edge = ConnectionEdge.Start;
                        }
                        else if (edgeText == "end")
                        {
                            edge = ConnectionEdge.End;
                        }
                        else if (edgeText == "both")
                        {
                            edge = ConnectionEdge.Both;
                        }
                    }
                    string connectionName = "#";
                    if (connectionNode.Attribute("name") != null)
                    {
                        connectionName = connectionNode.Attribute("name").Value;
                    }
                    connections.Add(new ConnectionDescription(new ComponentPoint(connectionNode.Attribute("start").Value), new ComponentPoint(connectionNode.Attribute("end").Value), edge, connectionName));
                }

                parsedConnectionGroups.Add(new ConnectionGroup(conditionCollection, connections.ToArray()));
            }

            description.Connections = parsedConnectionGroups.ToArray();
        }
        public static void LoadFromXml(this Line command, XElement element, LoadContext lc)
        {
            if (element.Attribute("thickness") != null)
                command.Thickness = double.Parse(element.Attribute("thickness").Value);

            string start;
            if (element.GetAttribute("start", lc, out start))
                command.Start = new ComponentPoint(start);

            string end;
            if (element.GetAttribute("end", lc, out end))
                command.End = new ComponentPoint(end);
        }
        public static void LoadFromXml(this Text command, XElement element, LoadContext lc)
        {
            if (element.Attribute("location") != null)
                command.Location = new ComponentPoint(element.Attribute("location").Value);
            else
            {
                string x = element.Attribute("x").Value;
                string y = element.Attribute("y").Value;
                command.Location = new ComponentPoint(x, y);
            }

            string tAlignment = "TopLeft";
            if (element.Attribute("align") != null)
                tAlignment = element.Attribute("align").Value;
            command.Alignment = (TextAlignment)Enum.Parse(typeof(TextAlignment), tAlignment, true);

            double size = ComponentHelper.SmallTextSize;
            if (element.Attribute("size") != null)
            {
                if (element.Attribute("size").Value.ToLowerInvariant() == "large")
                    size = ComponentHelper.LargeTextSize;
            }

            var textValueNode = element.Element(lc.NS + "value");
            if (textValueNode != null)
            {
                foreach (var spanNode in textValueNode.Elements())
                {
                    IXmlLineInfo line = spanNode as IXmlLineInfo;

                    string nodeValue = spanNode.Value;

                    if (spanNode.Name.LocalName == "span")
                        command.TextRuns.Add(new TextRun(nodeValue, TextRunFormatting.Normal));
                    else if (spanNode.Name.LocalName == "sub")
                        command.TextRuns.Add(new TextRun(nodeValue, TextRunFormatting.Subscript));
                    else if (spanNode.Name.LocalName == "sup")
                        command.TextRuns.Add(new TextRun(nodeValue, TextRunFormatting.Superscript));
                    else
                        lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition, LoadErrorCategory.Warning,
                            String.Format("Unexpected node {0}.", spanNode.Name.LocalName)));
                }
            }
            else
            {
                command.TextRuns.Add(new TextRun(element.Attribute("value").Value, new TextRunFormatting(TextRunFormattingType.Normal, size)));
            }
        }
        public static void LoadFromXml(this Ellipse command, XElement element, LoadContext lc)
        {
            if (element.Attribute("thickness") != null)
            {
                command.Thickness = double.Parse(element.Attribute("thickness").Value);
            }

            var fill = element.Attribute("fill");

            if (fill != null && fill.Value.ToLowerInvariant() != "false")
            {
                command.Fill = true;
            }

            if (element.Attribute("centre") != null)
            {
                command.Centre = new ComponentPoint(element.Attribute("centre").Value);
            }
            else
            {
                string x = element.Attribute("x").Value;
                string y = element.Attribute("y").Value;
                command.Centre = new ComponentPoint(x, y);
            }

            string radius = "r";

            if (lc.FormatVersion <= new Version(1, 1))
            {
                radius = "radius";
            }

            string rx;

            if (element.GetAttribute(radius + "x", lc, out rx))
            {
                command.RadiusX = double.Parse(rx);
            }

            string ry;

            if (element.GetAttribute(radius + "y", lc, out ry))
            {
                command.RadiusY = double.Parse(ry);
            }
        }
        public static bool GetAttribute(this XElement element, string name, LoadContext lc, out string set)
        {
            IXmlLineInfo line = element as IXmlLineInfo;

            var attr = element.Attribute(name);
            if (attr == null)
            {
                lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition, LoadErrorCategory.Error,
                    String.Format("Missing attribute '{0}' for <{1}> tag", name, element.Name.LocalName)));
                set = null;
                return false;
            }
            else
            {
                set = attr.Value;
                return true;
            }
        }
        public static bool GetAttribute(this XElement element, string name, LoadContext lc, out string set)
        {
            IXmlLineInfo line = element as IXmlLineInfo;

            var attr = element.Attribute(name);

            if (attr == null)
            {
                lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition, LoadErrorCategory.Error,
                                            String.Format("Missing attribute '{0}' for <{1}> tag", name, element.Name.LocalName)));
                set = null;
                return(false);
            }
            else
            {
                set = attr.Value;
                return(true);
            }
        }
        public static void LoadFromXml(this Line command, XElement element, LoadContext lc)
        {
            if (element.Attribute("thickness") != null)
            {
                command.Thickness = double.Parse(element.Attribute("thickness").Value);
            }

            string start;

            if (element.GetAttribute("start", lc, out start))
            {
                command.Start = new ComponentPoint(start);
            }

            string end;

            if (element.GetAttribute("end", lc, out end))
            {
                command.End = new ComponentPoint(end);
            }
        }
        public static void LoadFromXml(this RenderText command, XElement element, LoadContext lc)
        {
            if (element.Attribute("location") != null)
            {
                command.Location = new ComponentPoint(element.Attribute("location").Value);
            }
            else
            {
                string x = element.Attribute("x").Value;
                string y = element.Attribute("y").Value;
                command.Location = new ComponentPoint(x, y);
            }

            string tAlignment = "TopLeft";

            if (element.Attribute("align") != null)
            {
                tAlignment = element.Attribute("align").Value;
            }
            command.Alignment = (TextAlignment)Enum.Parse(typeof(TextAlignment), tAlignment, true);

            double size = 11d;

            if (element.Attribute("size") != null)
            {
                if (element.Attribute("size").Value.ToLowerInvariant() == "large")
                {
                    size = 12d;
                }
            }

            var textValueNode = element.Element(XmlLoader.ComponentNamespace + "value");

            if (textValueNode != null)
            {
                foreach (var spanNode in textValueNode.Elements())
                {
                    IXmlLineInfo line = spanNode as IXmlLineInfo;

                    string nodeValue = spanNode.Value;

                    if (spanNode.Name.LocalName == "span")
                    {
                        command.TextRuns.Add(new TextRun(nodeValue, TextRunFormatting.Normal));
                    }
                    else if (spanNode.Name.LocalName == "sub")
                    {
                        command.TextRuns.Add(new TextRun(nodeValue, TextRunFormatting.Subscript));
                    }
                    else if (spanNode.Name.LocalName == "sup")
                    {
                        command.TextRuns.Add(new TextRun(nodeValue, TextRunFormatting.Superscript));
                    }
                    else
                    {
                        lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition, LoadErrorCategory.Warning,
                                                    String.Format("Unexpected node {0}.", spanNode.Name.LocalName)));
                    }
                }
            }
            else
            {
                command.TextRuns.Add(new TextRun(element.Attribute("value").Value, new TextRunFormatting(TextRunFormattingType.Normal, size)));
            }
        }
Beispiel #14
0
        private void ReadConnectionsSection(XElement declaration, LoadContext lc, ComponentDescription description)
        {
            List<ConnectionGroup> parsedConnectionGroups = new List<ConnectionGroup>();
            var connectionGroupNodes = declaration.XPathSelectElements("/cd:connections/cd:group", lc.NamespaceManager);
            foreach (var connectionGroupNode in connectionGroupNodes)
            {
                IConditionTreeItem conditionCollection = ConditionTree.Empty;
                List<ConnectionDescription> connections = new List<ConnectionDescription>();

                var conditionsAttribute = connectionGroupNode.Attribute("conditions");
                if (conditionsAttribute != null)
                {
                    IXmlLineInfo line = conditionsAttribute as IXmlLineInfo;

                    try
                    {
                        conditionCollection = lc.ConditionParser.Parse(connectionGroupNode.Attribute("conditions").Value, lc.ParseContext);
                    }
                    catch (ConditionFormatException ex)
                    {
                        lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition + conditionsAttribute.Name.LocalName.Length + 2 + ex.PositionStart, LoadErrorCategory.Error, ex.Message));
                        continue;
                    }
                }

                foreach (var connectionNode in connectionGroupNode.Elements(ns + "connection"))
                {
                    ConnectionEdge edge = ConnectionEdge.None;
                    if (connectionNode.Attribute("edge") != null)
                    {
                        string edgeText = connectionNode.Attribute("edge").Value.ToLowerInvariant();
                        if (edgeText == "start")
                            edge = ConnectionEdge.Start;
                        else if (edgeText == "end")
                            edge = ConnectionEdge.End;
                        else if (edgeText == "both")
                            edge = ConnectionEdge.Both;
                    }
                    string connectionName = "#";
                    if (connectionNode.Attribute("name") != null)
                        connectionName = connectionNode.Attribute("name").Value;
                    connections.Add(new ConnectionDescription(new ComponentPoint(connectionNode.Attribute("start").Value), new ComponentPoint(connectionNode.Attribute("end").Value), edge, connectionName));
                }

                parsedConnectionGroups.Add(new ConnectionGroup(conditionCollection, connections.ToArray()));
            }

            description.Connections = parsedConnectionGroups.ToArray();
        }
Beispiel #15
0
        public void Load(System.IO.Stream stream)
        {
            LoadContext lc = new LoadContext();

            try
            {
                string fileName = "file.xml";
                if (stream is System.IO.FileStream)
                {
                    fileName = (stream as System.IO.FileStream).Name;
                }
                lc.FileName = fileName;

                var root = XElement.Load(stream, LoadOptions.SetLineInfo);

                // Read format version
                lc.FormatVersion = new Version(1, 2);
                if (root.Attribute("version") != null)
                {
                    lc.FormatVersion = new Version(root.Attribute("version").Value);
                }

                // Decide which condition parser to use
                if (lc.FormatVersion >= new Version(1, 1))
                {
                    ConditionFormat conditionParseFormat = new ConditionFormat();
                    if (lc.FormatVersion == new Version(1, 1))
                    {
                        conditionParseFormat.StatesUnderscored = true;
                    }
                    else
                    {
                        conditionParseFormat.StatesUnderscored = false;
                    }

                    lc.ConditionParser = new ConditionParser(conditionParseFormat);
                }
                else
                {
                    lc.ConditionParser = new LegacyConditionParser();
                }

                ComponentDescription description = new ComponentDescription();

                var declaration = root.Elements().First(x => x.Name == ComponentNamespace + "declaration");
                ReadDeclarationSection(declaration, ns, lc, description);

                ReadConnectionsSection(declaration, lc, description);

                ReadRenderSection(declaration, lc, description);

                m_descriptions = new [] { description };
            }
            catch (Exception ex)
            {
                lc.Errors.Add(new LoadError(lc.FileName, 0, 0, LoadErrorCategory.Error, "A fatal error occurred - '" + ex.Message + "'"));
                m_descriptions = new ComponentDescription[0];
            }
            finally
            {
                LoadErrors = lc.Errors;
            }
        }
Beispiel #16
0
        private void ReadMetaNode(XElement metaElement, LoadContext lc, ComponentDescription description)
        {
            string metaName;

            if (!metaElement.GetAttribute("name", lc, out metaName))
            {
                return;
            }

            string metaValue;

            if (!metaElement.GetAttribute("value", lc, out metaValue))
            {
                return;
            }

            IXmlLineInfo line = metaElement as IXmlLineInfo;

            switch (metaName)
            {
            case "name":
                description.ComponentName = metaValue;
                break;

            case "canresize":
                description.CanResize = metaValue.ToLower() != "false";
                break;

            case "canflip":
                description.CanFlip = metaValue.ToLower() != "false";
                break;

            case "minsize":
                double minSize;
                if (double.TryParse(metaValue, out minSize))
                {
                    description.MinSize = minSize;
                }
                else
                {
                    lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition, LoadErrorCategory.Error,
                                                "Illegal value for attribute 'value' on <meta> tag 'minsize' (expected decimal)"));
                }
                break;

            case "version":
                try
                {
                    description.Metadata.Version = new Version(metaValue);
                }
                catch
                {
                    lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition, LoadErrorCategory.Error,
                                                "Illegal value for attribute 'value' on <meta> tag 'version' (expected version number)"));
                }
                break;

            case "author":
                description.Metadata.Author = metaValue;
                break;

            case "additionalinformation":
                description.Metadata.AdditionalInformation = metaValue;
                break;

            case "implementset":
                description.Metadata.ImplementSet = metaValue;
                break;

            case "implementitem":
                description.Metadata.ImplementItem = metaValue;
                break;

            case "guid":
                try
                {
                    description.Metadata.GUID = new Guid(metaValue);
                }
                catch
                {
                    lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition, LoadErrorCategory.Error,
                                                "Illegal value for attribute 'value' on <meta> tag 'guid' (expected GUID)"));
                }
                break;

            default:
                description.Metadata.Entries.Add(metaName, metaValue);
                break;
            }
        }
Beispiel #17
0
        private ComponentProperty ReadPropertyNode(XElement propertyElement, LoadContext lc)
        {
            IXmlLineInfo elementLine = propertyElement as IXmlLineInfo;

            string propertyName = propertyElement.Attribute("name").Value;
            string type         = propertyElement.Attribute("type").Value;
            string defaultValue = propertyElement.Attribute("default").Value;
            string serializeAs  = propertyElement.Attribute("serialize").Value;
            string display      = propertyElement.Attribute("display").Value;

            PropertyType propertyType;

            switch (type.ToLowerInvariant())
            {
            case "double":
                propertyType = PropertyType.Decimal;
                if (lc.FormatVersion >= new Version(1, 2))
                {
                    lc.Errors.Add(new LoadError(lc.FileName, elementLine.LineNumber, elementLine.LinePosition, LoadErrorCategory.Warning,
                                                "Property type 'double' is deprecated, use 'decimal' instead"));
                }
                break;

            case "decimal":
                propertyType = PropertyType.Decimal;
                break;

            case "int":
                propertyType = PropertyType.Integer;
                break;

            case "bool":
                propertyType = PropertyType.Boolean;
                break;

            default:
                propertyType = PropertyType.String;
                break;
            }

            PropertyUnion propertyDefaultValue = new PropertyUnion(defaultValue, propertyType);

            List <string> propertyOptions = null;

            if (type == "enum")
            {
                propertyOptions = new List <string>();
                var optionNodes = propertyElement.Elements(ns + "option");
                foreach (var optionNode in optionNodes)
                {
                    propertyOptions.Add(optionNode.Value);
                }
            }

            List <ComponentPropertyFormat> formatRules = new List <ComponentPropertyFormat>();

            if (propertyElement.Attribute("format") != null)
            {
                formatRules.Add(new ComponentPropertyFormat(propertyElement.Attribute("format").Value, ConditionTree.Empty));
            }
            else
            {
                var formatRuleNodes = propertyElement.XPathSelectElements("cd:formatting/cd:format", lc.NamespaceManager);
                foreach (var formatNode in formatRuleNodes)
                {
                    IXmlLineInfo line = formatNode as IXmlLineInfo;

                    IConditionTreeItem conditionCollection = ConditionTree.Empty;
                    if (formatNode.Attribute("conditions") != null)
                    {
                        try
                        {
                            conditionCollection = lc.ConditionParser.Parse(formatNode.Attribute("conditions").Value, lc.ParseContext);
                        }
                        catch (ConditionFormatException ex)
                        {
                            lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition + formatNode.Name.LocalName.Length + 2 + ex.PositionStart, LoadErrorCategory.Error, ex.Message));
                            return(null);
                        }
                    }

                    formatRules.Add(new ComponentPropertyFormat(formatNode.Attribute("value").Value, conditionCollection));
                }
            }

            Dictionary <PropertyOtherConditionType, IConditionTreeItem> otherConditions = new Dictionary <PropertyOtherConditionType, IConditionTreeItem>();
            var otherConditionsNodes = propertyElement.XPathSelectElements("cd:other/cd:conditions", lc.NamespaceManager);

            foreach (var otherConditionNode in otherConditionsNodes)
            {
                if (otherConditionNode != null && otherConditionNode.Attribute("for") != null && otherConditionNode.Attribute("value") != null)
                {
                    IXmlLineInfo line = otherConditionNode as IXmlLineInfo;

                    string             conditionsFor    = otherConditionNode.Attribute("for").Value;
                    string             conditionsString = otherConditionNode.Attribute("value").Value;
                    IConditionTreeItem conditionCollection;

                    try
                    {
                        conditionCollection = lc.ConditionParser.Parse(conditionsString, lc.ParseContext);
                    }
                    catch (ConditionFormatException ex)
                    {
                        lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition + otherConditionNode.Name.LocalName.Length + 2 + ex.PositionStart, LoadErrorCategory.Error, ex.Message));
                        return(null);
                    }

                    if (Enum.IsDefined(typeof(PropertyOtherConditionType), conditionsFor))
                    {
                        otherConditions.Add((PropertyOtherConditionType)Enum.Parse(typeof(PropertyOtherConditionType), conditionsFor, true), conditionCollection);
                    }
                }
            }

            ComponentProperty property = new ComponentProperty(propertyName, serializeAs, display, propertyType, propertyDefaultValue, formatRules.ToArray(), otherConditions, (propertyOptions == null ? null : propertyOptions.ToArray()));

            return(property);
        }
Beispiel #18
0
        private void ReadDeclarationSection(XElement declaration, XNamespace ns, LoadContext lc, ComponentDescription description)
        {
            IXmlLineInfo line = declaration as IXmlLineInfo;

            // Read meta nodes
            foreach (var metaElement in declaration.Elements(ns + "meta"))
            {
                ReadMetaNode(metaElement, lc, description);
            }

            // Check all required metadata was set
            if (String.IsNullOrEmpty(description.ComponentName))
                lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition, LoadErrorCategory.Error,
                    "Component name is required"));

            //Read properties
            List<ComponentProperty> properties = new List<ComponentProperty>();
            foreach (var propertyElement in declaration.Elements(ns + "property"))
            {
                ScanPropertyNode(propertyElement, lc);
            }
            foreach (var propertyElement in declaration.Elements(ns + "property"))
            {
                ComponentProperty property = ReadPropertyNode(propertyElement, lc);
                properties.Add(property);
            }
            description.Properties = properties.ToArray();

            // Read flags
            List<Conditional<FlagOptions>> flagOptions = new List<Conditional<FlagOptions>>();
            foreach (var flagGroup in declaration.Elements(ns + "flags"))
            {
                var flags = ReadFlagOptionNode(flagGroup, lc);
                if (flags != null)
                    flagOptions.Add(flags);
            }
            description.Flags = flagOptions.ToArray();

            // Read configurations
            var componentConfigurations = new List<ComponentConfiguration>();
            var configurations = declaration.Element(ns + "configurations");
            if (configurations != null)
            {
                foreach (var node in configurations.Elements(ns + "configuration"))
                {
                    ComponentConfiguration newConfiguration = ReadConfigurationNode(node, description);

                    componentConfigurations.Add(newConfiguration);
                }
            }
            description.Metadata.Configurations.AddRange(componentConfigurations);
        }
Beispiel #19
0
        private void ReadRenderSection(XElement declaration, LoadContext lc, ComponentDescription description)
        {
            List <RenderDescription> parsedRenderDescriptions = new List <RenderDescription>();
            var renderDescriptions = declaration.XPathSelectElements("/cd:render/cd:group", lc.NamespaceManager);

            foreach (var renderNode in renderDescriptions)
            {
                IConditionTreeItem    conditionCollection = ConditionTree.Empty;
                List <IRenderCommand> commands            = new List <IRenderCommand>();

                var conditionsAttribute = renderNode.Attribute("conditions");
                if (conditionsAttribute != null)
                {
                    IXmlLineInfo line = conditionsAttribute as IXmlLineInfo;

                    try
                    {
                        conditionCollection = lc.ConditionParser.Parse(conditionsAttribute.Value, lc.ParseContext);
                    }
                    catch (ConditionFormatException ex)
                    {
                        lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition + conditionsAttribute.Name.LocalName.Length + 2 + ex.PositionStart, LoadErrorCategory.Error, ex.Message));
                        continue;
                    }
                }

                foreach (var renderCommandNode in renderNode.Descendants())
                {
                    string commandType = renderCommandNode.Name.LocalName;
                    if (commandType == "line")
                    {
                        var command = new Line();
                        command.LoadFromXml(renderCommandNode, lc);
                        commands.Add(command);
                    }
                    else if (commandType == "rect")
                    {
                        var command = new Rectangle();
                        command.LoadFromXml(renderCommandNode);
                        commands.Add(command);
                    }
                    else if (commandType == "ellipse")
                    {
                        var command = new Ellipse();
                        command.LoadFromXml(renderCommandNode, lc);
                        commands.Add(command);
                    }
                    else if (commandType == "text")
                    {
                        var command = new Text();
                        command.LoadFromXml(renderCommandNode, lc);
                        commands.Add(command);
                    }
                    else if (commandType == "path")
                    {
                        var command = new RenderPath();
                        command.LoadFromXml(renderCommandNode, lc);
                        commands.Add(command);
                    }
                }

                parsedRenderDescriptions.Add(new RenderDescription(conditionCollection, commands.ToArray()));
            }

            description.RenderDescriptions = parsedRenderDescriptions.ToArray();
        }
Beispiel #20
0
        private Conditional<FlagOptions> ReadFlagOptionNode(XElement flagElement, LoadContext lc)
        {
            IConditionTreeItem conditions = ConditionTree.Empty;
            var conditionsAttribute = flagElement.Attribute("conditions");
            if (conditionsAttribute != null)
            {
                IXmlLineInfo line = conditionsAttribute as IXmlLineInfo;

                try
                {
                    conditions = lc.ConditionParser.Parse(conditionsAttribute.Value, lc.ParseContext);
                }
                catch (ConditionFormatException ex)
                {
                    lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition + conditionsAttribute.Name.LocalName.Length + 2 + ex.PositionStart, LoadErrorCategory.Error, ex.Message));
                    return null;
                }
            }

            FlagOptions theOptions = FlagOptions.None;
            foreach (var node in flagElement.Descendants(lc.NS + "option"))
                theOptions |= (FlagOptions)Enum.Parse(typeof(FlagOptions), node.Value, true);

            return new Conditional<FlagOptions>(theOptions, conditions);
        }
Beispiel #21
0
        private void ReadMetaNode(XElement metaElement, LoadContext lc, ComponentDescription description)
        {
            string metaName;
            if (!metaElement.GetAttribute("name", lc, out metaName))
                return;

            string metaValue;
            if (!metaElement.GetAttribute("value", lc, out metaValue))
                return;

            IXmlLineInfo line = metaElement as IXmlLineInfo;

            switch (metaName)
            {
                case "name":
                    description.ComponentName = metaValue;
                    break;
                case "canresize":
                    description.CanResize = metaValue.ToLower() != "false";
                    break;
                case "canflip":
                    description.CanFlip = metaValue.ToLower() != "false";
                    break;
                case "minsize":
                    double minSize;
                    if (double.TryParse(metaValue, out minSize))
                        description.MinSize = minSize;
                    else
                        lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition, LoadErrorCategory.Error,
                            "Illegal value for attribute 'value' on <meta> tag 'minsize' (expected decimal)"));
                    break;
                case "version":
                    try
                    {
                        description.Metadata.Version = new Version(metaValue);
                    }
                    catch
                    {
                        lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition, LoadErrorCategory.Error,
                            "Illegal value for attribute 'value' on <meta> tag 'version' (expected version number)"));
                    }
                    break;
                case "author":
                    description.Metadata.Author = metaValue;
                    break;
                case "additionalinformation":
                    description.Metadata.AdditionalInformation = metaValue;
                    break;
                case "implementset":
                    description.Metadata.ImplementSet = metaValue;
                    break;
                case "implementitem":
                    description.Metadata.ImplementItem = metaValue;
                    break;
                case "guid":
                    try
                    {
                        description.Metadata.GUID = new Guid(metaValue);
                    }
                    catch
                    {
                        lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition, LoadErrorCategory.Error,
                            "Illegal value for attribute 'value' on <meta> tag 'guid' (expected GUID)"));
                    }
                    break;
            }
        }
Beispiel #22
0
        private ComponentProperty ReadPropertyNode(XElement propertyElement, LoadContext lc)
        {
            IXmlLineInfo elementLine = propertyElement as IXmlLineInfo;

            string propertyName = propertyElement.Attribute("name").Value;
            string type = propertyElement.Attribute("type").Value;
            string defaultValue = propertyElement.Attribute("default").Value;
            string serializeAs = propertyElement.Attribute("serialize").Value;
            string display = propertyElement.Attribute("display").Value;

            PropertyType propertyType;
            switch (type.ToLowerInvariant())
            {
                case "double":
                    propertyType = PropertyType.Decimal;
                    if (lc.FormatVersion >= new Version(1, 2))
                        lc.Errors.Add(new LoadError(lc.FileName, elementLine.LineNumber, elementLine.LinePosition, LoadErrorCategory.Warning,
                            "Property type 'double' is deprecated, use 'decimal' instead"));
                    break;
                case "decimal":
                    propertyType = PropertyType.Decimal;
                    break;
                case "int":
                    propertyType = PropertyType.Integer;
                    break;
                case "bool":
                    propertyType = PropertyType.Boolean;
                    break;
                default:
                    propertyType = PropertyType.String;
                    break;
            }

            PropertyUnion propertyDefaultValue = new PropertyUnion(defaultValue, propertyType);

            List<string> propertyOptions = null;
            if (type == "enum")
            {
                propertyOptions = new List<string>();
                var optionNodes = propertyElement.Elements(ns + "option");
                foreach (var optionNode in optionNodes)
                    propertyOptions.Add(optionNode.Value);
            }

            List<ComponentPropertyFormat> formatRules = new List<ComponentPropertyFormat>();
            if (propertyElement.Attribute("format") != null)
                formatRules.Add(new ComponentPropertyFormat(propertyElement.Attribute("format").Value, ConditionTree.Empty));
            else
            {
                var formatRuleNodes = propertyElement.XPathSelectElements("cd:formatting/cd:format", lc.NamespaceManager);
                foreach (var formatNode in formatRuleNodes)
                {
                    IXmlLineInfo line = formatNode as IXmlLineInfo;

                    IConditionTreeItem conditionCollection = ConditionTree.Empty;
                    if (formatNode.Attribute("conditions") != null)
                    {
                        try
                        {
                            conditionCollection = lc.ConditionParser.Parse(formatNode.Attribute("conditions").Value, lc.ParseContext);
                        }
                        catch (ConditionFormatException ex)
                        {
                            lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition + formatNode.Name.LocalName.Length + 2 + ex.PositionStart, LoadErrorCategory.Error, ex.Message));
                            return null;
                        }
                    }

                    formatRules.Add(new ComponentPropertyFormat(formatNode.Attribute("value").Value, conditionCollection));
                }
            }

            Dictionary<PropertyOtherConditionType, IConditionTreeItem> otherConditions = new Dictionary<PropertyOtherConditionType, IConditionTreeItem>();
            var otherConditionsNodes = propertyElement.XPathSelectElements("cd:other/cd:conditions", lc.NamespaceManager);
            foreach (var otherConditionNode in otherConditionsNodes)
            {
                if (otherConditionNode != null && otherConditionNode.Attribute("for") != null && otherConditionNode.Attribute("value") != null)
                {
                    IXmlLineInfo line = otherConditionNode as IXmlLineInfo;

                    string conditionsFor = otherConditionNode.Attribute("for").Value;
                    string conditionsString = otherConditionNode.Attribute("value").Value;
                    IConditionTreeItem conditionCollection;

                    try
                    {
                        conditionCollection = lc.ConditionParser.Parse(conditionsString, lc.ParseContext);
                    }
                    catch (ConditionFormatException ex)
                    {
                        lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition + otherConditionNode.Name.LocalName.Length + 2 + ex.PositionStart, LoadErrorCategory.Error, ex.Message));
                        return null;
                    }

                    if (Enum.IsDefined(typeof(PropertyOtherConditionType), conditionsFor))
                        otherConditions.Add((PropertyOtherConditionType)Enum.Parse(typeof(PropertyOtherConditionType), conditionsFor, true), conditionCollection);
                }
            }

            ComponentProperty property = new ComponentProperty(propertyName, serializeAs, display, propertyType, propertyDefaultValue, formatRules.ToArray(), otherConditions, (propertyOptions == null ? null : propertyOptions.ToArray()));

            return property;
        }
Beispiel #23
0
        private void ReadRenderSection(XElement declaration, LoadContext lc, ComponentDescription description)
        {
            List<RenderDescription> parsedRenderDescriptions = new List<RenderDescription>();
            var renderDescriptions = declaration.XPathSelectElements("/cd:render/cd:group", lc.NamespaceManager);
            foreach (var renderNode in renderDescriptions)
            {
                IConditionTreeItem conditionCollection = ConditionTree.Empty;
                List<IRenderCommand> commands = new List<IRenderCommand>();

                var conditionsAttribute = renderNode.Attribute("conditions");
                if (conditionsAttribute != null)
                {
                    IXmlLineInfo line = conditionsAttribute as IXmlLineInfo;

                    try
                    {
                        conditionCollection = lc.ConditionParser.Parse(conditionsAttribute.Value, lc.ParseContext);
                    }
                    catch (ConditionFormatException ex)
                    {
                        lc.Errors.Add(new LoadError(lc.FileName, line.LineNumber, line.LinePosition + conditionsAttribute.Name.LocalName.Length + 2 + ex.PositionStart, LoadErrorCategory.Error, ex.Message));
                        continue;
                    }
                }

                foreach (var renderCommandNode in renderNode.Descendants())
                {
                    string commandType = renderCommandNode.Name.LocalName;
                    if (commandType == "line")
                    {
                        var command = new Line();
                        command.LoadFromXml(renderCommandNode, lc);
                        commands.Add(command);
                    }
                    else if (commandType == "rect")
                    {
                        var command = new Rectangle();
                        command.LoadFromXml(renderCommandNode);
                        commands.Add(command);
                    }
                    else if (commandType == "ellipse")
                    {
                        var command = new Ellipse();
                        command.LoadFromXml(renderCommandNode, lc);
                        commands.Add(command);
                    }
                    else if (commandType == "text")
                    {
                        var command = new Text();
                        command.LoadFromXml(renderCommandNode, lc);
                        commands.Add(command);
                    }
                    else if (commandType == "path")
                    {
                        var command = new RenderPath();
                        command.LoadFromXml(renderCommandNode, lc);
                        commands.Add(command);
                    }
                }

                parsedRenderDescriptions.Add(new RenderDescription(conditionCollection, commands.ToArray()));
            }

            description.RenderDescriptions = parsedRenderDescriptions.ToArray();
        }
Beispiel #24
0
        private void ScanPropertyNode(XElement propertyElement, LoadContext lc)
        {
            string propertyName = propertyElement.Attribute("name").Value;
            string type = propertyElement.Attribute("type").Value;
            string defaultValue = propertyElement.Attribute("default").Value;

            PropertyType propertyType;
            switch (type.ToLowerInvariant())
            {
                case "double":
                    propertyType = PropertyType.Decimal;
                    break;
                case "decimal":
                    propertyType = PropertyType.Decimal;
                    break;
                case "int":
                    propertyType = PropertyType.Integer;
                    break;
                case "bool":
                    propertyType = PropertyType.Boolean;
                    break;
                default:
                    propertyType = PropertyType.String;
                    break;
            }

            PropertyUnion propertyDefaultValue = new PropertyUnion(defaultValue, propertyType);

            lc.ParseContext.PropertyTypes.Add(propertyName, propertyDefaultValue.InternalType);
        }
        public static void LoadFromXml(this RenderPath command, XElement element, LoadContext lc)
        {
            if (element.Attribute("thickness") != null)
                command.Thickness = double.Parse(element.Attribute("thickness").Value);

            string start;
            if (element.GetAttribute("start", lc, out start))
                command.Start = new ComponentPoint(start);

            var fill = element.Attribute("fill");
            if (fill != null && fill.Value.ToLowerInvariant() != "false")
                command.Fill = true;

            string data;
            if (element.GetAttribute("data", lc, out data))
                command.Commands = PathHelper.ParseCommands(data);
        }
Beispiel #26
0
        public void Load(System.IO.Stream stream)
        {
            LoadContext lc = new LoadContext();

            try
            {
                string fileName = "file.xml";
                if (stream is System.IO.FileStream)
                    fileName = (stream as System.IO.FileStream).Name;
                lc.FileName = fileName;

                var reader = new XmlTextReader(stream);
                var root = XElement.Load(reader, LoadOptions.SetLineInfo);

                ns = XNamespace.Get(ComponentNamespace);
                lc.NS = ns;

                lc.NamespaceManager = new XmlNamespaceManager(reader.NameTable);
                lc.NamespaceManager.AddNamespace("cd", ComponentNamespace);

                // Read format version
                lc.FormatVersion = new Version(1, 2);
                if (root.Attribute("version") != null)
                    lc.FormatVersion = new Version(root.Attribute("version").Value);

                // Decide which condition parser to use
                if (lc.FormatVersion >= new Version(1, 1))
                {
                    ConditionFormat conditionParseFormat = new ConditionFormat();
                    if (lc.FormatVersion == new Version(1, 1))
                        conditionParseFormat.StatesUnderscored = true;
                    else
                        conditionParseFormat.StatesUnderscored = false;

                    lc.ConditionParser = new ConditionParser(conditionParseFormat);

                }
                else
                    lc.ConditionParser = new LegacyConditionParser();

                ComponentDescription description = new ComponentDescription();

                var declaration = root.XPathSelectElement("/cd:declaration", lc.NamespaceManager);
                ReadDeclarationSection(declaration, ns, lc, description);

                ReadConnectionsSection(declaration, lc, description);

                ReadRenderSection(declaration, lc, description);

                m_descriptions = new ComponentDescription[] { description };
            }
            catch (Exception ex)
            {
                lc.Errors.Add(new LoadError(lc.FileName, 0, 0, LoadErrorCategory.Error, "A fatal error occurred - '" + ex.Message + "'"));
                m_descriptions = new ComponentDescription[0];
            }
            finally
            {
                LoadErrors = lc.Errors;
            }
        }