Exemple #1
0
        /// <summary>
        ///     Parse the text that contains the iCalendar representation.
        ///     Create an instance of VCALENDAR object with all the calendar components
        ///     and properties.
        /// </summary>
        /// <param name="calendarString">The calendar representation to be parsed.</param>
        /// <returns>The VCaledar instance.</returns>
        public static VCalendar iCalendarParser(string calendarString)
        {
            //used to create instance of calendar component objects
            var calCompFactory = new CalendarComponentFactory();

            //used to create instance of component property objects
            var compPropFactory = new ComponentPropertyFactory();
            var objStack        = new Stack <ICalendarObject>();

            var lines = CalendarReader(calendarString);

            foreach (var line in lines)
            {
                List <PropertyParameter> parameters;
                string lineValue;

                string firstLineString;
                if (!LineParser(line, out firstLineString, out parameters, out lineValue))
                {
                    continue;
                }
                switch (firstLineString)
                {
                case "BEGIN":


                    ///if the component is vcalendar then create is
                    /// if not then call the factory to get the object
                    /// that name.
                    var calComponent = lineValue == "VCALENDAR" ? new VCalendar() : calCompFactory.CreateIntance(lineValue);
                    objStack.Push(calComponent);
                    continue;

                case "END":
                    var endedObject = objStack.Pop();
                    //if the last object in the stack is an VCalendar then
                    //is the end of the parsing
                    var vCalendar = endedObject as VCalendar;
                    if (vCalendar != null)
                    {
                        return(vCalendar);
                    }

                    ///if the object is not a VCalendar means
                    /// that should be added to his father that
                    /// is the first in the stack
                    ((IAggregator)objStack.Peek()).AddItem(endedObject);
                    continue;
                }
                ///creates an instance of a property
                var compProperty = compPropFactory.CreateIntance(firstLineString, firstLineString);

                var topObj = objStack.Peek();
                //set the value and params in the compProperty via the Deserializer of the property
                ((IAggregator)topObj).AddItem(((IDeserialize)compProperty).Deserialize(lineValue, parameters));
            }

            throw new ArgumentException("The calendar file MUST contain at least an element.");
        }
Exemple #2
0
        /// <summary>
        /// Parse the string that contain a calendar defines
        /// under the protocol RFC 5545. Builds an instance
        /// of VCalendar with its components, properties..
        /// </summary>
        /// <param name="calendarString">The calendar to parse.</param>
        /// <returns>An instance of VCalendar.</returns>
        public static VCalendar Parse(string calendarString)
        {
            var             calCompFactory  = new CalendarComponentFactory();
            var             compPropFactory = new ComponentPropertyFactory();
            var             name            = "";
            var             value           = "";
            var             parameters      = new List <PropertyParameter>();
            ICalendarObject calComponent    = null;
            ICalendarObject compProperty    = null;
            var             objStack        = new Stack <ICalendarObject>();
            Type            type            = null;
            var             lines           = Parser.CalendarReader(calendarString);

            foreach (var line in lines)
            {
                if (!Parser.CalendarParser(line, out name, out parameters, out value))
                {
                    continue;
                }
                switch (name)
                {
                case "BEGIN":


                    ///if the component is vcalendar then create is
                    /// if not then call the factory to get the object
                    /// that name.
                    calComponent = value == "VCALENDAR" ? new VCalendar() : calCompFactory.CreateIntance(value);
                    objStack.Push(calComponent);
                    continue;

                case "END":
                    var endedObject = objStack.Pop();
                    //if the last object in the stack is an VCalendar then
                    //is the end of the parsing
                    var vCalendar = endedObject as VCalendar;
                    if (vCalendar != null)
                    {
                        return(vCalendar);
                    }

                    ///if the object is not a VCalendar means
                    /// that should be added to his father that
                    /// is the first in the stack
                    ((IAggregator)objStack.Peek()).AddItem(endedObject);
                    continue;
                }
                ///creates an instance of a property
                compProperty = compPropFactory.CreateIntance(name, name);

                var topObj = objStack.Peek();
                ((IAggregator)topObj).AddItem(((IDeserialize)compProperty).Deserialize(value, parameters));
            }

            throw new ArgumentException("The calendar file MUST contain at least an element.");
        }
Exemple #3
0
        public VCalendar(string calendarString)
        {
            var             calCompFactory       = new CalendarComponentFactory();
            var             compPropFactory      = new ComponentPropertyFactory();
            var             assemblyNameCalendar = "ICalendar.Calendar.";
            var             name         = "";
            var             value        = "";
            var             parameters   = new List <PropertyParameter>();
            ICalendarObject calComponent = null;
            ICalendarObject compProperty = null;
            var             objStack     = new Stack <ICalendarObject>();
            Type            type         = null;
            var             lines        = Parser.CalendarReader(calendarString);

            foreach (var line in lines)
            {
                if (!Parser.LineParser(line, out name, out parameters, out value))
                {
                    continue;
                }
                //TODO: Do the necessary with the objects that dont belong to CompProperties
                if (name == "BEGIN")
                {
                    var className = value;
                    className = className.Substring(0, 2) + className.Substring(2).ToLower();
                    if (value == "VCALENDAR")
                    {
                        type         = Type.GetType(assemblyNameCalendar + className);
                        calComponent = Activator.CreateInstance(type) as ICalendarObject;
                    }
                    else
                    {
                        calComponent = calCompFactory.CreateIntance(className);
                    }
                    objStack.Push(calComponent);
                    continue;
                }
                if (name == "END")
                {
                    var endedObject = objStack.Pop();
                    //if the last object in the stack is an VCalendar then
                    //is the end of the parsing
                    if (endedObject is VCalendar)
                    {
                        var calendar = endedObject as VCalendar;
                        Properties         = calendar.Properties;
                        CalendarComponents = calendar.CalendarComponents;
                        return;
                    }
                    ((IAggregator)objStack.Peek()).AddItem(endedObject);
                    continue;
                }
                var propSysName = name;
                if (name.Contains("-"))
                {
                    propSysName = name.Replace("-", "_");
                }
                propSysName  = propSysName.Substring(0, 1) + propSysName.Substring(1).ToLower();
                compProperty = compPropFactory.CreateIntance(propSysName, name);
                if (compProperty == null)
                {
                    continue;
                }

                var topObj = objStack.Peek();
                ((IAggregator)topObj).AddItem(((IDeserialize)compProperty).Deserialize(value, parameters));
            }

            throw new ArgumentException("The calendar file MUST contain at least an element.");
        }