Example #1
0
 public override TemplatePackageManifest GetPackageManifest()
 {
     using (Stream stream = GetFile("manifest.xml"))
     {
         return(TemplatePackageManifest.FromStream(stream));
     }
 }
        /// <summary>
        /// Create a package in one step.
        /// </summary>
        /// <param name="packagePath">The full file path were the package should be written to. Any existing file will be overwritten.</param>
        /// <param name="manifest">The manifest of the package. Passing null creates a package without a manifest.</param>
        /// <param name="filePaths">A complete list of all file paths to be added to the package.</param>
        /// <param name="rsaParamsXml">RSA key serialized to XML. It can be a public/private key pair or only a public key. If null, save an unencrypted package.</param>
        /// <param name="compression">The compression level.</param>
        public static void Create(string packagePath, TemplatePackageManifest manifest, IEnumerable <string> filePaths, string rsaParamsXml, CompressionOption compression)
        {
            TemplatePackage package = new TemplatePackage();

            //
            package.CurrentCompression = compression;
            foreach (var s in filePaths)
            {
                package.AddFile(s);
            }
            package.Manifest = manifest;
            package.SaveAs(packagePath, rsaParamsXml);
        }
        /// <summary>
        /// Extract a package in one step.
        /// </summary>
        /// <param name="packagePath">The full file path of the package.</param>
        /// <param name="rsaParamsXml">RSA key serialized to XML. If null, extract an unencrypted package.</param>
        /// <param name="targetFolderPath">The folder where the file should be extracted to. This folder will be created if needed. Any existing file will be overwritten.</param>
        /// <param name="manifest">The manifest of the package. If null, the package had no manifest.</param>
        /// <param name="filePaths">A list containing the full file paths of all extracted files.</param>
        public static void Extract(string packagePath, string rsaParamsXml, string targetFolderPath,
                                   out TemplatePackageManifest manifest, out IEnumerable <string> filePaths)
        {
            TemplatePackage package  = new TemplatePackage();
            List <string>   fileList = new List <string>();
            string          path;

            //
            package.Open(packagePath, rsaParamsXml);
            foreach (var s in package.GetFiles())
            {
                path = package.ExtractFile(s, targetFolderPath);
                fileList.Add(path);
            }
            manifest  = package.Manifest;
            filePaths = fileList;
        }
        private void LoadManifest()
        {
            Uri         partUri = InternalNameToPartUri(ManifestName);
            PackagePart packagePart;

            //
            _manifest = null;
            if (_package.PartExists(partUri))
            {
                packagePart = _package.GetPart(partUri);
                using (Stream ps = packagePart.GetStream(FileMode.Open, FileAccess.Read))
                {
                    _manifest = TemplatePackageManifest.FromStream(ps);
                }
            }
            else
            {
                throw new Exception("Package has no manifest");
            }
        }
        /// <summary>
        /// Create a deep copy.
        /// </summary>
        /// <returns>Returns a deep copy.</returns>
        public TemplatePackageManifest Copy()
        {
            TemplatePackageManifest m = new TemplatePackageManifest();
            int i;

            //
            m.Signature       = Signature;
            m.HotDocsVersion  = HotDocsVersion;
            m.PublishDateTime = PublishDateTime;
            m.ExpirationDate  = ExpirationDate;
            m.WarningDays     = WarningDays;
            m.ExtensionDays   = ExtensionDays;
            m.MainTemplate    = MainTemplate != null?MainTemplate.Copy() : new TemplateInfo();

            if (OtherTemplates != null)
            {
                m.OtherTemplates = new TemplateInfo[OtherTemplates.Length];
                for (i = 0; i < OtherTemplates.Length; i++)
                {
                    m.OtherTemplates[i] = OtherTemplates[i].Copy();
                }
            }
            else
            {
                m.OtherTemplates = new TemplateInfo[0];
            }
            if (AdditionalFiles != null)
            {
                m.AdditionalFiles = new string[AdditionalFiles.Length];
                for (i = 0; i < AdditionalFiles.Length; i++)
                {
                    m.AdditionalFiles[i] = AdditionalFiles[i];
                }
            }
            else
            {
                m.AdditionalFiles = new string[0];
            }
            return(m);
        }
        /// <summary>
        /// Check if a given manifest is valid for the current files in the package.
        /// </summary>
        /// <param name="manifest">The manifest object to test.</param>
        /// <param name="errMsg">If the function returns false, this string will contain an error message.</param>
        /// <returns>true if the manifest is valid, false otherwise.</returns>
        public bool IsValidManifest(TemplatePackageManifest manifest, out string errMsg)
        {
            Dictionary <string, Dependency> dict = new Dictionary <string, Dependency>();
            Dictionary <string, string>     dictT = new Dictionary <string, string>();
            string         name, key, location;
            DependencyType depType;
            int            i;

            //
            if (manifest == null)
            {
                errMsg = "No manifest defined";
                return(false);
            }
            if (string.IsNullOrEmpty(manifest.HotDocsVersion))
            {
                errMsg = "Missing HotDocsVersion in manifest";
                return(false);
            }
            if (manifest.Version >= 11 && string.IsNullOrEmpty(manifest.PublishDateTime))
            {
                errMsg = "Missing PublishDateTime in manifest";
                return(false);
            }
            if (manifest.MainTemplate == null)
            {
                errMsg = "MainTemplate in manifest cannot be null";
                return(false);
            }
            location = "MainTemplate";
            if (!CollectDependencies(manifest.MainTemplate, dict, out errMsg))
            {
                errMsg = "Error in " + location + " of manifest: " + errMsg;
                return(false);
            }
            name       = manifest.MainTemplate.FileName;
            key        = name.ToLower();
            dict[key]  = new Dependency(name, DependencyType.Assemble);
            dictT[key] = name;
            for (i = 0; i < manifest.OtherTemplates.Length; i++)
            {
                location = string.Format("OtherTemplates[{0}]", i + 1);
                if (!CollectDependencies(manifest.OtherTemplates[i], dict, out errMsg))
                {
                    errMsg = "Error in " + location + " of manifest: " + errMsg;
                    return(false);
                }
                name = manifest.OtherTemplates[i].FileName;
                key  = name.ToLower();
                if (dictT.ContainsKey(key))
                {
                    errMsg = "Error in " + location + " of manifest: template'" + name + "' has been defined already";
                    return(false);
                }
                dictT[key] = name;
            }
            foreach (var d in dict)
            {
                key     = d.Key;
                name    = d.Value.FileName;
                depType = d.Value.DependencyType;
                if (Dependency.IsTemplateDependency(depType) && !dictT.ContainsKey(key))
                {
                    errMsg = "Template '" + name + "' is not defined in the manifest but is listed as a dependency";
                    return(false);
                }
                if (!FileExists(name))
                {
                    errMsg = "File '" + name + "' in manifest doesn't exist in the package";
                    return(false);
                }
            }
            location = "MainTemplate";
            if (!IsValidTemplateInfo(manifest.MainTemplate, dict, out errMsg))
            {
                errMsg = "Error in " + location + " of manifest: " + errMsg;
                return(false);
            }
            for (i = 0; i < manifest.OtherTemplates.Length; i++)
            {
                location = string.Format("OtherTemplates[{0}]", i + 1);
                if (!IsValidTemplateInfo(manifest.OtherTemplates[i], dict, out errMsg))
                {
                    errMsg = "Error in " + location + " of manifest: " + errMsg;
                    return(false);
                }
            }
            if (manifest.AdditionalFiles == null)
            {
                errMsg = "AdditionalFiles in manifest cannot be null";
                return(false);
            }
            for (i = 0; i < manifest.AdditionalFiles.Length; i++)
            {
                location = string.Format("AdditionalFiles[{0}]", i + 1);
                name     = manifest.AdditionalFiles[i];
                if (string.IsNullOrEmpty(name))
                {
                    errMsg = location + " in manifest cannot be null or empty";
                    return(false);
                }
                key = name.ToLower();
                if (dict.ContainsKey(key))
                {
                    errMsg = "Additional file '" + name + "' in manifest is already referenced from a template or occurs twice in additional files";
                    return(false);
                }
                dict[key] = new Dependency(name, DependencyType.NoDependency);
                if (!FileExists(name))
                {
                    errMsg = "Additional file '" + name + "' in manifest doesn't exist in the package";
                    return(false);
                }
            }
            foreach (string s in GetFiles())
            {
                name = s;
                key  = name.ToLower();
                if (!dict.ContainsKey(key))
                {
                    errMsg = "File '" + name + "' in package is not referenced in manifest";
                    return(false);
                }
            }
            errMsg = string.Empty;
            return(true);
        }
 /// <summary>
 /// Extract an unencrypted package in one step.
 /// </summary>
 /// <param name="packagePath">The full file path of the package.</param>
 /// <param name="targetFolderPath">The folder where the file should be extracted to. This folder will be created if needed. Any existing file will be overwritten.</param>
 /// <param name="manifest">The manifest of the package. If null, the package had no manifest.</param>
 /// <param name="filePaths">A list containing the full file paths of all extracted files.</param>
 public static void Extract(string packagePath, string targetFolderPath,
                            out TemplatePackageManifest manifest, out IEnumerable <string> filePaths)
 {
     Extract(packagePath, null, targetFolderPath, out manifest, out filePaths);
 }
 /// <summary>
 /// Create an unencrypted package in one step using the default compression option (=Maximum).
 /// </summary>
 /// <param name="packagePath">The full file path were the package should be written to. Any existing file will be overwritten.</param>
 /// <param name="manifest">The manifest of the package. Passing null creates a package without a manifest.</param>
 /// <param name="filePaths">A complete list of all file paths to be added to the package.</param>
 public static void Create(string packagePath, TemplatePackageManifest manifest, IEnumerable <string> filePaths)
 {
     Create(packagePath, manifest, filePaths, null);
 }
 /// <summary>
 /// Create a package in one step using the default compression option (=Maximum).
 /// </summary>
 /// <param name="packagePath">The full file path were the package should be written to. Any existing file will be overwritten.</param>
 /// <param name="manifest">The manifest of the package. Passing null creates a package without a manifest.</param>
 /// <param name="filePaths">A complete list of all file paths to be added to the package.</param>
 /// <param name="rsaParamsXml">RSA key serialized to XML. It can be a public/private key pair or only a public key. If null, save an unencrypted package.</param>
 public static void Create(string packagePath, TemplatePackageManifest manifest, IEnumerable <string> filePaths, string rsaParamsXml)
 {
     Create(packagePath, manifest, filePaths, rsaParamsXml, CompressionOption.Maximum);
 }