-
Notifications
You must be signed in to change notification settings - Fork 544
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add namespace generator to SDK (#1092)
* Add namespace generator to SDK * "" should be office 2007 * fix reflection using
- Loading branch information
1 parent
66dab96
commit 1238452
Showing
24 changed files
with
1,337 additions
and
419 deletions.
There are no files selected for viewing
660 changes: 660 additions & 0 deletions
660
gen/DocumentFormat.OpenXml.Generator/Data/namespaces.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
gen/DocumentFormat.OpenXml.Generator/Models/NamespaceInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace DocumentFormat.OpenXml.Generator.Models; | ||
|
||
public class NamespaceInfo | ||
{ | ||
public string Prefix { get; set; } = null!; | ||
|
||
public string Uri { get; set; } = null!; | ||
|
||
public OfficeVersion Version { get; set; } | ||
} |
14 changes: 14 additions & 0 deletions
14
gen/DocumentFormat.OpenXml.Generator/Models/OfficeVersion.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace DocumentFormat.OpenXml.Generator.Models; | ||
|
||
public enum OfficeVersion | ||
{ | ||
Office2007 = 0, | ||
Office2010 = 1, | ||
Office2013 = 2, | ||
Office2016 = 3, | ||
Office2019 = 4, | ||
Office2021 = 5, | ||
} |
23 changes: 23 additions & 0 deletions
23
gen/DocumentFormat.OpenXml.Generator/NamespaceGeneration/NamespaceGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Microsoft.CodeAnalysis; | ||
using System; | ||
|
||
namespace DocumentFormat.OpenXml.Generator.NamespaceGeneration; | ||
|
||
[Generator] | ||
internal class NamespaceGenerator : ISourceGenerator | ||
{ | ||
public void Execute(GeneratorExecutionContext context) | ||
{ | ||
if (string.Equals(context.Compilation.AssemblyName, "DocumentFormat.OpenXml", StringComparison.Ordinal)) | ||
{ | ||
context.AddSource("Namespaces", OpenXmlGeneratorContext.Shared.Namespaces.Generate()); | ||
} | ||
} | ||
|
||
public void Initialize(GeneratorInitializationContext context) | ||
{ | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
gen/DocumentFormat.OpenXml.Generator/NamespaceGeneration/NamespaceGeneratorExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using DocumentFormat.OpenXml.Generator.Models; | ||
using System.CodeDom.Compiler; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
|
||
namespace DocumentFormat.OpenXml.Generator.NamespaceGeneration; | ||
|
||
internal static class NamespaceGeneratorExtensions | ||
{ | ||
public static string Generate(this IEnumerable<NamespaceInfo> namespaces) | ||
{ | ||
var sb = new StringWriter(); | ||
var writer = new IndentedTextWriter(sb); | ||
|
||
writer.WriteFileHeader(); | ||
writer.WriteLine("using System.Collections.Generic;"); | ||
writer.WriteLine(); | ||
writer.WriteLine("namespace DocumentFormat.OpenXml.Features;"); | ||
writer.WriteLine(); | ||
writer.WriteLine("internal partial class OpenXmlNamespaceResolver"); | ||
|
||
using (writer.AddBlock()) | ||
{ | ||
writer.WriteLine("private readonly Dictionary<string, string> _urlToPrefix = new()"); | ||
|
||
using (writer.AddBlock(includeSemiColon: true)) | ||
{ | ||
foreach (var ns in namespaces) | ||
{ | ||
writer.WriteList(ns.Uri, ns.Prefix); | ||
writer.WriteLine(","); | ||
} | ||
} | ||
|
||
writer.WriteLineNoTabs(); | ||
writer.WriteLine("private readonly Dictionary<string, string> _prefixToUrl = new()"); | ||
|
||
using (writer.AddBlock(includeSemiColon: true)) | ||
{ | ||
foreach (var ns in namespaces) | ||
{ | ||
writer.WriteList(ns.Prefix, ns.Uri); | ||
writer.WriteLine(","); | ||
} | ||
} | ||
|
||
writer.WriteLineNoTabs(); | ||
writer.WriteLine("private readonly Dictionary<string, FileFormatVersions> _prefixToVersion = new()"); | ||
|
||
using (writer.AddBlock(includeSemiColon: true)) | ||
{ | ||
foreach (var ns in namespaces) | ||
{ | ||
writer.WriteList(ns.Prefix, ns.Version); | ||
|
||
writer.WriteLine(","); | ||
} | ||
} | ||
} | ||
|
||
return sb.ToString(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
gen/DocumentFormat.OpenXml.Generator/OpenXmlGeneratorContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using DocumentFormat.OpenXml.Generator.Models; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Converters; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
|
||
namespace DocumentFormat.OpenXml.Generator; | ||
|
||
public record OpenXmlGeneratorContext | ||
{ | ||
private static readonly JsonSerializer _serializer = JsonSerializer.Create(new() | ||
{ | ||
Converters = new[] | ||
{ | ||
new StringEnumConverter(), | ||
}, | ||
}); | ||
|
||
public static OpenXmlGeneratorContext Shared { get; } = Load(); | ||
|
||
public IEnumerable<NamespaceInfo> Namespaces { get; init; } = Enumerable.Empty<NamespaceInfo>(); | ||
|
||
public static OpenXmlGeneratorContext Load() | ||
=> new() | ||
{ | ||
Namespaces = Load<NamespaceInfo[]>("namespaces.json"), | ||
}; | ||
|
||
private static T Load<T>(string name) | ||
{ | ||
using var stream = typeof(OpenXmlGeneratorContext).Assembly.GetManifestResourceStream(typeof(OpenXmlGeneratorContext), $"Data.{name}"); | ||
|
||
if (stream is null) | ||
{ | ||
throw new InvalidOperationException($"Could not find stream '{name}'"); | ||
} | ||
|
||
using var text = new StreamReader(stream); | ||
using var reader = new JsonTextReader(text); | ||
|
||
return _serializer.Deserialize<T>(reader)!; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace DocumentFormat.OpenXml.Generator; | ||
|
||
internal readonly struct Verbatim | ||
{ | ||
public Verbatim(string value) | ||
{ | ||
Value = value; | ||
} | ||
|
||
public string Value { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/DocumentFormat.OpenXml/Features/IOpenXmlNamespaceIdResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace DocumentFormat.OpenXml.Features | ||
{ | ||
internal interface IOpenXmlNamespaceIdResolver | ||
{ | ||
string GetPrefix(byte id); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/DocumentFormat.OpenXml/Features/IOpenXmlNamespaceResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using DocumentFormat.OpenXml.Framework; | ||
using System.Xml; | ||
|
||
namespace DocumentFormat.OpenXml.Features | ||
{ | ||
internal interface IOpenXmlNamespaceResolver : IXmlNamespaceResolver | ||
{ | ||
FileFormatVersions GetVersion(OpenXmlNamespace ns); | ||
|
||
/// <summary> | ||
/// Attempts to get the Transitional equivalent namespace. | ||
/// </summary> | ||
/// <param name="ns">Namespace to compare.</param> | ||
/// <param name="transitionalNamespace">An equivalent namespace in Transitional.</param> | ||
/// <returns>Returns true when a Transitional equivalent namespace is found, returns false when it is not found.</returns> | ||
bool TryGetTransitionalNamespace(OpenXmlNamespace ns, out OpenXmlNamespace transitionalNamespace); | ||
|
||
/// <summary> | ||
/// Attempts to get the Transitional equivalent relationship. | ||
/// </summary> | ||
/// <param name="ns">Namespace to compare.</param> | ||
/// <param name="transitionalRelationship">An equivalent relationship in Transitional.</param> | ||
/// <returns>Returns true when a Transitional equivalent relationship is found, returns false when it is not.</returns> | ||
bool TryGetTransitionalRelationship(OpenXmlNamespace ns, out OpenXmlNamespace transitionalRelationship); | ||
|
||
/// <summary> | ||
/// Try to get the expected namespace if the passed namespace is an obsolete. | ||
/// </summary> | ||
/// <param name="ns">Namespace to compare.</param> | ||
/// <param name="extNamespaceUri">The expected namespace when the passed namespace is an obsolete.</param> | ||
/// <returns>True when the passed namespace is an obsolete and the expected namespace found</returns> | ||
bool TryGetExtendedNamespace(OpenXmlNamespace ns, out OpenXmlNamespace extNamespaceUri); | ||
} | ||
} |
Oops, something went wrong.