Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        // Loads service tree.
        public void LoadXmlDoc(XmlDocument doc)
        {
            if (doc == null)
            {
                return;
            }
            if (doc.DocumentElement == null)
            {
                return;
            }

            XmlNodeList accountList;

            accountList = doc.SelectNodes("//Accounts");
            if (accountList == null)
            {
                return;
            }

            foreach (XmlNode a in accountList)
            {
                // Loop through the top level childs.
                XmlNodeList serviceList = a.ChildNodes;
                if (serviceList == null)
                {
                    continue;
                }

                foreach (XmlNode s in serviceList)
                {
                    if (s.LocalName.Equals("Service"))
                    {
                        var accountName = s.Attributes["Name"].Value;

                        var    userName     = s.Attributes["UserName"].Value;
                        var    userPassword = s.Attributes["UserPassword"].Value;
                        var    endpoint     = s.Attributes["EndPoint"].Value;
                        string api          = (s.Attributes["Api"] != null) ? s.Attributes["Api"].Value : "Unknown"; //
                        string tp           = (s.Attributes["Type"] != null) ? s.Attributes["Type"].Value : "Unknown";

                        var  selecteds   = string.IsNullOrEmpty(s.Attributes["Selected"].Value) ? "" : s.Attributes["Selected"].Value;
                        var  expandeds   = string.IsNullOrEmpty(s.Attributes["Expanded"].Value) ? "" : s.Attributes["Expanded"].Value;
                        bool isSelecteds = (selecteds == "true") ? true : false;
                        bool isExpandeds = (expandeds == "true") ? true : false;

                        ServiceTypes stp;
                        switch (tp)
                        {
                        case "AtomPub":
                            stp = ServiceTypes.AtomPub;
                            break;

                        case "Feed":
                            stp = ServiceTypes.Feed;
                            break;

                        case "XML-RPC":
                            stp = ServiceTypes.XmlRpc;
                            break;

                        case "AtomAPI":
                            stp = ServiceTypes.AtomApi;
                            break;

                        default:
                            stp = ServiceTypes.Unknown;
                            break;
                        }

                        ApiTypes at;
                        switch (api)
                        {
                        case "AtomPub":
                            at = ApiTypes.atAtomPub;
                            break;

                        case "AtomFeed":
                            at = ApiTypes.atFeed;
                            break;

                        case "RssFeed":
                            at = ApiTypes.atFeed;
                            break;

                        case "Feed":
                            at = ApiTypes.atFeed;
                            break;

                        case "XML-RPC_MovableType":
                            at = ApiTypes.atXMLRPC_MovableType;
                            break;

                        case "XML-RPC_WordPress":
                            at = ApiTypes.atXMLRPC_WordPress;
                            break;

                        case "AtomAPI":
                            at = ApiTypes.atAtomApi;
                            break;

                        default:
                            at = ApiTypes.atUnknown;
                            break;
                        }

                        string    viewType = (s.Attributes["ViewType"] != null) ? s.Attributes["ViewType"].Value : "Cards";
                        ViewTypes vt;
                        switch (viewType)
                        {
                        case "Cards":
                            vt = ViewTypes.vtCards;
                            break;

                        case "Magazine":
                            vt = ViewTypes.vtMagazine;
                            break;

                        case "ThreePanes":
                            vt = ViewTypes.vtThreePanes;
                            break;

                        default:
                            vt = ViewTypes.vtCards;
                            break;
                        }

                        if (stp == ServiceTypes.Feed)
                        {
                            continue;
                        }

                        if ((!string.IsNullOrEmpty(accountName)) && (!string.IsNullOrEmpty(userName)) && (!string.IsNullOrEmpty(userPassword)) && (!string.IsNullOrEmpty(endpoint)))
                        {
                            NodeService account = new NodeService(accountName, userName, userPassword, new Uri(endpoint), at, stp);
                            account.IsSelected = isSelecteds;
                            account.IsExpanded = isExpandeds;
                            account.Parent     = this;

                            account.ServiceType = stp;
                            account.Api         = at;

                            account.ViewType = vt;


                            XmlNodeList collectionList = s.SelectNodes("Collection");
                            foreach (XmlNode c in collectionList)
                            {
                                var  collectionName = c.Attributes["Name"].Value;
                                var  selectedc      = string.IsNullOrEmpty(c.Attributes["Selected"].Value) ? "" : c.Attributes["Selected"].Value;
                                var  expandedc      = string.IsNullOrEmpty(c.Attributes["Expanded"].Value) ? "" : c.Attributes["Expanded"].Value;
                                bool isSelectedc    = (selectedc == "true") ? true : false;
                                bool isExpandedc    = (expandedc == "true") ? true : false;

                                string collectionHref = (c.Attributes["Href"] != null) ? c.Attributes["Href"].Value : "";

                                string collectionId = (c.Attributes["Id"] != null) ? c.Attributes["Id"].Value : "";

                                if ((!string.IsNullOrEmpty(collectionName)) && (!string.IsNullOrEmpty(collectionHref)))
                                {
                                    NodeEntryCollection entries = null;

                                    // TODO:
                                    switch (account.Api)
                                    {
                                    case ApiTypes.atAtomPub:
                                        entries = new NodeAtomPubEntryCollection(collectionName, new Uri(collectionHref), collectionId);
                                        break;

                                    case ApiTypes.atXMLRPC_MovableType:
                                        entries = new NodeXmlRpcEntryCollection(collectionName, new Uri(collectionHref), collectionId);
                                        break;

                                    case ApiTypes.atXMLRPC_WordPress:
                                        entries = new NodeXmlRpcEntryCollection(collectionName, new Uri(collectionHref), collectionId);
                                        break;
                                        //case ApiTypes.atAtomAPI:
                                        //    break;
                                    }

                                    if (entries == null)
                                    {
                                        continue;
                                    }

                                    if (entries is NodeAtomPubEntryCollection)
                                    {
                                        if (c.Attributes["CategoriesUri"] != null)
                                        {
                                            var catsUrl = c.Attributes["CategoriesUri"].Value;
                                            if (!string.IsNullOrEmpty(catsUrl))
                                            {
                                                try
                                                {
                                                    Uri catsUri = new(catsUrl);
                                                    (entries as NodeAtomPubEntryCollection).CategoriesUri = catsUri;
                                                }
                                                catch { }
                                            }
                                        }

                                        var catFixed = c.Attributes["IsCategoryFixed"].Value;
                                        if (!string.IsNullOrEmpty(catFixed))
                                        {
                                            if (catFixed == "true")
                                            {
                                                (entries as NodeAtomPubEntryCollection).IsCategoryFixed = true;
                                            }
                                        }

                                        XmlNodeList acceptList = c.SelectNodes("Accept");
                                        foreach (XmlNode act in acceptList)
                                        {
                                            (entries as NodeAtomPubEntryCollection).AcceptTypes.Add(act.InnerText);
                                        }
                                    }

                                    XmlNodeList categoryList = c.SelectNodes("Category");
                                    foreach (XmlNode t in categoryList)
                                    {
                                        var  categoryName = t.Attributes["Name"].Value;
                                        var  selectedt    = string.IsNullOrEmpty(t.Attributes["Selected"].Value) ? "" : t.Attributes["Selected"].Value;
                                        var  expandedt    = string.IsNullOrEmpty(t.Attributes["Expanded"].Value) ? "" : t.Attributes["Expanded"].Value;
                                        bool isSelectedt  = (selectedc == "true") ? true : false;
                                        bool isExpandedt  = (expandedc == "true") ? true : false;

                                        if (!string.IsNullOrEmpty(categoryName))
                                        {
                                            NodeCategory category = null;

                                            switch (account.Api)
                                            {
                                            case ApiTypes.atAtomPub:
                                                category = new NodeAtomPubCategory(categoryName);
                                                break;

                                            case ApiTypes.atXMLRPC_MovableType:
                                                category = new NodeXmlRpcMTCategory(categoryName);
                                                break;

                                            case ApiTypes.atXMLRPC_WordPress:
                                                category = new NodeXmlRpcWPCategory(categoryName);
                                                break;
                                                //case ApiTypes.atAtomAPI:
                                                //    break;
                                            }

                                            if (category == null)
                                            {
                                                return;
                                            }

                                            if (category is NodeAtomPubCategory)
                                            {
                                                (category as NodeAtomPubCategory).Term = categoryName;

                                                var categoryScheme = t.Attributes["Scheme"].Value;
                                                (category as NodeAtomPubCategory).Scheme = categoryScheme;
                                            }


                                            category.IsSelected = isSelectedc;
                                            category.IsExpanded = isExpandedc;
                                            category.Parent     = entries;

                                            entries.Children.Add(category);
                                        }
                                    }

                                    entries.IsSelected = isSelectedc;
                                    entries.IsExpanded = isExpandedc;
                                    entries.Parent     = account;

                                    account.Children.Add(entries);
                                }
                            }

                            this.Children.Add(account);
                        }
                    }
                    else if (s.LocalName.Equals("Feed"))
                    {
                        NodeFeed feed = LoadXmlChildFeed(s);
                        if (feed == null)
                        {
                            continue;
                        }

                        feed.Parent = this;

                        if (feed != null)
                        {
                            this.Children.Add(feed);
                        }
                    }
                    else if (s.LocalName.Equals("Folder"))
                    {
                        var folderName = s.Attributes["Name"].Value;

                        if (!string.IsNullOrEmpty(folderName))
                        {
                            var  selecteds   = string.IsNullOrEmpty(s.Attributes["Selected"].Value) ? "" : s.Attributes["Selected"].Value;
                            var  expandeds   = string.IsNullOrEmpty(s.Attributes["Expanded"].Value) ? "" : s.Attributes["Expanded"].Value;
                            bool isSelecteds = (selecteds == "true") ? true : false;
                            bool isExpandeds = (expandeds == "true") ? true : false;

                            NodeFolder folder = new NodeFolder(folderName);
                            folder.IsSelected = isSelecteds;
                            folder.IsExpanded = isExpandeds;
                            folder.Parent     = this;

                            int unreadCount = 0;
                            var attr        = s.Attributes["UnreadCount"];
                            if (attr != null)
                            {
                                if (!string.IsNullOrEmpty(s.Attributes["UnreadCount"].Value))
                                {
                                    unreadCount = int.Parse(s.Attributes["UnreadCount"].Value);
                                }
                            }
                            folder.EntryCount = unreadCount;

                            string    viewType = (s.Attributes["ViewType"] != null) ? s.Attributes["ViewType"].Value : "Cards";
                            ViewTypes vt;
                            switch (viewType)
                            {
                            case "Cards":
                                vt = ViewTypes.vtCards;
                                break;

                            case "Magazine":
                                vt = ViewTypes.vtMagazine;
                                break;

                            case "ThreePanes":
                                vt = ViewTypes.vtThreePanes;
                                break;

                            default:
                                vt = ViewTypes.vtCards;
                                break;
                            }
                            folder.ViewType = vt;



                            XmlNodeList feedList = s.SelectNodes("Feed");
                            foreach (XmlNode f in feedList)
                            {
                                NodeFeed feed = LoadXmlChildFeed(f);
                                if (feed == null)
                                {
                                    continue;
                                }

                                feed.Parent = folder;

                                if (feed != null)
                                {
                                    folder.Children.Add(feed);
                                }
                            }

                            this.Children.Add(folder);
                        }
                    }
                }

                break;
            }
        }