Ejemplo n.º 1
0
        private static void computeIssuedDate(ref AtomDateConstruct element, string temp)
        {
            string dateTime = String.Empty;
            string offset   = String.Empty;

            char[] chrs      = { '+', '-' };
            bool   hasOffset = true;

            if (Atom.Utils.Utils.IsIso8601DateLocalNoTZ(temp))
            {
                dateTime = temp;
            }
            else if (Atom.Utils.Utils.IsIso8601DateLocal(temp))
            {
                try
                {
                    dateTime = temp.Substring(0, temp.LastIndexOfAny(chrs));
                }
                catch (ArgumentNullException)
                {
                    dateTime  = temp;
                    hasOffset = false;
                }

                if (hasOffset)
                {
                    int index  = temp.LastIndexOfAny(chrs);
                    int length = temp.Length - index;
                    offset = temp.Substring(index, length);

                    // remove the leading + cause TimeSpan.Parse gives
                    // an exception
                    if (offset.StartsWith("+"))
                    {
                        offset = offset.Substring(1, offset.Length - 1);
                    }

                    element.UtcOffset = TimeSpan.Parse(offset);
                }
            }
            else if (Atom.Utils.Utils.IsIso8601DateTZ(temp))
            {
                if (!hasSeconds(temp))
                {
                    temp = addSeconds(temp);
                }
                dateTime = temp.Substring(0, temp.IndexOf('Z'));
            }
            else
            {
                throw new FormatException("The given date is not in ISO 8601 format.");
            }

            element.DateTime = DateTime.Parse(dateTime);
        }
Ejemplo n.º 2
0
        internal static AtomDateConstruct Parse(XPathNavigator navigator, Uri ns)
        {
            AtomDateConstruct dateElement = new AtomDateConstruct(ns);
            string            temp        = String.Empty;

            XPathNavigator    nav  = navigator.Clone();
            XPathNodeIterator iter = nav.SelectDescendants(XPathNodeType.Element, true);

            while (iter.MoveNext())
            {
                string name = iter.Current.Name.ToLower();
                int    idx  = name.IndexOf(":");
                if (idx != -1)
                {
                    name = name.Split(new char[] { ':' }, 2)[1];
                }

                switch (name)
                {
                case "modified":
                case "issued":
                case "created":
                case "published":
                case "updated":
                    try
                    {
                        dateElement.XmlLang = Utils.Utils.ParseLanguage(iter.Current.XmlLang);
                    }
                    catch {}
                    dateElement.LocalName = name;
                    temp = iter.Current.Value;
                    break;
                }
            }

            switch (dateElement.LocalName)
            {
            case "modified":
            case "created":
            case "published":
            case "updated":
                computeModifiedCreatedDate(ref dateElement, temp);
                break;

            case "issued":
                computeIssuedDate(ref dateElement, temp);
                break;
            }

            return(dateElement);
        }
Ejemplo n.º 3
0
		public void TestCreated()
		{
			AtomDateConstruct createdDate = new AtomDateConstruct("created");
			createdDate.DateTime = DateTime.Now;
			createdDate.UtcOffset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Today);

			entry.Created = createdDate;

			Assert.AreSame(entry.Created, createdDate);

			createdDate = new AtomDateConstruct("created");

			entry.Created = createdDate;

			Assert.AreEqual(createdDate.LocalName, "created");
			Assert.AreEqual(createdDate.FullName, "atom:created");
			Assert.AreSame(entry.Created, createdDate);
		}
Ejemplo n.º 4
0
		public void TestIssued()
		{
			AtomDateConstruct issuedDate = new AtomDateConstruct("issued");
			issuedDate.DateTime = DateTime.Now;

			entry.Issued = issuedDate;

			Assert.AreSame(entry.Issued, issuedDate);

			issuedDate = new AtomDateConstruct("issued");

			entry.Issued = issuedDate;

			Assert.AreEqual(issuedDate.LocalName, "issued");
			Assert.AreEqual(issuedDate.FullName, "atom:issued");
			Assert.AreSame(entry.Issued, issuedDate);
		}
Ejemplo n.º 5
0
		public void TestModified()
		{
			AtomDateConstruct modifiedDate = new AtomDateConstruct();
			modifiedDate.DateTime = DateTime.Now;
			modifiedDate.UtcOffset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Today);

			entry.Modified = modifiedDate;

			Assert.AreSame(entry.Modified, modifiedDate);

			modifiedDate = new AtomDateConstruct();

			entry.Modified = modifiedDate;

			Assert.AreEqual(modifiedDate.LocalName, "modified");
			Assert.AreEqual(modifiedDate.FullName, "atom:modified");
			Assert.AreSame(entry.Modified, modifiedDate);
		}
Ejemplo n.º 6
0
        internal static AtomEntry Parse(XPathNavigator navigator)
        {
            AtomEntry entry = new AtomEntry();

            XPathNavigator    nav  = navigator.Clone();
            XPathNodeIterator iter = nav.SelectDescendants(XPathNodeType.All, true);

            while (iter.MoveNext())
            {
                string name = iter.Current.Name.ToLower();
                int    idx  = name.IndexOf(":");
                if (idx != -1)
                {
                    string prefix;
                    prefix = name.Split(new char[] { ':' }, 2)[0];
                    if (prefix == "atom")
                    {
                        name = name.Split(new char[] { ':' }, 2)[1];
                    }
                }

                switch (name)
                {
                case "entry":
                    try
                    {
                        entry.Uri     = FindAlternateUri(iter.Current);
                        entry.XmlLang = Utils.Utils.ParseLanguage(iter.Current.XmlLang);
                    }
                    catch {}
                    break;

                case "title":
                    AtomContentConstruct content = AtomContentConstruct.Parse(iter.Current);
                    entry.Title = content;
                    break;

                case "link":
                    entry.Links.Add(AtomLink.Parse(iter.Current));
                    break;

                case "author":
                    entry.Author = AtomPersonConstruct.Parse(iter.Current);
                    break;

                case "contributor":
                    entry.Contributors.Add(AtomPersonConstruct.Parse(iter.Current));
                    break;

                case "id":
                    entry.Id = new Uri(iter.Current.Value);
                    break;

                case "modified":
                    entry.Modified = AtomDateConstruct.Parse(iter.Current);
                    break;

                case "issued":
                    entry.Issued = AtomDateConstruct.Parse(iter.Current);
                    break;

                case "created":
                    entry.Created = AtomDateConstruct.Parse(iter.Current);
                    break;

                case "summary":
                    entry.Summary = AtomContentConstruct.Parse(iter.Current);
                    break;

                case "content":
                    entry.Contents.Add(AtomContent.Parse(iter.Current));
                    break;

                case "dc:title":
                case "dc:creator":
                case "dc:subject":
                case "dc:description":
                case "dc:publisher":
                case "dc:contributor":
                case "dc:date":
                case "dc:type":
                case "dc:format":
                case "dc:identifier":
                case "dc:source":
                case "dc:language":
                case "dc:relation":
                case "dc:coverage":
                case "dc:rights":
                    entry.AdditionalElements.Add(DcElement.Parse(iter.Current));
                    break;
                }
            }

            return(entry);
        }
Ejemplo n.º 7
0
        internal static AtomFeed Parse(XPathNavigator navigator)
        {
            AtomFeed feed = new AtomFeed();

            XPathNavigator    nav  = navigator.Clone();
            XPathNodeIterator iter = nav.SelectChildren(XPathNodeType.All);

            do
            {
                string name = iter.Current.Name.ToLower();
                int    idx  = name.IndexOf(":");
                if (idx != -1)
                {
                    name = name.Split(new char[] { ':' }, 2)[1];
                }

                switch (name)
                {
                case "feed":
                {
                    try
                    {
                        XPathNavigatorReader reader = new XPathNavigatorReader(nav);
                        string baseUri = reader.GetAttribute("base", XmlNamespaces.Xml);
                        if (baseUri != null && baseUri.Length > 0)
                        {
                            feed.XmlBase = new Uri(baseUri);
                        }
                    }
                    catch {}

                    try
                    {
                        feed.Uri = FindAlternateUri(iter.Current);
                    }
                    catch {}

                    try
                    {
                        feed.XmlLang = Utils.Utils.ParseLanguage(iter.Current.XmlLang);
                    }
                    catch {}

                    XPathNodeIterator attrIterator = nav.Select("@*");
                    while (attrIterator.MoveNext())
                    {
                        if (attrIterator.Current.Name.ToLower() == "version")
                        {
                            if (attrIterator.Current.Value != DefaultValues.AtomVersion)
                            {
                                string msg = String.Format("Atom {0} version is not supported!", attrIterator.Current.Value);
                                throw new InvalidOperationException(msg);
                            }
                        }
                    }
                    break;
                }

                case "title":
                    AtomContentConstruct content = AtomContentConstruct.Parse(iter.Current);
                    feed.Title = content;
                    break;

                case "link":
                    feed.Links.Add(AtomLink.Parse(iter.Current));
                    break;

                case "author":
                    feed.Author = AtomPersonConstruct.Parse(iter.Current);
                    break;

                case "contributor":
                    feed.Contributors.Add(AtomPersonConstruct.Parse(iter.Current));
                    break;

                case "tagline":
                    feed.Tagline = AtomContentConstruct.Parse(iter.Current);
                    break;

                case "id":
                    feed.Id = new Uri(iter.Current.Value);
                    break;

                case "copyright":
                    feed.Copyright = AtomContentConstruct.Parse(iter.Current);
                    break;

                case "info":
                    feed.Info = AtomContentConstruct.Parse(iter.Current);
                    break;

                case "modified":
                    feed.Modified = AtomDateConstruct.Parse(iter.Current);
                    break;

                case "entry":
                    feed.Entries.Add(AtomEntry.Parse(iter.Current));
                    break;
                }
            } while(iter.MoveNext());

            return(feed);
        }
Ejemplo n.º 8
0
		public void TestModified()
		{
            AtomDateConstruct modifiedDate = new AtomDateConstruct(new Uri("http://purl.org/atom/ns#"));
			modifiedDate.DateTime = DateTime.Now;
			modifiedDate.UtcOffset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Today);

			feed.Modified = modifiedDate;
			Assert.AreEqual(modifiedDate.LocalName, "modified");
			Assert.AreEqual(modifiedDate.FullName, "atom:modified");
			Assert.AreSame(feed.Modified, modifiedDate);
		}
Ejemplo n.º 9
0
		private static void computeIssuedDate(ref AtomDateConstruct element, string temp)
		{
			string dateTime = String.Empty;
			string offset = String.Empty;
			char[] chrs = {'+', '-'};
			bool hasOffset = true;

			if(Atom.Utils.Utils.IsIso8601DateLocalNoTZ(temp))
			{
				dateTime = temp;
			}
			else if(Atom.Utils.Utils.IsIso8601DateLocal(temp))
			{
				try
				{
					dateTime = temp.Substring(0, temp.LastIndexOfAny(chrs));
				}
				catch(ArgumentNullException)
				{
					dateTime = temp;
					hasOffset = false;
				}

				if(hasOffset)
				{
					int index = temp.LastIndexOfAny(chrs);
					int length = temp.Length - index;
					offset = temp.Substring(index, length);

					// remove the leading + cause TimeSpan.Parse gives
					// an exception
					if(offset.StartsWith("+"))
						offset = offset.Substring(1, offset.Length - 1);

					element.UtcOffset = TimeSpan.Parse(offset);
				}
			}
			else if(Atom.Utils.Utils.IsIso8601DateTZ(temp))
			{
				if(!hasSeconds(temp))
					temp = addSeconds(temp);
				dateTime = temp.Substring(0, temp.IndexOf('Z'));
			}
			else
				throw new FormatException("The given date is not in ISO 8601 format.");

			element.DateTime = DateTime.Parse(dateTime);
		}
Ejemplo n.º 10
0
		internal static AtomDateConstruct Parse(XPathNavigator navigator, Uri ns)
		{
			AtomDateConstruct dateElement = new AtomDateConstruct(ns);
			string temp = String.Empty;

			XPathNavigator nav = navigator.Clone();
			XPathNodeIterator iter = nav.SelectDescendants(XPathNodeType.Element, true);
			
			while(iter.MoveNext())
			{
				string name = iter.Current.Name.ToLower();
				int idx = name.IndexOf(":");
				if(idx != -1)
					name = name.Split(new char[] {':'}, 2)[1];

				switch(name)
				{
					case "modified":
					case "issued":
					case "created":
     case "published":
     case "updated":
						try
						{
							dateElement.XmlLang = Utils.Utils.ParseLanguage(iter.Current.XmlLang);
						}
						catch {}
						dateElement.LocalName = name;
						temp = iter.Current.Value;
						break;
				}
			}

			switch(dateElement.LocalName)
			{
				case "modified":
				case "created":
    case "published":
    case "updated":
					computeModifiedCreatedDate(ref dateElement, temp);
					break;

				case "issued":
					computeIssuedDate(ref dateElement, temp);
					break;
			}

			return dateElement;
		}