Skip to content

Commit

Permalink
DocumentFormat.OpenXml 3.0 beta 3 (IPackage) (#63)
Browse files Browse the repository at this point in the history
* feat: DocumentFormat.OpenXml (2.20)

* fix: PR to net6 branch

* fix: build errors

* fix: some errors

* feat: update dependencies

* feat: SixLabors.ImageSharp.Drawing (1.0)

* fix: MarkupCompatibilityProcessMode.NoProcess, // Otherwise OpenXml delete custom `pt14:Unid` attributes on save

* fix: null check issue due to class to struct convertion

* fix: deps update

* fix: dotnet version for docs

* feat: DocumentFormat.OpenXml 3.0.0-beta0003

* feat: ImageSharp v3

* Revert "feat: ImageSharp v3"

This reverts commit 59c2fda.
  • Loading branch information
sergey-tihon authored Sep 7, 2023
1 parent 4621959 commit 0ff1848
Show file tree
Hide file tree
Showing 25 changed files with 449 additions and 492 deletions.
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
]
},
"fake-cli": {
"version": "5.20.4",
"version": "6.0.0",
"commands": [
"fake"
]
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
pull_request:
branches:
- master
- net6

jobs:
build:
Expand All @@ -15,7 +16,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
dotnet: [6.0.401]
dotnet: [6.0.407]
runs-on: ${{ matrix.os }}

steps:
Expand Down Expand Up @@ -47,7 +48,7 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 6.0.401
dotnet-version: 6.0.407
- name: Install local tools
run: dotnet tool restore
- name: Paket Restore
Expand Down
5 changes: 2 additions & 3 deletions Clippit.Tests/Word/WmlComparerTests2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ public void CZ001_CompareTrackedInPrev(string testId, string name1, string name2

var source1Wml = new WmlDocument(source1CopiedToDestDocx.FullName);
var source2Wml = new WmlDocument(source2CopiedToDestDocx.FullName);
var settings = new WmlComparerSettings();
settings.DebugTempFileDi = thisTestTempDir;
var settings = new WmlComparerSettings { DebugTempFileDi = thisTestTempDir };
var comparedWml = WmlComparer.Compare(source1Wml, source2Wml, settings);

///////////////////////////
Expand All @@ -90,7 +89,7 @@ public void CZ001_CompareTrackedInPrev(string testId, string name1, string name2
{
var validator = new OpenXmlValidator();
var errors = validator.Validate(wDoc).Where(e => !ExpectedErrors.Contains(e.Description));
if (errors.Count() > 0)
if (errors.Any())
{

var ind = " ";
Expand Down
1 change: 1 addition & 0 deletions Clippit.Tests/paket.references
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
DocumentFormat.OpenXml
DocumentFormat.OpenXml.Framework
IDisposableAnalyzers
Microsoft.NET.Test.Sdk
xunit
Expand Down
1 change: 1 addition & 0 deletions Clippit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
paket.dependencies = paket.dependencies
README.md = README.md
RELEASE_NOTES.md = RELEASE_NOTES.md
build.fsx = build.fsx
EndProjectSection
EndProject
Global
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private static WmlDocument PreProcessMarkup(WmlDocument source, int startingIdFo
var os = new OpenSettings
{
MarkupCompatibilityProcessSettings = new MarkupCompatibilityProcessSettings(
MarkupCompatibilityProcessMode.ProcessAllParts,
MarkupCompatibilityProcessMode.NoProcess, // Otherwise OpenXml delete custom `pt14:Unid` attributes on save
FileFormatVersions.Office2007)
};

Expand Down
15 changes: 7 additions & 8 deletions Clippit/Comparer/WmlComparer.Private.Methods.ProduceDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Text;
using System.Xml.Linq;
using Clippit.Word;
using DocumentFormat.OpenXml.Experimental;
using DocumentFormat.OpenXml.Packaging;

namespace Clippit
Expand Down Expand Up @@ -846,9 +847,7 @@ private static object MarkContentAsDeletedOrInsertedTransform(XNode node, WmlCom
}
else if (status == "Inserted")
{
var rPr = pPr.Element(W.rPr);
if (rPr == null)
rPr = new XElement(W.rPr);
var rPr = pPr.Element(W.rPr) ?? new XElement(W.rPr);
rPr.Add(new XElement(W.ins,
new XAttribute(W.author, settings.AuthorForRevisions),
new XAttribute(W.id, s_maxId++),
Expand Down Expand Up @@ -892,7 +891,7 @@ private static void IgnorePt14Namespace(XElement root)
}

var ignorable = (string) root.Attribute(MC.Ignorable);
if (ignorable != null)
if (ignorable is not null)
{
var list = ignorable.Split(' ');
if (!list.Contains("pt14"))
Expand Down Expand Up @@ -2756,8 +2755,8 @@ private static object CoalesceRecurse(
var openXmlPartInNewDocument = part;
return gc.Select(gce =>
{
var packageOfDeletedContent = openXmlPartOfDeletedContent.OpenXmlPackage.Package;
var packageOfNewContent = openXmlPartInNewDocument.OpenXmlPackage.Package;
var packageOfDeletedContent = openXmlPartOfDeletedContent.OpenXmlPackage.GetPackage();
var packageOfNewContent = openXmlPartInNewDocument.OpenXmlPackage.GetPackage();
var partInDeletedDocument = packageOfDeletedContent.GetPart(part.Uri);
var partInNewDocument = packageOfNewContent.GetPart(part.Uri);

Expand All @@ -2781,8 +2780,8 @@ private static object CoalesceRecurse(
var openXmlPartInNewDocument = part;
return gc.Select(gce =>
{
var packageOfSourceContent = openXmlPartOfInsertedContent.OpenXmlPackage.Package;
var packageOfNewContent = openXmlPartInNewDocument.OpenXmlPackage.Package;
var packageOfSourceContent = openXmlPartOfInsertedContent.OpenXmlPackage.GetPackage();
var packageOfNewContent = openXmlPartInNewDocument.OpenXmlPackage.GetPackage();
var partInDeletedDocument = packageOfSourceContent.GetPart(part.Uri);
var partInNewDocument = packageOfNewContent.GetPart(part.Uri);

Expand Down
19 changes: 10 additions & 9 deletions Clippit/Comparer/WmlComparer.Private.Methods.Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
using System.Linq;
using System.Xml.Linq;
using Clippit.Internal;
using DocumentFormat.OpenXml.Packaging;

namespace Clippit
{
public static partial class WmlComparer
{
private static XElement MoveRelatedPartsToDestination(
PackagePart partOfDeletedContent,
PackagePart partInNewDocument,
IPackagePart partOfDeletedContent,
IPackagePart partInNewDocument,
XElement contentElement)
{
var elementsToUpdate = contentElement
Expand All @@ -34,7 +35,7 @@ private static XElement MoveRelatedPartsToDestination(
{
var rId = (string) att;

var relationshipForDeletedPart = partOfDeletedContent.GetRelationship(rId);
var relationshipForDeletedPart = partOfDeletedContent.Relationships[rId];

var targetUri = PackUriHelper
.ResolvePartUri(
Expand All @@ -60,30 +61,30 @@ private static XElement MoveRelatedPartsToDestination(
? new Uri(uriString, UriKind.Absolute)
: new Uri(uriString, UriKind.Relative);

var newPart = partInNewDocument.Package.CreatePart(uri, relatedPackagePart.ContentType);
var newPart = partInNewDocument.Package.CreatePart(uri, relatedPackagePart.ContentType, CompressionOption.Normal);

// ReSharper disable once PossibleNullReferenceException
using (var oldPartStream = relatedPackagePart.GetStream())
using (var newPartStream = newPart.GetStream())
using (var oldPartStream = relatedPackagePart.GetStream(FileMode.Open, FileAccess.Read))
using (var newPartStream = newPart.GetStream(FileMode.Create, FileAccess.Write))
{
oldPartStream.CopyTo(newPartStream);
}

var newRid = Relationships.GetNewRelationshipId();
partInNewDocument.CreateRelationship(newPart.Uri, TargetMode.Internal,
partInNewDocument.Relationships.Create(newPart.Uri, TargetMode.Internal,
relationshipForDeletedPart.RelationshipType, newRid);
att.Value = newRid;

if (newPart.ContentType.EndsWith("xml"))
{
XDocument newPartXDoc;
using (var stream = newPart.GetStream())
using (var stream = newPart.GetStream(FileMode.Open, FileAccess.Read))
{
newPartXDoc = XDocument.Load(stream);
MoveRelatedPartsToDestination(relatedPackagePart, newPart, newPartXDoc.Root);
}

using (var stream = newPart.GetStream())
using (var stream = newPart.GetStream(FileMode.Create, FileAccess.Write))
newPartXDoc.Save(stream);
}
}
Expand Down
5 changes: 3 additions & 2 deletions Clippit/Comparer/WmlComparer.Public.Methods.Consolidate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Text;
using System.Xml.Linq;
using Clippit.Word;
using DocumentFormat.OpenXml.Experimental;
using DocumentFormat.OpenXml.Packaging;

namespace Clippit
Expand Down Expand Up @@ -873,8 +874,8 @@ private static void AddToAnnotation(
ConsolidationInfo consolidationInfo,
WmlComparerSettings settings)
{
var packageOfDeletedContent = wDocDelta.MainDocumentPart.OpenXmlPackage.Package;
var packageOfNewContent = consolidatedWDoc.MainDocumentPart.OpenXmlPackage.Package;
var packageOfDeletedContent = wDocDelta.MainDocumentPart.OpenXmlPackage.GetPackage();
var packageOfNewContent = consolidatedWDoc.MainDocumentPart.OpenXmlPackage.GetPackage();
var partInDeletedDocument = packageOfDeletedContent.GetPart(wDocDelta.MainDocumentPart.Uri);
var partInNewDocument = packageOfNewContent.GetPart(consolidatedWDoc.MainDocumentPart.Uri);
consolidationInfo.RevisionElement =
Expand Down
16 changes: 9 additions & 7 deletions Clippit/MetricsGetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Packaging;
using System.Linq;
using System.Text;
using System.Xml.Linq;
Expand All @@ -15,6 +14,7 @@
using Clippit.PowerPoint;
using Clippit.Word;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Experimental;
using SixLabors.Fonts;

namespace Clippit
Expand Down Expand Up @@ -178,9 +178,10 @@ private static XElement GetWmlMetrics(string fileName, bool invalidHyperlink, Wo

private static XElement RetrieveContentTypeList(OpenXmlPackage oxPkg)
{
var pkg = oxPkg.Package;
var pkg = oxPkg.GetPackage();

var nonRelationshipParts = pkg.GetParts().Cast<ZipPackagePart>().Where(p => p.ContentType != "application/vnd.openxmlformats-package.relationships+xml");
var nonRelationshipParts = pkg.GetParts()
.Where(p => p.ContentType != "application/vnd.openxmlformats-package.relationships+xml");
var contentTypes = nonRelationshipParts
.Select(p => p.ContentType)
.OrderBy(t => t)
Expand All @@ -192,16 +193,17 @@ private static XElement RetrieveContentTypeList(OpenXmlPackage oxPkg)

private static XElement RetrieveNamespaceList(OpenXmlPackage oxPkg)
{
var pkg = oxPkg.Package;
var pkg = oxPkg.GetPackage();

var nonRelationshipParts = pkg.GetParts().Cast<ZipPackagePart>().Where(p => p.ContentType != "application/vnd.openxmlformats-package.relationships+xml");
var nonRelationshipParts = pkg.GetParts()
.Where(p => p.ContentType != "application/vnd.openxmlformats-package.relationships+xml");
var xmlParts = nonRelationshipParts
.Where(p => p.ContentType.ToLower().EndsWith("xml"));

var uniqueNamespaces = new HashSet<string>();
foreach (var xp in xmlParts)
{
using var st = xp.GetStream();
using var st = xp.GetStream(FileMode.Open, FileAccess.Read);
try
{
var xdoc = XDocument.Load(st);
Expand Down Expand Up @@ -450,7 +452,7 @@ private static void IncrementMetric(Dictionary<XName, int> metricCountDictionary
private static void ValidateImageExists(OpenXmlPart part, string relId, Dictionary<XName, int> metrics)
{
var imagePart = part.Parts.FirstOrDefault(ipp => ipp.RelationshipId == relId);
if (imagePart == null)
if (imagePart == default)
IncrementMetric(metrics, H.ReferenceToNullImage);
}

Expand Down
25 changes: 12 additions & 13 deletions Clippit/PowerPoint/FluentPresentationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Xml;
using System.Xml.Linq;
using Clippit.Internal;
using DocumentFormat.OpenXml.Experimental;
using DocumentFormat.OpenXml.Features;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Presentation;
using Path = System.IO.Path;
Expand Down Expand Up @@ -332,9 +334,8 @@ internal void AppendSlides(PresentationDocument sourceDocument, int start, int c
var newSlide = _newDocument.PresentationPart.AddNewPart<SlidePart>();

using (var sourceStream = slide.GetStream())
using (var targetStream = newSlide.GetStream(FileMode.Create, FileAccess.Write))
{
sourceStream.CopyTo(targetStream);
newSlide.FeedData(sourceStream);
}

var slideDocument = newSlide.GetXDocument();
Expand Down Expand Up @@ -656,8 +657,7 @@ private void CopyRelatedPartsForContentParts(OpenXmlPart oldContentPart, OpenXml
};
using (var oldObject = oldPart.GetStream(FileMode.Open, FileAccess.Read))
{
using var newObject = newPart.GetStream(FileMode.Create, FileAccess.ReadWrite);
oldObject.CopyTo(newObject);
newPart.FeedData(oldObject);
}
oleReference.Attribute(R.id).Value = newContentPart.GetIdOfPart(newPart);
}
Expand All @@ -676,7 +676,7 @@ private void CopyRelatedPartsForContentParts(OpenXmlPart oldContentPart, OpenXml
continue;

var oldPartIdPair2 = oldContentPart.Parts.FirstOrDefault(p => p.RelationshipId == relId);
if (oldPartIdPair2?.OpenXmlPart is ChartPart oldPart)
if (oldPartIdPair2.OpenXmlPart is ChartPart oldPart)
{
var oldChart = oldPart.GetXDocument();
var newPart = newContentPart.AddNewPart<ChartPart>();
Expand All @@ -695,7 +695,7 @@ private void CopyRelatedPartsForContentParts(OpenXmlPart oldContentPart, OpenXml
continue;

var oldPartIdPair2 = oldContentPart.Parts.FirstOrDefault(p => p.RelationshipId == relId);
if (oldPartIdPair2?.OpenXmlPart is ExtendedChartPart oldPart)
if (oldPartIdPair2.OpenXmlPart is ExtendedChartPart oldPart)
{
var oldChart = oldPart.GetXDocument();
var newPart = newContentPart.AddNewPart<ExtendedChartPart>();
Expand All @@ -714,7 +714,7 @@ private void CopyRelatedPartsForContentParts(OpenXmlPart oldContentPart, OpenXml
continue;

var oldPartIdPair3 = oldContentPart.Parts.FirstOrDefault(p => p.RelationshipId == relId);
if (oldPartIdPair3?.OpenXmlPart is ChartDrawingPart oldPart)
if (oldPartIdPair3.OpenXmlPart is ChartDrawingPart oldPart)
{
var oldXDoc = oldPart.GetXDocument();
var newPart = newContentPart.AddNewPart<ChartDrawingPart>();
Expand All @@ -733,7 +733,7 @@ private void CopyRelatedPartsForContentParts(OpenXmlPart oldContentPart, OpenXml
continue;

var oldPartIdPair4 = oldContentPart.Parts.FirstOrDefault(p => p.RelationshipId == relId);
if (oldPartIdPair4?.OpenXmlPart is UserDefinedTagsPart oldPart)
if (oldPartIdPair4.OpenXmlPart is UserDefinedTagsPart oldPart)
{
var oldXDoc = oldPart.GetXDocument();
var newPart = newContentPart.AddNewPart<UserDefinedTagsPart>();
Expand Down Expand Up @@ -884,8 +884,7 @@ private void CopyRelatedImage(OpenXmlPart oldContentPart, OpenXmlPart newContent
"image/svg+xml" => ".svg",
_ => ".image"
};
newContentPart.OpenXmlPackage.PartExtensionProvider
.MakeSurePartExtensionExist(contentType, targetExtension);
newContentPart.Features.GetRequired<IPartExtensionFeature>().Register(contentType, targetExtension);

var newPart = newContentPart switch
{
Expand Down Expand Up @@ -961,10 +960,10 @@ private void CopyRelatedImage(OpenXmlPart oldContentPart, OpenXmlPart newContent
}
else
{
var newPart = newContentPart.OpenXmlPackage.Package.GetParts().FirstOrDefault(p => p.Uri == newContentPart.Uri);
if (newPart?.RelationshipExists(relId) == false)
var newPart = newContentPart.OpenXmlPackage.GetPackage().GetParts().FirstOrDefault(p => p.Uri == newContentPart.Uri);
if (newPart is not null && !newPart.Relationships.Contains(relId))
{
newPart.CreateRelationship(new Uri("NULL", UriKind.RelativeOrAbsolute),
newPart.Relationships.Create(new Uri("NULL", UriKind.RelativeOrAbsolute),
System.IO.Packaging.TargetMode.Internal,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", relId);
}
Expand Down
Loading

0 comments on commit 0ff1848

Please sign in to comment.