Ejemplo n.º 1
0
 /// <summary>
 ///   Initializes a new <see cref="Document" /> instance.
 /// </summary>
 /// <param name="licenseHeaderInput">
 ///   The license header input representing the document being encapsulated by this
 ///   <see cref="Document" /> instance.
 /// </param>
 /// <param name="language">
 ///   The <see cref="Language" /> instance to be used when replacing license headers in the document
 ///   represented by <paramref name="licenseHeaderInput" />.
 /// </param>
 /// <param name="headerLines">
 ///   The document's current license header, one line per array index. Null if headers are to be
 ///   removed.
 /// </param>
 /// <param name="additionalProperties">
 ///   Additional properties to be used when inserting the license header that are already
 ///   expanded.
 /// </param>
 /// <param name="keywords">
 ///   If not <see langword="null" />, headers are only removed/replaced if they contain at least one
 ///   keyword of this <see cref="IEnumerable{T}" />.
 /// </param>
 public Document(
     LicenseHeaderInput licenseHeaderInput,
     Language language,
     string[] headerLines,
     IEnumerable <AdditionalProperty> additionalProperties = null,
     IEnumerable <string> keywords = null)
 {
     _licenseHeaderInput   = licenseHeaderInput;
     _additionalProperties = additionalProperties;
     _keywords             = keywords;
     _language             = language;
     _headerLines          = headerLines;
     _commentParser        = new CommentParser(language.LineComment, language.BeginComment, language.EndComment, language.BeginRegion, language.EndRegion);
 }
        /// <summary>
        ///   Tries to open a given project item as a Document which can be used to add or remove headers.
        /// </summary>
        /// <param name="licenseHeaderInput">A <see cref="LicenseHeaderInput" /> instance representing the document to be opened.</param>
        /// <param name="document">
        ///   In case of a success, i. e. return value of <see cref="CreateDocumentResult.DocumentCreated" />,
        ///   this parameter represents the <see cref="Document" /> instance that was created in the process.Otherwise
        ///   <see langword="null" />.
        /// </param>
        /// <returns>
        ///   Returns a <see cref="CreateDocumentResult" /> member describing the success status of the document opening
        ///   attempt.
        /// </returns>
        internal virtual CreateDocumentResult TryCreateDocument(LicenseHeaderInput licenseHeaderInput, out Document document)
        {
            document = null;

            if (licenseHeaderInput is LicenseHeaderPathInput pathInput && !File.Exists(pathInput.DocumentPath))
            {
                return(CreateDocumentResult.FileNotFound);
            }

            if (licenseHeaderInput.Extension == LicenseHeaderExtractor.HeaderDefinitionExtension)
            {
                return(CreateDocumentResult.LicenseHeaderDocument);
            }

            var language = GetLanguageFromExtension(licenseHeaderInput.Extension);

            if (language == null)
            {
                return(CreateDocumentResult.LanguageNotFound);
            }

            string[] header = null;
            if (licenseHeaderInput.Headers != null)
            {
                var extension = licenseHeaderInput.Headers.Keys
                                .OrderByDescending(x => x.Length)
                                .FirstOrDefault(x => licenseHeaderInput.Extension.EndsWith(x, StringComparison.OrdinalIgnoreCase));

                if (extension == null)
                {
                    return(CreateDocumentResult.NoHeaderFound);
                }

                header = licenseHeaderInput.Headers[extension];

                if (header.All(string.IsNullOrEmpty))
                {
                    return(CreateDocumentResult.EmptyHeader);
                }
            }

            document = new Document(licenseHeaderInput, language, header, licenseHeaderInput.AdditionalProperties, _keywords);

            return(CreateDocumentResult.DocumentCreated);
        }