Exemplo n.º 1
0
 /// <summary>
 /// Try get parameter infos.
 /// </summary>
 /// <param name="lineFormat"></param>
 /// <param name="parameterInfos"></param>
 /// <returns>true if returned infos</returns>
 public static bool TryGetParameterInfos(this ILineFormat lineFormat, out IParameterInfos parameterInfos)
 {
     ILineFactory lineFactory;
     if (lineFormat.TryGetLineFactory(out lineFactory) && lineFactory.TryGetParameterInfos(out parameterInfos)) return true;
     parameterInfos = default;
     return false;
 }
Exemplo n.º 2
0
 /// <summary>
 /// Get parameter infos.
 /// </summary>
 /// <param name="lineFormat"></param>
 /// <returns>infos or null</returns>
 public static IParameterInfos GetParameterInfos(this ILineFormat lineFormat)
 {
     ILineFactory lineFactory;
     IParameterInfos parameterInfos;
     if (lineFormat.TryGetLineFactory(out lineFactory) && lineFactory.TryGetParameterInfos(out parameterInfos)) return parameterInfos;
     return null;
 }
Exemplo n.º 3
0
 /// <summary>
 /// Try get parameter info.
 /// </summary>
 /// <param name="lineFormat"></param>
 /// <param name="parameterName"></param>
 /// <param name="parameterInfo"></param>
 /// <returns>true if returned info</returns>
 public static bool TryGetParameterInfo(this ILineFormat lineFormat, string parameterName, out IParameterInfo parameterInfo)
 {
     ILineFactory lineFactory;
     IParameterInfos parameterInfos;
     if (lineFormat.TryGetLineFactory(out lineFactory) && lineFactory.TryGetParameterInfos(out parameterInfos) && parameterInfos.TryGetValue(parameterName, out parameterInfo)) return true;
     parameterInfo = null;
     return false;
 }
Exemplo n.º 4
0
 /// <summary>
 /// Get parameter info.
 /// </summary>
 /// <param name="lineFormat"></param>
 /// <param name="parameterName"></param>
 /// <returns>info or null</returns>
 public static IParameterInfo GetParameterInfo(this ILineFormat lineFormat, string parameterName)
 {
     IParameterInfo info;
     ILineFactory lineFactory;
     IParameterInfos parameterInfos;
     if (lineFormat.TryGetLineFactory(out lineFactory) && lineFactory.TryGetParameterInfos(out parameterInfos) && parameterInfos.TryGetValue(parameterName, out info)) return info;
     return null;
 }
        /// <summary>
        /// Read key tree from <paramref name="text"/>.
        /// </summary>
        /// <param name="text"></param>
        /// <param name="lineFormat">uses parameter info</param>
        /// <returns></returns>
        public ILineTree ReadLineTree(TextReader text, ILineFormat lineFormat = default)
        {
            ILineFactory _lineFactory;

            if (!lineFormat.TryGetLineFactory(out _lineFactory))
            {
                _lineFactory = LineFactory;
            }
            return(ReadElement(Load(text).Root, new LineTree(), null, _lineFactory));
        }
        /// <summary>
        /// Read key tree from <paramref name="stream"/>.
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="lineFormat">uses parameter info</param>
        /// <returns></returns>
        public ILineTree ReadLineTree(Stream stream, ILineFormat lineFormat = default)
        {
            ILineFactory _lineFactory;

            if (!lineFormat.TryGetLineFactory(out _lineFactory))
            {
                _lineFactory = LineFactory;
            }
            return(ReadElement(Load(stream).Root, new LineTree(), null, _lineFactory));
        }
        /// <summary>
        /// Read key tree from <paramref name="element"/>
        /// </summary>
        /// <param name="element"></param>
        /// <param name="lineFormat">uses parameter info</param>
        /// <returns></returns>
        public ILineTree ReadLineTree(XElement element, ILineFormat lineFormat = default)
        {
            ILineFactory _lineFactory;

            if (!lineFormat.TryGetLineFactory(out _lineFactory))
            {
                _lineFactory = LineFactory;
            }
            return(ReadElement(element, new LineTree(), null, _lineFactory));
        }
        /// <summary>
        /// Read json token stream into <paramref name="node"/>
        /// </summary>
        /// <param name="json"></param>
        /// <param name="node">parent node to under which add nodes</param>
        /// <param name="lineFormat">unused</param>
        /// <param name="correspondenceContext">(optional) place to update correspondence. If set <paramref name="json"/> must implement <see cref="JTokenReader"/>.</param>
        /// <returns></returns>
        public ILineTree ReadJsonIntoTree(JsonReader json, ILineTree node, ILineFormat lineFormat, JsonCorrespondence correspondenceContext)
        {
            ILineFactory tmp;
            ILineFormat  _lineFormat = lineFormat.TryGetLineFactory(out tmp) && tmp != LineFactory ? new LineFormat(" :\\", false, " :\\", false, tmp, null) : this.lineFormat;

            ILineTree         current     = node;
            Stack <ILineTree> stack       = new Stack <ILineTree>();
            JTokenReader      tokenReader = json as JTokenReader;
            bool updateCorrespondence     = correspondenceContext != null && tokenReader != null;

            while (json.Read())
            {
                switch (json.TokenType)
                {
                case JsonToken.StartObject:
                    stack.Push(current);
                    if (updateCorrespondence)
                    {
                        correspondenceContext.Nodes.Put(current, tokenReader.CurrentToken);
                    }
                    break;

                case JsonToken.EndObject:
                    current = stack.Pop();
                    break;

                case JsonToken.PropertyName:
                    ILine key = null;
                    if (_lineFormat.TryParse(json.Value?.ToString(), out key))
                    {
                        current = key == null?stack.Peek() : stack.Peek()?.Create(key);

                        if (current != null && updateCorrespondence && !correspondenceContext.Nodes.ContainsLeft(current))
                        {
                            correspondenceContext.Nodes.Put(current, tokenReader.CurrentToken);
                        }
                    }
                    else
                    {
                        current = null;
                    }
                    break;

                case JsonToken.Raw:
                case JsonToken.Date:
                case JsonToken.String:
                case JsonToken.Boolean:
                case JsonToken.Float:
                case JsonToken.Integer:
                    if (current != null)
                    {
                        string value = json.Value?.ToString();
                        if (value != null)
                        {
                            int           ix = current.Values.Count;
                            IStringFormat stringFormat;
                            if (current.TryGetStringFormat(resolver, out stringFormat))
                            {
                                // Append FormatString
                                IString     valueString = stringFormat.Parse(value);
                                ILineString lineValue   = LineFactory.Create <ILineString, IString>(null, valueString);
                                current.Values.Add(lineValue);
                                if (updateCorrespondence)
                                {
                                    correspondenceContext.Values[new LineTreeValue(current, lineValue, ix)] = (JValue)tokenReader.CurrentToken;
                                }
                            }
                            else
                            {
                                // Append Hint
                                ILineHint lineValue = LineFactory.Create <ILineHint, string, string>(null, "String", value);
                                current.Values.Add(lineValue);
                                if (updateCorrespondence)
                                {
                                    correspondenceContext.Values[new LineTreeValue(current, lineValue, ix)] = (JValue)tokenReader.CurrentToken;
                                }
                            }
                        }
                    }
                    break;

                case JsonToken.StartArray:
                    if (updateCorrespondence)
                    {
                        correspondenceContext.Nodes.Put(current, tokenReader.CurrentToken);
                    }
                    break;

                case JsonToken.EndArray:
                    break;
                }
            }
            return(node);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Read ini token stream into <paramref name="root"/>
        /// </summary>
        /// <param name="ini">ini token stream. </param>
        /// <param name="root">parent node to under which add nodes</param>
        /// <param name="lineFormat">unused</param>
        /// <param name="correspondence">(optional) if set tokens are associated to key tree. If <paramref name="correspondence"/> is provided, then <paramref name="ini"/> must be a linked list. See <see cref="IniTokenizer.ToLinkedList"/></param>
        /// <returns><paramref name="root"/></returns>
        public ILineTree ReadIniIntoTree(IEnumerable <IniToken> ini, ILineTree root, ILineFormat lineFormat, IniCorrespondence correspondence)
        {
            ILineFactory tmp;
            ILineFormat  _escaper_section = lineFormat.TryGetLineFactory(out tmp) && tmp != LineFactory ? new LineFormat("\\:[]", true, "\\:[]", true, tmp, null) : this.escaper_section;
            ILineFormat  _escaper_key     = lineFormat.TryGetLineFactory(out tmp) && tmp != LineFactory ? new LineFormat("\\:= ", true, "\\:= ", true, tmp, null) : this.escaper_key;

            ILineTree section = null;

            foreach (IniToken token in ini)
            {
                switch (token.Type)
                {
                case IniTokenType.Section:
                    ILine key = null;
                    if (_escaper_section.TryParse(token.ValueText, out key))
                    {
                        section = key == null ? null : root.Create(key);
                        if (section != null && correspondence != null)
                        {
                            correspondence.Nodes.Put(section, token);
                        }
                    }
                    else
                    {
                        section = null;
                    }
                    break;

                case IniTokenType.KeyValue:
                    ILine key_ = null;
                    if (_escaper_key.TryParse(token.KeyText, out key_))
                    {
                        if (key_ == null)
                        {
                            key_ = LineFactory.Create <ILinePart>(null);
                        }
                        ILineTree current = (section ?? root).GetOrCreate(key_);
                        string    value   = escaper_value.UnescapeLiteral(token.ValueText);

                        if (value != null)
                        {
                            IStringFormat stringFormat;
                            ILine         lineValue;
                            if (current.TryGetStringFormat(resolver, out stringFormat))
                            {
                                // Append FormatString
                                IString valueString = stringFormat.Parse(value);
                                lineValue = LineFactory.Create <ILineString, IString>(null, valueString);
                            }
                            else
                            {
                                // Append Hint
                                lineValue = LineFactory.Create <ILineHint, string, string>(null, "String", value);
                            }

                            int ix = current.Values.Count;
                            current.Values.Add(lineValue);
                            if (correspondence != null)
                            {
                                correspondence.Values[new LineTreeValue(current, lineValue, ix)] = token;
                            }
                        }
                    }
                    break;

                case IniTokenType.Comment: break;

                case IniTokenType.Text: break;
                }
            }
            return(root);
        }