Skip to content

Commit 074d278

Browse files
committed
patch WorkbookRels
1 parent da5577b commit 074d278

20 files changed

Lines changed: 65 additions & 24 deletions

src/DeterministicIoPackaging/DeterministicPackage.cs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,14 @@ static void DuplicateEntry(Entry sourceEntry, Archive targetArchive)
6969

7070
if (IsRelationships(sourceEntry))
7171
{
72-
var xml = PatchRelationships(sourceStream);
72+
var xml = PatchRelationships(sourceStream, true);
73+
SaveXml(xml, targetStream);
74+
return;
75+
}
76+
77+
if (IsWorkbookRelationships(sourceEntry))
78+
{
79+
var xml = PatchRelationships(sourceStream, false);
7380
SaveXml(xml, targetStream);
7481
return;
7582
}
@@ -96,7 +103,14 @@ static async Task DuplicateEntryAsync(Entry sourceEntry, Archive targetArchive,
96103
using var targetStream = await targetEntry.OpenAsync(cancel);
97104
if (IsRelationships(sourceEntry))
98105
{
99-
var xml = PatchRelationships(sourceStream);
106+
var xml = PatchRelationships(sourceStream, true);
107+
await SaveXml(xml, targetStream, cancel);
108+
return;
109+
}
110+
111+
if (IsWorkbookRelationships(sourceEntry))
112+
{
113+
var xml = PatchRelationships(sourceStream, false);
100114
await SaveXml(xml, targetStream, cancel);
101115
return;
102116
}
@@ -111,21 +125,21 @@ static async Task DuplicateEntryAsync(Entry sourceEntry, Archive targetArchive,
111125
await sourceStream.CopyToAsync(targetStream, cancel);
112126
}
113127

114-
static Task SaveXml(XDocument xml, Stream targetStream, Cancel cancel) =>
128+
static Task SaveXml(XDocument xml, Stream targetStream, Cancel cancel) =>
115129
xml.SaveAsync(targetStream, SaveOptions.DisableFormatting, cancel);
116130

117-
static void SaveXml(XDocument xml, Stream targetStream) =>
131+
static void SaveXml(XDocument xml, Stream targetStream) =>
118132
xml.Save(targetStream, SaveOptions.DisableFormatting);
119133

120134
static XName relationshipName = XName.Get("Relationship", "http://schemas.openxmlformats.org/package/2006/relationships");
121135

122-
internal static XDocument PatchRelationships(Stream sourceStream)
136+
static XDocument PatchRelationships(Stream sourceStream, bool patchIds)
123137
{
124138
var xml = XDocument.Load(sourceStream);
125-
return PatchRelationships(xml);
139+
return PatchRelationships(xml, patchIds);
126140
}
127141

128-
internal static XDocument PatchRelationships(XDocument xml)
142+
internal static XDocument PatchRelationships(XDocument xml, bool patchIds)
129143
{
130144
var root = xml.Root!;
131145
var relationships = root.Elements(relationshipName)
@@ -135,10 +149,13 @@ internal static XDocument PatchRelationships(XDocument xml)
135149

136150
root.Elements(relationshipName).Remove();
137151

138-
for (var index = 0; index < relationships.Count; index++)
152+
if (patchIds)
139153
{
140-
var relationship = relationships[index];
141-
relationship.Attribute("Id")!.SetValue($"DeterministicId{index + 1}");
154+
for (var index = 0; index < relationships.Count; index++)
155+
{
156+
var relationship = relationships[index];
157+
relationship.Attribute("Id")!.SetValue($"DeterministicId{index + 1}");
158+
}
142159
}
143160

144161
root.Add(relationships);
@@ -179,7 +196,10 @@ static bool IsPsmdcp(Entry entry) =>
179196
entry.Name.EndsWith("psmdcp");
180197

181198
static bool IsRelationships(Entry _) =>
182-
_.FullName == "_rels/.rels";
199+
_.FullName is "_rels/.rels";
200+
201+
static bool IsWorkbookRelationships(Entry _) =>
202+
_.FullName is "xl/_rels/workbook.xml.rels";
183203

184204
static bool IsWorkbookXml(Entry _) =>
185205
_.FullName == "xl/workbook.xml";
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
2+
<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml" />
3+
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml" />
4+
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml" />
5+
</Relationships>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
2+
<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml" />
3+
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml" />
4+
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml" />
5+
</Relationships>

src/Tests/PatchRelationshipsTests.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,24 @@ public Task Run()
1212
<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>
1313
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>
1414
</Relationships>
15+
""");
16+
DeterministicPackage.PatchRelationships(xml, true);
1517

18+
return Verify(xml);
19+
}
20+
[Test]
21+
public Task RunWithoutIdPatch()
22+
{
23+
var xml = XDocument.Parse(
24+
"""
25+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
26+
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
27+
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>
28+
<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>
29+
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>
30+
</Relationships>
1631
""");
17-
DeterministicPackage.PatchRelationships(xml);
32+
DeterministicPackage.PatchRelationships(xml, false);
1833

1934
return Verify(xml);
2035
}
-1 Bytes
Binary file not shown.
-2 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/calcChain" Target="calcChain.xml" /><Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet2.xml" /><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml" /><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml" /><Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml" /><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml" /></Relationships>
1+
<?xml version="1.0" encoding="utf-8" standalone="yes"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/calcChain" Target="calcChain.xml" /><Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml" /><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml" /><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml" /><Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet2.xml" /><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml" /></Relationships>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/calcChain" Target="calcChain.xml" /><Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet2.xml" /><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml" /><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml" /><Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml" /><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml" /></Relationships>
1+
<?xml version="1.0" encoding="utf-8" standalone="yes"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/calcChain" Target="calcChain.xml" /><Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml" /><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml" /><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml" /><Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet2.xml" /><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml" /></Relationships>
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/><Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml"/></Relationships>
1+
<?xml version="1.0" encoding="utf-8" standalone="yes"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml" /><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml" /><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml" /><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml" /></Relationships>
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/><Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml"/></Relationships>
1+
<?xml version="1.0" encoding="utf-8" standalone="yes"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml" /><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml" /><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml" /><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml" /></Relationships>

0 commit comments

Comments
 (0)