get rid of File.SetObjectModel<> in favor of File.Source = new ObjectModelFileSource

This commit is contained in:
Michael Becker 2022-10-14 09:41:28 -04:00
parent 8f42f04af2
commit a9400d718b
No known key found for this signature in database
GPG Key ID: DA394832305DA332
5 changed files with 80 additions and 19 deletions

View File

@ -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<MarkupObjectModel>(new XMLDataFormat(), momDocument);
file.Source = new ObjectModelFileSource(momDocument, new XMLDataFormat());
objectModels.Push(package);

View File

@ -218,21 +218,6 @@ namespace UniversalEditor.ObjectModels.FileSystem
return om;
}
/// <summary>
/// Sets the data for this file using the specified <see cref="DataFormat" /> and <see cref="ObjectModel" />.
/// </summary>
/// <typeparam name="T">The type of <see cref="ObjectModel" /> to save.</typeparam>
/// <param name="dataFormat">The <see cref="DataFormat" /> specifying how to save the file data.</param>
/// <param name="objectModel">The <see cref="ObjectModel" /> containing the file data to save.</param>
public void SetObjectModel<T>(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()
{

View File

@ -0,0 +1,73 @@
//
// ObjectModelFileSource.cs
//
// Author:
// Michael Becker <alcexhim@gmail.com>
//
// 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 <http://www.gnu.org/licenses/>.
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();
}
}
}

View File

@ -227,6 +227,7 @@
<Compile Include="DataFormats\UEPackage\SettingsXMLSchema.cs" />
<Compile Include="ObjectModels\Markup\IMarkupElementContainer.cs" />
<Compile Include="ObjectModels\Markup\MarkupDefinition.cs" />
<Compile Include="ObjectModels\FileSystem\FileSources\ObjectModelFileSource.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\UniversalEditor.Core\UniversalEditor.Core.csproj">

View File

@ -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<ObjectModel> objectModels)
{
base.BeforeLoadInternal(objectModels);
objectModels.Push(new FileSystemObjectModel());
base.BeforeLoadInternal(objectModels);
}
protected override void AfterLoadInternal(Stack<ObjectModel> objectModels)
{
@ -119,7 +120,7 @@ namespace UniversalEditor.DataFormats.Package.OpenPackagingConvention
{
rels.Relationships.Add(rel);
}
_rels.SetObjectModel<RelationshipsObjectModel>(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<ContentTypesObjectModel>(new OPCContentTypesDataFormat(), contentTypes);
file.Source = new ObjectModelFileSource(contentTypes, new OPCContentTypesDataFormat());
fsom.Files.Add(file);
}