Example #1
0
        internal static bool TryParseWixVariable(string value, int start, out ParsedWixVariable parsedVariable)
        {
            parsedVariable = null;

            if (String.IsNullOrEmpty(value) || start >= value.Length)
            {
                return(false);
            }

            var startWixVariable = value.IndexOf("!(", start, StringComparison.Ordinal);

            if (startWixVariable == -1)
            {
                return(false);
            }

            var firstDot = value.IndexOf('.', startWixVariable + 1);

            if (firstDot == -1)
            {
                return(false);
            }

            var ns = value.Substring(startWixVariable + 2, firstDot - startWixVariable - 2);

            if (ns != "loc" && ns != "bind" && ns != "wix")
            {
                return(false);
            }

            var closeParen = value.IndexOf(')', firstDot);

            if (closeParen == -1)
            {
                return(false);
            }

            string name;
            string scope        = null;
            string defaultValue = null;

            var equalsDefaultValue = value.IndexOf('=', firstDot + 1, closeParen - firstDot);
            var end       = equalsDefaultValue == -1 ? closeParen : equalsDefaultValue;
            var secondDot = value.IndexOf('.', firstDot + 1, end - firstDot);

            if (secondDot == -1)
            {
                name = value.Substring(firstDot + 1, end - firstDot - 1);
            }
            else
            {
                name  = value.Substring(firstDot + 1, secondDot - firstDot - 1);
                scope = value.Substring(secondDot + 1, end - secondDot - 1);

                if (!Common.IsIdentifier(scope))
                {
                    return(false);
                }
            }

            if (!Common.IsIdentifier(name))
            {
                return(false);
            }

            if (equalsDefaultValue != -1 && equalsDefaultValue < closeParen)
            {
                defaultValue = value.Substring(equalsDefaultValue + 1, closeParen - equalsDefaultValue - 1);
            }

            parsedVariable = new ParsedWixVariable
            {
                Index        = startWixVariable,
                Length       = closeParen - startWixVariable + 1,
                Namespace    = ns,
                Name         = name,
                Scope        = scope,
                DefaultValue = defaultValue
            };

            return(true);
        }