Example #1
0
        public static Tiling LoadTiling([NotNull] Stream stream, out IImage thumbnail)
        {
            if (stream == null) throw new ArgumentNullException(nameof(stream));

            using (ZipArchive zip = new ZipArchive(stream, ZipArchiveMode.Read, true))
            {
                // Load the thumbnail
                ZipArchiveEntry thumbnailEntry = zip.Entries.FirstOrDefault(e => e.Name.StartsWith("Thumbnail."));
                if (thumbnailEntry == null)
                    thumbnail = null;
                else
                {
                    using (Stream thumbnailStream = thumbnailEntry.Open())
                    using (BinaryReader reader = new BinaryReader(thumbnailStream))
                    {
                        Debug.Assert(thumbnailEntry.Length < int.MaxValue);

                        byte[] data = reader.ReadBytes((int) thumbnailEntry.Length);

                        thumbnail = new MemoryImage(data);
                    }
                }

                // Load the template
                ZipArchiveEntry templateEntry = zip.GetEntry("Template.xml");
                if (templateEntry == null) throw new InvalidDataException("Template definition missing.");

                XDocument doc;
                using (Stream templateStream = templateEntry.Open())
                    doc = XDocument.Load(templateStream);
                if (doc.Root == null)
                    throw new InvalidDataException("Template definition empty.");

                Version version;
                if (!Version.TryParse(doc.Root.Attribute("version")?.Value, out version))
                    throw new InvalidDataException("Invalid version number");

                if (version > _version)
                {
                    throw new InvalidDataException(
                        "The template was created for an earlier version of the application and cannot be loaded.");
                }
                // Deal with different versions if needed.

                Template template = DeserializeTemplate(doc.Root);
                _template.Value = template;

                // Load images
                Dictionary<string, IImage> imageByFileName = _imageByFileName.Value;
                Debug.Assert(imageByFileName != null, "imageByFileName != null");
                imageByFileName.Clear();

                foreach (ZipArchiveEntry imgEntry in zip.Entries.Where(e => e.Name.StartsWith("Image")))
                {
                    IImage image;
                    using (Stream imgStream = imgEntry.Open())
                    using (BinaryReader reader = new BinaryReader(imgStream))
                    {
                        Debug.Assert(imgEntry.Length < int.MaxValue);

                        byte[] data = reader.ReadBytes((int) imgEntry.Length);

                        image = new MemoryImage(data);
                    }

                    imageByFileName.Add(imgEntry.Name, image);
                }

                // Load the tiling
                ZipArchiveEntry tilingEntry = zip.GetEntry("Tiling.xml");
                if (tilingEntry == null) throw new InvalidDataException("Tiling definition missing.");

                using (Stream tilingStream = tilingEntry.Open())
                    doc = XDocument.Load(tilingStream);
                if (doc.Root == null)
                    throw new InvalidDataException("Tiling definition empty.");

                if (!Version.TryParse(doc.Root.Attribute("version")?.Value, out version))
                    throw new InvalidDataException("Invalid version number");

                if (version > _version)
                {
                    throw new InvalidDataException(
                        "The template was created for an earlier version of the application and cannot be loaded.");
                }
                // Deal with different versions if needed.

                Tiling tiling = DeserializeTiling(doc.Root, template);

                imageByFileName.Clear();
                Debug.Assert(_shapeByName.Value != null, "_shapeByName.Value != null");
                _shapeByName.Value.Clear();
                Debug.Assert(_linesByPartShapeId.Value != null, "_linesByPartShapeId.Value != null");
                _linesByPartShapeId.Value.Clear();
                _template.Value = null;

                return tiling;
            }
        }
Example #2
0
        public static Template LoadTemplate([NotNull] Stream stream, out IImage thumbnail)
        {
            if (stream == null) throw new ArgumentNullException(nameof(stream));

            using (ZipArchive zip = new ZipArchive(stream, ZipArchiveMode.Read, true))
            {
                // Load the thumbnail
                ZipArchiveEntry thumbnailEntry = zip.Entries.FirstOrDefault(e => e.Name.StartsWith("Thumbnail."));
                if (thumbnailEntry == null)
                    thumbnail = null;
                else
                {
                    using (Stream thumbnailStream = thumbnailEntry.Open())
                    using (BinaryReader reader = new BinaryReader(thumbnailStream))
                    {
                        Debug.Assert(thumbnailEntry.Length < int.MaxValue);

                        byte[] data = reader.ReadBytes((int) thumbnailEntry.Length);

                        thumbnail = new MemoryImage(data);
                    }
                }

                // Load the template
                ZipArchiveEntry templateEntry = zip.GetEntry("Template.xml");
                if (templateEntry == null) throw new InvalidDataException("Template definition missing.");

                XDocument doc;
                using (Stream templateStream = templateEntry.Open())
                    doc = XDocument.Load(templateStream);
                if (doc.Root == null)
                    throw new InvalidDataException("Template definition empty.");

                Version version;
                if (!Version.TryParse(doc.Root.Attribute("version")?.Value, out version))
                    throw new InvalidDataException("Invalid version number");

                if (version > _version)
                {
                    throw new InvalidDataException(
                        "The template was created for an earlier version of the application and cannot be loaded.");
                }
                // Deal with different versions if needed.

                return DeserializeTemplate(doc.Root);
            }
        }