/// <summary>
 /// Reads an object data that has been written with <see cref="WriteInlineObjectStructuredElement"/>.
 /// If the reader is not positioned on the <paramref name="elementName"/>, an <see cref="XmlException"/> is thrown.
 /// </summary>
 /// <param name="sr">This <see cref="IStructuredReader"/> object.</param>
 /// <param name="elementName">Name of the Xml element.</param>
 /// <param name="o">Object to read that has been previously created or reinitialized. Can not be null.</param>
 /// <returns>The object.</returns>
 /// <remarks>
 /// If a <see cref="IStructuredSerializer{T}"/> for the runtime type (obtained by <see cref="Object.GetType"/>) is available in the services,
 /// it will be used. Otherwise, the object must implement <see cref="IStructuredSerializable"/>.
 /// </remarks>
 static public object ReadInlineObjectStructuredElement( this IStructuredReader sr, string elementName, object o )
 {
     if( o == null ) throw new ArgumentNullException( "o" );
     if( elementName == null ) throw new ArgumentNullException( "elementName" );
     // This does the trick: it raises an XmlException.
     if( !sr.Xml.IsStartElement( elementName ) ) sr.Xml.ReadStartElement( elementName );
     return sr.ReadInlineObjectStructured( o.GetType(), o );
 }
Example #2
0
        public object ReadInlineContent(IStructuredReader sr, Dog d)
        {
            // This is an independant scope...
            sr.ServiceContainer.Add <UniqueService>(new UniqueService());

            XmlReader r = sr.Xml;

            if (d == null)
            {
                d = new Dog();
            }

            d.Name = r.GetAttribute("Name");
            // Leaves the current open element with its attributes.
            r.Read();
            d.Paws = new List <Paw>();
            while (r.IsStartElement("Paw"))
            {
                d.Paws.Add((Paw)sr.ReadInlineObjectStructured(typeof(Paw)));
            }
            return(d);
        }
Example #3
0
        void IStructuredSerializable.ReadContent( IStructuredReader sr )
        {
            XmlReader r = sr.Xml;
            Debug.Assert( r.Name == "Key" );
            r.Read();
            r.ReadStartElement( "KeyModes" );
            while( r.IsStartElement( "KeyMode" ) )
            {
                // We consider a missing attribute Mode (GetAttribute will return null) as Mode="": we'll use the Context EmptyMode.
                IKeyboardMode keyMode = Context.ObtainMode( r.GetAttribute( "Mode" ) );
                IKeyboardMode availableKeyMode = Keyboard.AvailableMode.Intersect( keyMode );

                if( keyMode == availableKeyMode )
                {
                    // If the mode is defined at the keyboard level,
                    // we create a new or update the existing actual key.
                    KeyMode k = FindOrCreate( keyMode );
                    sr.ReadInlineObjectStructured( k );
                }
                else
                {
                    // Key mode is not defined... This is not a
                    // standard case. Since defining the mode at the keyboard level just because a
                    // key uses it is NOT an option, we choose to create the key with the actually available mode
                    // only if it does not already exist and we DO NOT change an existing key.
                    // This is a conservative approach that avoids blindly accepting biased data from the context.
                    KeyMode k = Find( availableKeyMode );
                    if( k == null )
                    {
                        k = FindOrCreate( keyMode );
                        sr.ReadInlineObjectStructured( k );
                    }
                    else r.Skip();
                }
            }
            r.Read();

            CK.Keyboard.Versionning.V150To160.Key150To160( this );
        }
 internal void ReadInlineContent( IStructuredReader sr )
 {
     XmlReader r = sr.Xml;
     Keyboard current = null;
     r.Read();
     while( r.IsStartElement( "Keyboard" ) )
     {
         string n = r.GetAttribute( "Name" );
         Keyboard kb = Create( n );
         bool isCurrent = r.GetAttribute( "IsCurrent" ) == "1";
         sr.ReadInlineObjectStructured( kb );
         if( isCurrent ) current = kb;
     }
     if( current != null ) Current = current;
 }
        void IStructuredSerializable.ReadContent( IStructuredReader sr )
        {
            XmlReader r = sr.Xml;
            bool isCurrent = false;

            //We are on the <Layouts> tag, we mode on to the content
            r.Read();

            while( r.IsStartElement( "Layout" ) )
            {
                Layout l = null;
                string n = r.GetAttribute( "Name" );
                if( n == null ) n = String.Empty;

                l = this[n];
                if( l == null ) l = Create( n );

                isCurrent = r.GetAttributeBoolean( "IsCurrent", false );
                sr.ReadInlineObjectStructured( l );
                if( isCurrent ) _currentLayout = l;
            }
        }
Example #6
0
        void IStructuredSerializable.ReadContent( IStructuredReader sr )
        {
            XmlReader r = sr.Xml;
            Debug.Assert( r.Name == "Zone" );
            if( !r.IsEmptyElement )
            {
                r.Read();

                if( r.IsStartElement( "Keys" ) )
                {
                    r.Read();
                    while( r.IsStartElement( "Key" ) )
                    {
                        Key k = Create( _keys.Count );
                        sr.ReadInlineObjectStructured( k );
                    }
                    r.Read();
                }
            }
        }
Example #7
0
        public void SimpleStructuredSerializerTest()
        {
            string testPath = TestBase.GetTestFilePath("CKTests.Storage", "StructuredSerializer");
            IStructuredSerializer <Dog> serializer = new SimpleStructuredDogSerializer();
            Dog dog = CreateDog();

            using (Stream str = new FileStream(testPath, FileMode.Create))
            {
                using (IStructuredWriter writer = SimpleStructuredWriter.CreateWriter(str, new SimpleServiceContainer()))
                {
                    // This is an independant scope: we just created the writer...
                    writer.ServiceContainer.Add <UniqueService>(new UniqueService());
                    writer.ServiceContainer.Add <IStructuredSerializer <Dog> >(new SimpleStructuredDogSerializer());
                    writer.ServiceContainer.Add <IStructuredSerializer <Paw> >(new SimpleStructuredPawSerializer());

                    writer.WriteObjectElement("Dog", dog);
                }
            }

            Dog readDog;

            // 1 - Use ReadInlineObject
            using (Stream str = new FileStream(testPath, FileMode.Open))
                using (IStructuredReader reader = CreateConfiguredReader(str))
                {
                    // This is an independant scope: we just created the reader...
                    reader.ServiceContainer.Add <UniqueService>(new UniqueService());

                    StandardReadStatus status;
                    object             o = reader.ReadInlineObject(out status);
                    readDog = o as Dog;
                }
            CheckReadDog(readDog);

            // 2 - Use ReadInlineObjectStructured( Type )
            using (Stream str = new FileStream(testPath, FileMode.Open))
                using (IStructuredReader reader = CreateConfiguredReader(str))
                {
                    // This is an independant scope: we just created the reader...
                    reader.ServiceContainer.Add <UniqueService>(new UniqueService());

                    Assert.That(reader.Xml.IsStartElement("Dog"));
                    // We ignore attributes added by WriteObjectElement: we directly call ReadInlineObjectStructured for the Dog type.
                    object o = reader.ReadInlineObjectStructured(typeof(Dog), null);
                    readDog = o as Dog;
                }
            CheckReadDog(readDog);

            // 3 - Use ReadInlineObjectStructured( object )
            using (Stream str = new FileStream(testPath, FileMode.Open))
                using (IStructuredReader reader = CreateConfiguredReader(str))
                {
                    // This is an independant scope: we just created the reader...
                    reader.ServiceContainer.Add <UniqueService>(new UniqueService());

                    Assert.That(reader.Xml.IsStartElement("Dog"));
                    readDog = new Dog();
                    // We ignore attributes added by WriteObjectElement: we directly call ReadInlineObjectStructured for an empty Dog object.
                    object o = reader.ReadInlineObjectStructured(readDog);
                }
            CheckReadDog(readDog);
        }
Example #8
0
        void IStructuredSerializable.ReadContent( IStructuredReader sr )
        {
            XmlReader r = sr.Xml;
            Debug.Assert( r.Name == "LayoutKey" );
            r.Read();
            while( r.IsStartElement( "LayoutKeyMode" ) )
            {
                IKeyboardMode keyMode = _key.Context.ObtainMode( r.GetAttribute( "Mode" ) );
                IKeyboardMode availableKeyMode = _key.Keyboard.AvailableMode.Intersect( keyMode );

                LayoutKeyMode k = FindOrCreate( availableKeyMode );
                sr.ReadInlineObjectStructured( k );
            }
        }
 /// <summary>
 /// Reads an object data that has been written with <see cref="IStructuredWriter.WriteInlineObjectStructured"/>.
 /// </summary>
 /// <param name="sr">This <see cref="IStructuredReader"/> object.</param>
 /// <param name="o">Object to read that has been previously created or reinitialized. Can not be null.</param>
 /// <returns>The object.</returns>
 /// <remarks>
 /// If a <see cref="IStructuredSerializer{T}"/> for the runtime type (obtained by <see cref="Object.GetType"/>) is available in the services,
 /// it will be used. Otherwise, the object must implement <see cref="IStructuredSerializable"/>.
 /// </remarks>
 static public object ReadInlineObjectStructured( this IStructuredReader sr, object o )
 {
     if( o == null ) throw new ArgumentNullException( "o" );
     return sr.ReadInlineObjectStructured( o.GetType(), o );
 }
 /// <summary>
 /// Reads an object that has been written with <see cref="IStructuredWriter.WriteInlineObjectStructured"/>.
 /// </summary>
 /// <param name="sr">This <see cref="IStructuredReader"/> object.</param>
 /// <param name="type">Type of the object to read. If a <see cref="IStructuredSerializer{T}"/> is available in the services, it is used, 
 /// otherwise, the type must both offer a default construtor and implement <see cref="IStructuredSerializable"/>.</param>
 /// <returns>Deserialized object (can be null).</returns>
 static public object ReadInlineObjectStructured( this IStructuredReader sr, Type type )
 {
     if( type == null ) throw new ArgumentNullException( "type" );
     return sr.ReadInlineObjectStructured( type, null );
 }