/// <summary>
        ///
        /// </summary>
        /// <param name="data"></param>
        /// <param name="opdsLinkRelation"></param>
        /// <param name="opdsLinkMediaType"></param>
        /// <param name="propertyAccessor"></param>
        /// <param name="nameMapping"></param>
        /// <returns></returns>
        public virtual OpdsLink Generate(object data, string opdsLinkRelation, string opdsLinkMediaType, IPropertyAccessor propertyAccessor, OpdsNames nameMapping)
        {
            if (nameMapping == null)
            {
                nameMapping = new OpdsNames();
            }

            switch (opdsLinkRelation)
            {
            // Download
            case OpdsRelations.OpenAcquisition:
            {
                var id = data.GetProperty(propertyAccessor, nameMapping.DownloadLinkId, nameMapping.Id).ToNullableString();
                if (!String.IsNullOrEmpty(id))
                {
                    return(GetDownloadLink(id, data.GetProperty(nameMapping.Title, propertyAccessor).ToNullableString()));
                }

                return(null);
            }

            case OpdsRelations.Buy:
            {
                var price = data.GetProperty(nameMapping.Price, propertyAccessor);
                if (price != null)
                {
                    var id = data.GetProperty(propertyAccessor, nameMapping.BuyLinkId, nameMapping.Id).ToNullableString();
                    if (!String.IsNullOrEmpty(id))
                    {
                        return(GetBuyLink(id, data.GetProperty(nameMapping.Title, propertyAccessor).ToNullableString(), Convert.ToDecimal(price)));
                    }
                }

                return(null);
            }

            case OpdsRelations.Alternate:
            {
                if (OpdsMediaType.AcquisitionFeed == opdsLinkMediaType ||
                    OpdsMediaType.NavigationFeed == opdsLinkMediaType)
                {
                    var id = data.GetProperty(nameMapping.Id, propertyAccessor).ToNullableString();
                    if (!String.IsNullOrEmpty(id))
                    {
                        return(GetNavigationLink(id, data.GetProperty(nameMapping.Title, propertyAccessor).ToNullableString()));
                    }
                }
                if (OpdsMediaType.Entry == opdsLinkMediaType)
                {
                    var id = data.GetProperty(propertyAccessor, nameMapping.DetailLinkId, nameMapping.Id).ToNullableString();
                    if (!String.IsNullOrEmpty(id))
                    {
                        // 详细页链接的Id和书籍的Id可能并没有对应关系。仅当没有提供详细页Id时,使用书籍的Id。
                        return(GetDetailLink(id, data.GetProperty(nameMapping.Title, propertyAccessor).ToNullableString()));
                    }
                }
                throw new NotSupportedException();
            }

            default:
                throw new NotSupportedException();
            }
        }
Exemple #2
0
 /// <summary>
 ///
 /// </summary>
 public NamingDataOpdsItemConverter()
 {
     Names = new OpdsNames();
 }