Beispiel #1
0
        public static string EncodeValue(object value, SharpValueType valueType, char delimiter = '\0', bool quoteQuotes = false)
        {
            if (value == null)
            {
                return("");
            }

            if (value is DateTime)
            {
                return(EncodedString.TrimStandardDate((DateTime)value));
            }

            if (!(value is string))
            {
                return(value.ToString());
            }

            var result = value.ToString();

            if (EncodedString.IsQuotedStringEncodingRequired(result, delimiter: delimiter, quoteQuotes: quoteQuotes))
            {
                return(EncodedString.EncodeQuotedString(result));
            }

            return(result);
        }
 public SharpNodeMapColumn(int nodeIndex, SharpValueType valueType = SharpValueType.None,
                           int width = -1, int offset = 0)
 {
     Index     = nodeIndex;
     ValueType = valueType;
     Width     = width;
     Offset    = offset;
 }
 public SharpNodeMapColumn(SharpNode node, int width = -1, int offset = 0)
 {
     Node      = node;
     Width     = width;
     Offset    = offset;
     Index     = node.Index;
     ValueType = node.ValueType;
 }
Beispiel #4
0
        public static string ToString(object value, SharpValueType type)
        {
            if (value == null)
            {
                return(string.Empty);
            }

            return(value.ToString());
        }
Beispiel #5
0
 public SharpMetaNodeRow(SharpMetaNodeRow source)
 {
     Index          = source.Index;
     Path           = source.Path;
     NodeType       = source.NodeType;
     ValueType      = source.ValueType;
     AdditionalInfo = source.AdditionalInfo.ToDictionary(x => x.Key, x => new SharpValue(x.Value));
     QueryPath      = source.QueryPath.ToDictionary(x => x.Key,
                                                    x => new KeyValuePair <string, string>(x.Value.Key, x.Value.Value));
 }
Beispiel #6
0
        public static bool TryGetValueList(object value, out SharpValueType type)
        {
            type = SharpValueType.None;

            if (value is string)
            {
                return(false);
            }

            if (value is IEnumerable <int> )
            {
                type = SharpValueType.Int;
                return(true);
            }

            if (value is IEnumerable <string> )
            {
                type = SharpValueType.String;
                return(true);
            }

            if (value is IEnumerable <bool> )
            {
                type = SharpValueType.Bool;
                return(true);
            }

            if (value is IEnumerable <DateTime> )
            {
                type = SharpValueType.DateTime;
                return(true);
            }

            if (value is IEnumerable <double> )
            {
                type = SharpValueType.Double;
                return(true);
            }

            if (value is IEnumerable <decimal> )
            {
                type = SharpValueType.Decimal;
                return(true);
            }

            if (value is IEnumerable <long> )
            {
                type = SharpValueType.Long;
                return(true);
            }

            return(value is IEnumerable);
        }
        public object GetValueList()
        {
            SharpValueType        listType = SharpValueType.None;
            List <SharpValueType> types    = _items.Select(x => x.GetType("#")).Distinct().ToList();

            if (types.Count == 1)
            {
                listType = types[0];
            }

            return(SharpValue.AsValueList(_items.Select(x => x.GetValue()), listType));
        }
Beispiel #8
0
 public SharpMetaNodeRow(SharpNode source)
 {
     Index     = source.Index;
     Path      = source.Path;
     NodeType  = source.NodeType;
     ValueType = source.ValueType;
     if (!string.IsNullOrEmpty(source.Format))
     {
         AdditionalInfo["Format"] = new SharpValue(source.Format);
     }
     if (source.DefaultValue != null)
     {
         AdditionalInfo["DefaultValue"] = new SharpValue(source.DefaultValue);
     }
 }
Beispiel #9
0
        public SharpValue(object value)
        {
            Value = value;

            if (value is string)
            {
                Type = SharpValueType.String;
            }
            else if (value is int)
            {
                Type = SharpValueType.Int;
            }
            else if (value is bool)
            {
                Type = SharpValueType.Bool;
            }
            else if (value is DateTime)
            {
                Type = SharpValueType.DateTime;
            }
            else if (value is TimeSpan)
            {
                Type = SharpValueType.TimeSpan;
            }
            else if (value is long)
            {
                Type = SharpValueType.Long;
            }
            else if (value is float || value is double)
            {
                Type = SharpValueType.Double;
            }
            else if (value is decimal)
            {
                Type = SharpValueType.Decimal;
            }
            else if (value is List <int> || value is int[])
            {
                Type = SharpValueType.List;
            }
            else if (value is List <string> || value is string[])
            {
                Type = SharpValueType.List;
            }
        }
Beispiel #10
0
        public static object GetDefaultValue(SharpValueType type)
        {
            switch (type)
            {
            case SharpValueType.String:
            case SharpValueType.NumericString:
            case SharpValueType.None:
                return(string.Empty);

            case SharpValueType.Bool:
                return(default(bool));

            case SharpValueType.Int:
                return(default(int));

            case SharpValueType.Long:
                return(default(long));

            case SharpValueType.Double:
                return(default(double));

            case SharpValueType.Decimal:
                return(Decimal.Zero);

            case SharpValueType.Date:
            case SharpValueType.DateTime:
            case SharpValueType.TimeStamp:
                return(DateTime.MinValue);

            case SharpValueType.TimeSpan:
                return(TimeSpan.Zero);

            case SharpValueType.List:
                return(new List <object>());

            case SharpValueType.Object:
                return(new SharpObject());

            default:
                return(null);
            }
        }
Beispiel #11
0
        public static Type GetFieldType(SharpValueType type)
        {
            switch (type)
            {
            case SharpValueType.None:
                return(typeof(string));

            case SharpValueType.Bool:
                return(typeof(bool));

            case SharpValueType.Int:
                return(typeof(int));

            case SharpValueType.Long:
                return(typeof(long));

            case SharpValueType.Double:
                return(typeof(double));

            case SharpValueType.Decimal:
                return(typeof(Decimal));

            case SharpValueType.Date:
            case SharpValueType.DateTime:
            case SharpValueType.TimeStamp:
                return(typeof(DateTime));

            case SharpValueType.TimeSpan:
                return(typeof(TimeSpan));

            case SharpValueType.List:
                return(typeof(IList <object>));

            case SharpValueType.Object:
                return(typeof(object));

            default:
                return(typeof(string));
            }
        }
Beispiel #12
0
        public static object AsValueList(IEnumerable <object> values, SharpValueType type)
        {
            try
            {
                switch (type)
                {
                case SharpValueType.Bool:
                    return(values.Cast <bool>());

                case SharpValueType.String:
                    return(values.Cast <string>());

                case SharpValueType.Int:
                    return(values.Cast <int>());

                case SharpValueType.Long:
                    return(values.Cast <long>());

                case SharpValueType.Decimal:
                    return(values.Cast <Decimal>());

                case SharpValueType.Double:
                    return(values.Cast <double>());

                case SharpValueType.Date:
                case SharpValueType.DateTime:
                case SharpValueType.TimeStamp:
                    return(values.Cast <DateTime>());

                case SharpValueType.TimeSpan:
                    return(values.Cast <TimeSpan>());
                }
            }
            catch
            {
            }

            return(values);
        }
Beispiel #13
0
        public static bool IsPrimitiveType(SharpValueType type)
        {
            switch (type)
            {
            case SharpValueType.String:
            case SharpValueType.None:
            case SharpValueType.Bool:
            case SharpValueType.Long:
            case SharpValueType.Double:
            case SharpValueType.Int:
                return(true);

            case SharpValueType.Date:
            case SharpValueType.DateTime:
            case SharpValueType.TimeStamp:
            case SharpValueType.TimeSpan:
            case SharpValueType.List:
            case SharpValueType.Decimal:
            case SharpValueType.Object:
                return(false);
            }
            return(false);
        }
Beispiel #14
0
        public void Parse(string row, string defaultPath = "")
        {
            AdditionalInfo.Clear();
            QueryPath.Clear();

            ValueType = SharpValueType.None;
            NodeType  = SharpNodeType.Any;

            StringBuilder sbPath         = new StringBuilder();
            StringBuilder sbType         = new StringBuilder();
            StringBuilder sbFormat       = new StringBuilder();
            StringBuilder sbDefaultValue = new StringBuilder();
            StringBuilder sbQueryPath    = new StringBuilder();
            StringBuilder sbQueryValue   = new StringBuilder();
            StringBuilder sbIndex        = new StringBuilder();

            bool parseIndex        = false;
            bool parsePath         = true;
            bool parseQueryPath    = false;
            bool parseQueryValue   = false;
            bool parseType         = false;
            bool parseFormat       = false;
            bool parseDefaultValue = false;

            bool isValueNode    = false;
            bool isAttribute    = false;
            bool isPathExpanded = false;
            bool isPathLocked   = false;

            int formatCount = 0;

            int pos = 0;

            while (pos < row.Length)
            {
                char c = row[pos++];
                if (char.IsWhiteSpace(c))
                {
                    continue;
                }

                if (c == '$')
                {
                    parseIndex = true;
                }
                else
                {
                    pos--;
                }
                break;
            }

            while (pos < row.Length)
            {
                char c = row[pos++];

                if (parseIndex)
                {
                    if (c == '=')
                    {
                        int index = -1;
                        if (int.TryParse(sbIndex.ToString().Trim(), out index))
                        {
                            Index = index;
                        }

                        parseIndex = false;
                    }
                    else
                    {
                        sbIndex.Append(c);
                    }
                }
                else if (parseDefaultValue)
                {
                    sbDefaultValue.Append(c);
                }
                else if (parseFormat)
                {
                    if (c == '}')
                    {
                        if (formatCount >= 0)
                        {
                            parseFormat = false;
                            parsePath   = true;
                            AdditionalInfo["Format"] = new SharpValue(sbFormat.ToString());
                        }
                        else
                        {
                            formatCount--;
                        }
                    }
                    else
                    {
                        if (c == '{')
                        {
                            formatCount++;
                        }
                        sbFormat.Append(c);
                    }
                }
                else if (parseType)
                {
                    if (c == ' ' || c == '=' || c == '{' || c == ':')
                    {
                        var t = sbType.ToString().Trim();

                        if (!string.IsNullOrEmpty(t))
                        {
                            SharpNodeType nt;
                            if (Enum.TryParse(t, true, out nt))
                            {
                                NodeType = nt;
                            }

                            SharpValueType vt;
                            if (Enum.TryParse(t, true, out vt))
                            {
                                ValueType = vt;
                            }
                        }

                        sbType.Clear();
                        if (c == '=' || c == ':')
                        {
                            parseType = false;
                            parsePath = true;
                            pos--;
                            continue;
                        }

                        else if (c == '{')
                        {
                            parseType   = false;
                            parseFormat = true;
                        }
                    }
                    else
                    {
                        sbType.Append(c);
                    }
                }

                else if (parseQueryValue)
                {
                    if (c == ']')
                    {
                        QueryPath[sbPath.ToString()] = new KeyValuePair <string, string>(sbQueryPath.ToString(), sbQueryValue.ToString());
                        parseQueryValue = false;
                        sbQueryPath.Clear();
                        sbQueryValue.Clear();
                    }
                    else
                    {
                        sbQueryValue.Append(c);
                    }
                }
                else if (parseQueryPath)
                {
                    if (c == '=')
                    {
                        parseQueryPath  = false;
                        parseQueryValue = true;
                    }
                    else if (c == ']')
                    {
                        parseQueryPath = false;
                        sbQueryPath.Clear();
                        sbQueryValue.Clear();
                    }
                    else
                    {
                        sbQueryPath.Append(c);
                    }
                }
                else if (parsePath)
                {
                    if (char.IsWhiteSpace(c))
                    {
                        continue;
                    }

                    if (!isPathExpanded)
                    {
                        if (c == '~' || c == '=' || c == '{' || c == '.' || c == '@')
                        {
                            sbPath.Append(defaultPath);
                            isPathExpanded = true;

                            if (c != '.')
                            {
                                isValueNode  = true;
                                isPathLocked = true;
                                sbPath.Append('/');
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }
                    else if (c == '.' && !isPathLocked)
                    {
                        var parts = sbPath.ToString().Split('/').Select(x => x.Trim()).Where(x => !string.IsNullOrEmpty(x)).ToList();
                        if (parts.Count > 1)
                        {
                            parts = parts.Take(parts.Count - 1).ToList();
                        }
                        sbPath = new StringBuilder(String.Join("/", parts));
                        continue;
                    }

                    if (c != '.')
                    {
                        isPathLocked = true;
                    }

                    if (c == '[')
                    {
                        parseQueryPath = true;
                    }
                    else if (c == '~')
                    {
                        parseType = true;
                        parsePath = false;
                    }
                    else if (c == '{')
                    {
                        parseFormat = true;
                        parsePath   = false;
                    }
                    else if (c == ':')
                    {
                        if (pos < row.Length && row[pos] == '=')
                        {
                            pos++;
                            parseDefaultValue = true;
                            parsePath         = false;
                        }
                    }
                    else if (c == '=')
                    {
                        isValueNode       = true;
                        parseDefaultValue = true;
                        parsePath         = false;
                    }
                    else
                    {
                        if (c == '@')
                        {
                            isAttribute = true;
                            isValueNode = true;
                        }
                        else if (c == '#')
                        {
                            isValueNode = true;
                        }
                        else if (c == '/')
                        {
                            isValueNode = false;
                            isAttribute = false;
                        }

                        sbPath.Append(c);
                        isPathExpanded = true;
                    }
                }
            }

            if (parseType)
            {
                var t = sbType.ToString().Trim();

                if (!string.IsNullOrEmpty(t))
                {
                    SharpNodeType nt;
                    if (Enum.TryParse(t, true, out nt))
                    {
                        NodeType = nt;
                    }

                    SharpValueType vt;
                    if (Enum.TryParse(t, true, out vt))
                    {
                        ValueType = vt;
                    }
                }

                parseType = false;
            }


            Path = sbPath.ToString();

            if (isValueNode && !isAttribute && !Path.EndsWith("/#"))
            {
                if (!Path.EndsWith("/"))
                {
                    Path += "/#";
                }
                else
                {
                    Path += "#";
                }
            }

            if (parseDefaultValue)
            {
                var defaultValue = sbDefaultValue.ToString().Trim();
                if (defaultValue.StartsWith("\""))
                {
                    defaultValue = EncodedString.ParseQuotedString(defaultValue);
                }

                AdditionalInfo["DefaultValue"] = new SharpValue(defaultValue);
            }
        }
Beispiel #15
0
 public SharpValue(SharpValue source)
 {
     Type   = source.Type;
     Value  = source.Value;
     Format = source.Format;
 }
Beispiel #16
0
 public SharpValue(string value, bool parseQuoted = false)
 {
     Type  = SharpValueType.String;
     Value = parseQuoted ? EncodedString.ParseQuotedString(value) : value;
 }
Beispiel #17
0
        public static object ToValue(string value, SharpValueType type, bool autoType = false)
        {
            if (value == null)
            {
                return(null);
            }

            switch (type)
            {
            case SharpValueType.None:
                if (autoType)
                {
                    return(ToValue(value, SharpValueType.Auto));
                }
                return(value);

            case SharpValueType.String:
                return(value);

            case SharpValueType.Bool:
            {
                bool boolResult;
                if (bool.TryParse(value, out boolResult))
                {
                    return(boolResult);
                }

                return(value);
            }

            case SharpValueType.Int:
            {
                int intResult;
                if (int.TryParse(value, out intResult))
                {
                    return(intResult);
                }

                return(value);
            }

            case SharpValueType.Long:
            {
                long longResult;
                if (long.TryParse(value, out longResult))
                {
                    return(longResult);
                }

                return(value);
            }

            case SharpValueType.Date:
            case SharpValueType.DateTime:
            case SharpValueType.TimeStamp:
            {
                DateTime dateResult;
                if (DateTime.TryParse(value, out dateResult))
                {
                    return(dateResult);
                }

                return(value);
            }

            case SharpValueType.TimeSpan:
            {
                TimeSpan timeSpanResult;
                if (TimeSpan.TryParse(value, out timeSpanResult))
                {
                    return(timeSpanResult);
                }
                return(value);
            }

            case SharpValueType.Double:
            {
                double doubleResult;
                if (double.TryParse(value, out doubleResult))
                {
                    return(doubleResult);
                }
                return(value);
            }

            case SharpValueType.Decimal:
            {
                Decimal decimalResult;
                if (Decimal.TryParse(value, out decimalResult))
                {
                    return(decimalResult);
                }
                return(value);
            }

            case SharpValueType.NumericString:
            {
                if (NumericString.CanEncode(value))
                {
                    return(new NumericString(value));
                }
                return(value);
            }

            case SharpValueType.List:
            {
                return(EncodedString.ParseList(value, trimStrings: true));
            }

            case SharpValueType.Object:
            {
                return(EncodedString.ParseObject(value));
            }
            }

            return(value);
        }