Beispiel #1
0
        /// <summary>
        /// Validate a package according to specific validation settings.
        /// </summary>
        /// <remarks>
        /// The <Typ>ValidationResults</Typ> log will contain the warnings and errors encountered during the package
        /// validation process, up to any <Typ>InvalidPackageException</Typ> that is thrown during the process, at
        /// which time the validation process ends.
        /// </remarks>
        /// <returns>
        /// A <Typ>ValidationResults</Typ> log that contains the warnings and errors encountered during the package
        /// validation process.
        /// </returns>
        /// <param name="packageValidatorSettings">The <c>PackageValidatorSettings</c> determining which
        /// validation rules should be applied during the validation process.</param>
        /// <param name="packageReader">The package to be valdated.</param>
        /// <exception cref="ArgumentNullException">Thrown if the <paramref name="packageReader"/> is null.</exception>
        /// <exception cref="InvalidPackageException">Thrown if the validation process encounters errors in the package
        /// for which the <paramref name="packageValidatorSettings"/> have a <Typ>ValidationBehavior</Typ> value
        /// of <c>ValidationBehavior.Enforce</c>.</exception>
        public static ValidationResults Validate(PackageReader packageReader, PackageValidatorSettings packageValidatorSettings)
        {
            ValidatorResources.Culture = LocalizationManager.GetCurrentCulture();

            Utilities.ValidateParameterNonNull("packageReader", packageReader);
            Utilities.ValidateParameterNonNull("packageValidatorSettings", packageValidatorSettings);

            ValidationResults log = Validate(packageReader);

            if (log.HasErrors || (packageValidatorSettings.ScormRequirementValidation == ValidationBehavior.None &&
                                  packageValidatorSettings.ScormRecommendationValidation == ValidationBehavior.None &&
                                  packageValidatorSettings.LrmRequirementValidation == ValidationBehavior.None &&
                                  packageValidatorSettings.MlcRequirementValidation == ValidationBehavior.None))
            {
                return(log);
            }

            ManifestReaderSettings manifestSettings = new ManifestReaderSettings(true, true);
            LrmSettings            lrmSettings      = new LrmSettings(true);

            ManifestReader manifestReader;

            PackageValidator.Validate(packageValidatorSettings, packageReader, false, log, manifestSettings, lrmSettings, out manifestReader);

            return(log);
        }
Beispiel #2
0
        internal static void Validate(PackageValidatorSettings packageValidatorSettings,
                                      PackageReader packageReader,
                                      bool logReplacement,
                                      ValidationResults log,
                                      ManifestReaderSettings manifestSettings,
                                      LrmSettings lrmSettings,
                                      out ManifestReader manifestReader)
        {
            ValidatorResources.Culture = LocalizationManager.GetCurrentCulture();

            if (packageReader == null)
            {
                throw new ArgumentNullException("packageReader");
            }

            ValidationResults manifestLog;
            XPathNavigator    manifest;

            packageReader.CreateManifestNavigator(packageValidatorSettings.LrmRequirementValidation, lrmSettings.FixLrmViolations,
                                                  out manifestLog, out manifest);

            if (manifestLog != null)
            {
                foreach (ValidationResult result in manifestLog.Results)
                {
                    log.AddResult(result);
                }
            }

            manifestReader = new ManifestReader(packageReader, manifestSettings, packageValidatorSettings, logReplacement, log, manifest);

            if (packageValidatorSettings.MlcRequirementValidation != ValidationBehavior.None)
            {
                int activityCount = 0;
                if (manifestReader.Organizations.Count > 0)
                {
                    foreach (OrganizationNodeReader nodeReader in manifestReader.Organizations)
                    {
                        activityCount += nodeReader.Activities.Count;
                    }
                    if (activityCount == 0)
                    {
                        ProcessError(packageValidatorSettings.MlcRequirementValidation,
                                     ValidatorResources.MlcViolationActivityMissing,
                                     log);
                    }
                }
                else
                {
                    ProcessError(packageValidatorSettings.MlcRequirementValidation,
                                 ValidatorResources.MlcViolationOrganizationMissing,
                                 log);
                }
            }
            ManifestValidator.Validate(manifestReader);

            // Add all files in the manifest to a Dictionary
            Dictionary <string, bool> manifestFilePaths = new Dictionary <string, bool>(StringComparer.OrdinalIgnoreCase);

            foreach (ResourceNodeReader r in manifestReader.Resources.Values)
            {
                foreach (FileNodeReader fileNode in r.Files)
                {
                    if (!fileNode.Location.IsAbsoluteUri)
                    {
                        // get the path component of the Location property, and URL decode the string
                        // so it is a proper file path.
                        string path = System.Web.HttpUtility.UrlDecode(RemoveQueryAndAnchor(fileNode.Location.OriginalString));
                        if (!manifestFilePaths.ContainsKey(path))
                        {
                            manifestFilePaths.Add(path, true);
                        }
                    }
                }
            }

            ReadOnlyCollection <string> packageFilePaths = packageReader.GetFilePaths();

            CheckManifestFiles(packageFilePaths, manifestFilePaths, log, packageValidatorSettings);
            CheckPackageFiles(packageFilePaths, manifestFilePaths, log, packageValidatorSettings);
        }