Bloom.Book.BookCopyrightAndLicense.GetMetadata C# (CSharp) Method

GetMetadata() public static method

Create a Clearshare.Metadata object by reading values out of the dom's bloomDataDiv
public static GetMetadata ( HtmlDom dom, string brandingNameOrFolderPath = "" ) : SIL.Windows.Forms.ClearShare.Metadata
dom HtmlDom
brandingNameOrFolderPath string Normally, the branding is just a name, which we look up in the official branding folder //but unit tests can instead provide a path to the folder. ///
return SIL.Windows.Forms.ClearShare.Metadata
        public static Metadata GetMetadata(HtmlDom dom, string brandingNameOrFolderPath = "")
        {
            if (ShouldSetToDefaultCopyrightAndLicense(dom))
            {
                return GetMetadataWithDefaultCopyrightAndLicense(brandingNameOrFolderPath);
            }
            var metadata = new Metadata();
            var copyright = dom.GetBookSetting("copyright");
            if (!copyright.Empty)
            {
                metadata.CopyrightNotice = WebUtility.HtmlDecode(copyright.GetFirstAlternative());
            }

            var licenseUrl = dom.GetBookSetting("licenseUrl").GetBestAlternativeString(new[] { "*", "en" });

            if (string.IsNullOrWhiteSpace(licenseUrl))
            {
                //NB: we are mapping "RightsStatement" (which comes from XMP-dc:Rights) to "LicenseNotes" in the html.
                //custom licenses live in this field, so if we have notes (and no URL) it is a custom one.
                var licenseNotes = dom.GetBookSetting("licenseNotes");
                if (!licenseNotes.Empty)
                {
                    metadata.License = new CustomLicense { RightsStatement = WebUtility.HtmlDecode(licenseNotes.GetFirstAlternative()) };
                }
                else
                {
                    // The only remaining current option is a NullLicense
                    metadata.License = new NullLicense(); //"contact the copyright owner
                }
            }
            else // there is a licenseUrl, which means it is a CC license
            {
                try
                {
                    metadata.License = CreativeCommonsLicense.FromLicenseUrl(licenseUrl);
                }
                catch (Exception e)
                {
                    throw new ApplicationException("Bloom had trouble parsing this license url: '" + licenseUrl + "'. (ref BL-4108)", e);
                }
                //are there notes that go along with that?
                var licenseNotes = dom.GetBookSetting("licenseNotes");
                if(!licenseNotes.Empty)
                {
                    var s = WebUtility.HtmlDecode(licenseNotes.GetFirstAlternative());
                    metadata.License.RightsStatement = HtmlDom.ConvertHtmlBreaksToNewLines(s);
                }
            }
            return metadata;
        }

Usage Example

Example #1
0
        /// <summary>
        /// Copy the copyright & license info to the originalCopyrightAndLicense,
        /// then remove the copyright so the translator can put in their own if they
        /// want. We retain the license, but the translator is allowed to change that.
        /// </summary>
        public static void SetOriginalCopyrightAndLicense(HtmlDom dom, BookData bookData, CollectionSettings collectionSettings)
        {
            if (bookData.GetMultiTextVariableOrEmpty("originalCopyrightAndLicense").Count > 0)
            {
                return;                 //leave the original there.
            }
            var    metadata = BookCopyrightAndLicense.GetMetadata(dom);
            string idOfLanguageUsed;
            var    languagePriorityIds = collectionSettings.LicenseDescriptionLanguagePriorities;

            //TODO HOW DO I GET THESE IN THE NATIONAL LANGUAGE INSTEAD OF THE UI LANGUAGE?

            var    license = metadata.License.GetMinimalFormForCredits(languagePriorityIds, out idOfLanguageUsed);
            string originalLicenseSentence;
            var    preferredLanguageIds = new[] { collectionSettings.Language2Iso639Code, LocalizationManager.UILanguageId, "en" };

            if (metadata.License is CustomLicense)
            {
                // I can imagine being more fancy... something like "Licensed under custom license:", and get localizations
                // for that... but sheesh, these are even now very rare in Bloom-land and should become more rare.
                // So for now, let's just print the custom license contents.
                originalLicenseSentence = license;
            }
            else
            {
                var licenseSentenceTemplate = LocalizationManager.GetString("EditTab.FrontMatter.OriginalLicenseSentence",
                                                                            "Licensed under {0}.",
                                                                            "On the Credits page of a book being translated, Bloom puts texts like 'Licensed under CC-BY', so that we have a record of what the license was for the original book. Put {0} in the translation, where the license should go in the sentence.",
                                                                            preferredLanguageIds, out idOfLanguageUsed);
                originalLicenseSentence = string.IsNullOrWhiteSpace(license) ? "" : string.Format(licenseSentenceTemplate, license);
                originalLicenseSentence = originalLicenseSentence.Replace("..", ".");                  // in case had notes which also had a period.
            }

            Console.WriteLine(originalLicenseSentence);
            var copyrightNotice = "";

            if (string.IsNullOrWhiteSpace(metadata.CopyrightNotice))
            {
                var noCopyrightSentence = LocalizationManager.GetString("EditTab.FrontMatter.OriginalHadNoCopyrightSentence",
                                                                        "Adapted from original without a copyright notice.",
                                                                        "On the Credits page of a book being translated, Bloom shows this if the original book did not have a copyright notice.",
                                                                        preferredLanguageIds, out idOfLanguageUsed);

                copyrightNotice = noCopyrightSentence + " " + originalLicenseSentence;
            }
            else
            {
                var originalCopyrightSentence = LocalizationManager.GetString("EditTab.FrontMatter.OriginalCopyrightSentence",
                                                                              "Adapted from original, {0}.",
                                                                              "On the Credits page of a book being translated, Bloom shows the original copyright. Put {0} in the translation where the copyright notice should go. For example in English, 'Adapted from original, {0}.' comes out like 'Adapted from original, Copyright 2011 SIL'.",
                                                                              preferredLanguageIds, out idOfLanguageUsed);
                copyrightNotice = String.Format(originalCopyrightSentence, metadata.CopyrightNotice.Trim()) + " " + originalLicenseSentence;
            }
            Console.WriteLine(copyrightNotice);
            bookData.Set("originalCopyrightAndLicense", copyrightNotice, "*");
            bookData.RemoveAllForms("copyright");              // RemoveAllForms does modify the dom
        }
All Usage Examples Of Bloom.Book.BookCopyrightAndLicense::GetMetadata