Esempio n. 1
0
        ///<summary>Transform variable</summary>
        ///<param name="source">Original variable value</param>
        ///<param name="rules">Transformation rules (<see cref="TransformRules"/></param>
        ///<returns>Transformed object</returns>
        public object Transform(object source, TransformRules rules)
        {
            object s = source;

            if (source != null && rules != TransformRules.None)
            {
                Type t = source.GetType();
                if (t == typeof(Nullable <>))
                {
                    t = Nullable.GetUnderlyingType(t);
                }
                if (t.IsPrimitive || t == typeof(Guid) || t == typeof(decimal))
                {
                    rules &= ~(TransformRules.ExpandMask | TransformRules.TrimMask);
                }

                if ((rules & TransformRules.ExpandAfterTrim) == TransformRules.ExpandAfterTrim)
                {
                    if (s != null)
                    {
                        s = Utils.TransformStr(Utils.To <string>(s), rules & TransformRules.TrimMask);
                    }
                    rules = (rules & ~TransformRules.ExpandAfterTrim & ~TransformRules.TrimMask) | TransformRules.Expand;
                }
                if ((rules & TransformRules.Expand) != 0)
                {
                    s = expandVars(rules, Utils.To <string>(s));
                }
                if ((rules & TransformRules.ExpandReplaceOnly) == TransformRules.ExpandReplaceOnly)
                {
                    rules &= ~TransformRules.ReplaceMask;
                }
                if ((rules & TransformRules.ExpandTrimOnly) == TransformRules.ExpandTrimOnly)
                {
                    rules &= ~TransformRules.TrimMask;
                }
                if ((rules & ~TransformRules.ExpandMask) != TransformRules.None)
                {
                    if (s != null)
                    {
                        s = Utils.TransformStr(Utils.To <string>(s), rules & ~TransformRules.Expand);
                    }
                }
            }
            return(s);
        }
Esempio n. 2
0
 /// <summary>
 /// Check if text is expression that needs to be calculated if transformed according to the expansion rules
 /// </summary>
 /// <param name="text">Text to verify</param>
 /// <param name="expand">Rules</param>
 /// <returns>true if text contains expressions</returns>
 public bool ContainsExpressions(string text, TransformRules expand)
 {
     if (String.IsNullOrEmpty(text))
     {
         return(false);
     }
     if ((expand & TransformRules.Expand) == TransformRules.Expand)
     {
         return(text.Contains("${"));
     }
     if ((expand & TransformRules.ExpandDual) == TransformRules.ExpandDual)
     {
         return(text.Contains("${{"));
     }
     if ((expand & TransformRules.ExpandDualSquare) == TransformRules.ExpandDualSquare)
     {
         return(text.Contains("[["));
     }
     if ((expand & TransformRules.ExpandSquare) == TransformRules.ExpandSquare)
     {
         return(text.Contains("["));
     }
     return(false);
 }
Esempio n. 3
0
        /// <summary>
        /// Verify that transformation is correct
        /// </summary>
        /// <param name="text">Text to verify</param>
        /// <param name="expand">Rules</param>
        public void AssertGoodTransform(string text, TransformRules expand)
        {
            if (String.IsNullOrEmpty(text))
            {
                return;
            }

            if ((expand & TransformRules.Expand) == TransformRules.Expand && text.Contains("${"))
            {
                if (!text.Contains("}"))
                {
                    throw new ParsingException("Closing } not found in '" + text + "'");
                }
            }
            if ((expand & TransformRules.ExpandDual) == TransformRules.ExpandDual && text.Contains("${{"))
            {
                if (!text.Contains("}}"))
                {
                    throw new ParsingException("Closing }} not found in '" + text + "'");
                }
            }
            if ((expand & TransformRules.ExpandDualSquare) == TransformRules.ExpandDualSquare && text.Contains("[["))
            {
                if (!text.Contains("]]"))
                {
                    throw new ParsingException("Closing ]] not found in '" + text + "'");
                }
            }
            if ((expand & TransformRules.ExpandSquare) == TransformRules.ExpandDualSquare && text.Contains("["))
            {
                if (!text.Contains("]"))
                {
                    throw new ParsingException("Closing ] not found in '" + text + "'");
                }
            }
        }
Esempio n. 4
0
 /// Constructor
 protected XsTransformableElement()
 {
     Transform = TransformRules.Default;
 }
Esempio n. 5
0
 /// Constructor
 public CallParam(string name, object value, TransformRules transformRules)
 {
     Name = name;
     Value = value;
     Transform = transformRules;
 }
Esempio n. 6
0
        private object expandVars(TransformRules rules, string s)
        {
            string begin;
            string end;

            if ((rules & TransformRules.ExpandDual) == TransformRules.ExpandDual)
            {
                begin = "${{";
                end   = "}}";
            }
            else if ((rules & TransformRules.ExpandDualSquare) == TransformRules.ExpandDualSquare)
            {
                begin = "[[";
                end   = "]]";
            }
            else if ((rules & TransformRules.ExpandSquare) == TransformRules.ExpandSquare)
            {
                begin = "[";
                end   = "]";
            }
            else if ((rules & TransformRules.Expand) == TransformRules.Expand)
            {
                begin = "${";
                end   = "}";
            }
            else
            {
                return(s);
            }

            if (s.IndexOf(begin, StringComparison.Ordinal) != -1)
            {
                StringBuilder sbNew = new StringBuilder();
                using (var sr = new ParsingReader(new StringReader(s)))
                {
                    int  ptr   = 0;
                    bool first = true;
                    while (!sr.IsEOF)
                    {
                        char ch = (char)sr.Read();
                        if (ch != begin[ptr])
                        {
                            sbNew.Append(begin, 0, ptr);
                            sbNew.Append(ch);
                            ptr   = 0;
                            first = false;
                            continue;
                        }
                        ptr++;
                        if (ptr < begin.Length)
                        {
                            continue;
                        }
                        if (sr.Peek() == '{' || sr.Peek() == '[')
                        {
                            sbNew.Append(begin);
                            ptr   = 0;
                            first = false;
                            continue;
                        }
                        //
                        object sv = EvalMulti(sr);
                        sv = ((rules & TransformRules.ExpandTrimOnly) == TransformRules.ExpandTrimOnly)
                                 ? Utils.TransformStr(Utils.To <string>(sv), rules & TransformRules.TrimMask)
                                 : sv;
                        sv = ((rules & TransformRules.ExpandReplaceOnly) == TransformRules.ExpandReplaceOnly)
                                 ? Utils.TransformStr(Utils.To <string>(sv), rules & TransformRules.ReplaceMask)
                                 : sv;

                        // Now read the trailing stuff
                        sr.SkipWhiteSpace();
                        for (ptr = 0; ptr < end.Length; ++ptr)
                        {
                            sr.ReadAndThrowIfNot(end[ptr]);
                        }
                        if (sr.IsEOF && first)
                        {
                            return(sv);
                        }
                        ptr   = 0;
                        first = false;
                        sbNew.Append(Utils.To <string>(sv));
                    }
                    for (int i = 0; i < ptr; ++i)
                    {
                        sbNew.Append(begin[i]);
                    }
                }


                return(sbNew.ToString());
            }
            return(s);
        }
Esempio n. 7
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="value">value for Value property of the created object</param>
 /// <param name="rules">value for Transform property of the created object</param>
 public Return(object value, TransformRules rules)
 {
     Value     = value;
     Transform = rules;
 }
Esempio n. 8
0
 /// Constructor accepting variable name, value to set and transformation rules for variable and value
 public Set(string name, string value, TransformRules tr) : this(name, value)
 {
     Transform = tr;
 }
Esempio n. 9
0
 /// Constructor with value
 public ShellArg(object value, TransformRules transformRules)
 {
     Value = value;
     Transform = transformRules;
 }
Esempio n. 10
0
 /// Constructor with value
 public ShellArg(object value, TransformRules transformRules)
 {
     Value     = value;
     Transform = transformRules;
 }
Esempio n. 11
0
 /// Constructor
 public SetAttr(string actionId, string name, object value, TransformRules tr)
     : this(actionId,name,value)
 {
     Transform = tr;
 }
Esempio n. 12
0
        /// Transform string according to the specified rules
        public static string TransformStr(string arguments, TransformRules trim)
        {
            if (arguments == null)
            {
                return(null);
            }
            if ((trim & TransformRules.Expand) != 0)
            {
                throw new ArgumentOutOfRangeException("trim", "Expand flag cannot be specified");
            }
            string ret;

            if ((trim & TransformRules.Multiline) == TransformRules.Multiline)
            {
                string        str    = TransformStr(arguments, trim & TransformRules.Trim);
                StringReader  reader = new StringReader(str);
                List <string> list   = new List <string>();
                while ((str = reader.ReadLine()) != null)
                {
                    list.Add(TransformStr(str, trim & ~TransformRules.Multiline));
                }
                ret = String.Join(Environment.NewLine, list.ToArray());

                return(ret);
            }

            TransformRules tr = trim & TransformRules.Trim;

            switch (tr)
            {
            default:
                ret = arguments;
                break;

            case TransformRules.Trim:
                ret = arguments.Trim();
                break;

            case TransformRules.TrimEnd:
                ret = arguments.TrimEnd();
                break;

            case TransformRules.TrimStart:
                ret = arguments.TrimStart();
                break;
            }

            if ((trim & TransformRules.TrimInternal) != 0)
            {
                ret = Regex.Replace(ret, @"\s{2,}", " ");
            }

            if ((trim & TransformRules.TildaToSpace) == TransformRules.TildaToSpace)
            {
                ret = ret.Replace("~", " ");
            }
            if ((trim & TransformRules.BackqToDouble) == TransformRules.BackqToDouble)
            {
                ret = ret.Replace("`", "\"");
            }
            if ((trim & TransformRules.CurvedToAngle) == TransformRules.CurvedToAngle)
            {
                ret = ret.Replace("{", "<").Replace("}", ">");
            }
            if ((trim & TransformRules.SquareToAngle) == TransformRules.SquareToAngle)
            {
                ret = ret.Replace("[", "<").Replace("]", ">");
            }
            if ((trim & TransformRules.NewLineToLF) == TransformRules.NewLineToLF)
            {
                ret = ret.Replace("\r\n", "\n").Replace("\r", "\n");
            }
            if ((trim & TransformRules.NewLineToCRLF) == TransformRules.NewLineToCRLF)
            {
                ret = ret.Replace("\r\n", "\n").Replace("\r", "\n").Replace("\n", "\r\n");
            }
            if ((trim & TransformRules.DoubleSingleQuotes) == TransformRules.DoubleSingleQuotes)
            {
                ret = ret.Replace("'", "''");
            }
            if ((trim & TransformRules.EscapeHtml) == TransformRules.EscapeHtml)
            {
                ret = Utils.EscapeHtml(ret);
            }
            if ((trim & TransformRules.EscapeXml) == TransformRules.EscapeXml)
            {
                ret = Utils.EscapeXml(ret);
            }
            if ((trim & TransformRules.QuoteArg) == TransformRules.QuoteArg)
            {
                ret = QuoteArg(ret);
            }
            if ((trim & TransformRules.EscapeRegex) == TransformRules.EscapeRegex)
            {
                ret = Regex.Escape(ret);
            }
            if ((trim & TransformRules.RemoveControl) == TransformRules.RemoveControl)
            {
                char[] ch = ret.ToCharArray();
                for (int i = 0; i < ch.Length; ++i)
                {
                    if (char.IsControl(ch[i]))
                    {
                        ch[i] = (char)127;
                    }
                }
                ret = new string(ch);
            }
            if ((trim & TransformRules.EscapeC) == TransformRules.EscapeC)
            {
                StringBuilder sb = new StringBuilder(ret.Length + 20);
                foreach (var c in ret)
                {
                    switch (c)
                    {
                    case '\0': sb.Append(@"\0"); break;

                    case '\v': sb.Append(@"\v"); break;

                    case '\a': sb.Append(@"\a"); break;

                    case '\t': sb.Append(@"\t"); break;

                    case '\r': sb.Append(@"\r"); break;

                    case '\n': sb.Append(@"\n"); break;

                    case '\b': sb.Append(@"\b"); break;

                    case '\f': sb.Append(@"\f"); break;

                    case '\"': sb.Append("\\\""); break;

                    case '\'': sb.Append(@"\'"); break;

                    case '\\':  sb.Append(@"\\"); break;

                    default:
                        if (c < ' ' || c == '\x7f')
                        {
                            sb.Append("\\u" + ((int)(c)).ToString("x4"));
                        }
                        else
                        {
                            sb.Append(c);
                        }
                        break;
                    }
                }
                ret = sb.ToString();
            }


            if ((trim & TransformRules.UnescapeC) == TransformRules.UnescapeC)
            {
                if (ret.IndexOf('\\') != -1)
                {
                    StringBuilder sb  = new StringBuilder(ret.Length + 20);
                    int           pos = 0;
                    int           n;
                    while ((n = ret.IndexOf('\\', pos)) != -1 && n != ret.Length - 1)
                    {
                        sb.Append(ret, pos, n - pos);
                        switch (ret[n + 1])
                        {
                        case 'a': sb.Append('\a'); pos = n + 2; break;

                        case 'b': sb.Append('\b'); pos = n + 2; break;

                        case 'f': sb.Append('\f'); pos = n + 2; break;

                        case 'n': sb.Append('\n'); pos = n + 2; break;

                        case 'r': sb.Append('\r'); pos = n + 2; break;

                        case 't': sb.Append('\t'); pos = n + 2; break;

                        case '0': sb.Append('\0'); pos = n + 2; break;

                        case '"': sb.Append('\"'); pos = n + 2; break;

                        case '\'': sb.Append('\''); pos = n + 2; break;

                        case '\\': sb.Append('\\'); pos = n + 2; break;

                        case 'u':
                        case 'x':
                            n = pos = n + 2;
                            for (int i = 0; i < 4; ++i)
                            {
                                if (ret.Length > n && isHex(ret[n]))
                                {
                                    n++;
                                }
                                else
                                {
                                    break;
                                }
                            }
                            if (n == pos)
                            {
                                sb.Append(ret[pos - 1]);
                            }
                            else
                            {
                                sb.Append((char)Convert.ToInt16(ret.Substring(pos, n - pos), 16));
                            }
                            pos = n;
                            break;

                        case 'U':
                            n = pos = n + 2;
                            for (int i = 0; i < 8; ++i)
                            {
                                if (ret.Length > n && isHex(ret[n]))
                                {
                                    n++;
                                }
                                else
                                {
                                    break;
                                }
                            }
                            if (n == pos)
                            {
                                sb.Append("u");
                            }
                            else
                            {
                                sb.Append(char.ConvertFromUtf32(Convert.ToInt32(ret.Substring(pos, n - pos), 16)));
                            }
                            pos = n;
                            break;

                        default:
                            sb.Append(ret[n + 1]);
                            pos = n + 1;
                            break;
                        }
                    }
                    sb.Append(ret, pos, ret.Length - pos);
                    ret = sb.ToString();
                }
            }

            if ((trim & TransformRules.TabToSpaces8) != 0)
            {
                StringBuilder sb = new StringBuilder(ret.Length + 20);
                int           ts = 8;
                if ((trim & TransformRules.TabToSpaces4) == TransformRules.TabToSpaces4)
                {
                    ts = 4;
                }
                if ((trim & TransformRules.TabToSpaces2) == TransformRules.TabToSpaces2)
                {
                    ts = 2;
                }
                int col = 0;
                foreach (var ch in ret)
                {
                    switch (ch)
                    {
                    case '\t': sb.Append(' ', ts - col % ts); col += (ts - col % ts); break;

                    case '\n':
                    case '\r': col = 0; sb.Append(ch); break;

                    default: col++; sb.Append(ch); break;
                    }
                }
                ret = sb.ToString();
            }


            return(ret);
        }
Esempio n. 13
0
 /// Convert object to string, expand all variables and output
 public void Print(object obj, TransformRules rules)
 {
     _context.WriteLine(_outputType, _context.Transform(obj, rules));
 }
Esempio n. 14
0
 /// Constructor accepting variable name, value to set and transformation rules for variable and value
 public Set(string name, string value, TransformRules tr)
     : this(name,value)
 {
     Transform = tr;
 }
Esempio n. 15
0
 /// Constructor
 public SetAttr(string actionId, string name, object value, TransformRules tr) : this(actionId, name, value)
 {
     Transform = tr;
 }
Esempio n. 16
0
        /// Transform string according to the specified rules
        public static string TransformStr(string arguments, TransformRules trim)
        {
            if (arguments == null)
                return null;
            if ((trim & TransformRules.Expand) != 0)
                throw new ArgumentOutOfRangeException("trim", "Expand flag cannot be specified");
            string ret;
            if ((trim & TransformRules.Multiline) == TransformRules.Multiline)
            {
                string str = TransformStr(arguments, trim & TransformRules.Trim);
                StringReader reader = new StringReader(str);
                List<string> list = new List<string>();
                while ((str = reader.ReadLine()) != null)
                {
                    list.Add(TransformStr(str, trim & ~TransformRules.Multiline));
                }
                ret = String.Join(Environment.NewLine, list.ToArray());

                return ret;
            }

            TransformRules tr = trim & TransformRules.Trim;
            switch (tr)
            {
                default:
                    ret = arguments;
                    break;

                case TransformRules.Trim:
                    ret = arguments.Trim();
                    break;

                case TransformRules.TrimEnd:
                    ret = arguments.TrimEnd();
                    break;

                case TransformRules.TrimStart:
                    ret = arguments.TrimStart();
                    break;
            }

            if ((trim & TransformRules.TrimInternal) != 0)
                ret = Regex.Replace(ret, @"\s{2,}", " ");

            if ((trim & TransformRules.TildaToSpace) == TransformRules.TildaToSpace)
                ret = ret.Replace("~", " ");
            if ((trim & TransformRules.BackqToDouble) == TransformRules.BackqToDouble)
                ret = ret.Replace("`", "\"");
            if ((trim & TransformRules.CurvedToAngle) == TransformRules.CurvedToAngle)
                ret = ret.Replace("{", "<").Replace("}", ">");
            if ((trim & TransformRules.SquareToAngle) == TransformRules.SquareToAngle)
                ret = ret.Replace("[", "<").Replace("]", ">");
            if ((trim & TransformRules.NewLineToLF) == TransformRules.NewLineToLF)
                ret = ret.Replace("\r\n", "\n").Replace("\r", "\n");
            if ((trim & TransformRules.NewLineToCRLF) == TransformRules.NewLineToCRLF)
                ret = ret.Replace("\r\n", "\n").Replace("\r", "\n").Replace("\n", "\r\n");
            if ((trim & TransformRules.DoubleSingleQuotes) == TransformRules.DoubleSingleQuotes)
                ret = ret.Replace("'", "''");
            if ((trim & TransformRules.EscapeHtml) == TransformRules.EscapeHtml)
                ret = Utils.EscapeHtml(ret);
            if ((trim & TransformRules.EscapeXml) == TransformRules.EscapeXml)
                ret = Utils.EscapeXml(ret);
            if ((trim & TransformRules.QuoteArg) == TransformRules.QuoteArg)
                ret = QuoteArg(ret);
            if ((trim & TransformRules.EscapeRegex) == TransformRules.EscapeRegex)
                ret = Regex.Escape(ret);
            if ((trim & TransformRules.RemoveControl) == TransformRules.RemoveControl)
            {
                char[] ch = ret.ToCharArray();
                for (int i = 0; i < ch.Length; ++i)
                    if (char.IsControl(ch[i]))
                        ch[i] = (char)127;
                ret = new string(ch);
            }
            if ((trim & TransformRules.EscapeC) == TransformRules.EscapeC)
            {
                StringBuilder sb = new StringBuilder(ret.Length+20);
                foreach (var c in ret)
                {
                    switch (c)
                    {
                        case '\0': sb.Append(@"\0"); break;
                        case '\v': sb.Append(@"\v"); break;
                        case '\a': sb.Append(@"\a"); break;
                        case '\t': sb.Append(@"\t"); break;
                        case '\r': sb.Append(@"\r"); break;
                        case '\n': sb.Append(@"\n"); break;
                        case '\b': sb.Append(@"\b"); break;
                        case '\f': sb.Append(@"\f"); break;
                        case '\"': sb.Append("\\\""); break;
                        case '\'': sb.Append(@"\'"); break;
                        case '\\':  sb.Append(@"\\"); break;
                        default:
                            if (c < ' ' || c == '\x7f')
                                sb.Append("\\u" + ((int)(c)).ToString("x4"));
                            else
                                sb.Append(c);
                            break;
                    }
                }
                ret=sb.ToString();
            }

            if ((trim & TransformRules.UnescapeC) == TransformRules.UnescapeC)
            {
                if (ret.IndexOf('\\')!=-1)
                {
                    StringBuilder sb = new StringBuilder(ret.Length + 20);
                    int pos = 0;
                    int n;
                    while ((n=ret.IndexOf('\\',pos))!=-1 && n!=ret.Length-1)
                    {
                        sb.Append(ret, pos, n - pos);
                        switch (ret[n + 1])
                        {
                            case 'a': sb.Append('\a'); pos = n + 2; break;
                            case 'b': sb.Append('\b'); pos = n + 2; break;
                            case 'f': sb.Append('\f'); pos = n + 2; break;
                            case 'n': sb.Append('\n'); pos = n + 2; break;
                            case 'r': sb.Append('\r'); pos = n + 2; break;
                            case 't': sb.Append('\t'); pos = n + 2; break;
                            case '0': sb.Append('\0'); pos = n + 2; break;
                            case '"': sb.Append('\"'); pos = n + 2; break;
                            case '\'': sb.Append('\''); pos = n + 2; break;
                            case '\\': sb.Append('\\'); pos = n + 2; break;
                            case 'u':
                            case 'x':
                                n=pos = n + 2;
                                for (int i = 0; i < 4; ++i)
                                    if (ret.Length > n && isHex(ret[n]))
                                        n++;
                                    else
                                        break;
                                if (n == pos)
                                    sb.Append(ret[pos-1]);
                                else
                                    sb.Append((char)Convert.ToInt16(ret.Substring(pos,n-pos),16));
                                pos = n;
                                break;

                            case 'U':
                                n = pos = n + 2;
                                for (int i = 0; i < 8; ++i)
                                    if (ret.Length > n && isHex(ret[n]))
                                        n++;
                                    else
                                        break;
                                if (n == pos)
                                    sb.Append("u");
                                else
                                    sb.Append(char.ConvertFromUtf32(Convert.ToInt32(ret.Substring(pos, n - pos), 16)));
                                pos = n;
                                break;
                            default:
                                sb.Append(ret[n + 1]);
                                pos = n + 1;
                                break;
                        }
                    }
                    sb.Append(ret, pos, ret.Length - pos);
                    ret=sb.ToString();
                }
            }

            if ((trim & TransformRules.TabToSpaces8) != 0)
            {
                StringBuilder sb = new StringBuilder(ret.Length + 20);
                int ts = 8;
                if ((trim & TransformRules.TabToSpaces4) == TransformRules.TabToSpaces4) ts = 4;
                if ((trim & TransformRules.TabToSpaces2) == TransformRules.TabToSpaces2) ts = 2;
                int col = 0;
                foreach (var ch in ret)
                {
                    switch (ch)
                    {
                        case '\t': sb.Append(' ', ts - col % ts); col += (ts - col % ts); break;
                        case '\n':
                        case '\r': col = 0; sb.Append(ch); break;
                        default: col++; sb.Append(ch); break;
                    }
                }
                ret = sb.ToString();
            }

            return ret;
        }
Esempio n. 17
0
 /// Constructor
 public CallParam(string name, object value, TransformRules transformRules)
 {
     Name      = name;
     Value     = value;
     Transform = transformRules;
 }
Esempio n. 18
0
 ///<summary>Transform string to another string</summary>
 ///<param name="source">Original variable value</param>
 ///<param name="rules">Transformation rules (<see cref="TransformRules"/></param>
 ///<returns>Transformed string</returns>
 public string TransformStr(string source, TransformRules rules)
 {
     return(Utils.To <string>(Transform(source, rules)));
 }
Esempio n. 19
0
 /// Convert object to string, expand all variables and output
 public void Print(object obj, TransformRules rules)
 {
     _context.WriteLine(_outputType, _context.Transform(obj,rules));
 }
Esempio n. 20
0
 /// Constructor
 public VersionInfo()
 {
     Transform = TransformRules.Expand;
 }