Skip to content

Commit

Permalink
Add a RootPart property to OpenXmlPackage (#661)
Browse files Browse the repository at this point in the history
  • Loading branch information
twsouthwick committed Jan 10, 2020
1 parent ce5b769 commit ffa7596
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Include symbols support for debugging (#650)
- Exposed `IXmlNamespaceResolver` from `XmlPath` instead of formatted list of strings to expose namespace/prefix mapping (#536)
- Implemented `IComparable<T>` and `IEquatable<T>` on `OpenXmlComparableSimpleValue` to allow comparisons without boxing (#550)
- Added `OpenXmlPackage.RootPart` to easily access the root part on any package (#661)

### Changes
- Updated to v4.7.0 of System.IO.Packaging which brings in a number of perf fixes (#660)
Expand Down
5 changes: 5 additions & 0 deletions src/DocumentFormat.OpenXml/Packaging/OpenXmlPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ protected OpenXmlPackage()
{
}

/// <summary>
/// Gets the root part for the package.
/// </summary>
public virtual OpenXmlPart RootPart => throw new InvalidDataException(ExceptionMessages.UnknownPackage);

/// <summary>
/// Initializes a new instance of the OpenXmlPackage class using the supplied Open XML package.
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions src/DocumentFormat.OpenXml/Packaging/PresentationDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,9 @@ public WebExTaskpanesPart AddWebExTaskpanesPart()
return childPart;
}

/// <inheritdoc />
public override OpenXmlPart RootPart => PresentationPart;

/// <summary>
/// Gets the PresentationPart of the PresentationDocument.
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions src/DocumentFormat.OpenXml/Packaging/SpreadsheetDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,9 @@ public WebExTaskpanesPart AddWebExTaskpanesPart()
return childPart;
}

/// <inheritdoc />
public override OpenXmlPart RootPart => WorkbookPart;

/// <summary>
/// Gets the WorkbookPart of the SpreadsheetDocument.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,9 @@ public WebExTaskpanesPart AddWebExTaskpanesPart()
return childPart;
}

/// <inheritdoc />
public override OpenXmlPart RootPart => MainDocumentPart;

/// <summary>
/// Gets the MainDocumentPart of the WordprocessingDocument.
/// </summary>
Expand Down
27 changes: 1 addition & 26 deletions src/DocumentFormat.OpenXml/Validation/DocumentValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,37 +131,12 @@ private void ValidatePart(OpenXmlPart part, ValidationContext context)
}
}

private static OpenXmlPart GetMainPart(OpenXmlPackage package)
{
if (package is null)
{
throw new ArgumentNullException(nameof(package));
}

if (package is WordprocessingDocument word)
{
return word.MainDocumentPart;
}
else if (package is SpreadsheetDocument spreadsheet)
{
return spreadsheet.WorkbookPart;
}
else if (package is PresentationDocument presentation)
{
return presentation.PresentationPart;
}
else
{
throw new System.IO.InvalidDataException(ExceptionMessages.UnknownPackage);
}
}

/// <summary>
/// Gets all the parts needs to be validated.
/// </summary>
private IEnumerable<OpenXmlPart> PartsToBeValidated(OpenXmlPackage package)
{
var mainPart = GetMainPart(package);
var mainPart = package.RootPart;
if (mainPart != null)
{
var parts = new Dictionary<OpenXmlPart, bool>();
Expand Down

0 comments on commit ffa7596

Please sign in to comment.