Ejemplo n.º 1
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));
 }
Ejemplo n.º 2
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);
     }
 }
Ejemplo n.º 3
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);
            }
        }
 public SharpTokenElement(SharpNodeType tokenNodeType) : base(tokenNodeType)
 {
 }
 public SharpTokenElement(SharpNodeType tokenNodeType) : base(tokenNodeType) { }