Beispiel #1
0
        private MapNode DeserializeIsolatedNode(EventReader r)
        {
            string text = null; NodePosition pos = NodePosition.Undefined; string id = null;

            string prop = r.Peek <Scalar>().Value;

            if (prop != null && prop.Equals(Text))
            {
                r.Expect <Scalar>();
                text = r.Expect <Scalar>().Value;
                prop = r.Peek <Scalar>().Value;
            }

            if (prop != null && prop.Equals(Pos))
            {
                r.Expect <Scalar>();
                pos = (NodePosition)Enum.Parse(typeof(NodePosition), r.Expect <Scalar>().Value);
                //prop = r.Peek<Scalar>().Value;
            }

            // Isolated nodes donot have Id
            //if (prop != null && prop.Equals(Id))
            //{
            //    r.Expect<Scalar>();
            //    id = r.Expect<Scalar>().Value;
            //    //prop = r.Peek<Scalar>().Value;
            //}

            MapNode node = MapNode.CreateIsolatedNode(pos);

            node.Text = text;
            return(node);
        }
Beispiel #2
0
        private MapNode DeserializeIsolatedNode(EventReader r)
        {
            string text = null; NodePosition pos = NodePosition.Undefined; string id = null;

            string prop = r.Peek<Scalar>().Value;

            if (prop != null && prop.Equals(Text))
            {
                r.Expect<Scalar>();
                text = r.Expect<Scalar>().Value;
                prop = r.Peek<Scalar>().Value;
            }

            if (prop != null && prop.Equals(Pos))
            {
                r.Expect<Scalar>();
                pos = (NodePosition)Enum.Parse(typeof(NodePosition), r.Expect<Scalar>().Value);
                //prop = r.Peek<Scalar>().Value;
            }

            // Isolated nodes donot have Id
            //if (prop != null && prop.Equals(Id))
            //{
            //    r.Expect<Scalar>();
            //    id = r.Expect<Scalar>().Value;
            //    //prop = r.Peek<Scalar>().Value;
            //}

            MapNode node = MapNode.CreateIsolatedNode(pos);
            node.Text = text;
            return node;
        }
Beispiel #3
0
        public object DeserializeValue(EventReader reader, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
        {
            var nodeEvent = reader.Peek <NodeEvent>();

            var nodeType = GetTypeFromEvent(nodeEvent, expectedType);

            try
            {
                foreach (var deserializer in deserializers)
                {
                    object value;
                    if (deserializer.Deserialize(reader, nodeType, (r, t) => nestedObjectDeserializer.DeserializeValue(r, t, state, nestedObjectDeserializer), out value))
                    {
                        return(value);
                    }
                }
            }
            catch (YamlException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new YamlException(nodeEvent.Start, nodeEvent.End, "Exception during deserialization", ex);
            }

            throw new YamlException(
                      nodeEvent.Start,
                      nodeEvent.End,
                      string.Format(
                          "No node deserializer was able to deserialize the node into type {0}",
                          expectedType.AssemblyQualifiedName
                          )
                      );
        }
        public object DeserializeValue(EventReader reader, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
        {
            var nodeEvent = reader.Peek<NodeEvent>();

            var nodeType = GetTypeFromEvent(nodeEvent, expectedType);

            try
            {
                foreach (var deserializer in deserializers)
                {
                    object value;
                    if (deserializer.Deserialize(reader, nodeType, (r, t) => nestedObjectDeserializer.DeserializeValue(r, t, state, nestedObjectDeserializer), out value))
                    {
                        return value;
                    }
                }
            }
            catch (YamlException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new YamlException(nodeEvent.Start, nodeEvent.End, "Exception during deserialization", ex);
            }

            throw new YamlException(
                nodeEvent.Start,
                nodeEvent.End,
                string.Format(
                    "No node deserializer was able to deserialize the node into type {0}",
                    expectedType.AssemblyQualifiedName
                )
            );
        }
Beispiel #5
0
        public object DeserializeValue(EventReader reader, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
        {
            object value;
            var    alias = reader.Allow <AnchorAlias>();

            if (alias != null)
            {
                var          aliasState = state.Get <AliasState>();
                ValuePromise valuePromise;
                if (!aliasState.TryGetValue(alias.Value, out valuePromise))
                {
                    valuePromise = new ValuePromise(alias);
                    aliasState.Add(alias.Value, valuePromise);
                }

                return(valuePromise.HasValue ? valuePromise.Value : valuePromise);
            }

            string anchor = null;

            var nodeEvent = reader.Peek <NodeEvent>();

            if (nodeEvent != null && !string.IsNullOrEmpty(nodeEvent.Anchor))
            {
                anchor = nodeEvent.Anchor;
            }

            value = innerDeserializer.DeserializeValue(reader, expectedType, state, nestedObjectDeserializer);

            if (anchor != null)
            {
                var aliasState = state.Get <AliasState>();

                ValuePromise valuePromise;
                if (!aliasState.TryGetValue(anchor, out valuePromise))
                {
                    aliasState.Add(anchor, new ValuePromise(value));
                }
                else if (!valuePromise.HasValue)
                {
                    valuePromise.Value = value;
                }
                else
                {
                    throw new DuplicateAnchorException(nodeEvent.Start, nodeEvent.End, string.Format(
                                                           "Anchor '{0}' already defined",
                                                           anchor
                                                           ));
                }
            }

            return(value);
        }
Beispiel #6
0
        bool INodeDeserializer.Deserialize(EventReader reader, Type expectedType, Func <EventReader, Type, object> nestedObjectDeserializer, out object value)
        {
            value = null;
            NodeEvent nodeEvent = reader.Peek <NodeEvent>();
            bool      flag      = (nodeEvent != null) && this.NodeIsNull(nodeEvent);

            if (flag)
            {
                reader.SkipThisAndNestedEvents();
            }
            return(flag);
        }
		bool INodeDeserializer.Deserialize(EventReader reader, Type expectedType, Func<EventReader, Type, object> nestedObjectDeserializer, out object value)
		{
			value = null;
			var evt = reader.Peek<NodeEvent>();
			var isNull = evt != null
				&& NodeIsNull(evt);

			if (isNull)
			{
				reader.SkipThisAndNestedEvents();
			}
			return isNull;
		}
        bool INodeDeserializer.Deserialize(EventReader reader, Type expectedType, Func <EventReader, Type, object> nestedObjectDeserializer, out object value)
        {
            value = null;
            var evt    = reader.Peek <NodeEvent>();
            var isNull = evt != null &&
                         NodeIsNull(evt);

            if (isNull)
            {
                reader.Skip();
            }
            return(isNull);
        }
        /// <summary>
        /// Throws YamlException
        /// </summary>
        /// <param name="metaModel"></param>
        /// <param name="textReader"></param>
        public void Deserialize(MetaModel.MetaModel metaModel, TextReader textReader)
        {
            Parser      p = new Parser(textReader);
            EventReader r = new EventReader(p);

            r.Expect <StreamStart>();
            r.Expect <DocumentStart>();
            r.Expect <MappingStart>();

            Scalar section = r.Peek <Scalar>();

            if (section.Value.Equals(Icons))
            {
                r.Expect <Scalar>();
                DeserializeIcons(metaModel, r);
                section = r.Peek <Scalar>();
            }

            if (section != null && section.Value.Equals(RecentFiles))
            {
                r.Expect <Scalar>();
                DeserializeRecentFiles(metaModel, r);
                section = r.Peek <Scalar>();
            }

            if (section != null && section.Value.Equals(LastOpenedFile))
            {
                r.Expect <Scalar>();
                metaModel.LastOpenedFile = r.Expect <Scalar>().Value;
                section = r.Peek <Scalar>();
            }

            if (section != null && section.Value.Equals(MapBackColor))
            {
                r.Expect <Scalar>();
                metaModel.MapEditorBackColor = (Color)(new ColorConverter().ConvertFromString(r.Expect <Scalar>().Value));
                section = r.Peek <Scalar>();
            }

            if (section != null && section.Value.Equals(NoteBackColor))
            {
                r.Expect <Scalar>();
                metaModel.NoteEditorBackColor = (Color)(new ColorConverter().ConvertFromString(r.Expect <Scalar>().Value));
                section = r.Peek <Scalar>();
            }

            if (section != null && section.Value.Equals(NodeStyles))
            {
                r.Expect <Scalar>();
                DeserializeNodeStyles(metaModel, r);
                //section = r.Peek<Scalar>(); //uncomment when adding another section
            }



            r.Expect <MappingEnd>();
            r.Expect <DocumentEnd>();
            r.Expect <StreamEnd>();
        }
Beispiel #10
0
        /// <summary>
        /// Throws YamlException
        /// </summary>
        /// <param name="metaModel"></param>
        /// <param name="textReader"></param>
        public void Deserialize(MetaModel.MetaModel metaModel, TextReader textReader)
        {
            Parser p = new Parser(textReader);
            EventReader r = new EventReader(p);

            r.Expect<StreamStart>();
            r.Expect<DocumentStart>();
            r.Expect<MappingStart>();

            Scalar section = r.Peek<Scalar>();
            if (section.Value.Equals(Icons))
            {
                r.Expect<Scalar>();
                DeserializeIcons(metaModel, r);
                section = r.Peek<Scalar>();
            }

            if (section != null && section.Value.Equals(RecentFiles))
            {
                r.Expect<Scalar>();
                DeserializeRecentFiles(metaModel, r);
                section = r.Peek<Scalar>();
            }

            if (section != null && section.Value.Equals(LastOpenedFile))
            {
                r.Expect<Scalar>();
                metaModel.LastOpenedFile = r.Expect<Scalar>().Value;
                section = r.Peek<Scalar>();
            }

            if (section != null && section.Value.Equals(MapBackColor))
            {
                r.Expect<Scalar>();
                metaModel.MapEditorBackColor = (Color)(new ColorConverter().ConvertFromString(r.Expect<Scalar>().Value));
                section = r.Peek<Scalar>();
            }

            if (section != null && section.Value.Equals(NoteBackColor))
            {
                r.Expect<Scalar>();
                metaModel.NoteEditorBackColor = (Color)(new ColorConverter().ConvertFromString(r.Expect<Scalar>().Value));
                section = r.Peek<Scalar>();
            }

            if (section != null && section.Value.Equals(NodeStyles))
            {
                r.Expect<Scalar>();
                DeserializeNodeStyles(metaModel, r);
                //section = r.Peek<Scalar>(); //uncomment when adding another section
            }

            r.Expect<MappingEnd>();
            r.Expect<DocumentEnd>();
            r.Expect<StreamEnd>();
        }
        public object DeserializeValue(EventReader reader, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
        {
            AnchorAlias alias = reader.Allow <AnchorAlias>();

            if (alias != null)
            {
                ValuePromise promise;
                AliasState   state2 = state.Get <AliasState>();
                if (!state2.TryGetValue(alias.Value, out promise))
                {
                    promise = new ValuePromise(alias);
                    state2.Add(alias.Value, promise);
                }
                return(!promise.HasValue ? promise : promise.Value);
            }
            string    key    = null;
            NodeEvent event2 = reader.Peek <NodeEvent>();

            if ((event2 != null) && !string.IsNullOrEmpty(event2.Anchor))
            {
                key = event2.Anchor;
            }
            object obj2 = this.innerDeserializer.DeserializeValue(reader, expectedType, state, nestedObjectDeserializer);

            if (key != null)
            {
                ValuePromise promise2;
                AliasState   state3 = state.Get <AliasState>();
                if (!state3.TryGetValue(key, out promise2))
                {
                    state3.Add(key, new ValuePromise(obj2));
                }
                else
                {
                    if (promise2.HasValue)
                    {
                        throw new DuplicateAnchorException(event2.Start, event2.End, $"Anchor '{key}' already defined");
                    }
                    promise2.Value = obj2;
                }
            }
            return(obj2);
        }
        public object DeserializeValue(EventReader reader, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
        {
            object value;
            var    alias = reader.Allow <AnchorAlias>();

            if (alias != null)
            {
                var aliasState = state.Get <AliasState>();
                if (aliasState.TryGetValue(alias.Value, out value))
                {
                    return(value);
                }

                throw new AnchorNotFoundException(alias.Start, alias.End, string.Format(
                                                      "Anchor '{0}' not found",
                                                      alias.Value
                                                      ));
            }

            string anchor = null;

            var nodeEvent = reader.Peek <NodeEvent>();

            if (nodeEvent != null && !string.IsNullOrEmpty(nodeEvent.Anchor))
            {
                anchor = nodeEvent.Anchor;
            }

            value = innerDeserializer.DeserializeValue(reader, expectedType, state, nestedObjectDeserializer);

            if (anchor != null)
            {
                var aliasState = state.Get <AliasState>();
                aliasState.Add(anchor, value);
            }

            return(value);
        }
Beispiel #13
0
        public object DeserializeValue(EventReader reader, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
        {
            var nodeEvent = reader.Peek <NodeEvent>();

            var nodeType = GetTypeFromEvent(nodeEvent, expectedType);

            foreach (var deserializer in deserializers)
            {
                object value;
                if (deserializer.Deserialize(reader, nodeType, (r, t) => nestedObjectDeserializer.DeserializeValue(r, t, state, nestedObjectDeserializer), out value))
                {
                    return(value);
                }
            }

            throw new SerializationException(
                      string.Format(
                          "No node deserializer was able to deserialize the node at {0} into type {1}",
                          reader.Parser.Current.Start,
                          expectedType.AssemblyQualifiedName
                          )
                      );
        }
		public object DeserializeValue (EventReader reader, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
		{
			var nodeEvent = reader.Peek<NodeEvent>();

			var nodeType = GetTypeFromEvent(nodeEvent, expectedType);

			foreach (var deserializer in deserializers)
			{
				object value;
				if (deserializer.Deserialize(reader, nodeType, (r, t) => nestedObjectDeserializer.DeserializeValue(r, t, state, nestedObjectDeserializer), out value))
				{
					return value;
				}
			}

			throw new SerializationException(
				string.Format(
					"No node deserializer was able to deserialize the node at {0} into type {1}",
					reader.Parser.Current.Start,
					expectedType.AssemblyQualifiedName
				)
			);
		}
        public bool Deserialize(EventReader reader, Type expectedType, Func<EventReader, Type, object> nestedObjectDeserializer, out object value)
        {
            if (reader == null)
            {
                value = null;
                return false;
            }

            // only try this if we're targeting a boolean or an untyped object
            if (expectedType == typeof(object) || expectedType == typeof(bool) ) 
            {
                // peek at the current token
                Scalar scalar = reader.Peek<Scalar>();

                // if it's unquoted 
                if (scalar != null && scalar.Style == ScalarStyle.Plain)
                {
                    // and the value is actually true or false
                    switch (scalar.Value.ToUpperInvariant())
                    {
                        case "TRUE":
                            value = true;
                            reader.Allow<Scalar>();
                            return true;
                        case "FALSE":
                            value = false;
                            reader.Allow<Scalar>();
                            return true;

                    }
                }
            }

            // otherwise, fall thru
            value = null;
            return false;
        }
Beispiel #16
0
        public bool Deserialize(EventReader reader, Type expectedType, Func <EventReader, Type, object> nestedObjectDeserializer, out object value)
        {
            if (reader == null)
            {
                value = null;
                return(false);
            }

            // only try this if we're targeting a boolean or an untyped object
            if (expectedType == typeof(object) || expectedType == typeof(bool))
            {
                // peek at the current token
                Scalar scalar = reader.Peek <Scalar>();

                // if it's unquoted
                if (scalar != null && scalar.Style == ScalarStyle.Plain)
                {
                    // and the value is actually true or false
                    switch (scalar.Value.ToUpperInvariant())
                    {
                    case "TRUE":
                        value = true;
                        reader.Allow <Scalar>();
                        return(true);

                    case "FALSE":
                        value = false;
                        reader.Allow <Scalar>();
                        return(true);
                    }
                }
            }

            // otherwise, fall thru
            value = null;
            return(false);
        }
Beispiel #17
0
        private void DeserializeScalarProperties(MapNode node, EventReader r)
        {
            Scalar prop = r.Peek<Scalar>();

            if (prop != null && prop.Value.Equals(Folded))
            {
                r.Expect<Scalar>();
                node.Folded = Boolean.Parse(r.Expect<Scalar>().Value);
                prop = r.Peek<Scalar>();
            }

            if (prop != null && prop.Value.Equals(Link))
            {
                r.Expect<Scalar>();
                node.Link = r.Expect<Scalar>().Value;
                prop = r.Peek<Scalar>();
            }

            if (prop != null && prop.Value.Equals(Created))
            {
                r.Expect<Scalar>();
                node.Created = DateHelper.ToDateTime(r.Expect<Scalar>().Value);
                prop = r.Peek<Scalar>();
            }

            if (prop != null && prop.Value.Equals(Modified))
            {
                r.Expect<Scalar>();
                node.Modified = DateHelper.ToDateTime(r.Expect<Scalar>().Value);
                prop = r.Peek<Scalar>();
            }

            if (prop != null && prop.Value.Equals(Bold))
            {
                r.Expect<Scalar>();
                node.Bold = Boolean.Parse(r.Expect<Scalar>().Value);
                prop = r.Peek<Scalar>();
            }

            if (prop != null && prop.Value.Equals(Italic))
            {
                r.Expect<Scalar>();
                node.Italic = Boolean.Parse(r.Expect<Scalar>().Value);
                prop = r.Peek<Scalar>();
            }

            if (prop != null && prop.Value.Equals(Strikeout))
            {
                r.Expect<Scalar>();
                node.Strikeout = Boolean.Parse(r.Expect<Scalar>().Value);
                prop = r.Peek<Scalar>();
            }

            if (prop != null && prop.Value.Equals(FontName))
            {
                r.Expect<Scalar>();
                node.FontName = r.Expect<Scalar>().Value;
                prop = r.Peek<Scalar>();
            }

            if (prop != null && prop.Value.Equals(FontSize))
            {
                r.Expect<Scalar>();
                node.FontSize = Single.Parse(r.Expect<Scalar>().Value);
                prop = r.Peek<Scalar>();
            }

            if (prop != null && prop.Value.Equals(BackColor))
            {
                r.Expect<Scalar>();
                node.BackColor = (Color)new ColorConverter().ConvertFromString(r.Expect<Scalar>().Value);
                prop = r.Peek<Scalar>();
            }

            if (prop != null && prop.Value.Equals(Color))
            {
                r.Expect<Scalar>();
                node.Color = (Color)new ColorConverter().ConvertFromString(r.Expect<Scalar>().Value);
                prop = r.Peek<Scalar>();
            }

            if (prop != null && prop.Value.Equals(Shape))
            {
                r.Expect<Scalar>();
                node.Shape = (NodeShape)Enum.Parse(typeof(NodeShape), r.Expect<Scalar>().Value);
                prop = r.Peek<Scalar>();
            }

            if (prop != null && prop.Value.Equals(LineWidth))
            {
                r.Expect<Scalar>();
                node.LineWidth = Int32.Parse(r.Expect<Scalar>().Value);
                prop = r.Peek<Scalar>();
            }

            if (prop != null && prop.Value.Equals(LinePattern))
            {
                r.Expect<Scalar>();
                node.LinePattern = (DashStyle)Enum.Parse(typeof(DashStyle), r.Expect<Scalar>().Value);
                prop = r.Peek<Scalar>();
            }

            if (prop != null && prop.Value.Equals(LineColor))
            {
                r.Expect<Scalar>();
                node.LineColor = (Color)new ColorConverter().ConvertFromString(r.Expect<Scalar>().Value);
                prop = r.Peek<Scalar>();
            }

            if (prop != null && prop.Value.Equals(NoteText))
            {
                r.Expect<Scalar>();
                node.NoteText = r.Expect<Scalar>().Value;
                prop = r.Peek<Scalar>();
            }

            //if (prop != null && prop.Value.Equals(Image))
            //{
            //    r.Expect<Scalar>();
            //    node.Image = r.Expect<Scalar>().Value;
            //    prop = r.Peek<Scalar>();
            //}

            if (prop != null && prop.Value.Equals(ImageAlignment))
            {
                r.Expect<Scalar>();
                node.ImageAlignment = (ImageAlignment)Enum.Parse(typeof(ImageAlignment), r.Expect<Scalar>().Value);
                prop = r.Peek<Scalar>();
            }

            if (prop != null && prop.Value.Equals(Label))
            {
                r.Expect<Scalar>();
                node.Label = r.Expect<Scalar>().Value;
                //prop = r.Peek<Scalar>();
            }
        }
Beispiel #18
0
        private void DeserializeScalarProperties(MapNode node, EventReader r)
        {
            Scalar prop = r.Peek <Scalar>();

            if (prop != null && prop.Value.Equals(Folded))
            {
                r.Expect <Scalar>();
                node.Folded = Boolean.Parse(r.Expect <Scalar>().Value);
                prop        = r.Peek <Scalar>();
            }

            if (prop != null && prop.Value.Equals(Link))
            {
                r.Expect <Scalar>();
                node.Link = r.Expect <Scalar>().Value;
                prop      = r.Peek <Scalar>();
            }

            if (prop != null && prop.Value.Equals(Created))
            {
                r.Expect <Scalar>();
                node.Created = DateHelper.ToDateTime(r.Expect <Scalar>().Value);
                prop         = r.Peek <Scalar>();
            }

            if (prop != null && prop.Value.Equals(Modified))
            {
                r.Expect <Scalar>();
                node.Modified = DateHelper.ToDateTime(r.Expect <Scalar>().Value);
                prop          = r.Peek <Scalar>();
            }

            if (prop != null && prop.Value.Equals(Bold))
            {
                r.Expect <Scalar>();
                node.Bold = Boolean.Parse(r.Expect <Scalar>().Value);
                prop      = r.Peek <Scalar>();
            }

            if (prop != null && prop.Value.Equals(Italic))
            {
                r.Expect <Scalar>();
                node.Italic = Boolean.Parse(r.Expect <Scalar>().Value);
                prop        = r.Peek <Scalar>();
            }

            if (prop != null && prop.Value.Equals(Strikeout))
            {
                r.Expect <Scalar>();
                node.Strikeout = Boolean.Parse(r.Expect <Scalar>().Value);
                prop           = r.Peek <Scalar>();
            }

            if (prop != null && prop.Value.Equals(FontName))
            {
                r.Expect <Scalar>();
                node.FontName = r.Expect <Scalar>().Value;
                prop          = r.Peek <Scalar>();
            }

            if (prop != null && prop.Value.Equals(FontSize))
            {
                r.Expect <Scalar>();
                node.FontSize = Single.Parse(r.Expect <Scalar>().Value);
                prop          = r.Peek <Scalar>();
            }

            if (prop != null && prop.Value.Equals(BackColor))
            {
                r.Expect <Scalar>();
                node.BackColor = (Color) new ColorConverter().ConvertFromString(r.Expect <Scalar>().Value);
                prop           = r.Peek <Scalar>();
            }

            if (prop != null && prop.Value.Equals(Color))
            {
                r.Expect <Scalar>();
                node.Color = (Color) new ColorConverter().ConvertFromString(r.Expect <Scalar>().Value);
                prop       = r.Peek <Scalar>();
            }

            if (prop != null && prop.Value.Equals(Shape))
            {
                r.Expect <Scalar>();
                node.Shape = (NodeShape)Enum.Parse(typeof(NodeShape), r.Expect <Scalar>().Value);
                prop       = r.Peek <Scalar>();
            }

            if (prop != null && prop.Value.Equals(LineWidth))
            {
                r.Expect <Scalar>();
                node.LineWidth = Int32.Parse(r.Expect <Scalar>().Value);
                prop           = r.Peek <Scalar>();
            }

            if (prop != null && prop.Value.Equals(LinePattern))
            {
                r.Expect <Scalar>();
                node.LinePattern = (DashStyle)Enum.Parse(typeof(DashStyle), r.Expect <Scalar>().Value);
                prop             = r.Peek <Scalar>();
            }

            if (prop != null && prop.Value.Equals(LineColor))
            {
                r.Expect <Scalar>();
                node.LineColor = (Color) new ColorConverter().ConvertFromString(r.Expect <Scalar>().Value);
                prop           = r.Peek <Scalar>();
            }

            if (prop != null && prop.Value.Equals(NoteText))
            {
                r.Expect <Scalar>();
                node.NoteText = r.Expect <Scalar>().Value;
                prop          = r.Peek <Scalar>();
            }

            //if (prop != null && prop.Value.Equals(Image))
            //{
            //    r.Expect<Scalar>();
            //    node.Image = r.Expect<Scalar>().Value;
            //    prop = r.Peek<Scalar>();
            //}

            if (prop != null && prop.Value.Equals(ImageAlignment))
            {
                r.Expect <Scalar>();
                node.TextAlignment = (TextAlignment)Enum.Parse(typeof(TextAlignment), r.Expect <Scalar>().Value);
                prop = r.Peek <Scalar>();
            }

            if (prop != null && prop.Value.Equals(Label))
            {
                r.Expect <Scalar>();
                node.Label = r.Expect <Scalar>().Value;
                //prop = r.Peek<Scalar>();
            }
        }
Beispiel #19
0
        private static object FromYaml(string yamlText, string yamlFile, bool expectOnlyFrontMatter, out TextPosition position)
        {
            try
            {
                position = new TextPosition();
                if (yamlText == null)
                {
                    return(null);
                }

                var parser = new Parser(new StringReader(yamlText));
                var reader = new EventReader(parser);

                if (!reader.Accept <StreamStart>())
                {
                    return(null);
                }

                reader.Expect <StreamStart>();
                var docStart         = reader.Expect <DocumentStart>();
                var hasDocumentStart = true;

                object      result  = null;
                ScriptArray objects = null;

                // If we expect to read multiple documents, we will return an array of result
                if (expectOnlyFrontMatter && docStart.IsImplicit)
                {
                    return(null);
                }

                Mark endPosition;

                while (true)
                {
                    if (reader.Accept <StreamEnd>())
                    {
                        var evt = reader.Expect <StreamEnd>();
                        endPosition = evt.End;
                        break;
                    }

                    if (hasDocumentStart && reader.Accept <DocumentEnd>())
                    {
                        reader.Expect <DocumentEnd>();

                        hasDocumentStart = false;

                        if (expectOnlyFrontMatter)
                        {
                            reader.Accept <DocumentStart>();
                            // Don't consume the token as the parser will try to parse
                            // the following characters and could hit non YAML syntax (in Markdown)
                            // and would throw a parser exception
                            var nextDocStart = reader.Peek <DocumentStart>();
                            endPosition = nextDocStart.End;
                            break;
                        }

                        continue;
                    }

                    if (reader.Accept <DocumentStart>())
                    {
                        reader.Expect <DocumentStart>();
                        hasDocumentStart = true;
                    }

                    var obj = ReadEvent(reader);

                    if (result == null)
                    {
                        result = obj;
                    }
                    else
                    {
                        if (objects == null)
                        {
                            objects = new ScriptArray {
                                result
                            };
                            result = objects;
                        }

                        objects.Add(obj);
                    }
                }

                position = new TextPosition(endPosition.Index, endPosition.Line, endPosition.Column);
                return(result);
            }
            catch (Exception ex)
            {
                throw new LunetException($"Error while parsing {yamlFile}. {ex.Message}");
            }
        }