diff --git a/CSharp/Libraries/UniversalEditor.Essential/ObjectModels/FileSystem/IFileSystemContainer.cs b/CSharp/Libraries/UniversalEditor.Essential/ObjectModels/FileSystem/IFileSystemContainer.cs index f839e674..4e5a34b8 100644 --- a/CSharp/Libraries/UniversalEditor.Essential/ObjectModels/FileSystem/IFileSystemContainer.cs +++ b/CSharp/Libraries/UniversalEditor.Essential/ObjectModels/FileSystem/IFileSystemContainer.cs @@ -10,5 +10,7 @@ namespace UniversalEditor.ObjectModels.FileSystem File.FileCollection Files { get; } Folder.FolderCollection Folders { get; } string GetNewFolderName(); + + File AddFile(string name, byte[] fileData = null); } } diff --git a/CSharp/Plugins/UniversalEditor.Plugins.FileSystem/DataFormats/FileSystem/ZIP/Internal/ZIPCentralDirectoryFooter.cs b/CSharp/Plugins/UniversalEditor.Plugins.FileSystem/DataFormats/FileSystem/ZIP/Internal/ZIPCentralDirectoryFooter.cs new file mode 100644 index 00000000..4d1973ea --- /dev/null +++ b/CSharp/Plugins/UniversalEditor.Plugins.FileSystem/DataFormats/FileSystem/ZIP/Internal/ZIPCentralDirectoryFooter.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace UniversalEditor.DataFormats.FileSystem.ZIP.Internal +{ + internal struct ZIPCentralDirectoryFooter + { + public uint unknown1; + public ushort unknown2; + public ushort unknown3; + public uint centralDirectoryLength; + public uint centralDirectoryOffset; + public ushort unknown4; + } +} diff --git a/CSharp/Plugins/UniversalEditor.Plugins.FileSystem/UniversalEditor.Plugins.FileSystem.csproj b/CSharp/Plugins/UniversalEditor.Plugins.FileSystem/UniversalEditor.Plugins.FileSystem.csproj index cdf56f8c..38c487ea 100644 --- a/CSharp/Plugins/UniversalEditor.Plugins.FileSystem/UniversalEditor.Plugins.FileSystem.csproj +++ b/CSharp/Plugins/UniversalEditor.Plugins.FileSystem/UniversalEditor.Plugins.FileSystem.csproj @@ -208,6 +208,7 @@ + diff --git a/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/DataFormats/Package/ContentTypes/OPCContentTypesDataFormat.cs b/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/DataFormats/Package/ContentTypes/OPCContentTypesDataFormat.cs new file mode 100644 index 00000000..5f49b3e9 --- /dev/null +++ b/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/DataFormats/Package/ContentTypes/OPCContentTypesDataFormat.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +using UniversalEditor.DataFormats.Markup.XML; +using UniversalEditor.ObjectModels.Markup; +using UniversalEditor.ObjectModels.Package.ContentTypes; + +namespace UniversalEditor.DataFormats.Package.ContentTypes +{ + public class OPCContentTypesDataFormat : XMLDataFormat + { + protected override void BeforeLoadInternal(Stack objectModels) + { + base.BeforeLoadInternal(objectModels); + objectModels.Push(new MarkupObjectModel()); + } + protected override void AfterLoadInternal(Stack objectModels) + { + base.AfterLoadInternal(objectModels); + + MarkupObjectModel mom = (objectModels.Pop() as MarkupObjectModel); + ContentTypesObjectModel types = (objectModels.Pop() as ContentTypesObjectModel); + + MarkupTagElement tagTypes = (mom.Elements["Types"] as MarkupTagElement); + foreach (MarkupElement elType in tagTypes.Elements) + { + MarkupTagElement tagType = (elType as MarkupTagElement); + if (elType == null) continue; + + ContentType type = new ContentType(); + MarkupAttribute attExtension = tagType.Attributes["Extension"]; + if (attExtension != null) type.Extension = attExtension.Value; + + MarkupAttribute attContentType = tagType.Attributes["ContentType"]; + if (attContentType != null) type.Value = attContentType.Value; + + switch (elType.FullName) + { + case "Default": + { + types.ContentTypes.Add(type); + break; + } + } + } + } + } +} diff --git a/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/DataFormats/Package/OpenPackagingConvention/OPCDataFormat.cs b/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/DataFormats/Package/OpenPackagingConvention/OPCDataFormat.cs new file mode 100644 index 00000000..bedf0c36 --- /dev/null +++ b/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/DataFormats/Package/OpenPackagingConvention/OPCDataFormat.cs @@ -0,0 +1,135 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +using UniversalEditor.Accessors; + +using UniversalEditor.ObjectModels.FileSystem; +using UniversalEditor.DataFormats.FileSystem.ZIP; + +using UniversalEditor.ObjectModels.Package; +using UniversalEditor.ObjectModels.Package.Relationships; + +using UniversalEditor.DataFormats.Package.Relationships; +using UniversalEditor.ObjectModels.Package.ContentTypes; +using UniversalEditor.DataFormats.Package.ContentTypes; + +namespace UniversalEditor.DataFormats.Package.OpenPackagingConvention +{ + public class OPCDataFormat : ZIPDataFormat + { + protected override void BeforeLoadInternal(Stack objectModels) + { + base.BeforeLoadInternal(objectModels); + objectModels.Push(new FileSystemObjectModel()); + } + protected override void AfterLoadInternal(Stack objectModels) + { + base.AfterLoadInternal(objectModels); + + FileSystemObjectModel fsom = (objectModels.Pop() as FileSystemObjectModel); + PackageObjectModel package = (objectModels.Pop() as PackageObjectModel); + + Folder _rels = fsom.Folders["_rels"]; + if (_rels != null) + { + foreach (File file in _rels.Files) + { + string relatedFileName = null; + if (file.Name.EndsWith(".rels") && file.Name != ".rels") + { + relatedFileName = file.Name.Substring(0, file.Name.Length - ".rels".Length); + } + + + byte[] data = file.GetData(); + + OPCRelationshipsDataFormat df = new OPCRelationshipsDataFormat(); + RelationshipsObjectModel rels = new RelationshipsObjectModel(); + Document.Load(rels, df, new MemoryAccessor(data)); + + if (relatedFileName != null) + { + + } + else + { + foreach (Relationship rel in rels.Relationships) + { + package.Relationships.Add(rel); + } + } + } + } + + File _Content_Types = fsom.Files["[Content_Types].xml"]; + { + byte[] data = _Content_Types.GetData(); + + OPCContentTypesDataFormat df = new OPCContentTypesDataFormat(); + ContentTypesObjectModel contentTypes = new ContentTypesObjectModel(); + Document.Load(contentTypes, df, new MemoryAccessor(data)); + + foreach (ContentType type in contentTypes.ContentTypes) + { + package.ContentTypes.Add(type); + } + } + + objectModels.Push(package); + } + + protected override void BeforeSaveInternal(Stack objectModels) + { + base.BeforeSaveInternal(objectModels); + + PackageObjectModel package = (objectModels.Pop() as PackageObjectModel); + FileSystemObjectModel fsom = new FileSystemObjectModel(); + + #region _rels + { + Folder fldr = new Folder(); + fldr.Name = "_rels"; + + File _rels = new File(); + _rels.Name = ".rels"; + + fldr.Files.Add(_rels); + + fsom.Folders.Add(fldr); + } + #endregion + #region Documents + { + Folder fldr = new Folder(); + fldr.Name = "Documents"; + fsom.Folders.Add(fldr); + } + #endregion + #region Metadata + { + Folder fldr = new Folder(); + fldr.Name = "Metadata"; + fsom.Folders.Add(fldr); + } + #endregion + #region [Content_Types].xml + { + File file = new File(); + file.Name = "[Content_Types].xml"; + fsom.Files.Add(file); + } + #endregion + #region FixedDocumentSequence.fdseq + { + File file = new File(); + file.Name = "FixedDocumentSequence.fdseq"; + fsom.Files.Add(file); + } + #endregion + + objectModels.Push(fsom); + } + } +} diff --git a/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/DataFormats/Package/Relationships/OPCRelationshipsDataFormat.cs b/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/DataFormats/Package/Relationships/OPCRelationshipsDataFormat.cs new file mode 100644 index 00000000..34a9be38 --- /dev/null +++ b/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/DataFormats/Package/Relationships/OPCRelationshipsDataFormat.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +using UniversalEditor.DataFormats.Markup.XML; +using UniversalEditor.ObjectModels.Markup; +using UniversalEditor.ObjectModels.Package.Relationships; + +namespace UniversalEditor.DataFormats.Package.Relationships +{ + public class OPCRelationshipsDataFormat : XMLDataFormat + { + protected override void BeforeLoadInternal(Stack objectModels) + { + base.BeforeLoadInternal(objectModels); + objectModels.Push(new MarkupObjectModel()); + } + protected override void AfterLoadInternal(Stack objectModels) + { + base.AfterLoadInternal(objectModels); + + MarkupObjectModel mom = (objectModels.Pop() as MarkupObjectModel); + RelationshipsObjectModel rels = (objectModels.Pop() as RelationshipsObjectModel); + + MarkupTagElement tagRelationships = (mom.Elements["Relationships"] as MarkupTagElement); + foreach (MarkupElement elRelationship in tagRelationships.Elements) + { + MarkupTagElement tagRelationship = (elRelationship as MarkupTagElement); + if (tagRelationship == null) continue; + if (tagRelationship.FullName != "Relationship") continue; + + Relationship rel = new Relationship(); + + MarkupAttribute attTarget = tagRelationship.Attributes["Target"]; + if (attTarget != null) rel.Target = attTarget.Value; + + MarkupAttribute attID = tagRelationship.Attributes["Id"]; + if (attID != null) rel.ID = attID.Value; + + MarkupAttribute attType = tagRelationship.Attributes["Type"]; + if (attType != null) rel.Schema = attType.Value; + + rels.Relationships.Add(rel); + } + } + } +} diff --git a/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/DataFormats/Text/Formatted/XPS/XPSDataFormat.cs b/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/DataFormats/Text/Formatted/XPS/XPSDataFormat.cs index 9ca09b53..63f0c7ad 100644 --- a/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/DataFormats/Text/Formatted/XPS/XPSDataFormat.cs +++ b/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/DataFormats/Text/Formatted/XPS/XPSDataFormat.cs @@ -20,14 +20,15 @@ // along with this program. If not, see . using System; -using UniversalEditor.DataFormats.FileSystem.ZIP; -using UniversalEditor.ObjectModels.FileSystem; +using UniversalEditor.DataFormats.Package.OpenPackagingConvention; + +using UniversalEditor.ObjectModels.Package; using UniversalEditor.ObjectModels.Text.Formatted; namespace UniversalEditor.DataFormats.Text.Formatted.XPS { - public class XPSDataFormat : ZIPDataFormat + public class XPSDataFormat : OPCDataFormat { private static DataFormatReference _dfr = null; protected override DataFormatReference MakeReferenceInternal() @@ -42,57 +43,25 @@ namespace UniversalEditor.DataFormats.Text.Formatted.XPS protected override void BeforeLoadInternal(System.Collections.Generic.Stack objectModels) { - objectModels.Push(new FileSystemObjectModel()); + objectModels.Push(new PackageObjectModel()); + base.BeforeLoadInternal(objectModels); } protected override void AfterLoadInternal(System.Collections.Generic.Stack objectModels) { - FileSystemObjectModel fsom = (objectModels.Pop() as FileSystemObjectModel); + base.AfterLoadInternal(objectModels); + + PackageObjectModel package = (objectModels.Pop() as PackageObjectModel); FormattedTextObjectModel text = (objectModels.Pop() as FormattedTextObjectModel); + } protected override void BeforeSaveInternal(System.Collections.Generic.Stack objectModels) { FormattedTextObjectModel text = (objectModels.Pop() as FormattedTextObjectModel); + + PackageObjectModel package = new PackageObjectModel(); - FileSystemObjectModel fsom = new FileSystemObjectModel(); - - #region _rels - { - Folder fldr = new Folder(); - fldr.Name = "_rels"; - fsom.Folders.Add(fldr); - } - #endregion - #region Documents - { - Folder fldr = new Folder(); - fldr.Name = "Documents"; - fsom.Folders.Add(fldr); - } - #endregion - #region Metadata - { - Folder fldr = new Folder(); - fldr.Name = "Metadata"; - fsom.Folders.Add(fldr); - } - #endregion - #region [Content_Types].xml - { - File file = new File(); - file.Name = "[Content_Types].xml"; - fsom.Files.Add(file); - } - #endregion - #region FixedDocumentSequence.fdseq - { - File file = new File(); - file.Name = "FixedDocumentSequence.fdseq"; - fsom.Files.Add(file); - } - #endregion - - objectModels.Push(fsom); + objectModels.Push(package); } } } diff --git a/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/ObjectModels/Package/ContentTypes/ContentType.cs b/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/ObjectModels/Package/ContentTypes/ContentType.cs new file mode 100644 index 00000000..fa7096ab --- /dev/null +++ b/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/ObjectModels/Package/ContentTypes/ContentType.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace UniversalEditor.ObjectModels.Package.ContentTypes +{ + public class ContentType : ICloneable + { + public class ContentTypeCollection + : System.Collections.ObjectModel.Collection + { + + } + + private string mvarExtension = String.Empty; + public string Extension { get { return mvarExtension; } set { mvarExtension = value; } } + + private string mvarValue = String.Empty; + public string Value { get { return mvarValue; } set { mvarValue = value; } } + + public override string ToString() + { + StringBuilder sb = new StringBuilder(); + sb.Append("*."); + sb.Append(mvarExtension); + sb.Append("; "); + sb.Append(mvarValue); + return sb.ToString(); + } + + public object Clone() + { + ContentType clone = new ContentType(); + clone.Value = (mvarValue.Clone() as string); + clone.Extension = (mvarExtension.Clone() as string); + return clone; + } + } +} diff --git a/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/ObjectModels/Package/ContentTypes/ContentTypesObjectModel.cs b/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/ObjectModels/Package/ContentTypes/ContentTypesObjectModel.cs new file mode 100644 index 00000000..dd03e271 --- /dev/null +++ b/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/ObjectModels/Package/ContentTypes/ContentTypesObjectModel.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace UniversalEditor.ObjectModels.Package.ContentTypes +{ + public class ContentTypesObjectModel : ObjectModel + { + private ContentType.ContentTypeCollection mvarContentTypes = new ContentType.ContentTypeCollection(); + public ContentType.ContentTypeCollection ContentTypes { get { return mvarContentTypes; } } + + public override void Clear() + { + mvarContentTypes.Clear(); + } + + public override void CopyTo(ObjectModel where) + { + ContentTypesObjectModel clone = (where as ContentTypesObjectModel); + if (clone == null) throw new ObjectModelNotSupportedException(); + + foreach (ContentType item in mvarContentTypes) + { + clone.ContentTypes.Add(item.Clone() as ContentType); + } + } + } +} diff --git a/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/ObjectModels/Package/PackageObjectModel.cs b/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/ObjectModels/Package/PackageObjectModel.cs new file mode 100644 index 00000000..7ed69df3 --- /dev/null +++ b/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/ObjectModels/Package/PackageObjectModel.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using UniversalEditor.ObjectModels.Package.ContentTypes; +using UniversalEditor.ObjectModels.Package.Relationships; + +namespace UniversalEditor.ObjectModels.Package +{ + public class PackageObjectModel : ObjectModel + { + + private ContentType.ContentTypeCollection mvarContentTypes = new ContentType.ContentTypeCollection(); + public ContentType.ContentTypeCollection ContentTypes { get { return mvarContentTypes; } } + + private Relationship.RelationshipCollection mvarRelationships = new Relationship.RelationshipCollection(); + public Relationship.RelationshipCollection Relationships { get { return mvarRelationships; } } + + public override void Clear() + { + mvarContentTypes.Clear(); + mvarRelationships.Clear(); + } + + public override void CopyTo(ObjectModel where) + { + PackageObjectModel clone = (where as PackageObjectModel); + if (clone == null) throw new ObjectModelNotSupportedException(); + + foreach (ContentType item in mvarContentTypes) + { + clone.ContentTypes.Add(item.Clone() as ContentType); + } + foreach (Relationship item in mvarRelationships) + { + clone.Relationships.Add(item.Clone() as Relationship); + } + } + } +} diff --git a/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/ObjectModels/Package/Relationships/Relationship.cs b/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/ObjectModels/Package/Relationships/Relationship.cs new file mode 100644 index 00000000..81e893d9 --- /dev/null +++ b/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/ObjectModels/Package/Relationships/Relationship.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace UniversalEditor.ObjectModels.Package.Relationships +{ + public class Relationship : ICloneable + { + public class RelationshipCollection + : System.Collections.ObjectModel.Collection + { + + } + + private string mvarID = String.Empty; + public string ID { get { return mvarID; } set { mvarID = value; } } + + private string mvarSchema = String.Empty; + public string Schema { get { return mvarSchema; } set { mvarSchema = value; } } + + private string mvarTarget = String.Empty; + public string Target { get { return mvarTarget; } set { mvarTarget = value; } } + + + + public object Clone() + { + Relationship clone = new Relationship(); + clone.ID = (mvarID.Clone() as string); + clone.Schema = (mvarSchema.Clone() as string); + clone.Target = (mvarTarget.Clone() as string); + return clone; + } + } +} diff --git a/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/ObjectModels/Package/Relationships/RelationshipsObjectModel.cs b/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/ObjectModels/Package/Relationships/RelationshipsObjectModel.cs new file mode 100644 index 00000000..a4b6913a --- /dev/null +++ b/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/ObjectModels/Package/Relationships/RelationshipsObjectModel.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace UniversalEditor.ObjectModels.Package.Relationships +{ + public class RelationshipsObjectModel : ObjectModel + { + private Relationship.RelationshipCollection mvarRelationships = new Relationship.RelationshipCollection(); + public Relationship.RelationshipCollection Relationships { get { return mvarRelationships; } } + + public override void Clear() + { + mvarRelationships.Clear(); + } + + public override void CopyTo(ObjectModel where) + { + RelationshipsObjectModel clone = (where as RelationshipsObjectModel); + foreach (Relationship item in mvarRelationships) + { + clone.Relationships.Add(item.Clone() as Relationship); + } + } + } +} diff --git a/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/UniversalEditor.Plugins.Microsoft.csproj b/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/UniversalEditor.Plugins.Microsoft.csproj index a678186f..185b1003 100644 --- a/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/UniversalEditor.Plugins.Microsoft.csproj +++ b/CSharp/Plugins/UniversalEditor.Plugins.Microsoft/UniversalEditor.Plugins.Microsoft.csproj @@ -77,6 +77,9 @@ + + + @@ -84,6 +87,11 @@ + + + + +