/// <summary>
        /// Parses the given style data and converts it to CanvasStrokeStyle.
        /// </summary>
        /// <param name="styleData">Style data</param>
        /// <returns>CanvasStrokeStyle</returns>
        internal static CanvasStrokeStyle Parse(string styleData)
        {
            var matches = RegexFactory.CanvasStrokeStyleRegex.Matches(styleData);

            // If no match is found or no captures in the match, then it means that the style data is invalid.
            if (matches.Count == 0)
            {
                ThrowForZeroCount();
            }

            // If the match contains more than one captures, it means that there
            // are multiple CanvasStrokeStyles present in the CanvasStrokeStyle data. There should
            // be only one CanvasStrokeStyle defined in the CanvasStrokeStyle data.
            if (matches.Count > 1)
            {
                ThrowForNotOneCount();
            }

            // There should be only one match
            var match        = matches[0];
            var styleElement = new CanvasStrokeStyleElement(match);

            // Perform validation to check if there are any invalid characters in the brush data that were not captured
            var preValidationCount  = RegexFactory.ValidationRegex.Replace(styleData, string.Empty).Length;
            var postValidationCount = styleElement.ValidationCount;

            // If there are invalid characters, extract them and add them to the ArgumentException message
            if (preValidationCount != postValidationCount)
            {
                static void ThrowForInvalidCharacters(CanvasStrokeStyleElement styleElement, string styleData)
                {
                    var parseIndex = 0;

                    if (!string.IsNullOrWhiteSpace(styleElement.Data))
                    {
                        parseIndex = styleData.IndexOf(styleElement.Data, parseIndex, StringComparison.Ordinal);
                    }

                    var errorString = styleData.Substring(parseIndex);

                    if (errorString.Length > 30)
                    {
                        errorString = $"{errorString.Substring(0, 30)}...";
                    }

                    throw new ArgumentException($"STYLE_ERR003:Style data contains invalid characters!\nIndex: {parseIndex}\n{errorString}");
                }
Ejemplo n.º 3
0
        /// <summary>
        /// Parses the given style data and converts it to CanvasStrokeStyle
        /// </summary>
        /// <param name="styleData">Style data</param>
        /// <returns>CanvasStrokeStyle</returns>
        internal static CanvasStrokeStyle Parse(string styleData)
        {
            var matches = RegexFactory.CanvasStrokeStyleRegex.Matches(styleData);

            // If no match is found or no captures in the match, then it means
            // that the style data is invalid.
            if ((matches == null) || (matches.Count == 0))
            {
                throw new ArgumentException($"Invalid CanvasStrokeStyle data!\nCanvasStrokeStyle Data: {styleData}", nameof(styleData));
            }

            // If the match contains more than one captures, it means that there
            // are multiple CanvasStrokeStyles present in the CanvasStrokeStyle data. There should
            // be only one CanvasStrokeStyle defined in the CanvasStrokeStyle data.
            if (matches.Count > 1)
            {
                throw new ArgumentException("Multiple CanvasStrokeStyles defined in CanvasStrokeStyle Data! " +
                                            "There should be only one CanvasStrokeStyle definition within the CanvasStrokeStyle Data. " +
                                            "You can either remove CanvasStrokeStyle definitions or split the CanvasStrokeStyle Data " +
                                            "into multiple CanvasStrokeStyle Data and call the CanvasObject.CreateStrokeStyle() method on each of them." +
                                            $"\nCanvasStrokeStyle Data: {styleData}");
            }

            // There should be only one match
            var match        = matches[0];
            var styleElement = new CanvasStrokeStyleElement(match);

            // Perform validation to check if there are any invalid characters in the brush data that were not captured
            var preValidationCount = RegexFactory.ValidationRegex.Replace(styleData, string.Empty).Length;

            var postValidationCount = styleElement.ValidationCount;

            if (preValidationCount != postValidationCount)
            {
                throw new ArgumentException($"CanvasStrokeStyle data contains invalid characters!\nCanvasStrokeStyle Data: {styleData}", nameof(styleData));
            }

            return(styleElement.Style);
        }