From a9400d718bd7553e8822fe73f4ef600b6a41ae57 Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Fri, 14 Oct 2022 09:41:28 -0400 Subject: [PATCH] get rid of File.SetObjectModel<> in favor of File.Source = new ObjectModelFileSource --- .../UniversalEditor.TestProject/Program.cs | 3 +- .../ObjectModels/FileSystem/File.cs | 15 ---- .../FileSources/ObjectModelFileSource.cs | 73 +++++++++++++++++++ .../UniversalEditor.Essential.csproj | 1 + .../OpenPackagingConvention/OPCDataFormat.cs | 7 +- 5 files changed, 80 insertions(+), 19 deletions(-) create mode 100644 Libraries/UniversalEditor.Essential/ObjectModels/FileSystem/FileSources/ObjectModelFileSource.cs diff --git a/Applications/UniversalEditor.TestProject/Program.cs b/Applications/UniversalEditor.TestProject/Program.cs index 66e88f97..563eaeaa 100644 --- a/Applications/UniversalEditor.TestProject/Program.cs +++ b/Applications/UniversalEditor.TestProject/Program.cs @@ -12,6 +12,7 @@ using UniversalEditor.ObjectModels.Multimedia3D.Model; using UniversalEditor.ObjectModels.Package; using UniversalEditor.ObjectModels.Text.Formatted; using UniversalEditor.Plugins.Genealogy.DataFormats.GEDCOM; +using UniversalEditor.ObjectModels.FileSystem.FileSources; namespace UniversalEditor.TestProject { @@ -51,7 +52,7 @@ namespace UniversalEditor.TestProject MarkupObjectModel momDocument = CreateDocument(model); File file = package.FileSystem.AddFile("model/document.xml"); - file.SetObjectModel(new XMLDataFormat(), momDocument); + file.Source = new ObjectModelFileSource(momDocument, new XMLDataFormat()); objectModels.Push(package); diff --git a/Libraries/UniversalEditor.Essential/ObjectModels/FileSystem/File.cs b/Libraries/UniversalEditor.Essential/ObjectModels/FileSystem/File.cs index 17985158..70729214 100644 --- a/Libraries/UniversalEditor.Essential/ObjectModels/FileSystem/File.cs +++ b/Libraries/UniversalEditor.Essential/ObjectModels/FileSystem/File.cs @@ -218,21 +218,6 @@ namespace UniversalEditor.ObjectModels.FileSystem return om; } - /// - /// Sets the data for this file using the specified and . - /// - /// The type of to save. - /// The specifying how to save the file data. - /// The containing the file data to save. - public void SetObjectModel(DataFormat dataFormat, T objectModel) where T : ObjectModel - { - MemoryAccessor ma = new MemoryAccessor(); - - Document.Save(objectModel, dataFormat, ma); - - byte[] data = ma.ToArray(); - SetData(data); - } public override string ToString() { diff --git a/Libraries/UniversalEditor.Essential/ObjectModels/FileSystem/FileSources/ObjectModelFileSource.cs b/Libraries/UniversalEditor.Essential/ObjectModels/FileSystem/FileSources/ObjectModelFileSource.cs new file mode 100644 index 00000000..8b8b5e73 --- /dev/null +++ b/Libraries/UniversalEditor.Essential/ObjectModels/FileSystem/FileSources/ObjectModelFileSource.cs @@ -0,0 +1,73 @@ +// +// ObjectModelFileSource.cs +// +// Author: +// Michael Becker +// +// Copyright (c) 2022 Mike Becker's Software +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +using UniversalEditor.Accessors; + +namespace UniversalEditor.ObjectModels.FileSystem.FileSources +{ + public class ObjectModelFileSource : FileSource + { + public ObjectModel ObjectModel { get; set; } + public DataFormat DataFormat { get; set; } + + public ObjectModelFileSource(ObjectModel objectModel, DataFormat dataFormat) + { + ObjectModel = objectModel; + DataFormat = dataFormat; + } + + private byte[] _data = null; + public override byte[] GetDataInternal(long offset, long length) + { + __getData(); + if (_data != null && (offset >= 0 && (offset + length) <= _data.Length)) + { + byte[] slice = new byte[length]; + Array.Copy(_data, offset, slice, 0, length); + return slice; + } + return null; + } + + public override long GetLength() + { + __getData(); + if (_data != null) + { + return _data.Length; + } + return -1; + } + + private void __getData() + { + if (_data != null) + { + return; + } + MemoryAccessor ma = new MemoryAccessor(); + + Document.Save(ObjectModel, DataFormat, ma); + + _data = ma.ToArray(); + } + } +} diff --git a/Libraries/UniversalEditor.Essential/UniversalEditor.Essential.csproj b/Libraries/UniversalEditor.Essential/UniversalEditor.Essential.csproj index 75b06fa8..e0a66d9a 100644 --- a/Libraries/UniversalEditor.Essential/UniversalEditor.Essential.csproj +++ b/Libraries/UniversalEditor.Essential/UniversalEditor.Essential.csproj @@ -227,6 +227,7 @@ + diff --git a/Plugins/UniversalEditor.Plugins.Microsoft/DataFormats/Package/OpenPackagingConvention/OPCDataFormat.cs b/Plugins/UniversalEditor.Plugins.Microsoft/DataFormats/Package/OpenPackagingConvention/OPCDataFormat.cs index 33f793f3..9e2cc37d 100644 --- a/Plugins/UniversalEditor.Plugins.Microsoft/DataFormats/Package/OpenPackagingConvention/OPCDataFormat.cs +++ b/Plugins/UniversalEditor.Plugins.Microsoft/DataFormats/Package/OpenPackagingConvention/OPCDataFormat.cs @@ -30,6 +30,7 @@ using UniversalEditor.ObjectModels.Package.Relationships; using UniversalEditor.DataFormats.Package.Relationships; using UniversalEditor.ObjectModels.Package.ContentTypes; using UniversalEditor.DataFormats.Package.ContentTypes; +using UniversalEditor.ObjectModels.FileSystem.FileSources; namespace UniversalEditor.DataFormats.Package.OpenPackagingConvention { @@ -41,8 +42,8 @@ namespace UniversalEditor.DataFormats.Package.OpenPackagingConvention { protected override void BeforeLoadInternal(Stack objectModels) { - base.BeforeLoadInternal(objectModels); objectModels.Push(new FileSystemObjectModel()); + base.BeforeLoadInternal(objectModels); } protected override void AfterLoadInternal(Stack objectModels) { @@ -119,7 +120,7 @@ namespace UniversalEditor.DataFormats.Package.OpenPackagingConvention { rels.Relationships.Add(rel); } - _rels.SetObjectModel(new OPCRelationshipsDataFormat(), rels); + _rels.Source = new ObjectModelFileSource(rels, new OPCRelationshipsDataFormat()); fldr.Files.Add(_rels); @@ -141,7 +142,7 @@ namespace UniversalEditor.DataFormats.Package.OpenPackagingConvention { contentTypes.OverrideDefinitions.Add(type); } - file.SetObjectModel(new OPCContentTypesDataFormat(), contentTypes); + file.Source = new ObjectModelFileSource(contentTypes, new OPCContentTypesDataFormat()); fsom.Files.Add(file); }