/// <summary> /// Append value to <paramref name="prev"/>> /// </summary> /// <param name="parent"></param> /// <param name="_lineFactory"></param> /// <param name="prev">(optional)</param> /// <param name="parameterName"></param> /// <param name="parameterValue"></param> /// <returns></returns> ILine Append(ILineTree parent, ILineFactory _lineFactory, ILine prev, string parameterName, string parameterValue) { if (parameterName == "String") { IStringFormat stringFormat; if (parent.TryGetStringFormat(resolver, out stringFormat)) { IString valueString = stringFormat.Parse(parameterValue); return(LineFactory.Create <ILineString, IString>(prev, valueString)); } else { return(LineFactory.Create <ILineHint, string, string>(prev, "String", parameterValue)); } } else { return(LineFactory.Create <ILineParameter, string, string>(prev, parameterName, parameterValue)); } }
/// <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); }
/// <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); }