Пример #1
0
        public IEnumerable <ICalendarComponent> Deserialize(TextReader reader)
        {
            var context = new SerializationContext();
            var stack   = new Stack <ICalendarComponent>();
            var current = default(ICalendarComponent);

            foreach (var contentLineString in GetContentLines(reader))
            {
                var contentLine = ParseContentLine(context, contentLineString);
                if (string.Equals(contentLine.Name, "BEGIN", StringComparison.OrdinalIgnoreCase))
                {
                    stack.Push(current);
                    current = _componentFactory.Build((string)contentLine.Value);
                    SerializationUtil.OnDeserializing(current);
                }
                else
                {
                    if (current == null)
                    {
                        throw new SerializationException($"Expected 'BEGIN', found '{contentLine.Name}'");
                    }
                    if (string.Equals(contentLine.Name, "END", StringComparison.OrdinalIgnoreCase))
                    {
                        if (!string.Equals((string)contentLine.Value, current.Name, StringComparison.OrdinalIgnoreCase))
                        {
                            throw new SerializationException($"Expected 'END:{current.Name}', found 'END:{contentLine.Value}'");
                        }
                        SerializationUtil.OnDeserialized(current);
                        var finished = current;
                        current = stack.Pop();
                        if (current == null)
                        {
                            yield return(finished);
                        }
                        else
                        {
                            current.Children.Add(finished);
                        }
                    }
                    else
                    {
                        current.Properties.Add(contentLine);
                    }
                }
            }
            if (current != null)
            {
                throw new SerializationException($"Unclosed component {current.Name}");
            }
        }
Пример #2
0
        public ICalendarComponent  component(

            ISerializationContext ctx,
            ISerializerFactory sf,
            ICalendarComponentFactory cf,
            ICalendarObject o

            ) //throws RecognitionException, TokenStreamException
        {
            ICalendarComponent c = null;;

            IToken n = null;
            IToken m = null;

            match(BEGIN);
            match(COLON);
            {
                switch (LA(1))
                {
                case IANA_TOKEN:
                {
                    n = LT(1);
                    match(IANA_TOKEN);
                    c = cf.Build(n.getText().ToLower(), true);
                    break;
                }

                case X_NAME:
                {
                    m = LT(1);
                    match(X_NAME);
                    c = cf.Build(m.getText().ToLower(), true);
                    break;
                }

                default:
                {
                    throw new NoViableAltException(LT(1), getFilename());
                }
                }
            }

            ISerializationProcessor <ICalendarComponent> processor = ctx.GetService(typeof(ISerializationProcessor <ICalendarComponent>)) as ISerializationProcessor <ICalendarComponent>;

            // Do some pre-processing on the component
            if (processor != null)
            {
                processor.PreDeserialization(c);
            }

            SerializationUtil.OnDeserializing(c);

            // Push the component onto the serialization context stack
            ctx.Push(c);

            if (o != null)
            {
                // Add the component as a child immediately, in case
                // embedded components need to access this component,
                // or the iCalendar itself.
                o.AddChild(c);
            }

            c.Line   = n.getLine();
            c.Column = n.getColumn();

            {        // ( ... )*
                for (;;)
                {
                    if ((LA(1) == CRLF))
                    {
                        match(CRLF);
                    }
                    else
                    {
                        goto _loop16_breakloop;
                    }
                }
                _loop16_breakloop :;
            }        // ( ... )*
            {        // ( ... )*
                for (;;)
                {
                    switch (LA(1))
                    {
                    case IANA_TOKEN:
                    case X_NAME:
                    {
                        property(ctx, c);
                        break;
                    }

                    case BEGIN:
                    {
                        component(ctx, sf, cf, c);
                        break;
                    }

                    default:
                    {
                        goto _loop18_breakloop;
                    }
                    }
                }
                _loop18_breakloop :;
            }        // ( ... )*
            match(END);
            match(COLON);
            match(IANA_TOKEN);
            {        // ( ... )*
                for (;;)
                {
                    if ((LA(1) == CRLF))
                    {
                        match(CRLF);
                    }
                    else
                    {
                        goto _loop20_breakloop;
                    }
                }
                _loop20_breakloop :;
            }        // ( ... )*

            // Do some final processing on the component
            if (processor != null)
            {
                processor.PostDeserialization(c);
            }

            // Notify that the component has been loaded
            c.OnLoaded();

            SerializationUtil.OnDeserialized(c);

            // Pop the component off the serialization context stack
            ctx.Pop();

            return(c);
        }
Пример #3
0
	public ICalendarComponent  component(
		
	ISerializationContext ctx,
	ISerializerFactory sf,
	ICalendarComponentFactory cf,
	ICalendarObject o

	) //throws RecognitionException, TokenStreamException
{
		ICalendarComponent c = null;;
		
		IToken  n = null;
		IToken  m = null;
		
		match(BEGIN);
		match(COLON);
		{
			switch ( LA(1) )
			{
			case IANA_TOKEN:
			{
				n = LT(1);
				match(IANA_TOKEN);
				c = cf.Build(n.getText().ToLower(), true);
				break;
			}
			case X_NAME:
			{
				m = LT(1);
				match(X_NAME);
				c = cf.Build(m.getText().ToLower(), true);
				break;
			}
			default:
			{
				throw new NoViableAltException(LT(1), getFilename());
			}
			 }
		}
		
			ISerializationProcessor<ICalendarComponent> processor = ctx.GetService(typeof(ISerializationProcessor<ICalendarComponent>)) as ISerializationProcessor<ICalendarComponent>;
			// Do some pre-processing on the component
			if (processor != null)
				processor.PreDeserialization(c);
		
			SerializationUtil.OnDeserializing(c);
		
			// Push the component onto the serialization context stack
			ctx.Push(c);
		
			if (o != null)
			{
				// Add the component as a child immediately, in case
				// embedded components need to access this component,
				// or the iCalendar itself.
				o.AddChild(c); 
			}
			
			c.Line = n.getLine();
			c.Column = n.getColumn();
		
		{    // ( ... )*
			for (;;)
			{
				if ((LA(1)==CRLF))
				{
					match(CRLF);
				}
				else
				{
					goto _loop16_breakloop;
				}
				
			}
_loop16_breakloop:			;
		}    // ( ... )*
		{    // ( ... )*
			for (;;)
			{
				switch ( LA(1) )
				{
				case IANA_TOKEN:
				case X_NAME:
				{
					property(ctx, c);
					break;
				}
				case BEGIN:
				{
					component(ctx, sf, cf, c);
					break;
				}
				default:
				{
					goto _loop18_breakloop;
				}
				 }
			}
_loop18_breakloop:			;
		}    // ( ... )*
		match(END);
		match(COLON);
		match(IANA_TOKEN);
		{    // ( ... )*
			for (;;)
			{
				if ((LA(1)==CRLF))
				{
					match(CRLF);
				}
				else
				{
					goto _loop20_breakloop;
				}
				
			}
_loop20_breakloop:			;
		}    // ( ... )*
			
			// Do some final processing on the component
			if (processor != null)
				processor.PostDeserialization(c);
		
			// Notify that the component has been loaded
			c.OnLoaded();
			
			SerializationUtil.OnDeserialized(c);
			
			// Pop the component off the serialization context stack
			ctx.Pop();
		
		return c;
	}