/// <summary> /// Loads and returns a content-object, of the specified kind, from the supplied location and optional Part-URI. /// </summary> protected static Tuple <TContent, string> LoadFromLocation <TContent>(string Kind, Uri Location, Uri PartUri = null, bool RegisterAsRecentDoc = true) { TContent Content = default(TContent); if (Location == null) { throw new UsageAnomaly("Cannot load document (" + Kind + ") without a source location."); } if (!File.Exists(Location.LocalPath)) { throw new ExternalAnomaly("File not found: " + Location.LocalPath); } try { Console.WriteLine("Reading file: " + Location.LocalPath); if (PartUri == null) { PartUri = PART_DEFAULT; } var Pack = Package.Open(Location.LocalPath, FileMode.Open, FileAccess.Read, FileShare.Read); if (Pack == null) { throw new ExternalAnomaly("Zip package cannot be open."); } var Parts = Pack.GetParts(); var Part = Parts.Where(part => part.Uri.IsEqual(PartUri)).FirstOrDefault().NullDefault(Parts.First()); Content = BytesHandling.Deserialize <TContent>(Part.GetStream()); Pack.Close(); var Identified = Content as IIdentifiableElement; Console.WriteLine("Document (" + Kind + ") successfully loaded: " + (Identified == null ? Location.ToString() : Identified.Name) + ". Part=" + PartUri.ToString()); if (RegisterAsRecentDoc) { RegisterRecentDocument(Location.LocalPath); } } catch (Exception Problem) { throw new ExternalAnomaly("Cannot load document (" + Kind + ") from: " + Location.LocalPath, Problem); } return(Tuple.Create(Content, "")); }
/// <summary> /// Static Constructor. /// </summary> static LinkDataType() { StoreBox.RegisterStorableType <LinkDataType>(block => { if (block == null || block.Length < 1) { return(null); } if (block[0] == LINKTYPECODE_GENERIC) { return(GenericLink); } if (block[0] == LINKTYPECODE_INTERNAL) { return(InternalLinkType.InternalTypeAny); } return(ResourceLinkType.PredefinedResourceTypes .FirstOrDefault(predef => predef.TechName == block.ExtractSegment(1).BytesToString()) .NullDefault(ResourceLinkType.ResourceTypeAny)); }, linkt => { if (linkt == null) { return(null); } if (linkt is InternalLinkType) { return(LINKTYPECODE_INTERNAL.IntoArray()); } if (linkt.GetType() == typeof(LinkDataType)) { return(LINKTYPECODE_GENERIC.IntoArray()); } return(BytesHandling.FusionateByteArrays(LINKTYPECODE_RESOURCE.IntoArray(), ((ResourceLinkType)linkt).TechName.AbsentDefault(ResourceLinkType.ResourceTypeAny.TechName).StringToBytes())); }); }
/// <summary> /// Saves a content-object, of the specified kind and content-type, in an specified location and with optional Part-URI. /// Returns an error message if problems were detected. /// </summary> public static string StoreToLocation <TContent>(TContent Content, string Kind, string ContentType, Uri Location, Uri PartUri = null, bool RegisterAsRecentDoc = true, bool SilentSave = false, IFormalizedRecognizableElement Descriptor = null, Visual Snapshot = null, bool SafeSaving = true) { if (Location == null) { throw new UsageAnomaly("Cannot store document without a destination location.", Location); } if (!SilentSave) { Console.WriteLine("Writing file: " + Location.LocalPath); } SafeSaving = (SafeSaving && File.Exists(Location.LocalPath)); var WorkFilePath = (SafeSaving ? Path.Combine(Path.GetDirectoryName(Location.LocalPath), Path.GetFileName(Location.LocalPath) + "." + DateTime.Now.ToString("yyyyMMdd-hhmmss") + FILEEXT_SAV_NEW) : Location.LocalPath); try { var Pack = Package.Open(WorkFilePath, FileMode.Create); PackagePart Part = null; if (PartUri == null) { PartUri = PART_DEFAULT; } // Main Content if (Pack.PartExists(PartUri)) { Part = Pack.GetPart(PartUri); } else { Part = Pack.CreatePart(PartUri, ContentType, CompressionOption.Maximum); } /*T var TestFile = new FileStream(Location.LocalPath + ".BIN", FileMode.Create); * var NewSer = new StandardBinarySerializer(TestFile); * NewSer.Serialize(Content); * TestFile.Close(); * Display.DialogMessage("TEST", "Saved using new binary format."); */ BytesHandling.Serialize <TContent>(Content, Part.GetStream()); // Descriptor if (Descriptor != null) { Pack.PackageProperties.Title = Descriptor.Name; Pack.PackageProperties.Subject = Descriptor.TechName; Pack.PackageProperties.Identifier = Descriptor.GlobalId.ToString(); Pack.PackageProperties.Description = Descriptor.Summary; if (Descriptor.Version != null) { Pack.PackageProperties.Version = Descriptor.Version.VersionNumber; Pack.PackageProperties.Revision = Descriptor.Version.VersionSequence.ToStringAlways(); Pack.PackageProperties.Creator = Descriptor.Version.Creator; Pack.PackageProperties.Created = Descriptor.Version.Creation; Pack.PackageProperties.LastModifiedBy = Descriptor.Version.LastModifier; Pack.PackageProperties.Modified = Descriptor.Version.LastModification; } if (Descriptor.Pictogram != null) { if (Pack.PartExists(PART_PICTOGRAM)) { Part = Pack.GetPart(PART_PICTOGRAM); } else { Part = Pack.CreatePart(PART_PICTOGRAM, "image/png", CompressionOption.Normal); } var Encoder = new PngBitmapEncoder(); using (var Torrent = Part.GetStream()) Display.ExportImageTo(Encoder, Torrent, Descriptor.Pictogram.ToVisual(PART_PICTOGRAM_SIZE, PART_PICTOGRAM_SIZE), (int)PART_PICTOGRAM_SIZE, (int)PART_PICTOGRAM_SIZE); } } // Snapshot if (Snapshot != null) { if (Pack.PartExists(PART_SNAPSHOT)) { Part = Pack.GetPart(PART_SNAPSHOT); } else { Part = Pack.CreatePart(PART_SNAPSHOT, "image/jpg", CompressionOption.Normal); } var Encoder = new JpegBitmapEncoder(); Encoder.QualityLevel = Display.DEF_JPEG_QUALITY; using (var Torrent = Part.GetStream()) Display.ExportImageTo(Encoder, Torrent, Snapshot, (int)PART_SNAPSHOT_WIDTH, (int)PART_SNAPSHOT_HEIGHT); } else if (Pack.PartExists(PART_SNAPSHOT)) { Pack.DeletePart(PART_SNAPSHOT); } Pack.Close(); } catch (Exception Problem) { AppExec.LogException(Problem); return("Cannot save content into file.\n" + "Anomaly: " + Problem.Message); } var Identified = Content as IIdentifiableElement; if (!SilentSave) { Console.WriteLine("Document (" + Kind + ") successfully stored: " + (Identified == null ? Location.ToString() : Identified.Name)); } if (RegisterAsRecentDoc) { RegisterRecentDocument(Location.LocalPath); } if (SafeSaving) { // Create a temporal "old" name for the original file (meaningful, so it could be recovered) var OrigFilePath = Location.LocalPath; var DeleteFilePath = Path.Combine(Path.GetDirectoryName(OrigFilePath), Path.GetFileName(OrigFilePath) + "." + DateTime.Now.ToString("yyyyMMdd-hhmmss") + FILEEXT_SAV_OLD); try { // Rename the original file to the temporal "old" name File.Move(OrigFilePath, DeleteFilePath); // Rename the working "new" file to that of the original File.Move(WorkFilePath, OrigFilePath); // Delete the temporal "old" file File.Delete(DeleteFilePath); } catch (Exception Problem) { Console.WriteLine("Unsuccessful file saving of: " + OrigFilePath); Console.WriteLine("Backup Old-file: " + DeleteFilePath); Console.WriteLine("Temporal New-file: " + WorkFilePath); return("Cannot complete the file saving.\n" + "Anomaly: " + Problem.Message); } } return(""); }