Пример #1
0
        /// <summary>
        /// Find the available book groups, books, and packages for the selected language
        /// </summary>
        /// <param name="data">
        /// The page data downloaded containing the book data
        /// </param>
        /// <returns>
        /// Collection of book groups for the help collection
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// If data is null
        /// </exception>
        /// <exception cref="XmlException">
        /// If there was a error processing the xml data
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// If there was a error processing the xml data
        /// </exception>
        public static ICollection<BookGroup> LoadBooks( byte[] data )
        {
            if ( data == null )
            {
                throw new ArgumentNullException( "data" );
            }

            XDocument document;
            List<BookGroup> result = new List<BookGroup>();

            using ( MemoryStream stream = new MemoryStream( data ) )
            {
                document = XDocument.Load( stream );
            }

            if ( document.Root != null )
            {
                IEnumerable<XElement> groups =
                    document.Root.Elements()
                            .Where( x => x.GetClassName() == "book-list" )
                            .Take( 1 )
                            .Single()
                            .Elements()
                            .Where( x => x.GetClassName() == "book-groups" )
                            .Take( 1 )
                            .Single()
                            .Elements()
                            .Where( x => x.GetClassName() == "book-group" );
                foreach ( XElement group in groups )
                {
                    BookGroup bookGroup = new BookGroup
                                        {
                                            Code = group.GetChildClassValue( "id" ),
                                            Name = group.GetChildClassValue( "name" ),
                                            Vendor = group.GetChildClassValue( "vendor" ),
                                            Books = new List<Book>()
                                        };

                    result.Add( bookGroup );

                    IEnumerable<XElement> books = group.Elements().Where( x => x.GetClassName() == "book" );
                    foreach ( XElement book in books )
                    {
                        Book b = new Book
                        {
                            Code = book.GetChildClassValue( "id" ),
                            Locale = new Locale { Code = book.GetChildClassValue( "locale" ) },
                            //Local = book.GetChildClassValue( "locale" ),
                            Name = book.GetChildClassValue( "name" ),
                            Description = book.GetChildClassValue( "description" ),
                            BrandingPackageName = book.GetChildClassValue( "BrandingPackageName" ),
                            Paths = new List<MSDNPath>(),
                            Packages = new List<Package>()
                        };

                        IEnumerable<XElement> paths =
                            book.Elements()
                                .Where(x => x.GetClassName() == "properties")
                                .Take(1)
                                .Single()
                                .Elements()
                                .Where(x => x.GetClassName() == "paths")
                                .Take(1)
                                .Single()
                                .Elements()
                                .Where(x => x.GetClassName() == "path");
                                //.Take( 1 )
                                //.Single();

                        foreach (XElement path in paths)
                        {
                            MSDNPath p = new MSDNPath
                                            {
                                                Languages = path.GetChildClassValue( "languages" ),
                                                Membership = path.GetChildClassValue( "membership" ),
                                                Name = path.GetChildClassValue( "name" ),
                                                //Priority = path.GetChildClassValue( "priority" ),
                                                Priority = long.Parse( path.GetChildClassValue( "priority" ), CultureInfo.InvariantCulture ),
                                                //SkuId = path.GetChildClassValue( "skuId" ),
                                                SkuId = long.Parse( path.GetChildClassValue( "skuId" ), CultureInfo.InvariantCulture ),
                                                SkuName = path.GetChildClassValue( "skuName" )
                                            };

                                            if ( p.SkuName == "Enterprise" )
                                                p.SkuId = 3000;
                                            else if ( p.SkuName == "Professional" )
                                                p.SkuId = 2000;
                                            else if ( p.SkuName == "Standard" )
                                                p.SkuId = 1000;
                                            else if ( p.SkuName == "Express" )
                                                p.SkuId = 500;

                            b.Paths.Add(p);

                            string bookPath = p.Name.TrimStart(new[] { '\\' });
                            b.Category = bookPath;
                        }

                        IEnumerable<XElement> packages =
                            book.Elements()
                                .Where( x => x.GetClassName() == "packages" )
                                .Take( 1 )
                                .Single()
                                .Elements()
                                .Where( x => x.GetClassName() == "package" );
                        foreach ( XElement package in packages )
                        {
                            Package pa = new Package
                                            {
                                                PackageType = package.GetChildClassValue( "packageType" ),
                                                PackageFormat = package.GetChildClassValue( "packageFormat" ),
                                                Name = package.GetChildClassValue( "name" ),
                                                Deployed = package.GetChildClassValue( "deployed" ),
                                                LastModified = DateTime.Parse( package.GetChildClassValue( "last-modified" ), CultureInfo.InvariantCulture ),
                                                Tag = package.GetChildClassValue( "package-etag" ),
                                                Link = package.GetChildClassAttributeValue( "current-link", "href" ),
                                                Size = long.Parse( package.GetChildClassValue( "package-size-bytes" ), CultureInfo.InvariantCulture ),// unused
                                                UncompressedSize = long.Parse( package.GetChildClassValue( "package-size-bytes-uncompressed" ), CultureInfo.InvariantCulture ),// unused
                                                ConstituentLink = package.GetChildClassAttributeValue( "package-constituent-link", "href" )
                                            };

                            b.Packages.Add( pa );
                        }

                        bookGroup.Books.Add(b);
                    }
                }

            }
            else
            {
                throw new XmlException( "Missing document root" );
            }

            return result;
        }
Пример #2
0
        //public static string CreateBookGroupBooksIndex( BookGroup bookGroup )
        //{
        //    XDocument document = new XDocument( new XDeclaration( "1.0", "utf-8", null ), CreateElement( "html", null, null ) );
        //    XElement headElement = CreateElement( "head", null, null );
        //    XElement metaDateElemet = CreateElement( "meta", null, null );
        //    metaDateElemet.SetAttributeValue( XName.Get( "http-equiv", string.Empty ), "Date" );
        //    metaDateElemet.SetAttributeValue( XName.Get( "content", string.Empty ), DateTime.Now.ToString( "R", CultureInfo.InvariantCulture ) );
        //    headElement.Add( metaDateElemet );
        //    XElement bodyElement = CreateElement( "body", "product", null );
        //    XElement detailsElement = CreateElement( "div", "details", null );
        //    detailsElement.Add(
        //        CreateElement( "span", "name", bookGroup.Name ),
        //        CreateElement( "span", "locale", bookGroup.Locale.Code ),
        //        CreateElement( "span", "description", bookGroup.Description ) );
        //    XElement bookListElement = CreateElement( "div", "book-list", null );
        //    foreach ( Book book in bookGroup.Books )
        //    {
        //        if ( book.Wanted )
        //        {
        //            XElement bookElement = CreateElement( "div", "book", null );
        //            XElement linkElement = CreateElement( "a", "book-link", null );
        //            linkElement.SetAttributeValue( XName.Get( "href", string.Empty ), book.  CreateFileName() );
        //            bookElement.Add(
        //                CreateElement( "span", "name", book.Name ),
        //                CreateElement( "span", "locale", book.Locale.Code ),
        //                CreateElement( "span", "description", book.Description ),
        //                linkElement );
        //            bookListElement.Add( bookElement );
        //        }
        //    }
        //    bodyElement.Add( detailsElement, bookListElement );
        //    if ( document.Root != null )
        //    {
        //        document.Root.Add( headElement, bodyElement );
        //    }
        //    return document.ToStringWithDeclaration();
        //}
        /// <summary>
        /// Create book packages index.
        /// </summary>
        /// <param name="bookGroup">
        /// The book Group associated with the book.
        /// </param>
        /// <param name="book">
        /// The book associated with the packages
        /// </param>
        /// <returns>
        /// The xml document text
        /// </returns>
        public static XElement CreateBookPackagesIndex( Book book, string codeLocale )
        {
            XElement bookElement = CreateElement( "div", "book", null );

            XElement propertiesElement = CreateElement( "div", "properties", null );
            XElement pathsElement = CreateElement( "div", "paths", null );
            foreach (MSDNPath path in book.Paths)
            {
                XElement pathElement = CreateElement("div", "path", null);

                pathElement.Add(
                    CreateElement( "span", "languages", path.Languages ),
                    CreateElement( "span", "membership", path.Membership ),
                    CreateElement( "span", "name", path.Name ),
                    CreateElement( "span", "priority", path.Priority.ToString() ),
                    CreateElement( "span", "skuId", path.SkuId.ToString() ),
                    CreateElement( "span", "skuName", path.SkuName )
                    );

                pathsElement.Add( pathElement );
            }
            propertiesElement.Add( pathsElement );

            XElement packageListElement = CreateElement( "div", "packages", null );
            string packageListTick = @"The following packages are available: in this book:";
            //packageListElement.Add( new XText( packageListTick ) );
            //packageListElement.Value = packageListTick;
            //XElement child1 = packageListElement.Element( "div" );
            //child1.( packageListTick );
            //child1.AddAfterSelf( new XElement( packageListTick ) );

            string deployedTick = @"Deployed: ";
            string leftRoundBracketsTick = @"(Package Constituents: ";
            string rightRoundBracketsTick = @")";

            foreach ( Package package in book.Packages )
            {
                XElement packageElement = CreateElement( "div", "package", null );

                XElement currentLinkElement = CreateElement( "a", "current-link", package.CreateFileName() );

                string curlink = string.Format( CultureInfo.InvariantCulture, "packages/{0}", ( package.CreateFileName() ).ToLowerInvariant() );
                if ( package.Name.ToLowerInvariant().Contains( @"en-us" ) )
                    curlink = string.Format( CultureInfo.InvariantCulture, "packages/en-us/{0}", ( package.CreateFileName() ).ToLowerInvariant() );
                else if ( package.Name.ToLowerInvariant().Contains( codeLocale.ToLowerInvariant() ) )
                    curlink = string.Format( CultureInfo.InvariantCulture, "packages/{0}/{1}", codeLocale.ToLowerInvariant() , ( package.CreateFileName() ).ToLowerInvariant());
                else
                    curlink = string.Format( CultureInfo.InvariantCulture, "packages/{0}", ( package.CreateFileName() ).ToLowerInvariant() );

                currentLinkElement.SetAttributeValue(
                    XName.Get( "href", string.Empty ),
                    curlink
                    );

                XElement constituentLinkElement = CreateElement("a", "package-constituent-link", package.Name);
                constituentLinkElement.SetAttributeValue( XName.Get( "href", string.Empty), package.ConstituentLink );

                XElement lastModified;
                if ( ( package.LastModified.Millisecond % 10 ) == 0 )
                    lastModified = CreateElement( "span", "last-modified", package.LastModified.ToUniversalTime().ToString( "yyyy-MM-ddThh:mm:ss.ffZ", CultureInfo.InvariantCulture ) );
                else
                    lastModified = CreateElement( "span", "last-modified", package.LastModified.ToUniversalTime().ToString( "yyyy-MM-ddThh:mm:ss.fffZ", CultureInfo.InvariantCulture ) );

                packageElement.Add(
                    CreateElement( "span", "packageType", package.PackageType ),
                    CreateElement( "span", "packageFormat", package.PackageFormat),
                    CreateElement( "span", "name", package.Name ),
                    //new XText( "Deployed: " ),
                    CreateElement( "span", "deployed", package.Deployed ),
                    lastModified,
                    //CreateElement( "span", "last-modified", package.LastModified.ToUniversalTime().ToString( "{O:G}", CultureInfo.InvariantCulture ) ),
                    //CreateElement( "span", "last-modified", package.LastModified.ToUniversalTime().ToString( "yyyy-MM-ddThh:mm:ss.fffZ", CultureInfo.InvariantCulture ) ),
                    CreateElement( "span", "package-etag", package.Tag ),
                    currentLinkElement,
                    CreateElement( "span", "package-size-bytes", package.Size.ToString() ),
                    CreateElement( "span", "package-size-bytes-uncompressed", package.UncompressedSize.ToString() ),
                    //new XText( @"(Package Constituents: " ),
                    constituentLinkElement
                    //new XText( @")" )
                    );

                packageListElement.Add( packageElement );
            }

            bookElement.Add(
                CreateElement( "span", "id", book.Code ),
                CreateElement( "span", "locale", book.Locale.Code ),
                CreateElement( "span", "name", book.Name ),
                CreateElement( "span", "description", book.Description ),
                CreateElement( "span", "BrandingPackageName", book.BrandingPackageName ),
                propertiesElement,
                packageListElement
                );

            return bookElement;
        }