Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

A Flexible Plugin System

, 3 Sep 2008
A generic plugin system used to load and manage plugins
fadd-15373.zip
trunk
dlls
xunit.dll
Examples
Plugins
ExampleApplication.Shared
Properties
ExampleApplication
Properties
ExamplePlugin.Shared
Properties
ExamplePlugin
Properties
Fadd.Globalization.Yaml
fadd.snk
Properties
Tests
fadd
Commands
Net
Tests
fadd.snk
Globalization
Tests
Logging
Plugins
Properties
Tests
Validation
using System.Collections;
using System.Collections.Generic;
using System.IO;

namespace Fadd.Globalization.Yaml
{
    /// <summary>
    /// An implementation of YAML that only support those things that is needed by the Language loader.
    /// </summary>
    public class YamlLight : IEnumerable<YamlLight>
    {
        private string _name = string.Empty;
        private string _value = string.Empty;
        private readonly LinkedList<YamlLight> _children = new LinkedList<YamlLight>();
        private readonly YamlLight _parent = null;
        private readonly int _intendation = 0;

        public YamlLight(YamlLight parent, int intendation)
        {
            _parent = parent;
            _intendation = intendation;
            if (_parent != null)
                _parent._children.AddLast(this);
        }

        public YamlLight(YamlLight parent, string key, string value)
        {
            _parent = parent;
            _intendation = parent.Intendation+2;
            _name = key;
            _value = value;
            if (_parent != null)
                _parent._children.AddLast(this);
        }

        public int Count
        {
            get { return _children.Count; }
        }
        public int Intendation
        {
            get { return _intendation; }
        }

        public YamlLight Parent
        {
            get { return _parent; }
        }

        public void Add(string key, string value)
        {
            new YamlLight(this, key, value);
        }

        public YamlLight this[string name]
        {
            get
            {
                foreach (YamlLight node in _children)
                {
                    if (node.Name.Equals(name))
                        return node;
                }

                return null;
            }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }

        public static YamlLight Parse(TextReader reader)
        {
            YamlLight mother = new YamlLight(null, -1);

            int lineNumber = 0;
            string line = reader.ReadLine();
            YamlLight curNode = mother;
            while (line != null)
            {
                ++lineNumber;
                YamlLight node = HandlePlacement(curNode, line, lineNumber);
                if (node != null)
                {
                    curNode = node;
                    curNode.ParseData(line, curNode.Intendation, ref lineNumber, reader);
                }

                line = reader.ReadLine();
            }

            return mother;
        }

        private static YamlLight HandlePlacement(YamlLight curNode, string line, int lineNumber)
        {
            if (line.Trim() == string.Empty)
                return null;
            int intend = GetIntend(line);
            if (intend == -1)
                throw new InvalidDataException("Invalid intendation on line " + lineNumber);

            YamlLight node;

            // new node is a child to current node
            if (curNode.Intendation < intend)
                node = new YamlLight(curNode, intend);
                // both are chilren to the parent
            else if (curNode.Intendation == intend)
                node = new YamlLight(curNode.Parent, intend);
                // same level as parent or less
            else
            {
                YamlLight parent = curNode.Parent;
                while (parent != null && parent.Intendation > intend)
                    parent = parent.Parent;
                if (parent == null)
                    throw new InvalidDataException("Failed to find parent node on line " + lineNumber);
                node = new YamlLight(parent.Parent, intend);
            }

            return node;
        }

        protected void ParseData(string line, int offset, ref int lineNumber, TextReader reader)
        {
            int pos = line.IndexOf(':', offset);
            if (pos == -1)
                throw new InvalidDataException("Failed to find value on line " + lineNumber);

            Name = line.Substring(offset, pos - offset);
            if (pos < line.Length-1)
                Value = line.Substring(pos + 1).Trim();

            if(Value.EndsWith("|"))
            {
                Value = Value.TrimEnd('|');

                while((line = reader.ReadLine()) != null)
                {
                    lineNumber++;
                    Value += line.TrimStart('\t').Trim().TrimEnd('|');

                    if(!line.EndsWith("|"))
                        break;
                }
            }
        }

        static int GetIntend(string line)
        {
            for (int i = 0; i < line.Length; ++i)
            {
                if (!char.IsWhiteSpace(line[i]))
                    return i;
            }

            return -1;
        }

        IEnumerator<YamlLight> IEnumerable<YamlLight>.GetEnumerator()
        {
            return _children.GetEnumerator();
        }

        public IEnumerator GetEnumerator()
        {
            return _children.GetEnumerator();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

About the Author

jgauffin
Founder Gauffin Interactive AB
Sweden Sweden
Founder of OneTrueError, a .NET service which captures, analyzes and provide possible solutions for exceptions.
 
blog | twitter
Follow on   Twitter   LinkedIn

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 3 Sep 2008
Article Copyright 2008 by jgauffin
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid