Пример #1
0
        public async Task <NodeAtomPubCatetories> GetCategiries(Uri categoriesUrl)
        {
            NodeAtomPubCatetories cats = new NodeAtomPubCatetories("AtomPub Categories");

            var HTTPResponseMessage = await _HTTPConn.Client.GetAsync(categoriesUrl);

            if (HTTPResponseMessage.IsSuccessStatusCode)
            {
                string s = await HTTPResponseMessage.Content.ReadAsStringAsync();

                ToDebugWindow(">> HTTP Request GET "
                              + Environment.NewLine
                              + categoriesUrl.AbsoluteUri
                              + Environment.NewLine + Environment.NewLine
                              + "<< HTTP Response " + HTTPResponseMessage.StatusCode.ToString()
                              + Environment.NewLine
                              + s + Environment.NewLine);

                /*
                 * <app:categories xmlns:app="http://www.w3.org/2007/app" xmlns="http://www.w3.org/2005/Atom" fixed="yes" scheme="http://torum.jp/en">
                 * <category term="Blogroll" />
                 * <category term="Software" />
                 * <category term="testCat" />
                 * <category term="Uncategorized" />
                 * </app:categories>
                 */

                string contenTypeString = HTTPResponseMessage.Content.Headers.GetValues("Content-Type").FirstOrDefault();

                if (!contenTypeString.StartsWith("application/atomcat+xml"))
                {
                    System.Diagnostics.Debug.WriteLine("Content-Type is invalid: " + contenTypeString);

                    ToDebugWindow("<< Content-Type is invalid: " + contenTypeString
                                  + Environment.NewLine
                                  + "expecting " + "application/atomcat+xml"
                                  + Environment.NewLine);

                    return(cats);
                }

                XmlDocument xdoc = new XmlDocument();
                try
                {
                    xdoc.LoadXml(s);
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine("LoadXml failed: " + e.Message);

                    ToDebugWindow("<< Invalid XML returned:"
                                  + Environment.NewLine
                                  + e.Message
                                  + Environment.NewLine);

                    return(cats);
                }

                XmlNamespaceManager atomNsMgr = new XmlNamespaceManager(xdoc.NameTable);
                atomNsMgr.AddNamespace("atom", "http://www.w3.org/2005/Atom");
                atomNsMgr.AddNamespace("app", "http://www.w3.org/2007/app");

                if (xdoc.DocumentElement.Attributes["fixed"] != null)
                {
                    string catFix = xdoc.DocumentElement.Attributes["fixed"].Value;
                    if (!string.IsNullOrEmpty(catFix))
                    {
                        if (catFix == "yes")
                        {
                            cats.IsCategoryFixed = true;
                        }
                        else
                        {
                            cats.IsCategoryFixed = false;
                        }
                    }
                }

                if (xdoc.DocumentElement.Attributes["scheme"] != null)
                {
                    string catScheme = xdoc.DocumentElement.Attributes["scheme"].Value;
                    if (!string.IsNullOrEmpty(catScheme))
                    {
                        cats.Scheme = catScheme;
                    }
                }

                XmlNodeList categoryList;
                categoryList = xdoc.SelectNodes("//app:categories/atom:category", atomNsMgr);
                if (categoryList == null)
                {
                    return(cats);
                }

                foreach (XmlNode c in categoryList)
                {
                    if (c.Attributes["term"] != null)
                    {
                        NodeAtomPubCategory category = new NodeAtomPubCategory(c.Attributes["term"].Value);

                        if (c.Attributes["scheme"] != null)
                        {
                            category.Scheme = c.Attributes["scheme"].Value;
                        }

                        if (string.IsNullOrEmpty(category.Scheme))
                        {
                            category.Scheme = cats.Scheme;
                        }

                        cats.Children.Add(category);
                    }
                }
            }
            else
            {
                var contents = await HTTPResponseMessage.Content.ReadAsStringAsync();

                if (contents != null)
                {
                    ToDebugWindow(">> HTTP Request GET (Failed)"
                                  + Environment.NewLine
                                  + categoriesUrl.AbsoluteUri
                                  + Environment.NewLine + Environment.NewLine
                                  + "<< HTTP Response " + HTTPResponseMessage.StatusCode.ToString()
                                  + Environment.NewLine
                                  + contents + Environment.NewLine);
                }
            }

            return(cats);
        }
Пример #2
0
        private async Task <List <NodeAtomPubEntryCollection> > GetEntryNodesFromXML(XmlNode w, XmlNamespaceManager atomNsMgr)
        {
            List <NodeAtomPubEntryCollection> cols = new List <NodeAtomPubEntryCollection>();

            /*
             * <?xml version="1.0" encoding="utf-8"?>
             * <service xmlns="http://www.w3.org/2007/app">
             * <workspace>
             * <atom:title xmlns:atom="http://www.w3.org/2005/Atom">hoge</atom:title>
             * <collection href="https://127.0.0.1/atom/entry">
             * <atom:title xmlns:atom="http://www.w3.org/2005/Atom">fuga</atom:title>
             * <accept>application/atom+xml;type=entry</accept>
             * </collection>
             * </workspace>
             * </service>
             */

            /*
             * <?xml version="1.0" encoding="utf-8"?>
             * <service xmlns="http://www.w3.org/2007/app" xmlns:atom="http://www.w3.org/2005/Atom">
             * <workspace>
             * <atom:title>hoge Workspace</atom:title>
             * <collection href="http://torum.jp/en/wp-app.php/service/posts">
             * <atom:title>hoge Posts</atom:title>
             * <accept>application/atom+xml;type=entry</accept>
             * <categories href="http://hoge.jp/wp-app.php/service/categories" />
             * </collection>
             * <collection href="http://hoge.jp/wp-app.php/service/attachments">
             * <atom:title>hoge Media</atom:title>
             * <accept>image/*</accept><accept>audio/*</accept><accept>video/*</accept>
             * </collection>
             * </workspace>
             * </service>
             */

            /*
             * <?xml version="1.0" encoding="UTF-8"?>
             * <service xmlns="http://www.w3.org/2007/app" xmlns:atom="http://www.w3.org/2005/Atom">
             *  <workspace>
             *      <atom:title>BlogTitle</atom:title>
             *      <collection href="https://livedoor.blogcms.jp/atompub/userid/article">
             *          <atom:title>BlogTitle - Entries</atom:title>
             *          <accept>application/atom+xml;type=entry</accept>
             *
             *          <categories fixed="no" scheme="https://livedoor.blogcms.jp/atompub/userid/category">
             *          </categories>
             *      </collection>
             *      <collection href="https://livedoor.blogcms.jp/atompub/userid/image">
             *          <atom:title>BlogTitle - Images</atom:title>
             *          <accept>image/png</accept>
             *          <accept>image/jpeg</accept>
             *          <accept>image/gif</accept>
             *      </collection>
             *  </workspace>
             * </service>
             */

            XmlNodeList collectionList = w.SelectNodes("app:collection", atomNsMgr);

            if (collectionList == null)
            {
                return(cols);
            }

            foreach (XmlNode n in collectionList)
            {
                var hrefAttr = n.Attributes["href"].Value;
                if (hrefAttr == null)
                {
                    continue;
                }

                XmlNode title = n.SelectSingleNode("atom:title", atomNsMgr);
                if (title == null)
                {
                    continue;
                }

                NodeAtomPubEntryCollection entries = new NodeAtomPubEntryCollection(title.InnerText, new Uri(hrefAttr), hrefAttr);


                XmlNodeList acceptList = n.SelectNodes("app:accept", atomNsMgr);
                if (acceptList != null)
                {
                    foreach (XmlNode a in acceptList)
                    {
                        string acpt = a.InnerText;
                        if (!string.IsNullOrEmpty(acpt))
                        {
                            entries.AcceptTypes.Add(acpt);

                            if ((acpt == "application/atom+xml;type=entry") ||
                                (acpt == "application/atom+xml"))
                            {
                                entries.IsAcceptEntry = true;
                            }
                        }
                    }
                }
                else
                {
                    // default entry
                    entries.IsAcceptEntry = true;
                }

                /*
                 * XmlNode cats = n.SelectSingleNode("app:categories", atomNsMgr);
                 * if (cats != null)
                 * {
                 *  // Look for category document.
                 *  if (cats.Attributes["href"] != null)
                 *  {
                 *      var hrefCat = cats.Attributes["href"];
                 *      try
                 *      {
                 *          entries.CategoriesUri = new Uri(hrefCat.Value);
                 *      }
                 *      catch { }
                 *  }
                 *
                 *  // Inline categories
                 *  XmlNodeList catList = cats.SelectNodes("atom:category", atomNsMgr);
                 *  foreach (XmlNode c in catList)
                 *  {
                 *      if (c.Attributes["term"] != null)
                 *      {
                 *          NodeCategory category = new NodeCategory(c.Attributes["term"].Value);
                 *          entries.Children.Add(category);
                 *      }
                 *  }
                 * }
                 */
                XmlNodeList categoriesList = n.SelectNodes("app:categories", atomNsMgr);
                if (categoriesList != null)
                {
                    foreach (XmlNode cats in categoriesList)
                    {
                        NodeAtomPubCatetories categories = new NodeAtomPubCatetories("Categories");
                        categories.IsExpanded = true;
                        categories.Parent     = entries;

                        Uri catHrefUri = null;
                        if (cats.Attributes["href"] != null)
                        {
                            string cathref = cats.Attributes["href"].Value;
                            if (!string.IsNullOrEmpty(cathref))
                            {
                                try
                                {
                                    catHrefUri = new Uri(cathref);

                                    categories.Href = catHrefUri;
                                }
                                catch { }
                            }
                        }

                        if (cats.Attributes["fixed"] != null)
                        {
                            string catFix = cats.Attributes["fixed"].Value;
                            if (!string.IsNullOrEmpty(catFix))
                            {
                                if (catFix == "yes")
                                {
                                    categories.IsCategoryFixed = true;
                                }
                                else
                                {
                                    categories.IsCategoryFixed = false;
                                }
                            }
                        }

                        if (cats.Attributes["scheme"] != null)
                        {
                            string catScheme = cats.Attributes["scheme"].Value;
                            if (!string.IsNullOrEmpty(catScheme))
                            {
                                categories.Scheme = catScheme;
                            }
                        }
                        // scheme

                        XmlNodeList categoryList = cats.SelectNodes("atom:category", atomNsMgr);
                        if (categoryList != null)
                        {
                            foreach (XmlNode cat in categoryList)
                            {
                                NodeAtomPubCategory category = new NodeAtomPubCategory("Category");
                                category.IsExpanded = true;
                                category.Parent     = categories;

                                if (cat.Attributes["term"] != null)
                                {
                                    string term = cat.Attributes["term"].Value;
                                    if (!string.IsNullOrEmpty(term))
                                    {
                                        category.Term = term;
                                    }
                                }

                                if (cat.Attributes["scheme"] != null)
                                {
                                    string scheme = cat.Attributes["scheme"].Value;
                                    if (!string.IsNullOrEmpty(scheme))
                                    {
                                        category.Scheme = scheme;
                                    }
                                }

                                category.Name = category.Term;

                                if (string.IsNullOrEmpty(category.Scheme))
                                {
                                    category.Scheme = categories.Scheme;
                                }

                                categories.Children.Add(category);
                            }
                        }

                        entries.CategoriesUri = catHrefUri;

                        entries.IsCategoryFixed = categories.IsCategoryFixed;

                        if (categories.Children.Count > 0)
                        {
                            //entries.Children.Add(categories);

                            foreach (NodeAtomPubCategory c in categories.Children)
                            {
                                c.Parent = entries;
                                entries.Children.Add(c);
                            }
                        }
                    }
                }

                // Get category document.
                if (entries.CategoriesUri != null)
                {
                    NodeAtomPubCatetories nc = await GetCategiries(entries.CategoriesUri);

                    entries.IsCategoryFixed = nc.IsCategoryFixed;

                    foreach (NodeAtomPubCategory c in nc.Children)
                    {
                        bool isExists = false;

                        // check if exists
                        foreach (var hoge in entries.Children)
                        {
                            if (hoge is NodeAtomPubCategory)
                            {
                                if ((hoge as NodeAtomPubCategory).Term.Equals(c.Term))
                                {
                                    isExists = true;

                                    break;
                                }
                            }
                        }

                        if (!isExists)
                        {
                            c.Parent = entries;
                            entries.Children.Add(c);
                        }
                    }
                }

                cols.Add(entries);
            }

            return(cols);
        }