Bloom.Book.BookStorage.ExpensiveInitialization C# (CSharp) Method

ExpensiveInitialization() private method

Do whatever is needed to do more than just show a title and thumbnail
private ExpensiveInitialization ( ) : void
return void
        private void ExpensiveInitialization()
        {
            Debug.WriteLine(string.Format("ExpensiveInitialization({0})", _folderPath));
            _dom = new HtmlDom();
            //the fileLocator we get doesn't know anything about this particular book.
            _fileLocator.AddPath(_folderPath);
            RequireThat.Directory(_folderPath).Exists();
            string pathToExistingHtml;
            try
            {
                pathToExistingHtml = PathToExistingHtml;
            }
            catch (UnauthorizedAccessException error)
            {
                ProcessAccessDeniedError(error);
                return;
            }

            if (!RobustFile.Exists(pathToExistingHtml))
            {
                var files = new List<string>(Directory.GetFiles(_folderPath));
                var b = new StringBuilder();
                b.AppendLine("Could not determine which html file in the folder to use.");
                if (files.Count == 0)
                    b.AppendLine("***There are no files.");
                else
                {
                    b.AppendLine("Files in this book are:");
                    foreach (var f in files)
                    {
                        b.AppendLine("  " + f);
                    }
                }
                throw new ApplicationException(b.ToString());
            }
            else
            {
                XmlDocument xmlDomFromHtmlFile;
                try
                {
                    xmlDomFromHtmlFile = XmlHtmlConverter.GetXmlDomFromHtmlFile(PathToExistingHtml, false);
                }
                catch (UnauthorizedAccessException error)
                {
                    ProcessAccessDeniedError(error);
                    return;
                }
                catch (Exception error)
                {
                    ErrorMessagesHtml = WebUtility.HtmlEncode(error.Message);
                    Logger.WriteEvent("*** ERROR in " + PathToExistingHtml);
                    Logger.WriteEvent("*** ERROR: " + error.Message.Replace("{", "{{").Replace("}", "}}"));
                    return;
                }

                _dom = new HtmlDom(xmlDomFromHtmlFile); //with throw if there are errors
                // Don't let spaces between <strong>, <em>, or <u> elements be removed. (BL-2484)
                _dom.RawDom.PreserveWhitespace = true;

                //Validating here was taking a 1/3 of the startup time
                // eventually, we need to restructure so that this whole Storage isn't created until actually needed, then maybe this can come back
                //ErrorMessages = ValidateBook(PathToExistingHtml);
                // REVIEW: we did in fact change things so that storage isn't used until we've shown all the thumbnails we can (then we go back and update in background)...
                // so maybe it would be ok to reinstate the above?

                //For now, we really need to do this check, at least. This will get picked up by the Book later (feeling kludgy!)
                //I assume the following will never trigger (I also note that the dom isn't even loaded):

                if (!string.IsNullOrEmpty(ErrorMessagesHtml))
                {
                    _dom.RawDom.LoadXml(
                        "<html><body>There is a problem with the html structure of this book which will require expert help.</body></html>");
                    Logger.WriteEvent(
                        "{0}: There is a problem with the html structure of this book which will require expert help: {1}",
                        PathToExistingHtml, ErrorMessagesHtml);
                }
                    //The following is a patch pushed out on the 25th build after 1.0 was released in order to give us a bit of backwards version protection (I should have done this originally and in a less kludgy fashion than I'm forced to do now)
                else
                {
                    var incompatibleVersionMessage = GetHtmlMessageIfVersionIsIncompatibleWithThisBloom(Dom,this.PathToExistingHtml);
                    if (!string.IsNullOrWhiteSpace(incompatibleVersionMessage))
                    {
                        ErrorMessagesHtml = incompatibleVersionMessage;
                        Logger.WriteEvent("*** ERROR: " + incompatibleVersionMessage);
                        _errorAlreadyContainsInstructions = true;
                        return;
                    }
                    else
                    {
                        Logger.WriteEvent("BookStorage Loading Dom from {0}", PathToExistingHtml);
                    }
                }

                Dom.UpdatePageDivs();

                UpdateSupportFiles();

                CleanupUnusedImageFiles();
            }
        }