예제 #1
0
        public static async Task <TSyndicationItem> LoadAsync <TSyndicationItem>(XmlReader reader)
            where TSyndicationItem : SyndicationItem, new()
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            Atom10ItemFormatter <TSyndicationItem> atomSerializer = new Atom10ItemFormatter <TSyndicationItem>();

            if (atomSerializer.CanRead(reader))
            {
                atomSerializer.ReadFrom(reader);
                return(atomSerializer.Item as TSyndicationItem);
            }

            Rss20ItemFormatter <TSyndicationItem> rssSerializer = new Rss20ItemFormatter <TSyndicationItem>();

            if (rssSerializer.CanRead(reader))
            {
                await rssSerializer.ReadFromAsync(reader).ConfigureAwait(false);

                return(rssSerializer.Item as TSyndicationItem);
            }

            throw new XmlException(SR.Format(SR.UnknownItemXml, reader.LocalName, reader.NamespaceURI));
        }
예제 #2
0
        protected virtual SyndicationItem ReadItem(XmlReader reader, SyndicationFeed feed)
        {
            Atom10ItemFormatter formatter = new Atom10ItemFormatter();

            formatter.ReadFrom(reader);
            return(formatter.Item);
        }
        public override object ReadFromStream(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger) {

            HttpContentHeaders contentHeaders = content == null ? null : content.Headers;

            // If content length is 0 then return default value for this type
            if (contentHeaders != null && contentHeaders.ContentLength == null) {

                return GetDefaultValueForType(type);
            }

            try {
                using (readStream) {
                    using (var reader = XmlReader.Create(readStream)) {

                        var formatter = new Atom10ItemFormatter();
                        formatter.ReadFrom(reader);

                        var command = Activator.CreateInstance(type);
                        ((IPublicationCommand)command).ReadSyndicationItem(formatter.Item);

                        return command;
                    }
                }
            }
            catch (Exception e) {

                if (formatterLogger == null) {
                    throw;
                }
                formatterLogger.LogError(String.Empty, e);
                return GetDefaultValueForType(type);
            }
        }
        public void FunctionalTest()
        {
            var configuration = HttpHostConfiguration.Create()
                .SetResourceFactory((type, instanceContext, request) => new RoomResource(Maze.NewInstance(), Monsters.NullEncounters()), (instanceContext, obj) => { });

            // Workaround for serialization issue in Preview 4.
            // Must clear default XML formatter from Formatters before adding Atom formatter.
            var hostConfiguration = (HttpHostConfiguration) configuration;
            hostConfiguration.OperationHandlerFactory.Formatters.Clear();
            hostConfiguration.OperationHandlerFactory.Formatters.Insert(0, AtomMediaType.Formatter);

            using (var host = new HttpConfigurableServiceHost(typeof (RoomResource), configuration, new Uri("http://" + Environment.MachineName + ":8081/rooms/")))
            {
                host.Open();

                var entryFormatter = new Atom10ItemFormatter();
                var client = AtomClient.CreateDefault();

                using (var firstResponse = client.Get("http://" + Environment.MachineName + ":8081/rooms/1"))
                {
                    entryFormatter.ReadFrom(XmlReader.Create(firstResponse.Content.ContentReadStream));
                }

                var firstRoom = entryFormatter.Item;
                var northLink = firstRoom.Links.First(l => l.RelationshipType.Equals("north"));
                var northUri = new Uri(firstRoom.BaseUri, northLink.Uri);

                using (var secondResponse = client.Get(northUri))
                {
                    entryFormatter.ReadFrom(XmlReader.Create(secondResponse.Content.ContentReadStream));
                }

                var nextRoom = entryFormatter.Item;

                //See Maze class for layout of the maze. Room 4 is north of room 1.
                Assert.AreEqual(Maze.NewInstance().Get(4).Description, nextRoom.Summary.Text);

                host.Close();
            }
        }
예제 #5
0
        internal static TSyndicationItem LoadItem <TSyndicationItem> (XmlReader reader) where TSyndicationItem : SyndicationItem, new()
        {
            switch (DetectVersion(reader, ReaderKind.Item))
            {
            case SyndicationVersions.Atom10:
                Atom10ItemFormatter af = new Atom10ItemFormatter <TSyndicationItem> ();
                af.ReadFrom(reader);
                return((TSyndicationItem)af.Item);

            case SyndicationVersions.Rss20:
            default:             // anything else are rejected by DetectVersion
                Rss20ItemFormatter rf = new Rss20ItemFormatter <TSyndicationItem> ();
                rf.ReadFrom(reader);
                return((TSyndicationItem)rf.Item);
            }
        }
        public override object OnReadFromStream(Type type, Stream stream, HttpContentHeaders contentHeaders)
        {
            stream.Seek(0, SeekOrigin.Begin);

            if (type.Equals(typeof(SyndicationItem)))
            {
                var entryFormatter = new Atom10ItemFormatter();
                entryFormatter.ReadFrom(XmlReader.Create(stream));
                return entryFormatter.Item;
            }

            if (type.Equals(typeof(SyndicationFeed)))
            {
                var feedFormatter = new Atom10FeedFormatter();
                feedFormatter.ReadFrom(XmlReader.Create(stream));
                return feedFormatter.Feed;
            }

            throw new InvalidOperationException("Expected to be called with type SyndicationItem or SyndicationFeed.");
        }
예제 #7
0
        public static TSyndicationItem Load <TSyndicationItem>(XmlReader reader) where TSyndicationItem : SyndicationItem, new()
        {
            if (reader == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("reader");
            }
            Atom10ItemFormatter <TSyndicationItem> formatter = new Atom10ItemFormatter <TSyndicationItem>();

            if (formatter.CanRead(reader))
            {
                formatter.ReadFrom(reader);
                return(formatter.Item as TSyndicationItem);
            }
            Rss20ItemFormatter <TSyndicationItem> formatter2 = new Rss20ItemFormatter <TSyndicationItem>();

            if (!formatter2.CanRead(reader))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.ServiceModel.SR.GetString("UnknownItemXml", new object[] { reader.LocalName, reader.NamespaceURI })));
            }
            formatter2.ReadFrom(reader);
            return(formatter2.Item as TSyndicationItem);
        }
예제 #8
0
        public static TSyndicationItem Load <TSyndicationItem>(XmlReader reader)
            where TSyndicationItem : SyndicationItem, new()
        {
            if (reader == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader));
            }
            Atom10ItemFormatter <TSyndicationItem> atomSerializer = new Atom10ItemFormatter <TSyndicationItem>();

            if (atomSerializer.CanRead(reader))
            {
                atomSerializer.ReadFrom(reader);
                return(atomSerializer.Item as TSyndicationItem);
            }
            Rss20ItemFormatter <TSyndicationItem> rssSerializer = new Rss20ItemFormatter <TSyndicationItem>();

            if (rssSerializer.CanRead(reader))
            {
                rssSerializer.ReadFrom(reader);
                return(rssSerializer.Item as TSyndicationItem);
            }
            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.UnknownItemXml, reader.LocalName, reader.NamespaceURI)));
        }
 protected virtual SyndicationItem ReadItem(XmlReader reader, SyndicationFeed feed)
 {
     Atom10ItemFormatter formatter = new Atom10ItemFormatter ();
     formatter.ReadFrom (reader);
     return formatter.Item;
 }
예제 #10
0
		public void ReadFrom_Link ()
		{
			Atom10ItemFormatter f = new Atom10ItemFormatter ();

			f.ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><link href='urn:foo' rel='enclosure' length='50' type='text/html' wcf='wtf'><extended /></link></entry>"));

			Assert.AreEqual (1, f.Item.Links.Count, "#1");
			SyndicationLink link = f.Item.Links [0];
			Assert.AreEqual (50, link.Length, "#2");
			Assert.AreEqual ("urn:foo", link.Uri.ToString (), "#3");
			Assert.AreEqual ("text/html", link.MediaType, "#4");
			Assert.AreEqual ("enclosure", link.RelationshipType, "#5");
			Assert.AreEqual (1, link.AttributeExtensions.Count, "#6");
			Assert.AreEqual (1, link.ElementExtensions.Count, "#7");
		}
예제 #11
0
		public void ReadFrom_Id ()
		{
			Atom10ItemFormatter f = new Atom10ItemFormatter ();
			f.ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><id>urn:myid</id></entry>"));
			Assert.IsNotNull (f.Item, "#1");
			Assert.AreEqual ("urn:myid", f.Item.Id, "#2");
		}
예제 #12
0
		public void ReadFrom_Extension ()
		{
			Atom10ItemFormatter f = new Atom10ItemFormatter ();
			f.ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><ext>external</ext></entry>"));
			Assert.IsNotNull (f.Item, "#1");
			Assert.AreEqual (1, f.Item.ElementExtensions.Count, "#2");
		}
예제 #13
0
		public void ReadFrom_WrongDate ()
		{
			Atom10ItemFormatter f = new Atom10ItemFormatter ();
			f.ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><published>Sat, 01 Jan 2000 00:00:00 Z</pubDate></entry>"));
		}
예제 #14
0
		// It is not rejected. Though I think it is .NET bug.
		public void ReadFrom_EmptyDate ()
		{
			Atom10ItemFormatter f = new Atom10ItemFormatter ();
			f.ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><pubDate /></entry>"));
		}
예제 #15
0
		public void ReadFrom1 ()
		{
			Atom10ItemFormatter f = new Atom10ItemFormatter ();
			Assert.IsNull (f.Item, "#1");
			f.ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><title>test</title></entry>"));
			SyndicationItem item1 = f.Item;
			Assert.IsNotNull (f.Item.Title, "#2");
			Assert.AreEqual ("test", f.Item.Title.Text, "#3");
			f.ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><title>test</title></entry>"));
			Assert.IsFalse (object.ReferenceEquals (item1, f.Item), "#4");
		}