Exemple #1
0
        /// <summary>
        /// Deserializes the supplied XML into a <see cref="DocumentMetadataBase"/>
        /// derived entity (i.e. <see cref="ExportMetadata"/> or <see cref="ExcelDocumentMetadata"/>)
        /// </summary>
        internal static DocumentMetadataBase Deserialize(string value)
        {
            DocumentMetadataBase documentMetadata = null;

            using (var sr = new StringReader(value))
            {
                using (var xr = new XmlTextReader(sr))
                {
                    documentMetadata = (DocumentMetadataBase)XamlReader.Load(xr);
                }
            }

            if (documentMetadata is ExportMetadata)
            {
                documentMetadata.DocumentMetadataType = DocumentMetadataType.ExportMetadata;
            }
            else if (documentMetadata is ExcelDocumentMetadata)
            {
                documentMetadata.DocumentMetadataType = DocumentMetadataType.ExcelDocument;

                var excelDocumentMetadata = (ExcelDocumentMetadata)documentMetadata;
                excelDocumentMetadata.LoadSheetResources(value);
                excelDocumentMetadata.ResourceStore = ResourceStore.Parse(value, null);
            }

            return(documentMetadata);
        }
Exemple #2
0
        /// <summary>
        /// Generates the document.
        /// </summary>
        /// <param name="dataParts">The data parts.</param>
        /// <param name="metadata">The metadata.</param>
        /// <param name="resourcePackage">The resource package.</param>
        /// <param name="exportParameters">The export parameters.</param>
        /// <returns></returns>
        public ExportToMemoryStreamResult GenerateDocument(IEnumerable <IDataPart> dataParts, DocumentMetadataBase metadata, ResourcePackage resourcePackage, ExportParameters exportParameters = null)
        {
            Guard.IsNotNull(dataParts, "dataParts");
            Guard.IsNotNull(metadata, "metadata");
            Guard.IsNotNull(resourcePackage, "resourcePackage");

            var result = new ExportToMemoryStreamResult();

            try
            {
                if (metadata.DocumentMetadataType == DocumentMetadataType.ExcelDocument)
                {
                    result.MemoryStream = this.GenerateExcelInternal(exportParameters, (ExcelDocumentMetadata)metadata, dataParts, resourcePackage);
                }
                else
                {
                    throw new ExportException("Unknown document metadata type");
                }
            }
            catch (Exception ex)
            {
                result.Error = ex;
            }
            return(result);
        }
Exemple #3
0
        public static ExportMetadataPackage Open(string fileName)
        {
            // avoid contention whilst opening file, should be v quick anyway
            lock (padLock)
            {
                try
                {
                    if (string.IsNullOrEmpty(fileName))
                    {
                        throw new ArgumentNullException("fileName");
                    }

                    var fi = new FileInfo(fileName);
                    if (!fi.Exists)
                    {
                        throw new MetadataException(string.Format("File not found <{0}>", fileName));
                    }

                    Output(string.Format("Starting open of package <{0}>", fileName));

                    ExportMetadataPackage package = new ExportMetadataPackage();

                    List <PackagePart> metadataParts = new List <PackagePart>();
                    List <PackagePart> fileParts     = new List <PackagePart>();

                    using (var zip = ZipPackage.Open(fileName, FileMode.Open, FileAccess.Read))
                    {
                        Output("Zip opened");

                        foreach (var part in zip.GetParts())
                        {
                            string uriString = part.Uri.OriginalString;

                            if (uriString.StartsWith(string.Concat(UriDelim, MetadataPathPart)))
                            {
                                metadataParts.Add(part);

                                Output(string.Format("Found Metadata part: Uri <{0}>", uriString));
                            }
                            else if (uriString.StartsWith(string.Concat(UriDelim, FilesPathPart)))
                            {
                                fileParts.Add(part);

                                Output(string.Format("Found TemplateFile part: Uri <{0}>", uriString));
                            }
                        }

                        if (metadataParts.Count == 0)
                        {
                            throw new MetadataException("No metadata files found");
                        }
                        else if (metadataParts.Count > 1)
                        {
                            throw new MetadataException("Only one metadata file allowed in a ExportMetadataPackage");
                        }

                        // metadata process
                        {
                            // read all the templates files into our internal store
                            var m = metadataParts[0];

                            long   length = m.GetStream().Length;
                            byte[] data   = new byte[length];
                            m.GetStream().Read(data, 0, (int)length);

                            Output(string.Format("Loading Metadata part: Uri <{0}> Size {1}", m.Uri.OriginalString, length));

                            bool hasBomb = false;
                            if (data.Length > 2 && data[0] == 0xEF && data[1] == 0xBB && data[2] == 0xBF)
                            {
                                hasBomb = true;
                                Output("Stripping Byte Order Mark");
                            }

                            UTF8Encoding encoding       = new UTF8Encoding(true);
                            string       resourceString = encoding.GetString(hasBomb ? data.Skip(3).ToArray() : data);

                            Output("Stripping Byte Order Mark");

                            package.ExportMetadata = (ExportMetadata)DocumentMetadataBase.Deserialize(resourceString);

                            Output("Export metadata deserialized");
                        }

                        // template file process
                        if (!string.IsNullOrEmpty(package.ExportMetadata.TemplateFileName))
                        {
                            Output(string.Format("Checking for existence of the TemplateFilePath <{0}>", package.ExportMetadata.TemplateFileName));

                            if (fileParts.Count == 0)
                            {
                                throw new MetadataException(string.Format("No file parts found and <{0}> expected", package.ExportMetadata.TemplateFileName));
                            }
                            else if (fileParts.Count > 1)
                            {
                                throw new MetadataException(string.Format("More than 1 file parts found and only <{0}> expected", package.ExportMetadata.TemplateFileName));
                            }

                            var f = fileParts[0];

                            string name   = f.Uri.OriginalString.Replace(string.Concat(UriDelim, FilesPathPart, UriDelim), null);
                            long   length = f.GetStream().Length;

                            package.ExportMetadata.HasTemplate  = true;
                            package.ExportMetadata.TemplateData = new byte[length];
                            f.GetStream().Read(package.ExportMetadata.TemplateData, 0, (int)length);

                            Output(string.Format("Loaded TemplateFile data : Uri <{0}> Size {1}", f.Uri.OriginalString, length));
                        }
                    }

                    package.ValidateInternal();

                    return(package);
                }
                catch (Exception ex)
                {
                    Output(ex.ToString());
                    throw;
                }
            }
        }