├── .gitattributes ├── Documentation └── images │ ├── DocxToSourceIntro.gif │ ├── DocxToSourceSyntax.gif │ └── DocxToSourceNewLang.gif ├── .gitmodules ├── src ├── DocxToSource.Wpf │ ├── App.config │ ├── Properties │ │ ├── Settings.settings │ │ ├── Settings.Designer.cs │ │ ├── AssemblyInfo.cs │ │ ├── Resources.Designer.cs │ │ └── Resources.resx │ ├── App.xaml.cs │ ├── App.xaml │ ├── packages.config │ ├── MainWindow.xaml.cs │ ├── Languages │ │ ├── CSharpLanguageDefinition.cs │ │ ├── VBLanguageDefinition.cs │ │ ├── BooLanguageDefinition.cs │ │ └── LanguageDefinition.cs │ ├── Controls │ │ ├── OpenXmlPackageTreeViewItem.cs │ │ ├── OpenXmlTreeView.cs │ │ ├── OpenXmlPartTreeViewItem.cs │ │ ├── OpenXmlElementTreeViewItem.cs │ │ └── OpenXmlTreeViewItem.cs │ ├── MainWindow.xaml │ ├── DocxToSource.Wpf.csproj │ └── MainWindowModel.cs └── DocxToSource │ ├── App.xaml │ ├── App.xaml.cs │ ├── MainWindow.xaml.cs │ ├── AssemblyInfo.cs │ ├── DocxToSource.csproj │ ├── Languages │ ├── CSharpLanguageDefinition.cs │ ├── VBLanguageDefinition.cs │ └── LanguageDefinition.cs │ ├── Controls │ ├── OpenXmlPackageTreeViewItem.cs │ ├── OpenXmlTreeView.cs │ ├── OpenXmlPartTreeViewItem.cs │ ├── OpenXmlElementTreeViewItem.cs │ └── OpenXmlTreeViewItem.cs │ ├── MainWindow.xaml │ └── MainWindowModel.cs ├── .config └── dotnet-tools.json ├── .editorconfig ├── README.md ├── LICENSE ├── .vscode ├── launch.json └── tasks.json ├── DocxToSource.sln └── .gitignore /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /Documentation/images/DocxToSourceIntro.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rmboggs/DocxToSource/HEAD/Documentation/images/DocxToSourceIntro.gif -------------------------------------------------------------------------------- /Documentation/images/DocxToSourceSyntax.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rmboggs/DocxToSource/HEAD/Documentation/images/DocxToSourceSyntax.gif -------------------------------------------------------------------------------- /Documentation/images/DocxToSourceNewLang.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rmboggs/DocxToSource/HEAD/Documentation/images/DocxToSourceNewLang.gif -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | 2 | [submodule "src/Serialize.OpenXml.CodeGen"] 3 | path = src/Serialize.OpenXml.CodeGen 4 | url = https://github.com/Docx2Src/Serialize.OpenXml.CodeGen 5 | -------------------------------------------------------------------------------- /src/DocxToSource.Wpf/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.config/dotnet-tools.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": 1, 3 | "isRoot": true, 4 | "tools": { 5 | "cake.tool": { 6 | "version": "1.3.0", 7 | "commands": [ 8 | "dotnet-cake" 9 | ] 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /src/DocxToSource.Wpf/Properties/Settings.settings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # top-most EditorConfig file 2 | root = true 3 | 4 | # Unix-style newlines with a newline ending every file 5 | [*] 6 | end_of_line = lf 7 | trim_trailing_whitespace = true 8 | indent_style = space 9 | 10 | [*.ps1] 11 | indent_size = 2 12 | 13 | [*.{cs,cake,csproj}] 14 | indent_size = 4 15 | -------------------------------------------------------------------------------- /src/DocxToSource/App.xaml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /src/DocxToSource/App.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Configuration; 4 | using System.Data; 5 | using System.Linq; 6 | using System.Threading.Tasks; 7 | using System.Windows; 8 | 9 | namespace DocxToSource 10 | { 11 | /// 12 | /// Interaction logic for App.xaml 13 | /// 14 | public partial class App : Application 15 | { 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/DocxToSource.Wpf/App.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Configuration; 4 | using System.Data; 5 | using System.Linq; 6 | using System.Threading.Tasks; 7 | using System.Windows; 8 | 9 | namespace DocxToSource.Wpf 10 | { 11 | /// 12 | /// Interaction logic for App.xaml 13 | /// 14 | public partial class App : Application 15 | { 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/DocxToSource.Wpf/App.xaml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /src/DocxToSource/MainWindow.xaml.cs: -------------------------------------------------------------------------------- 1 | using ICSharpCode.AvalonEdit.Search; 2 | using System.Windows; 3 | 4 | namespace DocxToSource 5 | { 6 | /// 7 | /// Interaction logic for MainWindow.xaml 8 | /// 9 | public partial class MainWindow : Window 10 | { 11 | public MainWindow() 12 | { 13 | InitializeComponent(); 14 | SearchPanel.Install(this.xXmlSourceEditor); 15 | SearchPanel.Install(this.xCodeSourceEditor); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/DocxToSource/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Windows; 2 | 3 | [assembly:ThemeInfo( 4 | ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located 5 | //(used if a resource is not found in the page, 6 | // or application resource dictionaries) 7 | ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located 8 | //(used if a resource is not found in the page, 9 | // app, or any theme specific resource dictionaries) 10 | )] 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DocxToSource 2 | A multi-platform application that will be able to generate dotnet source code based on OpenXml based documents. This is still a work in progress. 3 | 4 | An initial mockup using WPF technologies is working, albeit not yet fully functional. Screenshots below.... 5 | 6 | Open an OpenXml based document: 7 | ![Open OpenXml based document](https://github.com/rmboggs/DocxToSource/blob/master/Documentation/images/DocxToSourceIntro.gif) 8 | 9 | Highlight Syntax: 10 | ![Highlight Syntax](https://github.com/rmboggs/DocxToSource/blob/master/Documentation/images/DocxToSourceSyntax.gif) 11 | 12 | Switch Language: 13 | ![Switch Language](https://github.com/rmboggs/DocxToSource/blob/master/Documentation/images/DocxToSourceNewLang.gif) 14 | -------------------------------------------------------------------------------- /src/DocxToSource.Wpf/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /src/DocxToSource.Wpf/MainWindow.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Windows; 7 | using System.Windows.Controls; 8 | using System.Windows.Data; 9 | using System.Windows.Documents; 10 | using System.Windows.Input; 11 | using System.Windows.Media; 12 | using System.Windows.Media.Imaging; 13 | using System.Windows.Navigation; 14 | using System.Windows.Shapes; 15 | 16 | namespace DocxToSource.Wpf 17 | { 18 | /// 19 | /// Interaction logic for MainWindow.xaml 20 | /// 21 | public partial class MainWindow : Window 22 | { 23 | public MainWindow() 24 | { 25 | InitializeComponent(); 26 | ICSharpCode.AvalonEdit.Search.SearchPanel.Install(this.xXmlSourceEditor); 27 | ICSharpCode.AvalonEdit.Search.SearchPanel.Install(this.xCodeSourceEditor); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Ryan Boggs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/DocxToSource.Wpf/Properties/Settings.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.42000 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace DocxToSource.Wpf.Properties 12 | { 13 | 14 | 15 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 16 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] 17 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase 18 | { 19 | 20 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); 21 | 22 | public static Settings Default 23 | { 24 | get 25 | { 26 | return defaultInstance; 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to find out which attributes exist for C# debugging 3 | // Use hover for the description of the existing attributes 4 | // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "name": ".NET Core Launch (console)", 9 | "type": "coreclr", 10 | "request": "launch", 11 | "preLaunchTask": "build", 12 | // If you have changed target frameworks, make sure to update the program path. 13 | "program": "${workspaceFolder}/src/DocxToSource.Avalonia/bin/Debug/netcoreapp3.0/DocxToSource.Avalonia.dll", 14 | "args": [], 15 | "cwd": "${workspaceFolder}/src/DocxToSource.Avalonia", 16 | // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console 17 | "console": "internalConsole", 18 | "stopAtEntry": false 19 | }, 20 | { 21 | "name": ".NET Core Attach", 22 | "type": "coreclr", 23 | "request": "attach", 24 | "processId": "${command:pickProcess}" 25 | } 26 | ] 27 | } -------------------------------------------------------------------------------- /src/DocxToSource/DocxToSource.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | WinExe 5 | net6.0-windows 6 | true 7 | MIT 8 | Ryan Boggs 9 | Ryan Boggs 10 | https://github.com/rmboggs/docxtosource 11 | https://github.com/rmboggs/docxtosource 12 | git 13 | openxml;DocumentFormat 14 | 0.1.0-alpha 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.0.0", 3 | "tasks": [ 4 | { 5 | "label": "build", 6 | "command": "dotnet", 7 | "type": "process", 8 | "args": [ 9 | "build", 10 | "${workspaceFolder}/src/DocxToSource.Avalonia/DocxToSource.Avalonia.csproj", 11 | "/property:GenerateFullPaths=true", 12 | "/consoleloggerparameters:NoSummary" 13 | ], 14 | "problemMatcher": "$msCompile" 15 | }, 16 | { 17 | "label": "publish", 18 | "command": "dotnet", 19 | "type": "process", 20 | "args": [ 21 | "publish", 22 | "${workspaceFolder}/src/DocxToSource.Avalonia/DocxToSource.Avalonia.csproj", 23 | "/property:GenerateFullPaths=true", 24 | "/consoleloggerparameters:NoSummary" 25 | ], 26 | "problemMatcher": "$msCompile" 27 | }, 28 | { 29 | "label": "watch", 30 | "command": "dotnet", 31 | "type": "process", 32 | "args": [ 33 | "watch", 34 | "run", 35 | "${workspaceFolder}/src/DocxToSource.Avalonia/DocxToSource.Avalonia.csproj", 36 | "/property:GenerateFullPaths=true", 37 | "/consoleloggerparameters:NoSummary" 38 | ], 39 | "problemMatcher": "$msCompile" 40 | } 41 | ] 42 | } -------------------------------------------------------------------------------- /src/DocxToSource/Languages/CSharpLanguageDefinition.cs: -------------------------------------------------------------------------------- 1 | /* MIT License 2 | 3 | Copyright (c) 2020 Ryan Boggs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 6 | software and associated documentation files (the "Software"), to deal in the Software 7 | without restriction, including without limitation the rights to use, copy, modify, 8 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 9 | permit persons to whom the Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all copies 13 | or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 16 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 17 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 18 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | using Microsoft.CSharp; 24 | 25 | namespace DocxToSource.Languages 26 | { 27 | /// 28 | /// Definition for the CSharp language. 29 | /// 30 | public class CSharpLanguageDefinition : LanguageDefinition 31 | { 32 | #region Public Constructors 33 | 34 | /// 35 | /// Initializes a new instance of the 36 | /// class that is empty. 37 | /// 38 | public CSharpLanguageDefinition() : base(new CSharpCodeProvider()) 39 | { 40 | DisplayName = "C#"; 41 | } 42 | 43 | #endregion 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/DocxToSource.Wpf/Languages/CSharpLanguageDefinition.cs: -------------------------------------------------------------------------------- 1 | /* MIT License 2 | 3 | Copyright (c) 2020 Ryan Boggs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 6 | software and associated documentation files (the "Software"), to deal in the Software 7 | without restriction, including without limitation the rights to use, copy, modify, 8 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 9 | permit persons to whom the Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all copies 13 | or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 16 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 17 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 18 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | using Microsoft.CSharp; 24 | 25 | namespace DocxToSource.Wpf.Languages 26 | { 27 | /// 28 | /// Definition for the CSharp language. 29 | /// 30 | public class CSharpLanguageDefinition : LanguageDefinition 31 | { 32 | #region Public Constructors 33 | 34 | /// 35 | /// Initializes a new instance of the 36 | /// class that is empty. 37 | /// 38 | public CSharpLanguageDefinition() : base(new CSharpCodeProvider()) 39 | { 40 | DisplayName = "C#"; 41 | } 42 | 43 | #endregion 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/DocxToSource/Languages/VBLanguageDefinition.cs: -------------------------------------------------------------------------------- 1 | /* MIT License 2 | 3 | Copyright (c) 2020 Ryan Boggs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 6 | software and associated documentation files (the "Software"), to deal in the Software 7 | without restriction, including without limitation the rights to use, copy, modify, 8 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 9 | permit persons to whom the Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all copies 13 | or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 16 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 17 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 18 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | using Microsoft.VisualBasic; 24 | 25 | namespace DocxToSource.Languages 26 | { 27 | /// 28 | /// Definition for the Visual Basic.net language. 29 | /// 30 | public class VBLanguageDefinition : LanguageDefinition 31 | { 32 | #region Public Constructors 33 | 34 | /// 35 | /// Initializes a new instance of the 36 | /// class that is empty. 37 | /// 38 | public VBLanguageDefinition() : base(new VBCodeProvider()) 39 | { 40 | DisplayName = "Visual Basic.Net"; 41 | } 42 | 43 | #endregion 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/DocxToSource.Wpf/Languages/VBLanguageDefinition.cs: -------------------------------------------------------------------------------- 1 | /* MIT License 2 | 3 | Copyright (c) 2020 Ryan Boggs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 6 | software and associated documentation files (the "Software"), to deal in the Software 7 | without restriction, including without limitation the rights to use, copy, modify, 8 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 9 | permit persons to whom the Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all copies 13 | or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 16 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 17 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 18 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | using Microsoft.VisualBasic; 24 | 25 | namespace DocxToSource.Wpf.Languages 26 | { 27 | /// 28 | /// Definition for the Visual Basic.net language. 29 | /// 30 | public class VBLanguageDefinition : LanguageDefinition 31 | { 32 | #region Public Constructors 33 | 34 | /// 35 | /// Initializes a new instance of the 36 | /// class that is empty. 37 | /// 38 | public VBLanguageDefinition() : base(new VBCodeProvider()) 39 | { 40 | DisplayName = "Visual Basic.Net"; 41 | } 42 | 43 | #endregion 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/DocxToSource.Wpf/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Resources; 3 | using System.Runtime.CompilerServices; 4 | using System.Runtime.InteropServices; 5 | using System.Windows; 6 | 7 | // General Information about an assembly is controlled through the following 8 | // set of attributes. Change these attribute values to modify the information 9 | // associated with an assembly. 10 | [assembly: AssemblyTitle("DocxToSource.Wpf")] 11 | [assembly: AssemblyDescription("")] 12 | [assembly: AssemblyConfiguration("")] 13 | [assembly: AssemblyCompany("")] 14 | [assembly: AssemblyProduct("DocxToSource.Wpf")] 15 | [assembly: AssemblyCopyright("Copyright © 2020")] 16 | [assembly: AssemblyTrademark("")] 17 | [assembly: AssemblyCulture("")] 18 | 19 | // Setting ComVisible to false makes the types in this assembly not visible 20 | // to COM components. If you need to access a type in this assembly from 21 | // COM, set the ComVisible attribute to true on that type. 22 | [assembly: ComVisible(false)] 23 | 24 | //In order to begin building localizable applications, set 25 | //CultureYouAreCodingWith in your .csproj file 26 | //inside a . For example, if you are using US english 27 | //in your source files, set the to en-US. Then uncomment 28 | //the NeutralResourceLanguage attribute below. Update the "en-US" in 29 | //the line below to match the UICulture setting in the project file. 30 | 31 | //[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)] 32 | 33 | 34 | [assembly: ThemeInfo( 35 | ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located 36 | //(used if a resource is not found in the page, 37 | // or application resource dictionaries) 38 | ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located 39 | //(used if a resource is not found in the page, 40 | // app, or any theme specific resource dictionaries) 41 | )] 42 | 43 | 44 | // Version information for an assembly consists of the following four values: 45 | // 46 | // Major Version 47 | // Minor Version 48 | // Build Number 49 | // Revision 50 | // 51 | // You can specify all the values or you can default the Build and Revision Numbers 52 | // by using the '*' as shown below: 53 | // [assembly: AssemblyVersion("1.0.*")] 54 | [assembly: AssemblyVersion("1.0.0.0")] 55 | [assembly: AssemblyFileVersion("1.0.0.0")] 56 | -------------------------------------------------------------------------------- /src/DocxToSource.Wpf/Languages/BooLanguageDefinition.cs: -------------------------------------------------------------------------------- 1 | /* MIT License 2 | 3 | Copyright (c) 2020 Ryan Boggs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 6 | software and associated documentation files (the "Software"), to deal in the Software 7 | without restriction, including without limitation the rights to use, copy, modify, 8 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 9 | permit persons to whom the Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all copies 13 | or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 16 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 17 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 18 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | using Boo.Lang.CodeDom; 24 | using Serialize.OpenXml.CodeGen; 25 | 26 | namespace DocxToSource.Wpf.Languages 27 | { 28 | /// 29 | /// Definition of the Boo language. 30 | /// 31 | public class BooLanguageDefinition : LanguageDefinition 32 | { 33 | #region Public Constructors 34 | 35 | /// 36 | /// Initializes a new instance of the class 37 | /// that is empty. 38 | /// 39 | public BooLanguageDefinition() 40 | : base(new BooNamespaceAliasOptions(), new BooCodeProvider()) 41 | { 42 | DisplayName = "Boo"; 43 | } 44 | 45 | #endregion 46 | 47 | /// 48 | /// Custom needed for the Boo language. 49 | /// 50 | private sealed class BooNamespaceAliasOptions : NamespaceAliasOptions 51 | { 52 | #region Public Constructors 53 | 54 | /// 55 | /// Initializes a new instance of the 56 | /// class that is empty. 57 | /// 58 | public BooNamespaceAliasOptions() : base() 59 | { 60 | Order = NamespaceAliasOrder.NamespaceFirst; 61 | AssignmentOperator = "as"; 62 | } 63 | 64 | #endregion 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/DocxToSource/Controls/OpenXmlPackageTreeViewItem.cs: -------------------------------------------------------------------------------- 1 | /* MIT License 2 | 3 | Copyright (c) 2020 Ryan Boggs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 6 | software and associated documentation files (the "Software"), to deal in the Software 7 | without restriction, including without limitation the rights to use, copy, modify, 8 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 9 | permit persons to whom the Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all copies 13 | or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 16 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 17 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 18 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | using DocumentFormat.OpenXml.Packaging; 24 | using Serialize.OpenXml.CodeGen; 25 | using System.CodeDom.Compiler; 26 | using System.Collections.Generic; 27 | using System.Linq; 28 | 29 | namespace DocxToSource.Controls 30 | { 31 | /// 32 | /// derived class that contains information 33 | /// about a given object. 34 | /// 35 | public class OpenXmlPackageTreeViewItem : OpenXmlTreeViewItem 36 | { 37 | #region Private Instance Fields 38 | 39 | /// 40 | /// Holds the OpenXmlPackage that is currently open. 41 | /// 42 | private readonly OpenXmlPackage _package; 43 | 44 | #endregion 45 | 46 | #region Public Constructors 47 | 48 | /// 49 | /// Initializes a new instance of the class 50 | /// that is empty. 51 | /// 52 | public OpenXmlPackageTreeViewItem(OpenXmlPackage pkg) 53 | : base(pkg.Parts != null && pkg.Parts.Any()) { _package = pkg; } 54 | 55 | #endregion 56 | 57 | #region Public Instance Methods 58 | 59 | /// 60 | public override string BuildCodeDomTextDocument(CodeDomProvider provider) 61 | { 62 | return _package.GenerateSourceCode(provider); 63 | } 64 | 65 | #endregion 66 | 67 | #region Protected Instance Methods 68 | 69 | /// 70 | protected override IEnumerable GetOpenXmlSubtreeItems() 71 | { 72 | return _package.Parts is null ? null : BuildOpenXmlPartTreeViewItems(_package.Parts); 73 | } 74 | 75 | #endregion 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/DocxToSource.Wpf/Controls/OpenXmlPackageTreeViewItem.cs: -------------------------------------------------------------------------------- 1 | /* MIT License 2 | 3 | Copyright (c) 2020 Ryan Boggs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 6 | software and associated documentation files (the "Software"), to deal in the Software 7 | without restriction, including without limitation the rights to use, copy, modify, 8 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 9 | permit persons to whom the Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all copies 13 | or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 16 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 17 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 18 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | using DocumentFormat.OpenXml.Packaging; 24 | using Serialize.OpenXml.CodeGen; 25 | using System.CodeDom.Compiler; 26 | using System.Collections.Generic; 27 | using System.Linq; 28 | 29 | namespace DocxToSource.Wpf.Controls 30 | { 31 | /// 32 | /// derived class that contains information 33 | /// about a given object. 34 | /// 35 | public class OpenXmlPackageTreeViewItem : OpenXmlTreeViewItem 36 | { 37 | #region Private Instance Fields 38 | 39 | /// 40 | /// Holds the OpenXmlPackage that is currently open. 41 | /// 42 | private readonly OpenXmlPackage _package; 43 | 44 | #endregion 45 | 46 | #region Public Constructors 47 | 48 | /// 49 | /// Initializes a new instance of the class 50 | /// that is empty. 51 | /// 52 | public OpenXmlPackageTreeViewItem(OpenXmlPackage pkg) 53 | : base(pkg.Parts != null && pkg.Parts.Count() > 0) { _package = pkg; } 54 | 55 | #endregion 56 | 57 | #region Public Instance Methods 58 | 59 | /// 60 | public override string BuildCodeDomTextDocument(CodeDomProvider provider) 61 | { 62 | return _package.GenerateSourceCode(provider); 63 | } 64 | 65 | #endregion 66 | 67 | #region Protected Instance Methods 68 | 69 | /// 70 | protected override IEnumerable GetOpenXmlSubtreeItems() 71 | { 72 | if (_package.Parts is null) return null; 73 | return BuildOpenXmlPartTreeViewItems(_package.Parts); 74 | } 75 | 76 | #endregion 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/DocxToSource.Wpf/Properties/Resources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.42000 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace DocxToSource.Wpf.Properties 12 | { 13 | 14 | 15 | /// 16 | /// A strongly-typed resource class, for looking up localized strings, etc. 17 | /// 18 | // This class was auto-generated by the StronglyTypedResourceBuilder 19 | // class via a tool like ResGen or Visual Studio. 20 | // To add or remove a member, edit your .ResX file then rerun ResGen 21 | // with the /str option, or rebuild your VS project. 22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] 23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 25 | internal class Resources 26 | { 27 | 28 | private static global::System.Resources.ResourceManager resourceMan; 29 | 30 | private static global::System.Globalization.CultureInfo resourceCulture; 31 | 32 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 33 | internal Resources() 34 | { 35 | } 36 | 37 | /// 38 | /// Returns the cached ResourceManager instance used by this class. 39 | /// 40 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 41 | internal static global::System.Resources.ResourceManager ResourceManager 42 | { 43 | get 44 | { 45 | if ((resourceMan == null)) 46 | { 47 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("DocxToSource.Wpf.Properties.Resources", typeof(Resources).Assembly); 48 | resourceMan = temp; 49 | } 50 | return resourceMan; 51 | } 52 | } 53 | 54 | /// 55 | /// Overrides the current thread's CurrentUICulture property for all 56 | /// resource lookups using this strongly typed resource class. 57 | /// 58 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 59 | internal static global::System.Globalization.CultureInfo Culture 60 | { 61 | get 62 | { 63 | return resourceCulture; 64 | } 65 | set 66 | { 67 | resourceCulture = value; 68 | } 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/DocxToSource/Controls/OpenXmlTreeView.cs: -------------------------------------------------------------------------------- 1 | /* MIT License 2 | 3 | Copyright (c) 2020 Ryan Boggs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 6 | software and associated documentation files (the "Software"), to deal in the Software 7 | without restriction, including without limitation the rights to use, copy, modify, 8 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 9 | permit persons to whom the Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all copies 13 | or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 16 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 17 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 18 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | using System.Windows; 24 | using System.Windows.Controls; 25 | 26 | namespace DocxToSource.Controls 27 | { 28 | /// 29 | /// A subclass of the default wpf designed 30 | /// specifically for objects from the DocumentFormat.OpenXml library. 31 | /// 32 | public class OpenXmlTreeView : TreeView 33 | { 34 | #region Public Static Fields 35 | 36 | /// 37 | /// Static dependency property handler for the SelectedTreeItem property. 38 | /// 39 | public static readonly DependencyProperty SelectedItem_Property = 40 | DependencyProperty.Register("SelectedTreeItem", typeof(object), 41 | typeof(OpenXmlTreeView), new UIPropertyMetadata(null)); 42 | 43 | #endregion 44 | 45 | #region Public Constructors 46 | 47 | /// 48 | /// Initializes a new instance of the class that 49 | /// is empty. 50 | /// 51 | public OpenXmlTreeView() : base() 52 | { 53 | SelectedItemChanged += 54 | new RoutedPropertyChangedEventHandler(MapSelecteditem); 55 | } 56 | 57 | #endregion 58 | 59 | #region Public Instance Properties 60 | 61 | /// 62 | /// Gets or sets the item currently selected in the tree. 63 | /// 64 | public object SelectedTreeItem 65 | { 66 | get => (object)GetValue(SelectedItem_Property); 67 | set => SetValue(SelectedItem_Property, value); 68 | } 69 | 70 | #endregion 71 | 72 | #region Private Instance Methods 73 | 74 | /// 75 | /// Maps the selected item object to the selected tree item property. 76 | /// 77 | /// 78 | /// Object sending event. 79 | /// 80 | /// 81 | /// Event args 82 | /// 83 | private void MapSelecteditem(object sender, RoutedPropertyChangedEventArgs e) 84 | { 85 | if (SelectedItem != null) 86 | { 87 | SetValue(SelectedItem_Property, SelectedItem); 88 | } 89 | } 90 | 91 | #endregion 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /DocxToSource.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.30320.27 5 | MinimumVisualStudioVersion = 15.0.26124.0 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serialize.OpenXml.CodeGen", "src\Serialize.OpenXml.CodeGen\src\Serialize.OpenXml.CodeGen\Serialize.OpenXml.CodeGen.csproj", "{37F5CAA1-EFBD-4339-A3C8-A55FFE7CA873}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DocxToSource", "src\DocxToSource\DocxToSource.csproj", "{36832DD8-0566-43DA-BD5A-EC9D0615C0D0}" 9 | EndProject 10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{32A4D3A0-1968-4F9D-A905-2E625C3F5CD0}" 11 | ProjectSection(SolutionItems) = preProject 12 | .editorconfig = .editorconfig 13 | .gitignore = .gitignore 14 | build.cake = build.cake 15 | LICENSE = LICENSE 16 | README.md = README.md 17 | EndProjectSection 18 | EndProject 19 | Global 20 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 21 | Debug|Any CPU = Debug|Any CPU 22 | Debug|x64 = Debug|x64 23 | Debug|x86 = Debug|x86 24 | Release|Any CPU = Release|Any CPU 25 | Release|x64 = Release|x64 26 | Release|x86 = Release|x86 27 | EndGlobalSection 28 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 29 | {37F5CAA1-EFBD-4339-A3C8-A55FFE7CA873}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 30 | {37F5CAA1-EFBD-4339-A3C8-A55FFE7CA873}.Debug|Any CPU.Build.0 = Debug|Any CPU 31 | {37F5CAA1-EFBD-4339-A3C8-A55FFE7CA873}.Debug|x64.ActiveCfg = Debug|Any CPU 32 | {37F5CAA1-EFBD-4339-A3C8-A55FFE7CA873}.Debug|x64.Build.0 = Debug|Any CPU 33 | {37F5CAA1-EFBD-4339-A3C8-A55FFE7CA873}.Debug|x86.ActiveCfg = Debug|Any CPU 34 | {37F5CAA1-EFBD-4339-A3C8-A55FFE7CA873}.Debug|x86.Build.0 = Debug|Any CPU 35 | {37F5CAA1-EFBD-4339-A3C8-A55FFE7CA873}.Release|Any CPU.ActiveCfg = Release|Any CPU 36 | {37F5CAA1-EFBD-4339-A3C8-A55FFE7CA873}.Release|Any CPU.Build.0 = Release|Any CPU 37 | {37F5CAA1-EFBD-4339-A3C8-A55FFE7CA873}.Release|x64.ActiveCfg = Release|Any CPU 38 | {37F5CAA1-EFBD-4339-A3C8-A55FFE7CA873}.Release|x64.Build.0 = Release|Any CPU 39 | {37F5CAA1-EFBD-4339-A3C8-A55FFE7CA873}.Release|x86.ActiveCfg = Release|Any CPU 40 | {37F5CAA1-EFBD-4339-A3C8-A55FFE7CA873}.Release|x86.Build.0 = Release|Any CPU 41 | {36832DD8-0566-43DA-BD5A-EC9D0615C0D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 42 | {36832DD8-0566-43DA-BD5A-EC9D0615C0D0}.Debug|Any CPU.Build.0 = Debug|Any CPU 43 | {36832DD8-0566-43DA-BD5A-EC9D0615C0D0}.Debug|x64.ActiveCfg = Debug|Any CPU 44 | {36832DD8-0566-43DA-BD5A-EC9D0615C0D0}.Debug|x64.Build.0 = Debug|Any CPU 45 | {36832DD8-0566-43DA-BD5A-EC9D0615C0D0}.Debug|x86.ActiveCfg = Debug|Any CPU 46 | {36832DD8-0566-43DA-BD5A-EC9D0615C0D0}.Debug|x86.Build.0 = Debug|Any CPU 47 | {36832DD8-0566-43DA-BD5A-EC9D0615C0D0}.Release|Any CPU.ActiveCfg = Release|Any CPU 48 | {36832DD8-0566-43DA-BD5A-EC9D0615C0D0}.Release|Any CPU.Build.0 = Release|Any CPU 49 | {36832DD8-0566-43DA-BD5A-EC9D0615C0D0}.Release|x64.ActiveCfg = Release|Any CPU 50 | {36832DD8-0566-43DA-BD5A-EC9D0615C0D0}.Release|x64.Build.0 = Release|Any CPU 51 | {36832DD8-0566-43DA-BD5A-EC9D0615C0D0}.Release|x86.ActiveCfg = Release|Any CPU 52 | {36832DD8-0566-43DA-BD5A-EC9D0615C0D0}.Release|x86.Build.0 = Release|Any CPU 53 | EndGlobalSection 54 | GlobalSection(SolutionProperties) = preSolution 55 | HideSolutionNode = FALSE 56 | EndGlobalSection 57 | GlobalSection(ExtensibilityGlobals) = postSolution 58 | SolutionGuid = {C8E14DAF-E20E-4E79-A2BD-412E5B801C94} 59 | EndGlobalSection 60 | EndGlobal 61 | -------------------------------------------------------------------------------- /src/DocxToSource.Wpf/Controls/OpenXmlTreeView.cs: -------------------------------------------------------------------------------- 1 | /* MIT License 2 | 3 | Copyright (c) 2020 Ryan Boggs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 6 | software and associated documentation files (the "Software"), to deal in the Software 7 | without restriction, including without limitation the rights to use, copy, modify, 8 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 9 | permit persons to whom the Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all copies 13 | or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 16 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 17 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 18 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | using System.Windows; 24 | using System.Windows.Controls; 25 | 26 | namespace DocxToSource.Wpf.Controls 27 | { 28 | /// 29 | /// A subclass of the default wpf designed 30 | /// specifically for objects from the DocumentFormat.OpenXml library. 31 | /// 32 | public class OpenXmlTreeView : TreeView 33 | { 34 | #region Public Static Fields 35 | 36 | /// 37 | /// Static dependency property handler for the SelectedTreeItem property. 38 | /// 39 | public static readonly DependencyProperty SelectedItem_Property = 40 | DependencyProperty.Register("SelectedTreeItem", typeof(object), 41 | typeof(OpenXmlTreeView), new UIPropertyMetadata(null)); 42 | 43 | #endregion 44 | 45 | #region Public Constructors 46 | 47 | /// 48 | /// Initializes a new instance of the class that 49 | /// is empty. 50 | /// 51 | public OpenXmlTreeView() : base() 52 | { 53 | this.SelectedItemChanged += 54 | new RoutedPropertyChangedEventHandler(MapSelecteditem); 55 | } 56 | 57 | #endregion 58 | 59 | #region Public Instance Properties 60 | 61 | /// 62 | /// Gets or sets the item currently selected in the tree. 63 | /// 64 | public object SelectedTreeItem 65 | { 66 | get { return (object)GetValue(SelectedItem_Property); } 67 | set { SetValue(SelectedItem_Property, value); } 68 | } 69 | 70 | #endregion 71 | 72 | #region Private Instance Methods 73 | 74 | /// 75 | /// Maps the selected item object to the selected tree item property. 76 | /// 77 | /// 78 | /// Object sending event. 79 | /// 80 | /// 81 | /// Event args 82 | /// 83 | private void MapSelecteditem(object sender, RoutedPropertyChangedEventArgs e) 84 | { 85 | if (SelectedItem != null) 86 | { 87 | SetValue(SelectedItem_Property, SelectedItem); 88 | } 89 | } 90 | 91 | #endregion 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/DocxToSource/Controls/OpenXmlPartTreeViewItem.cs: -------------------------------------------------------------------------------- 1 | /* MIT License 2 | 3 | Copyright (c) 2020 Ryan Boggs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 6 | software and associated documentation files (the "Software"), to deal in the Software 7 | without restriction, including without limitation the rights to use, copy, modify, 8 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 9 | permit persons to whom the Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all copies 13 | or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 16 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 17 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 18 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | using DocumentFormat.OpenXml.Packaging; 24 | using Serialize.OpenXml.CodeGen; 25 | using System; 26 | using System.CodeDom.Compiler; 27 | using System.Collections.Generic; 28 | using System.Linq; 29 | 30 | namespace DocxToSource.Controls 31 | { 32 | /// 33 | /// derived class that contains information 34 | /// about a given object. 35 | /// 36 | public class OpenXmlPartTreeViewItem : OpenXmlTreeViewItem 37 | { 38 | #region Private Instance Fields 39 | 40 | /// 41 | /// Holds the object for this instance. 42 | /// 43 | private readonly IdPartPair part; 44 | 45 | #endregion 46 | 47 | #region Public Constructors 48 | 49 | /// 50 | /// Initializes a new instance of the class 51 | /// with the referenced object. 52 | /// 53 | /// 54 | /// The object that the new instance will hold. 55 | /// 56 | /// 57 | /// is . 58 | /// 59 | public OpenXmlPartTreeViewItem(IdPartPair p) 60 | : base(p != null && p.OpenXmlPart != null && p.OpenXmlPart.Parts != null && 61 | (p.OpenXmlPart.Parts.Any() || p.OpenXmlPart.RootElement != null)) 62 | { 63 | part = p ?? throw new ArgumentNullException(nameof(p)); 64 | } 65 | 66 | #endregion 67 | 68 | #region Public Instance Methods 69 | 70 | /// 71 | public override string BuildCodeDomTextDocument(CodeDomProvider provider) 72 | { 73 | return part.OpenXmlPart.GenerateSourceCode(provider); 74 | } 75 | 76 | #endregion 77 | 78 | #region Protected Instance Methods 79 | 80 | /// 81 | protected override IEnumerable GetOpenXmlSubtreeItems() 82 | { 83 | List result = new(); 84 | 85 | result.AddRange(BuildOpenXmlPartTreeViewItems(part.OpenXmlPart.Parts)); 86 | if (part.OpenXmlPart.RootElement != null) 87 | { 88 | result.AddRange(BuildOpenXmlElementTreeViewItems(part.OpenXmlPart.RootElement)); 89 | } 90 | 91 | return result; 92 | } 93 | 94 | #endregion 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/DocxToSource.Wpf/Controls/OpenXmlPartTreeViewItem.cs: -------------------------------------------------------------------------------- 1 | /* MIT License 2 | 3 | Copyright (c) 2020 Ryan Boggs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 6 | software and associated documentation files (the "Software"), to deal in the Software 7 | without restriction, including without limitation the rights to use, copy, modify, 8 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 9 | permit persons to whom the Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all copies 13 | or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 16 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 17 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 18 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | using DocumentFormat.OpenXml.Packaging; 24 | using Serialize.OpenXml.CodeGen; 25 | using System; 26 | using System.CodeDom.Compiler; 27 | using System.Collections.Generic; 28 | using System.Linq; 29 | 30 | namespace DocxToSource.Wpf.Controls 31 | { 32 | /// 33 | /// derived class that contains information 34 | /// about a given object. 35 | /// 36 | public class OpenXmlPartTreeViewItem : OpenXmlTreeViewItem 37 | { 38 | #region Private Instance Fields 39 | 40 | /// 41 | /// Holds the object for this instance. 42 | /// 43 | private readonly IdPartPair part; 44 | 45 | #endregion 46 | 47 | #region Public Constructors 48 | 49 | /// 50 | /// Initializes a new instance of the class 51 | /// with the referenced object. 52 | /// 53 | /// 54 | /// The object that the new instance will hold. 55 | /// 56 | /// 57 | /// is . 58 | /// 59 | public OpenXmlPartTreeViewItem(IdPartPair p) 60 | : base(p != null && p.OpenXmlPart != null && p.OpenXmlPart.Parts != null && 61 | (p.OpenXmlPart.Parts.Count() > 0 || p.OpenXmlPart.RootElement != null)) 62 | { 63 | part = p ?? throw new ArgumentNullException(nameof(p)); 64 | } 65 | 66 | #endregion 67 | 68 | #region Public Instance Methods 69 | 70 | /// 71 | public override string BuildCodeDomTextDocument(CodeDomProvider provider) 72 | { 73 | return part.OpenXmlPart.GenerateSourceCode(provider); 74 | } 75 | 76 | #endregion 77 | 78 | #region Protected Instance Methods 79 | 80 | /// 81 | protected override IEnumerable GetOpenXmlSubtreeItems() 82 | { 83 | var result = new List(); 84 | 85 | result.AddRange(BuildOpenXmlPartTreeViewItems(part.OpenXmlPart.Parts)); 86 | if (part.OpenXmlPart.RootElement != null) 87 | { 88 | result.AddRange(BuildOpenXmlElementTreeViewItems(part.OpenXmlPart.RootElement)); 89 | } 90 | 91 | return result; 92 | } 93 | 94 | #endregion 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/DocxToSource/Controls/OpenXmlElementTreeViewItem.cs: -------------------------------------------------------------------------------- 1 | /* MIT License 2 | 3 | Copyright (c) 2020 Ryan Boggs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 6 | software and associated documentation files (the "Software"), to deal in the Software 7 | without restriction, including without limitation the rights to use, copy, modify, 8 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 9 | permit persons to whom the Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all copies 13 | or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 16 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 17 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 18 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | using DocumentFormat.OpenXml; 24 | using Serialize.OpenXml.CodeGen; 25 | using System; 26 | using System.CodeDom.Compiler; 27 | using System.Collections.Generic; 28 | using System.IO; 29 | using System.Text; 30 | using System.Xml; 31 | 32 | namespace DocxToSource.Controls 33 | { 34 | /// 35 | /// derived class that contains information 36 | /// about a given object. 37 | /// 38 | public class OpenXmlElementTreeViewItem : OpenXmlTreeViewItem 39 | { 40 | #region Private Instance Fields 41 | 42 | /// 43 | /// Holds the object for this instance. 44 | /// 45 | private readonly OpenXmlElement element; 46 | 47 | #endregion 48 | 49 | #region Public Constructors 50 | 51 | /// 52 | /// Initializes a new instance of the class 53 | /// with the referenced object. 54 | /// 55 | /// 56 | /// The object that the new instance will hold. 57 | /// 58 | /// 59 | /// is . 60 | /// 61 | public OpenXmlElementTreeViewItem(OpenXmlElement e) : base(e != null && e.HasChildren) 62 | { 63 | element = e ?? throw new ArgumentNullException(nameof(e)); 64 | } 65 | 66 | #endregion 67 | 68 | #region Public Instance Methods 69 | 70 | /// 71 | public override string BuildCodeDomTextDocument(CodeDomProvider provider) 72 | { 73 | return element.GenerateSourceCode(provider); 74 | } 75 | 76 | /// 77 | public override string BuildXmlTextDocument() 78 | { 79 | StringBuilder sb = new(); 80 | 81 | using (StringWriter writer = new(sb)) 82 | { 83 | using XmlTextWriter xTarget = new(writer); 84 | XmlDocument xDoc = new(); 85 | xDoc.LoadXml(element.OuterXml); 86 | 87 | xTarget.Formatting = Formatting.Indented; 88 | xTarget.Indentation = 2; 89 | 90 | xDoc.Normalize(); 91 | xDoc.PreserveWhitespace = true; 92 | xDoc.WriteContentTo(xTarget); 93 | 94 | xTarget.Flush(); 95 | xTarget.Close(); 96 | } 97 | 98 | return sb.ToString(); 99 | } 100 | 101 | #endregion 102 | 103 | #region Protected Instance Methods 104 | 105 | /// 106 | protected override IEnumerable GetOpenXmlSubtreeItems() 107 | { 108 | return BuildOpenXmlElementTreeViewItems(element.Elements()); 109 | } 110 | 111 | #endregion 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /src/DocxToSource.Wpf/Controls/OpenXmlElementTreeViewItem.cs: -------------------------------------------------------------------------------- 1 | /* MIT License 2 | 3 | Copyright (c) 2020 Ryan Boggs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 6 | software and associated documentation files (the "Software"), to deal in the Software 7 | without restriction, including without limitation the rights to use, copy, modify, 8 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 9 | permit persons to whom the Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all copies 13 | or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 16 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 17 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 18 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | using DocumentFormat.OpenXml; 24 | using Serialize.OpenXml.CodeGen; 25 | using System; 26 | using System.CodeDom.Compiler; 27 | using System.Collections.Generic; 28 | using System.IO; 29 | using System.Text; 30 | using System.Xml; 31 | 32 | namespace DocxToSource.Wpf.Controls 33 | { 34 | /// 35 | /// derived class that contains information 36 | /// about a given object. 37 | /// 38 | public class OpenXmlElementTreeViewItem : OpenXmlTreeViewItem 39 | { 40 | #region Private Instance Fields 41 | 42 | /// 43 | /// Holds the object for this instance. 44 | /// 45 | private readonly OpenXmlElement element; 46 | 47 | #endregion 48 | 49 | #region Public Constructors 50 | 51 | /// 52 | /// Initializes a new instance of the class 53 | /// with the referenced object. 54 | /// 55 | /// 56 | /// The object that the new instance will hold. 57 | /// 58 | /// 59 | /// is . 60 | /// 61 | public OpenXmlElementTreeViewItem(OpenXmlElement e) : base(e != null && e.HasChildren) 62 | { 63 | if (e is null) throw new ArgumentNullException(nameof(e)); 64 | element = e; 65 | } 66 | 67 | #endregion 68 | 69 | #region Public Instance Methods 70 | 71 | /// 72 | public override string BuildCodeDomTextDocument(CodeDomProvider provider) 73 | { 74 | return element.GenerateSourceCode(provider); 75 | } 76 | 77 | /// 78 | public override string BuildXmlTextDocument() 79 | { 80 | var sb = new StringBuilder(); 81 | 82 | using (var writer = new StringWriter(sb)) 83 | { 84 | using (var xTarget = new XmlTextWriter(writer)) 85 | { 86 | 87 | var xDoc = new XmlDocument(); 88 | xDoc.LoadXml(element.OuterXml); 89 | 90 | xTarget.Formatting = Formatting.Indented; 91 | xTarget.Indentation = 2; 92 | 93 | xDoc.Normalize(); 94 | xDoc.PreserveWhitespace = true; 95 | xDoc.WriteContentTo(xTarget); 96 | 97 | xTarget.Flush(); 98 | xTarget.Close(); 99 | } 100 | } 101 | 102 | return sb.ToString(); 103 | } 104 | 105 | #endregion 106 | 107 | #region Protected Instance Methods 108 | 109 | /// 110 | protected override IEnumerable GetOpenXmlSubtreeItems() 111 | { 112 | return BuildOpenXmlElementTreeViewItems(element.Elements()); 113 | } 114 | 115 | #endregion 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /src/DocxToSource/Languages/LanguageDefinition.cs: -------------------------------------------------------------------------------- 1 | /* MIT License 2 | 3 | Copyright (c) 2020 Ryan Boggs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 6 | software and associated documentation files (the "Software"), to deal in the Software 7 | without restriction, including without limitation the rights to use, copy, modify, 8 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 9 | permit persons to whom the Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all copies 13 | or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 16 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 17 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 18 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | using ICSharpCode.AvalonEdit.Highlighting; 24 | using Serialize.OpenXml.CodeGen; 25 | using System; 26 | using System.CodeDom.Compiler; 27 | 28 | namespace DocxToSource.Languages 29 | { 30 | /// 31 | /// Base class for language definitions for the project. 32 | /// 33 | public abstract class LanguageDefinition 34 | { 35 | #region Protected Constructor 36 | 37 | /// 38 | /// Initializes a new instance of the class 39 | /// with the object that will be used to 40 | /// create the language source code. 41 | /// 42 | /// 43 | /// The object for the new 44 | /// provider. 45 | /// 46 | /// 47 | /// is . 48 | /// 49 | protected LanguageDefinition(CodeDomProvider provider) 50 | : this(NamespaceAliasOptions.Default, provider) { } 51 | 52 | /// 53 | /// Initializes a new instance of the class 54 | /// with the object that will be used to 55 | /// create the language source code and predefined . 56 | /// 57 | /// 58 | /// Custom for the language definition. 59 | /// 60 | /// 61 | /// The object for the new 62 | /// provider. 63 | /// 64 | /// 65 | /// or is . 66 | /// 67 | protected LanguageDefinition(NamespaceAliasOptions opts, CodeDomProvider provider) 68 | { 69 | Options = opts ?? throw new ArgumentNullException(nameof(opts)); 70 | Provider = provider ?? throw new ArgumentNullException(nameof(provider)); 71 | Highlighting = HighlightingManager.Instance.GetDefinitionByExtension("." + Provider.FileExtension); 72 | } 73 | 74 | #endregion 75 | 76 | #region Public Instance Properties 77 | 78 | /// 79 | /// Gets the name to display to the user. 80 | /// 81 | public string DisplayName { get; protected set; } 82 | 83 | /// 84 | /// Gets the to use for the generated 85 | /// source code. 86 | /// 87 | public IHighlightingDefinition Highlighting { get; private set; } 88 | 89 | /// 90 | /// Gets the to use when generating 91 | /// source code. 92 | /// 93 | public NamespaceAliasOptions Options { get; private set; } 94 | 95 | /// 96 | /// Gets the to use when generating 97 | /// source code. 98 | /// 99 | public CodeDomProvider Provider { get; private set; } 100 | 101 | #endregion 102 | 103 | #region Public Instance Methods 104 | 105 | /// 106 | public override string ToString() => DisplayName; 107 | 108 | #endregion 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /src/DocxToSource.Wpf/Languages/LanguageDefinition.cs: -------------------------------------------------------------------------------- 1 | /* MIT License 2 | 3 | Copyright (c) 2020 Ryan Boggs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 6 | software and associated documentation files (the "Software"), to deal in the Software 7 | without restriction, including without limitation the rights to use, copy, modify, 8 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 9 | permit persons to whom the Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all copies 13 | or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 16 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 17 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 18 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | using ICSharpCode.AvalonEdit.Highlighting; 24 | using Serialize.OpenXml.CodeGen; 25 | using System; 26 | using System.CodeDom.Compiler; 27 | 28 | namespace DocxToSource.Wpf.Languages 29 | { 30 | /// 31 | /// Base class for language definitions for the project. 32 | /// 33 | public abstract class LanguageDefinition 34 | { 35 | #region Protected Constructor 36 | 37 | /// 38 | /// Initializes a new instance of the class 39 | /// with the object that will be used to 40 | /// create the language source code. 41 | /// 42 | /// 43 | /// The object for the new 44 | /// provider. 45 | /// 46 | /// 47 | /// is . 48 | /// 49 | protected LanguageDefinition(CodeDomProvider provider) 50 | : this(NamespaceAliasOptions.Default, provider) { } 51 | 52 | /// 53 | /// Initializes a new instance of the class 54 | /// with the object that will be used to 55 | /// create the language source code and predefined . 56 | /// 57 | /// 58 | /// Custom for the language definition. 59 | /// 60 | /// 61 | /// The object for the new 62 | /// provider. 63 | /// 64 | /// 65 | /// or is . 66 | /// 67 | protected LanguageDefinition(NamespaceAliasOptions opts, CodeDomProvider provider) 68 | { 69 | Options = opts ?? throw new ArgumentNullException(nameof(opts)); 70 | Provider = provider ?? throw new ArgumentNullException(nameof(provider)); 71 | Highlighting = HighlightingManager.Instance.GetDefinitionByExtension("." + Provider.FileExtension); 72 | } 73 | 74 | #endregion 75 | 76 | #region Public Instance Properties 77 | 78 | /// 79 | /// Gets the name to display to the user. 80 | /// 81 | public string DisplayName { get; protected set; } 82 | 83 | /// 84 | /// Gets the to use for the generated 85 | /// source code. 86 | /// 87 | public IHighlightingDefinition Highlighting { get; private set; } 88 | 89 | /// 90 | /// Gets the to use when generating 91 | /// source code. 92 | /// 93 | public NamespaceAliasOptions Options { get; private set; } 94 | 95 | /// 96 | /// Gets the to use when generating 97 | /// source code. 98 | /// 99 | public CodeDomProvider Provider { get; private set; } 100 | 101 | #endregion 102 | 103 | #region Public Instance Methods 104 | 105 | /// 106 | public override string ToString() => DisplayName; 107 | 108 | #endregion 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /src/DocxToSource.Wpf/Properties/Resources.resx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | text/microsoft-resx 107 | 108 | 109 | 2.0 110 | 111 | 112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 113 | 114 | 115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | -------------------------------------------------------------------------------- /src/DocxToSource/MainWindow.xaml: -------------------------------------------------------------------------------- 1 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | Language: 45 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 65 | 66 | 67 | 68 | 69 | 70 | 80 | 81 | 82 | 83 | 84 | 94 | 95 | 96 | 97 | 98 | 99 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | -------------------------------------------------------------------------------- /src/DocxToSource.Wpf/MainWindow.xaml: -------------------------------------------------------------------------------- 1 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | Language: 45 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 66 | 67 | 68 | 69 | 70 | 71 | 81 | 82 | 83 | 84 | 85 | 95 | 96 | 97 | 98 | 99 | 100 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Aa][Rr][Mm]/ 27 | [Aa][Rr][Mm]64/ 28 | bld/ 29 | [Bb]in/ 30 | [Oo]bj/ 31 | [Ll]og/ 32 | [Ll]ogs/ 33 | 34 | # Visual Studio 2015/2017 cache/options directory 35 | .vs/ 36 | # Uncomment if you have tasks that create the project's static files in wwwroot 37 | #wwwroot/ 38 | 39 | # Visual Studio 2017 auto generated files 40 | Generated\ Files/ 41 | 42 | # MSTest test Results 43 | [Tt]est[Rr]esult*/ 44 | [Bb]uild[Ll]og.* 45 | 46 | # NUnit 47 | *.VisualState.xml 48 | TestResult.xml 49 | nunit-*.xml 50 | 51 | # Build Results of an ATL Project 52 | [Dd]ebugPS/ 53 | [Rr]eleasePS/ 54 | dlldata.c 55 | 56 | # Benchmark Results 57 | BenchmarkDotNet.Artifacts/ 58 | 59 | # .NET Core 60 | project.lock.json 61 | project.fragment.lock.json 62 | artifacts/ 63 | 64 | # StyleCop 65 | StyleCopReport.xml 66 | 67 | # Files built by Visual Studio 68 | *_i.c 69 | *_p.c 70 | *_h.h 71 | *.ilk 72 | *.meta 73 | *.obj 74 | *.iobj 75 | *.pch 76 | *.pdb 77 | *.ipdb 78 | *.pgc 79 | *.pgd 80 | *.rsp 81 | *.sbr 82 | *.tlb 83 | *.tli 84 | *.tlh 85 | *.tmp 86 | *.tmp_proj 87 | *_wpftmp.csproj 88 | *.log 89 | *.vspscc 90 | *.vssscc 91 | .builds 92 | *.pidb 93 | *.svclog 94 | *.scc 95 | 96 | # Chutzpah Test files 97 | _Chutzpah* 98 | 99 | # Visual C++ cache files 100 | ipch/ 101 | *.aps 102 | *.ncb 103 | *.opendb 104 | *.opensdf 105 | *.sdf 106 | *.cachefile 107 | *.VC.db 108 | *.VC.VC.opendb 109 | 110 | # Visual Studio profiler 111 | *.psess 112 | *.vsp 113 | *.vspx 114 | *.sap 115 | 116 | # Visual Studio Trace Files 117 | *.e2e 118 | 119 | # TFS 2012 Local Workspace 120 | $tf/ 121 | 122 | # Guidance Automation Toolkit 123 | *.gpState 124 | 125 | # ReSharper is a .NET coding add-in 126 | _ReSharper*/ 127 | *.[Rr]e[Ss]harper 128 | *.DotSettings.user 129 | 130 | # TeamCity is a build add-in 131 | _TeamCity* 132 | 133 | # DotCover is a Code Coverage Tool 134 | *.dotCover 135 | 136 | # AxoCover is a Code Coverage Tool 137 | .axoCover/* 138 | !.axoCover/settings.json 139 | 140 | # Visual Studio code coverage results 141 | *.coverage 142 | *.coveragexml 143 | 144 | # NCrunch 145 | _NCrunch_* 146 | .*crunch*.local.xml 147 | nCrunchTemp_* 148 | 149 | # MightyMoose 150 | *.mm.* 151 | AutoTest.Net/ 152 | 153 | # Web workbench (sass) 154 | .sass-cache/ 155 | 156 | # Installshield output folder 157 | [Ee]xpress/ 158 | 159 | # DocProject is a documentation generator add-in 160 | DocProject/buildhelp/ 161 | DocProject/Help/*.HxT 162 | DocProject/Help/*.HxC 163 | DocProject/Help/*.hhc 164 | DocProject/Help/*.hhk 165 | DocProject/Help/*.hhp 166 | DocProject/Help/Html2 167 | DocProject/Help/html 168 | 169 | # Click-Once directory 170 | publish/ 171 | 172 | # Publish Web Output 173 | *.[Pp]ublish.xml 174 | *.azurePubxml 175 | # Note: Comment the next line if you want to checkin your web deploy settings, 176 | # but database connection strings (with potential passwords) will be unencrypted 177 | *.pubxml 178 | *.publishproj 179 | 180 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 181 | # checkin your Azure Web App publish settings, but sensitive information contained 182 | # in these scripts will be unencrypted 183 | PublishScripts/ 184 | 185 | # NuGet Packages 186 | *.nupkg 187 | # NuGet Symbol Packages 188 | *.snupkg 189 | # The packages folder can be ignored because of Package Restore 190 | **/[Pp]ackages/* 191 | # except build/, which is used as an MSBuild target. 192 | !**/[Pp]ackages/build/ 193 | # Uncomment if necessary however generally it will be regenerated when needed 194 | #!**/[Pp]ackages/repositories.config 195 | # NuGet v3's project.json files produces more ignorable files 196 | *.nuget.props 197 | *.nuget.targets 198 | 199 | # Microsoft Azure Build Output 200 | csx/ 201 | *.build.csdef 202 | 203 | # Microsoft Azure Emulator 204 | ecf/ 205 | rcf/ 206 | 207 | # Windows Store app package directories and files 208 | AppPackages/ 209 | BundleArtifacts/ 210 | Package.StoreAssociation.xml 211 | _pkginfo.txt 212 | *.appx 213 | *.appxbundle 214 | *.appxupload 215 | 216 | # Visual Studio cache files 217 | # files ending in .cache can be ignored 218 | *.[Cc]ache 219 | # but keep track of directories ending in .cache 220 | !?*.[Cc]ache/ 221 | 222 | # Others 223 | ClientBin/ 224 | ~$* 225 | *~ 226 | *.dbmdl 227 | *.dbproj.schemaview 228 | *.jfm 229 | *.pfx 230 | *.publishsettings 231 | orleans.codegen.cs 232 | 233 | # Including strong name files can present a security risk 234 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 235 | #*.snk 236 | 237 | # Since there are multiple workflows, uncomment next line to ignore bower_components 238 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 239 | #bower_components/ 240 | 241 | # RIA/Silverlight projects 242 | Generated_Code/ 243 | 244 | # Backup & report files from converting an old project file 245 | # to a newer Visual Studio version. Backup files are not needed, 246 | # because we have git ;-) 247 | _UpgradeReport_Files/ 248 | Backup*/ 249 | UpgradeLog*.XML 250 | UpgradeLog*.htm 251 | ServiceFabricBackup/ 252 | *.rptproj.bak 253 | 254 | # SQL Server files 255 | *.mdf 256 | *.ldf 257 | *.ndf 258 | 259 | # Business Intelligence projects 260 | *.rdl.data 261 | *.bim.layout 262 | *.bim_*.settings 263 | *.rptproj.rsuser 264 | *- [Bb]ackup.rdl 265 | *- [Bb]ackup ([0-9]).rdl 266 | *- [Bb]ackup ([0-9][0-9]).rdl 267 | 268 | # Microsoft Fakes 269 | FakesAssemblies/ 270 | 271 | # GhostDoc plugin setting file 272 | *.GhostDoc.xml 273 | 274 | # Node.js Tools for Visual Studio 275 | .ntvs_analysis.dat 276 | node_modules/ 277 | 278 | # Visual Studio 6 build log 279 | *.plg 280 | 281 | # Visual Studio 6 workspace options file 282 | *.opt 283 | 284 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 285 | *.vbw 286 | 287 | # Visual Studio LightSwitch build output 288 | **/*.HTMLClient/GeneratedArtifacts 289 | **/*.DesktopClient/GeneratedArtifacts 290 | **/*.DesktopClient/ModelManifest.xml 291 | **/*.Server/GeneratedArtifacts 292 | **/*.Server/ModelManifest.xml 293 | _Pvt_Extensions 294 | 295 | # Paket dependency manager 296 | .paket/paket.exe 297 | paket-files/ 298 | 299 | # FAKE - F# Make 300 | .fake/ 301 | 302 | # CodeRush personal settings 303 | .cr/personal 304 | 305 | # Python Tools for Visual Studio (PTVS) 306 | __pycache__/ 307 | *.pyc 308 | 309 | # Cake - Uncomment if you are using it 310 | tools/** 311 | !tools/packages.config 312 | 313 | # Tabs Studio 314 | *.tss 315 | 316 | # Telerik's JustMock configuration file 317 | *.jmconfig 318 | 319 | # BizTalk build output 320 | *.btp.cs 321 | *.btm.cs 322 | *.odx.cs 323 | *.xsd.cs 324 | 325 | # OpenCover UI analysis results 326 | OpenCover/ 327 | 328 | # Azure Stream Analytics local run output 329 | ASALocalRun/ 330 | 331 | # MSBuild Binary and Structured Log 332 | *.binlog 333 | 334 | # NVidia Nsight GPU debugger configuration file 335 | *.nvuser 336 | 337 | # MFractors (Xamarin productivity tool) working folder 338 | .mfractor/ 339 | 340 | # Local History for Visual Studio 341 | .localhistory/ 342 | 343 | # BeatPulse healthcheck temp database 344 | healthchecksdb 345 | 346 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 347 | MigrationBackup/ 348 | 349 | # Ionide (cross platform F# VS Code tools) working folder 350 | .ionide/ 351 | 352 | /.idea/ 353 | -------------------------------------------------------------------------------- /src/DocxToSource.Wpf/DocxToSource.Wpf.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {D1C3D3D1-129B-48E1-B209-7EDCD9CAF33A} 8 | WinExe 9 | DocxToSource.Wpf 10 | DocxToSource.Wpf 11 | v4.7.2 12 | 512 13 | {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 14 | 4 15 | true 16 | true 17 | 18 | 19 | AnyCPU 20 | true 21 | full 22 | false 23 | bin\Debug\ 24 | DEBUG;TRACE 25 | prompt 26 | 4 27 | 28 | 29 | AnyCPU 30 | pdbonly 31 | true 32 | bin\Release\ 33 | TRACE 34 | prompt 35 | 4 36 | 37 | 38 | 39 | ..\..\packages\CommonServiceLocator.2.0.4\lib\net47\CommonServiceLocator.dll 40 | 41 | 42 | ..\..\packages\DocumentFormat.OpenXml.2.11.3\lib\net46\DocumentFormat.OpenXml.dll 43 | 44 | 45 | ..\..\packages\AvalonEdit.6.0.1\lib\net45\ICSharpCode.AvalonEdit.dll 46 | 47 | 48 | ..\..\packages\Prism.Core.7.2.0.1422\lib\net45\Prism.dll 49 | 50 | 51 | ..\..\packages\Prism.Wpf.7.2.0.1422\lib\net45\Prism.Wpf.dll 52 | 53 | 54 | 55 | ..\..\packages\System.CodeDom.4.7.0\lib\net461\System.CodeDom.dll 56 | 57 | 58 | 59 | 60 | ..\..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll 61 | True 62 | True 63 | 64 | 65 | ..\..\packages\System.IO.Packaging.4.7.0\lib\net46\System.IO.Packaging.dll 66 | 67 | 68 | ..\..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll 69 | 70 | 71 | 72 | ..\..\packages\Prism.Wpf.7.2.0.1422\lib\net45\System.Windows.Interactivity.dll 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 4.0 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | MSBuild:Compile 90 | Designer 91 | 92 | 93 | MSBuild:Compile 94 | Designer 95 | 96 | 97 | App.xaml 98 | Code 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | MainWindow.xaml 110 | Code 111 | 112 | 113 | 114 | 115 | 116 | Code 117 | 118 | 119 | True 120 | True 121 | Resources.resx 122 | 123 | 124 | True 125 | Settings.settings 126 | True 127 | 128 | 129 | ResXFileCodeGenerator 130 | Resources.Designer.cs 131 | 132 | 133 | 134 | SettingsSingleFileGenerator 135 | Settings.Designer.cs 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | {37f5caa1-efbd-4339-a3c8-a55ffe7ca873} 144 | Serialize.OpenXml.CodeGen 145 | 146 | 147 | 148 | -------------------------------------------------------------------------------- /src/DocxToSource/Controls/OpenXmlTreeViewItem.cs: -------------------------------------------------------------------------------- 1 | /* MIT License 2 | 3 | Copyright (c) 2020 Ryan Boggs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 6 | software and associated documentation files (the "Software"), to deal in the Software 7 | without restriction, including without limitation the rights to use, copy, modify, 8 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 9 | permit persons to whom the Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all copies 13 | or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 16 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 17 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 18 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | using DocumentFormat.OpenXml; 24 | using DocumentFormat.OpenXml.Packaging; 25 | using DocumentFormat.OpenXml.Spreadsheet; 26 | using System; 27 | using System.CodeDom.Compiler; 28 | using System.Collections.Generic; 29 | using System.Linq; 30 | using System.Windows.Controls; 31 | 32 | namespace DocxToSource.Controls 33 | { 34 | /// 35 | /// Base class to use for all openxml sdk objects of a 36 | /// selected document. 37 | /// 38 | public abstract class OpenXmlTreeViewItem : TreeViewItem 39 | { 40 | #region Protected Static Fields 41 | 42 | /// 43 | /// Holds the code generation options to use when building the source code text. 44 | /// 45 | protected readonly CodeGeneratorOptions Cgo = new() { BracingStyle = "C" }; 46 | 47 | #endregion 48 | 49 | #region Private Instance Fields 50 | 51 | /// 52 | /// Indicates whether or not the child tree view items have been loaded. 53 | /// 54 | private bool _subtreeIsLoaded = false; 55 | 56 | #endregion 57 | 58 | #region Protected Constructors 59 | 60 | /// 61 | /// Initializes a new instance of the class that is 62 | /// empty. 63 | /// 64 | /// 65 | /// Indicates whether or not the new object is expected to have sub tree items. 66 | /// 67 | protected OpenXmlTreeViewItem(bool hasChildren) : base() 68 | { 69 | if (hasChildren) 70 | { 71 | TreeViewItem placeholder = new() { Header = "{Please wait}" }; 72 | _ = Items.Add(placeholder); 73 | 74 | // Setup the expanded event 75 | Expanded += LoadSubtree; 76 | } 77 | } 78 | 79 | #endregion 80 | 81 | #region Public Instance Methods 82 | 83 | /// 84 | /// Creates source code text provided by the CodeDomProvider 85 | /// object that represents the selected object. 86 | /// 87 | /// 88 | /// The to use when generating the source code. 89 | /// 90 | /// 91 | /// The source code text provided by the CodeDomProvider 92 | /// object that represents the selected object. 93 | /// 94 | public abstract string BuildCodeDomTextDocument(CodeDomProvider provider); 95 | 96 | /// 97 | /// Creates the xml source code of the currently selected object. 98 | /// 99 | /// 100 | /// The xml source code of the currently selected object. 101 | /// 102 | public virtual string BuildXmlTextDocument() 103 | { 104 | return String.Empty; 105 | } 106 | 107 | #endregion 108 | 109 | #region Protected Instance Methods 110 | 111 | /// 112 | /// Creates a new collection based on the 113 | /// elements from . 114 | /// 115 | /// 116 | /// The existing collection of items. 117 | /// 118 | /// 119 | /// A new collection of elements. 120 | /// 121 | protected IEnumerable BuildOpenXmlElementTreeViewItems( 122 | params OpenXmlElement[] elements) 123 | { 124 | return BuildOpenXmlElementTreeViewItems(elements.ToList()); 125 | } 126 | 127 | /// 128 | /// Creates a new collection based on the 129 | /// elements from . 130 | /// 131 | /// 132 | /// The existing collection of items. 133 | /// 134 | /// 135 | /// A new collection of elements. 136 | /// 137 | protected virtual IEnumerable BuildOpenXmlElementTreeViewItems( 138 | IEnumerable elements) 139 | { 140 | if (elements is null || !elements.Any()) 141 | { 142 | throw new ArgumentNullException(nameof(elements)); 143 | } 144 | string header; 145 | Row row; 146 | Cell cell; 147 | uint index = 0; 148 | 149 | foreach (OpenXmlElement e in elements) 150 | { 151 | header = $"<{index++}> {e.LocalName} ({e.GetType().Name})"; 152 | row = e as Row; 153 | cell = e as Cell; 154 | 155 | if (row != null && row.RowIndex != null && row.RowIndex.HasValue) 156 | { 157 | header += $" [{(e as Row).RowIndex.Value}]"; 158 | } 159 | else if (cell != null && cell.CellReference != null && cell.CellReference.HasValue) 160 | { 161 | header += $" [{(e as Cell).CellReference.Value}]"; 162 | } 163 | yield return new OpenXmlElementTreeViewItem(e) 164 | { 165 | Header = header 166 | }; 167 | } 168 | } 169 | 170 | /// 171 | /// Creates a new collection based on the 172 | /// parts from . 173 | /// 174 | /// 175 | /// The existing collection of items. 176 | /// 177 | /// 178 | /// A new collection of elements. 179 | /// 180 | protected IEnumerable BuildOpenXmlPartTreeViewItems( 181 | params IdPartPair[] parts) 182 | { 183 | return BuildOpenXmlPartTreeViewItems(parts.ToList()); 184 | } 185 | 186 | /// 187 | /// Creates a new collection based on the 188 | /// parts from . 189 | /// 190 | /// 191 | /// The existing collection of items. 192 | /// 193 | /// 194 | /// A new collection of elements. 195 | /// 196 | protected virtual IEnumerable BuildOpenXmlPartTreeViewItems( 197 | IEnumerable parts) 198 | { 199 | foreach (IdPartPair p in parts) 200 | { 201 | yield return new OpenXmlPartTreeViewItem(p) 202 | { 203 | Header = $"[{p.RelationshipId}] {p.OpenXmlPart.Uri} ({p.OpenXmlPart.GetType().Name})" 204 | }; 205 | } 206 | } 207 | 208 | /// 209 | /// Retrieves all of the that should be directly 210 | /// added as child elements to the current object. 211 | /// 212 | /// 213 | /// A collection of elements to add to the 214 | /// current object. 215 | /// 216 | protected abstract IEnumerable GetOpenXmlSubtreeItems(); 217 | 218 | #endregion 219 | 220 | #region Private Instance Methods 221 | 222 | /// 223 | /// Event triggered when the current tree item is expanded. 224 | /// 225 | /// 226 | /// Object raising this event. 227 | /// 228 | /// Event arguments 229 | /// 230 | /// This structure was setup to limit how much of the current openxml document 231 | /// is processed at a single time to reduce memory footprint. 232 | /// 233 | private void LoadSubtree(object sender, EventArgs e) 234 | { 235 | if (_subtreeIsLoaded) return; 236 | 237 | IEnumerable children = GetOpenXmlSubtreeItems(); 238 | Items.Clear(); 239 | 240 | foreach (OpenXmlTreeViewItem c in children) 241 | { 242 | _ = Items.Add(c); 243 | } 244 | _subtreeIsLoaded = true; 245 | } 246 | 247 | #endregion 248 | } 249 | } 250 | -------------------------------------------------------------------------------- /src/DocxToSource.Wpf/Controls/OpenXmlTreeViewItem.cs: -------------------------------------------------------------------------------- 1 | /* MIT License 2 | 3 | Copyright (c) 2020 Ryan Boggs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 6 | software and associated documentation files (the "Software"), to deal in the Software 7 | without restriction, including without limitation the rights to use, copy, modify, 8 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 9 | permit persons to whom the Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all copies 13 | or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 16 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 17 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 18 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | using DocumentFormat.OpenXml; 24 | using DocumentFormat.OpenXml.Packaging; 25 | using DocumentFormat.OpenXml.Spreadsheet; 26 | using System; 27 | using System.CodeDom.Compiler; 28 | using System.Collections.Generic; 29 | using System.Linq; 30 | using System.Windows.Controls; 31 | 32 | namespace DocxToSource.Wpf.Controls 33 | { 34 | /// 35 | /// Base class to use for all openxml sdk objects of a 36 | /// selected document. 37 | /// 38 | public abstract class OpenXmlTreeViewItem : TreeViewItem 39 | { 40 | #region Protected Static Fields 41 | 42 | /// 43 | /// Holds the code generation options to use when building the source code text. 44 | /// 45 | protected readonly CodeGeneratorOptions Cgo = new CodeGeneratorOptions() 46 | { 47 | BracingStyle = "C" 48 | }; 49 | 50 | #endregion 51 | 52 | #region Private Instance Fields 53 | 54 | /// 55 | /// Indicates whether or not the child tree view items have been loaded. 56 | /// 57 | private bool _subtreeIsLoaded = false; 58 | 59 | #endregion 60 | 61 | #region Protected Constructors 62 | 63 | /// 64 | /// Initializes a new instance of the class that is 65 | /// empty. 66 | /// 67 | /// 68 | /// Indicates whether or not the new object is expected to have sub tree items. 69 | /// 70 | protected OpenXmlTreeViewItem(bool hasChildren) : base() 71 | { 72 | if (hasChildren) 73 | { 74 | var placeholder = new TreeViewItem() { Header = "{Please wait}" }; 75 | this.Items.Add(placeholder); 76 | 77 | // Setup the expanded event 78 | this.Expanded += LoadSubtree; 79 | } 80 | } 81 | 82 | #endregion 83 | 84 | #region Public Instance Methods 85 | 86 | /// 87 | /// Creates source code text provided by the CodeDomProvider 88 | /// object that represents the selected object. 89 | /// 90 | /// 91 | /// The to use when generating the source code. 92 | /// 93 | /// 94 | /// The source code text provided by the CodeDomProvider 95 | /// object that represents the selected object. 96 | /// 97 | public abstract string BuildCodeDomTextDocument(CodeDomProvider provider); 98 | 99 | /// 100 | /// Creates the xml source code of the currently selected object. 101 | /// 102 | /// 103 | /// The xml source code of the currently selected object. 104 | /// 105 | public virtual string BuildXmlTextDocument() 106 | { 107 | return String.Empty; 108 | } 109 | 110 | #endregion 111 | 112 | #region Protected Instance Methods 113 | 114 | /// 115 | /// Creates a new collection based on the 116 | /// elements from . 117 | /// 118 | /// 119 | /// The existing collection of items. 120 | /// 121 | /// 122 | /// A new collection of elements. 123 | /// 124 | protected IEnumerable BuildOpenXmlElementTreeViewItems( 125 | params OpenXmlElement[] elements) 126 | { 127 | return BuildOpenXmlElementTreeViewItems(elements.ToList()); 128 | } 129 | 130 | /// 131 | /// Creates a new collection based on the 132 | /// elements from . 133 | /// 134 | /// 135 | /// The existing collection of items. 136 | /// 137 | /// 138 | /// A new collection of elements. 139 | /// 140 | protected virtual IEnumerable BuildOpenXmlElementTreeViewItems( 141 | IEnumerable elements) 142 | { 143 | if (elements is null || elements.Count() == 0) 144 | { 145 | throw new ArgumentNullException(nameof(elements)); 146 | } 147 | string header; 148 | Row row; 149 | Cell cell; 150 | uint index = 0; 151 | 152 | foreach (var e in elements) 153 | { 154 | header = $"<{index++}> {e.LocalName} ({e.GetType().Name})"; 155 | row = e as Row; 156 | cell = e as Cell; 157 | 158 | if (row != null && row.RowIndex != null && row.RowIndex.HasValue) 159 | { 160 | header += $" [{(e as Row).RowIndex.Value}]"; 161 | } 162 | else if (cell != null && cell.CellReference != null && cell.CellReference.HasValue) 163 | { 164 | header += $" [{(e as Cell).CellReference.Value}]"; 165 | } 166 | yield return new OpenXmlElementTreeViewItem(e) 167 | { 168 | Header = header 169 | }; 170 | } 171 | } 172 | 173 | /// 174 | /// Creates a new collection based on the 175 | /// parts from . 176 | /// 177 | /// 178 | /// The existing collection of items. 179 | /// 180 | /// 181 | /// A new collection of elements. 182 | /// 183 | protected IEnumerable BuildOpenXmlPartTreeViewItems( 184 | params IdPartPair[] parts) 185 | { 186 | return BuildOpenXmlPartTreeViewItems(parts.ToList()); 187 | } 188 | 189 | /// 190 | /// Creates a new collection based on the 191 | /// parts from . 192 | /// 193 | /// 194 | /// The existing collection of items. 195 | /// 196 | /// 197 | /// A new collection of elements. 198 | /// 199 | protected virtual IEnumerable BuildOpenXmlPartTreeViewItems( 200 | IEnumerable parts) 201 | { 202 | foreach (var p in parts) 203 | { 204 | yield return new OpenXmlPartTreeViewItem(p) 205 | { 206 | Header = $"[{p.RelationshipId}] {p.OpenXmlPart.Uri} ({p.OpenXmlPart.GetType().Name})" 207 | }; 208 | } 209 | } 210 | 211 | /// 212 | /// Retrieves all of the that should be directly 213 | /// added as child elements to the current object. 214 | /// 215 | /// 216 | /// A collection of elements to add to the 217 | /// current object. 218 | /// 219 | protected abstract IEnumerable GetOpenXmlSubtreeItems(); 220 | 221 | #endregion 222 | 223 | #region Private Instance Methods 224 | 225 | /// 226 | /// Event triggered when the current tree item is expanded. 227 | /// 228 | /// 229 | /// Object raising this event. 230 | /// 231 | /// Event arguments 232 | /// 233 | /// This structure was setup to limit how much of the current openxml document 234 | /// is processed at a single time to reduce memory footprint. 235 | /// 236 | private void LoadSubtree(object sender, EventArgs e) 237 | { 238 | if (_subtreeIsLoaded) return; 239 | 240 | var children = GetOpenXmlSubtreeItems(); 241 | this.Items.Clear(); 242 | 243 | foreach (var c in children) 244 | { 245 | Items.Add(c); 246 | } 247 | _subtreeIsLoaded = true; 248 | } 249 | 250 | #endregion 251 | } 252 | } 253 | -------------------------------------------------------------------------------- /src/DocxToSource/MainWindowModel.cs: -------------------------------------------------------------------------------- 1 | /* MIT License 2 | 3 | Copyright (c) 2020 Ryan Boggs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 6 | software and associated documentation files (the "Software"), to deal in the Software 7 | without restriction, including without limitation the rights to use, copy, modify, 8 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 9 | permit persons to whom the Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all copies 13 | or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 16 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 17 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 18 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | using DocumentFormat.OpenXml; 24 | using DocumentFormat.OpenXml.Packaging; 25 | using DocxToSource.Controls; 26 | using DocxToSource.Languages; 27 | using ICSharpCode.AvalonEdit.Document; 28 | using ICSharpCode.AvalonEdit.Highlighting; 29 | using Microsoft.Win32; 30 | using Prism.Commands; 31 | using Prism.Mvvm; 32 | using System; 33 | using System.Collections.Generic; 34 | using System.Collections.ObjectModel; 35 | using System.ComponentModel; 36 | using System.IO; 37 | using System.IO.Packaging; 38 | using System.Windows; 39 | using System.Windows.Input; 40 | 41 | namespace DocxToSource 42 | { 43 | /// 44 | /// View model class for the main window. 45 | /// 46 | public sealed class MainWindowModel : BindableBase, IDisposable 47 | { 48 | #region Private Static Fields 49 | 50 | /// 51 | /// Holds the default to use for 52 | /// the Xml window. 53 | /// 54 | private static readonly IHighlightingDefinition _defaultXmlDefinition; 55 | 56 | #endregion 57 | 58 | #region Private Instance Fields 59 | 60 | /// 61 | /// Holds the source code of the current selected openxml object. 62 | /// 63 | private TextDocument _codeDocument; 64 | 65 | /// 66 | /// Holds the highlighting definition for the source code text editor. 67 | /// 68 | private IHighlightingDefinition _codeSyntax; 69 | 70 | /// 71 | /// Holds the object containing the full path 72 | /// to use as the initial path in any OpenFileDialog windows. 73 | /// 74 | private DirectoryInfo _currentFileDirectory; 75 | 76 | /// 77 | /// Holds the full path and filename of the file that is currently open. 78 | /// 79 | private string _fileName; 80 | 81 | /// 82 | /// Indicates whether or not to automatically generate source code when 83 | /// selecting DOM nodes. 84 | /// 85 | private bool _generateSourceCode; 86 | 87 | /// 88 | /// Indicates whether or not to enable syntax highlighting in the source code 89 | /// windows. 90 | /// 91 | private bool _highlightSyntax; 92 | 93 | /// 94 | /// Indicates whether or not the selected item represents an 95 | /// object. 96 | /// 97 | private bool _isOpenXmlElement; 98 | 99 | /// 100 | /// Holds the openxml file package the is currently being reviewed. 101 | /// 102 | private OpenXmlPackage _oPkg; 103 | 104 | /// 105 | /// Holds the raw package used to stage the stream information for 106 | /// validation purposes. 107 | /// 108 | private Package _pkg; 109 | 110 | /// 111 | /// Holds the current treeviewitem that is currently selected in the treeview. 112 | /// 113 | private object _selectedItem; 114 | 115 | /// 116 | /// Holds the currently selected object. 117 | /// 118 | private LanguageDefinition _selectedLanguage; 119 | 120 | /// 121 | /// Holds the io stream containing the contents of the openxml file package. 122 | /// 123 | private Stream _stream; 124 | 125 | /// 126 | /// Holds the detailed exception information to display in the tree list view. 127 | /// 128 | private ObservableCollection _treeData; 129 | 130 | /// 131 | /// Indicates whether or not to have the text in the source code windows word wrap. 132 | /// 133 | private bool _wordWrap; 134 | 135 | /// 136 | /// Holds the xml code fo the current selected openxml element. 137 | /// 138 | private TextDocument _xmlDocument; 139 | 140 | /// 141 | /// Holds the highlighting definition for the XML Text Editor 142 | /// 143 | private IHighlightingDefinition _xmlDocumentSyntax; 144 | 145 | #endregion 146 | 147 | #region Static Constructors 148 | 149 | /// 150 | /// Static Constructor. 151 | /// 152 | static MainWindowModel() 153 | { 154 | // Setup the default Xml definition to use when syntax highlighting is requested 155 | _defaultXmlDefinition = HighlightingManager.Instance.GetDefinition("XML"); 156 | } 157 | 158 | #endregion 159 | 160 | #region Public Constructors 161 | 162 | /// 163 | /// Initializes a new instance of the class that 164 | /// is empty. 165 | /// 166 | public MainWindowModel() : base() 167 | { 168 | _currentFileDirectory = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)); 169 | _treeData = new ObservableCollection(); 170 | 171 | ObservableCollection langeDefs = new(); 172 | LanguageDefinitions = new ReadOnlyObservableCollection(langeDefs); 173 | 174 | // Load the language definition list 175 | langeDefs.Add(new CSharpLanguageDefinition()); 176 | langeDefs.Add(new VBLanguageDefinition()); 177 | 178 | // Set the default language 179 | SelectedLanguage = LanguageDefinitions[0]; 180 | 181 | _codeDocument = new TextDocument(); 182 | _xmlDocument = new TextDocument(); 183 | 184 | CloseCommand = new DelegateCommand(() => 185 | { 186 | Dispose(); 187 | _treeData.Clear(); 188 | RefreshSourceCodeWindows(null); 189 | }); 190 | OpenCommand = new DelegateCommand(OpenOfficeDocument); 191 | 192 | QuitCommand = new DelegateCommand(() => 193 | { 194 | Dispose(); 195 | Application.Current.Shutdown(); 196 | }); 197 | } 198 | 199 | #endregion 200 | 201 | #region Public Instance Properties 202 | 203 | /// 204 | /// Gets the command to close the current document. 205 | /// 206 | public ICommand CloseCommand { get; private set; } 207 | 208 | /// 209 | /// Gets or sets the source code document object to display to the user. 210 | /// 211 | public TextDocument CodeDocument 212 | { 213 | get => _codeDocument; 214 | set 215 | { 216 | _codeDocument = value; 217 | FireChangeEvent(nameof(CodeDocument)); 218 | } 219 | } 220 | 221 | /// 222 | /// Gets or sets the syntax highlighting definition for the source code text editor. 223 | /// 224 | public IHighlightingDefinition CodeDocumentSyntax 225 | { 226 | get => _codeSyntax; 227 | set 228 | { 229 | _codeSyntax = value; 230 | FireChangeEvent(nameof(CodeDocumentSyntax)); 231 | } 232 | } 233 | 234 | /// 235 | /// Gets or sets the source code text to display to the user. 236 | /// 237 | public string CodeDocumentText 238 | { 239 | get => _codeDocument.Text; 240 | private set 241 | { 242 | _codeDocument.Text = value; 243 | FireChangeEvent(nameof(CodeDocument)); 244 | } 245 | } 246 | 247 | /// 248 | /// Indicates whether or not to automatically generate source code when 249 | /// selecting DOM nodes. 250 | /// 251 | public bool GenerateSourceCode 252 | { 253 | get => _generateSourceCode; 254 | set 255 | { 256 | _generateSourceCode = value; 257 | var item = GenerateSourceCode ? SelectedItem as OpenXmlTreeViewItem : null; 258 | RefreshSourceCodeWindows(item); 259 | FireChangeEvent(nameof(GenerateSourceCode)); 260 | } 261 | } 262 | 263 | /// 264 | /// Indicates whether or not to enable syntax highlighting in the source code 265 | /// windows. 266 | /// 267 | public bool HighlightSyntax 268 | { 269 | get => _highlightSyntax; 270 | set 271 | { 272 | _highlightSyntax = value; 273 | ToggleSyntaxHighlighting(GenerateSourceCode && !(SelectedItem is null) && _highlightSyntax); 274 | FireChangeEvent(nameof(HighlightSyntax)); 275 | } 276 | } 277 | 278 | /// 279 | /// Indicates whether or not the selected item represents an 280 | /// object. 281 | /// 282 | public bool IsOpenXmlElement 283 | { 284 | get => _isOpenXmlElement; 285 | set 286 | { 287 | _isOpenXmlElement = value; 288 | FireChangeEvent(nameof(IsOpenXmlElement)); 289 | } 290 | } 291 | 292 | /// 293 | /// Gets the collection of objects that 294 | /// the user can select. 295 | /// 296 | public ReadOnlyObservableCollection LanguageDefinitions 297 | { 298 | get; 299 | private set; 300 | } 301 | 302 | /// 303 | /// Gets the command to open a new office 2007+ document. 304 | /// 305 | public ICommand OpenCommand { get; private set; } 306 | 307 | /// 308 | /// Gets the command that shuts down the application. 309 | /// 310 | public ICommand QuitCommand { get; private set; } 311 | 312 | /// 313 | /// Gets or sets the that is currently selected. 314 | /// 315 | public object SelectedItem 316 | { 317 | get => _selectedItem; 318 | set 319 | { 320 | _selectedItem = value; 321 | 322 | if (GenerateSourceCode && _selectedItem is OpenXmlTreeViewItem) 323 | { 324 | RefreshSourceCodeWindows(_selectedItem as OpenXmlTreeViewItem); 325 | } 326 | FireChangeEvent(nameof(SelectedItem)); 327 | } 328 | } 329 | 330 | /// 331 | /// Gets or sets the object currently 332 | /// selected by the user. 333 | /// 334 | public LanguageDefinition SelectedLanguage 335 | { 336 | get => _selectedLanguage; 337 | set 338 | { 339 | _selectedLanguage = value; 340 | if (GenerateSourceCode) 341 | { 342 | RefreshSourceCodeWindows(SelectedItem as OpenXmlTreeViewItem); 343 | } 344 | FireChangeEvent(nameof(SelectedLanguage)); 345 | } 346 | } 347 | 348 | /// 349 | /// Gets all of the openxml objects to display in the tree. 350 | /// 351 | public ObservableCollection TreeData 352 | { 353 | get => _treeData; 354 | private set 355 | { 356 | _treeData = value; 357 | FireChangeEvent(nameof(TreeData)); 358 | } 359 | } 360 | 361 | /// 362 | /// Indicates whether or not to have the text in the source code windows word wrap. 363 | /// 364 | public bool WordWrap 365 | { 366 | get => _wordWrap; 367 | set 368 | { 369 | _wordWrap = value; 370 | FireChangeEvent(nameof(WordWrap)); 371 | } 372 | } 373 | 374 | /// 375 | /// Gets or sets the source code to display to the user. 376 | /// 377 | public TextDocument XmlSourceDocument 378 | { 379 | get => _xmlDocument; 380 | set 381 | { 382 | _xmlDocument = value; 383 | FireChangeEvent(nameof(XmlSourceDocument)); 384 | } 385 | } 386 | 387 | /// 388 | /// Gets or sets the syntax highlighting definition for the xml document text editor. 389 | /// 390 | public IHighlightingDefinition XmlSourceDocumentSyntax 391 | { 392 | get => _xmlDocumentSyntax; 393 | set 394 | { 395 | _xmlDocumentSyntax = value; 396 | FireChangeEvent(nameof(XmlSourceDocumentSyntax)); 397 | } 398 | } 399 | 400 | /// 401 | /// Gets or sets the source code text to display to the user. 402 | /// 403 | public string XmlSourceDocumentText 404 | { 405 | get => _xmlDocument.Text; 406 | set 407 | { 408 | _xmlDocument.Text = value; 409 | FireChangeEvent(nameof(XmlSourceDocument)); 410 | } 411 | } 412 | 413 | #endregion 414 | 415 | #region Public Instance Methods 416 | 417 | /// 418 | /// Method to make sure that all unmanaged resources are released properly. 419 | /// 420 | public void Dispose() 421 | { 422 | if (_oPkg != null) 423 | { 424 | _oPkg.Close(); 425 | _oPkg.Dispose(); 426 | _oPkg = null; 427 | } 428 | if (_pkg != null) 429 | { 430 | _pkg.Close(); 431 | _pkg = null; 432 | } 433 | if (_stream != null) 434 | { 435 | _stream.Close(); 436 | _stream.Dispose(); 437 | _stream = null; 438 | } 439 | } 440 | 441 | #endregion 442 | 443 | #region Private Instance Methods 444 | 445 | /// 446 | /// Shortcut method to raise the 447 | /// event. 448 | /// 449 | /// 450 | /// Name of the property raising the event. 451 | /// 452 | private void FireChangeEvent(string name) => 453 | OnPropertyChanged(new PropertyChangedEventArgs(name)); 454 | 455 | /// 456 | /// Resets the main window controls and loads a requested OpenXml based file. 457 | /// 458 | private void OpenOfficeDocument() 459 | { 460 | const string docxIdUri = "/word/document.xml"; 461 | const string xlsxIdUri = "/xl/workbook.xml"; 462 | const string pptxIdUri = "/ppt/presentation.xml"; 463 | const string fileFilter = 464 | "All Microsoft Office 2007+ valid documents|*.xlsx;*.xlsm;*.xltx;*.pptx;*.pptm;*.potx;*.docx;*.docm;*.dotx;*.dotm" + 465 | "|Microsoft Excel 2007+ documents|*.xlsx;*.xlsm;*.xltx" + 466 | "|Microsoft Powerpoint 2007+ documents|*.pptx;*.pptm;*.potx" + 467 | "|Microsoft Word 2007+ documents|*.docx;*.docm;*.dotx;*.dotm" + 468 | "|All files|*.*"; 469 | 470 | bool? dialogResult; 471 | OpenFileDialog ofDialog = new() 472 | { 473 | InitialDirectory = _currentFileDirectory.FullName, 474 | Multiselect = false, 475 | CheckFileExists = true, 476 | CheckPathExists = true, 477 | Filter = fileFilter, 478 | FilterIndex = 1 479 | }; 480 | 481 | dialogResult = ofDialog.ShowDialog(); 482 | if (!dialogResult.GetValueOrDefault(false)) 483 | { 484 | // If the user cancels out; exit method. 485 | return; 486 | } 487 | 488 | // Ensure that everything is cleared out before proceeding 489 | Dispose(); 490 | CodeDocument.FileName = null; 491 | XmlSourceDocument.FileName = null; 492 | CodeDocumentText = String.Empty; 493 | XmlSourceDocumentText = String.Empty; 494 | _fileName = String.Empty; 495 | 496 | // Get the selected file details 497 | FileInfo fi = new(ofDialog.FileName); 498 | _currentFileDirectory = fi.Directory; 499 | _fileName = fi.Name; 500 | _stream = fi.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 501 | _pkg = Package.Open(_stream); 502 | 503 | // Setup a quick look up for easier package validation 504 | Dictionary> quickPicks = new(3) 505 | { 506 | { docxIdUri, WordprocessingDocument.Open }, 507 | { xlsxIdUri, SpreadsheetDocument.Open }, 508 | { pptxIdUri, PresentationDocument.Open } 509 | }; 510 | 511 | foreach (KeyValuePair> qp in quickPicks) 512 | { 513 | if (_pkg.PartExists(new Uri(qp.Key, UriKind.Relative))) 514 | { 515 | _oPkg = qp.Value.Invoke(_pkg); 516 | break; 517 | } 518 | } 519 | 520 | // Make sure that a valid package was found before proceeding. 521 | if (_oPkg == null) 522 | { 523 | throw new InvalidDataException("Selected file is not a known/valid OpenXml document"); 524 | } 525 | 526 | // Wrap it up 527 | OpenXmlPackageTreeViewItem mainItem = new(_oPkg) { Header = _fileName }; 528 | _treeData.Clear(); 529 | _treeData.Add(mainItem); 530 | } 531 | 532 | /// 533 | /// Refreshes the controls 534 | /// in the main window. 535 | /// 536 | /// 537 | /// The currently selected by the user. 538 | /// 539 | /// 540 | /// Passing as the will cause 541 | /// the controls to clear their 542 | /// contents. 543 | /// 544 | private void RefreshSourceCodeWindows(OpenXmlTreeViewItem item) 545 | { 546 | if (item is null) 547 | { 548 | CodeDocument.FileName = null; 549 | XmlSourceDocument.FileName = null; 550 | CodeDocumentText = String.Empty; 551 | XmlSourceDocumentText = String.Empty; 552 | ToggleSyntaxHighlighting(false); 553 | } 554 | else 555 | { 556 | var randName = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()); 557 | CodeDocument.FileName = randName + "." + SelectedLanguage.Provider.FileExtension; 558 | XmlSourceDocument.FileName = randName + ".xml"; 559 | 560 | CodeDocumentText = item.BuildCodeDomTextDocument(SelectedLanguage.Provider); 561 | XmlSourceDocumentText = item.BuildXmlTextDocument(); 562 | 563 | ToggleSyntaxHighlighting(HighlightSyntax); 564 | } 565 | IsOpenXmlElement = !String.IsNullOrWhiteSpace(XmlSourceDocumentText); 566 | FireChangeEvent(nameof(IsOpenXmlElement)); 567 | } 568 | 569 | /// 570 | /// Enables or disables syntax highlighting in the code windows. 571 | /// 572 | /// 573 | /// to turn on syntax highlighting; 574 | /// to turn it off. 575 | /// 576 | private void ToggleSyntaxHighlighting(bool enable) 577 | { 578 | CodeDocumentSyntax = enable ? SelectedLanguage.Highlighting : null; 579 | XmlSourceDocumentSyntax = enable ? _defaultXmlDefinition : null; 580 | } 581 | 582 | #endregion 583 | } 584 | } 585 | -------------------------------------------------------------------------------- /src/DocxToSource.Wpf/MainWindowModel.cs: -------------------------------------------------------------------------------- 1 | /* MIT License 2 | 3 | Copyright (c) 2020 Ryan Boggs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 6 | software and associated documentation files (the "Software"), to deal in the Software 7 | without restriction, including without limitation the rights to use, copy, modify, 8 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 9 | permit persons to whom the Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all copies 13 | or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 16 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 17 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 18 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | using DocumentFormat.OpenXml; 24 | using DocumentFormat.OpenXml.Packaging; 25 | using DocxToSource.Wpf.Controls; 26 | using DocxToSource.Wpf.Languages; 27 | using ICSharpCode.AvalonEdit.Document; 28 | using ICSharpCode.AvalonEdit.Highlighting; 29 | using Microsoft.Win32; 30 | using Prism.Commands; 31 | using Prism.Mvvm; 32 | using System; 33 | using System.Collections.Generic; 34 | using System.Collections.ObjectModel; 35 | using System.ComponentModel; 36 | using System.IO; 37 | using System.IO.Packaging; 38 | using System.Windows; 39 | using System.Windows.Input; 40 | 41 | namespace DocxToSource.Wpf 42 | { 43 | /// 44 | /// View model class for the main window. 45 | /// 46 | public class MainWindowModel : BindableBase, IDisposable 47 | { 48 | #region Private Static Fields 49 | 50 | /// 51 | /// Holds the default to use for 52 | /// the Xml window. 53 | /// 54 | private static readonly IHighlightingDefinition _defaultXmlDefinition; 55 | 56 | #endregion 57 | 58 | #region Private Instance Fields 59 | 60 | /// 61 | /// Holds the source code of the current selected openxml object. 62 | /// 63 | private TextDocument _codeDocument; 64 | 65 | /// 66 | /// Holds the highlighting definition for the source code text editor. 67 | /// 68 | private IHighlightingDefinition _codeSyntax; 69 | 70 | /// 71 | /// Holds the object containing the full path 72 | /// to use as the initial path in any OpenFileDialog windows. 73 | /// 74 | private DirectoryInfo _currentFileDirectory; 75 | 76 | /// 77 | /// Holds the full path and filename of the file that is currently open. 78 | /// 79 | private string _fileName; 80 | 81 | /// 82 | /// Indicates whether or not to automatically generate source code when 83 | /// selecting DOM nodes. 84 | /// 85 | private bool _generateSourceCode; 86 | 87 | /// 88 | /// Indicates whether or not to enable syntax highlighting in the source code 89 | /// windows. 90 | /// 91 | private bool _highlightSyntax; 92 | 93 | /// 94 | /// Indicates whether or not the selected item represents an 95 | /// object. 96 | /// 97 | private bool _isOpenXmlElement; 98 | 99 | /// 100 | /// Holds the openxml file package the is currently being reviewed. 101 | /// 102 | private OpenXmlPackage _oPkg; 103 | 104 | /// 105 | /// Holds the raw package used to stage the stream information for 106 | /// validation purposes. 107 | /// 108 | private Package _pkg; 109 | 110 | /// 111 | /// Holds the current treeviewitem that is currently selected in the treeview. 112 | /// 113 | private object _selectedItem; 114 | 115 | /// 116 | /// Holds the currently selected object. 117 | /// 118 | private LanguageDefinition _selectedLanguage; 119 | 120 | /// 121 | /// Holds the io stream containing the contents of the openxml file package. 122 | /// 123 | private Stream _stream; 124 | 125 | /// 126 | /// Holds the detailed exception information to display in the tree list view. 127 | /// 128 | private ObservableCollection _treeData; 129 | 130 | /// 131 | /// Indicates whether or not to have the text in the source code windows word wrap. 132 | /// 133 | private bool _wordWrap; 134 | 135 | /// 136 | /// Holds the xml code fo the current selected openxml element. 137 | /// 138 | private TextDocument _xmlDocument; 139 | 140 | /// 141 | /// Holds the highlighting definition for the XML Text Editor 142 | /// 143 | private IHighlightingDefinition _xmlDocumentSyntax; 144 | 145 | #endregion 146 | 147 | #region Static Constructors 148 | 149 | /// 150 | /// Static Constructor. 151 | /// 152 | static MainWindowModel() 153 | { 154 | // Setup the default Xml definition to use when syntax highlighting is requested 155 | _defaultXmlDefinition = HighlightingManager.Instance.GetDefinition("XML"); 156 | } 157 | 158 | #endregion 159 | 160 | #region Public Constructors 161 | 162 | /// 163 | /// Initializes a new instance of the class that 164 | /// is empty. 165 | /// 166 | public MainWindowModel() : base() 167 | { 168 | _currentFileDirectory = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)); 169 | _treeData = new ObservableCollection(); 170 | 171 | var langeDefs = new ObservableCollection(); 172 | LanguageDefinitions = new ReadOnlyObservableCollection(langeDefs); 173 | 174 | // Load the language definition list 175 | langeDefs.Add(new CSharpLanguageDefinition()); 176 | langeDefs.Add(new VBLanguageDefinition()); 177 | //langeDefs.Add(new BooLanguageDefinition()); 178 | 179 | // Set the default language 180 | SelectedLanguage = LanguageDefinitions[0]; 181 | 182 | _codeDocument = new TextDocument(); 183 | _xmlDocument = new TextDocument(); 184 | 185 | CloseCommand = new DelegateCommand(() => 186 | { 187 | Dispose(); 188 | _treeData.Clear(); 189 | RefreshSourceCodeWindows(null); 190 | }); 191 | OpenCommand = new DelegateCommand(OpenOfficeDocument); 192 | 193 | QuitCommand = new DelegateCommand(() => 194 | { 195 | Dispose(); 196 | Application.Current.Shutdown(); 197 | }); 198 | } 199 | 200 | #endregion 201 | 202 | #region Public Instance Properties 203 | 204 | /// 205 | /// Gets the command to close the current document. 206 | /// 207 | public ICommand CloseCommand { get; private set; } 208 | 209 | /// 210 | /// Gets or sets the source code document object to display to the user. 211 | /// 212 | public TextDocument CodeDocument 213 | { 214 | get { return _codeDocument; } 215 | set 216 | { 217 | _codeDocument = value; 218 | FireChangeEvent(nameof(CodeDocument)); 219 | } 220 | } 221 | 222 | /// 223 | /// Gets or sets the syntax highlighting definition for the source code text editor. 224 | /// 225 | public IHighlightingDefinition CodeDocumentSyntax 226 | { 227 | get { return _codeSyntax; } 228 | set 229 | { 230 | _codeSyntax = value; 231 | FireChangeEvent(nameof(CodeDocumentSyntax)); 232 | } 233 | } 234 | 235 | /// 236 | /// Gets or sets the source code text to display to the user. 237 | /// 238 | public string CodeDocumentText 239 | { 240 | get { return _codeDocument.Text; } 241 | private set 242 | { 243 | _codeDocument.Text = value; 244 | FireChangeEvent(nameof(CodeDocument)); 245 | } 246 | } 247 | 248 | /// 249 | /// Indicates whether or not to automatically generate source code when 250 | /// selecting DOM nodes. 251 | /// 252 | public bool GenerateSourceCode 253 | { 254 | get { return _generateSourceCode; } 255 | set 256 | { 257 | _generateSourceCode = value; 258 | var item = GenerateSourceCode ? SelectedItem as OpenXmlTreeViewItem : null; 259 | RefreshSourceCodeWindows(item); 260 | FireChangeEvent(nameof(GenerateSourceCode)); 261 | } 262 | } 263 | 264 | /// 265 | /// Indicates whether or not to enable syntax highlighting in the source code 266 | /// windows. 267 | /// 268 | public bool HighlightSyntax 269 | { 270 | get { return _highlightSyntax; } 271 | set 272 | { 273 | _highlightSyntax = value; 274 | ToggleSyntaxHighlighting(GenerateSourceCode && !(SelectedItem is null) && _highlightSyntax); 275 | FireChangeEvent(nameof(HighlightSyntax)); 276 | } 277 | } 278 | 279 | /// 280 | /// Indicates whether or not the selected item represents an 281 | /// object. 282 | /// 283 | public bool IsOpenXmlElement 284 | { 285 | get { return _isOpenXmlElement; } 286 | set 287 | { 288 | _isOpenXmlElement = value; 289 | FireChangeEvent(nameof(IsOpenXmlElement)); 290 | } 291 | } 292 | 293 | /// 294 | /// Gets the collection of objects that 295 | /// the user can select. 296 | /// 297 | public ReadOnlyObservableCollection LanguageDefinitions 298 | { 299 | get; 300 | private set; 301 | } 302 | 303 | /// 304 | /// Gets the command to open a new office 2007+ document. 305 | /// 306 | public ICommand OpenCommand { get; private set; } 307 | 308 | /// 309 | /// Gets the command that shuts down the application. 310 | /// 311 | public ICommand QuitCommand { get; private set; } 312 | 313 | /// 314 | /// Gets or sets the that is currently selected. 315 | /// 316 | public object SelectedItem 317 | { 318 | get { return _selectedItem; } 319 | set 320 | { 321 | _selectedItem = value; 322 | 323 | if (GenerateSourceCode && _selectedItem is OpenXmlTreeViewItem) 324 | { 325 | RefreshSourceCodeWindows(_selectedItem as OpenXmlTreeViewItem); 326 | } 327 | FireChangeEvent(nameof(SelectedItem)); 328 | } 329 | } 330 | 331 | /// 332 | /// Gets or sets the object currently 333 | /// selected by the user. 334 | /// 335 | public LanguageDefinition SelectedLanguage 336 | { 337 | get { return _selectedLanguage; } 338 | set 339 | { 340 | _selectedLanguage = value; 341 | if (GenerateSourceCode) 342 | { 343 | RefreshSourceCodeWindows(SelectedItem as OpenXmlTreeViewItem); 344 | } 345 | FireChangeEvent(nameof(SelectedLanguage)); 346 | } 347 | } 348 | 349 | /// 350 | /// Gets all of the openxml objects to display in the tree. 351 | /// 352 | public ObservableCollection TreeData 353 | { 354 | get { return _treeData; } 355 | private set 356 | { 357 | _treeData = value; 358 | FireChangeEvent(nameof(TreeData)); 359 | } 360 | } 361 | 362 | /// 363 | /// Indicates whether or not to have the text in the source code windows word wrap. 364 | /// 365 | public bool WordWrap 366 | { 367 | get { return _wordWrap; } 368 | set 369 | { 370 | _wordWrap = value; 371 | FireChangeEvent(nameof(WordWrap)); 372 | } 373 | } 374 | 375 | /// 376 | /// Gets or sets the source code to display to the user. 377 | /// 378 | public TextDocument XmlSourceDocument 379 | { 380 | get { return _xmlDocument; } 381 | set 382 | { 383 | _xmlDocument = value; 384 | FireChangeEvent(nameof(XmlSourceDocument)); 385 | } 386 | } 387 | 388 | /// 389 | /// Gets or sets the syntax highlighting definition for the xml document text editor. 390 | /// 391 | public IHighlightingDefinition XmlSourceDocumentSyntax 392 | { 393 | get { return _xmlDocumentSyntax; } 394 | set 395 | { 396 | _xmlDocumentSyntax = value; 397 | FireChangeEvent(nameof(XmlSourceDocumentSyntax)); 398 | } 399 | } 400 | 401 | /// 402 | /// Gets or sets the source code text to display to the user. 403 | /// 404 | public string XmlSourceDocumentText 405 | { 406 | get { return _xmlDocument.Text; } 407 | set 408 | { 409 | _xmlDocument.Text = value; 410 | FireChangeEvent(nameof(XmlSourceDocument)); 411 | } 412 | } 413 | 414 | #endregion 415 | 416 | #region Public Instance Methods 417 | 418 | /// 419 | /// Method to make sure that all unmanaged resources are released properly. 420 | /// 421 | public void Dispose() 422 | { 423 | if (_oPkg != null) 424 | { 425 | _oPkg.Close(); 426 | _oPkg.Dispose(); 427 | _oPkg = null; 428 | } 429 | if (_pkg != null) 430 | { 431 | _pkg.Close(); 432 | _pkg = null; 433 | } 434 | if (_stream != null) 435 | { 436 | _stream.Close(); 437 | _stream.Dispose(); 438 | _stream = null; 439 | } 440 | } 441 | 442 | #endregion 443 | 444 | #region Private Instance Methods 445 | 446 | /// 447 | /// Shortcut method to raise the 448 | /// event. 449 | /// 450 | /// 451 | /// Name of the property raising the event. 452 | /// 453 | private void FireChangeEvent(string name) => 454 | OnPropertyChanged(new PropertyChangedEventArgs(name)); 455 | 456 | /// 457 | /// Resets the main window controls and loads a requested OpenXml based file. 458 | /// 459 | private void OpenOfficeDocument() 460 | { 461 | const string docxIdUri = "/word/document.xml"; 462 | const string xlsxIdUri = "/xl/workbook.xml"; 463 | const string pptxIdUri = "/ppt/presentation.xml"; 464 | const string fileFilter = 465 | "All Microsoft Office 2007+ valid documents (*.xlsx;*.xlsm;*.pptx;*.pptm;*.docx;*.docm)|*.xlsx;*.xlsm;*.pptx;*.pptm;*.docx;*.docm" + 466 | "|Microsoft Excel 2007+ documents (*.xlsx;*.xlsm)|*.xlsx;*.xlsm" + 467 | "|Microsoft Powerpoint 2007+ documents (*.pptx;*.pptm)|*.pptx;*.pptm" + 468 | "|Microsoft Word 2007+ documents (*.docx;*.docm)|*.docx;*.docm" + 469 | "|All files | *.*"; 470 | 471 | bool? dialogResult; 472 | var ofDialog = new OpenFileDialog() 473 | { 474 | InitialDirectory = _currentFileDirectory.FullName, 475 | Multiselect = false, 476 | CheckFileExists = true, 477 | CheckPathExists = true, 478 | ReadOnlyChecked = true, 479 | Filter = fileFilter, 480 | FilterIndex = 1 481 | }; 482 | 483 | dialogResult = ofDialog.ShowDialog(); 484 | if (!dialogResult.GetValueOrDefault(false)) 485 | { 486 | // If the user cancels out; exit method. 487 | return; 488 | } 489 | 490 | // Ensure that everything is cleared out before proceeding 491 | Dispose(); 492 | CodeDocument.FileName = null; 493 | XmlSourceDocument.FileName = null; 494 | CodeDocumentText = String.Empty; 495 | XmlSourceDocumentText = String.Empty; 496 | _fileName = String.Empty; 497 | 498 | // See if the user left the Open as readonly flag on 499 | var fShare = ofDialog.ReadOnlyChecked ? FileShare.ReadWrite : FileShare.Read; 500 | 501 | // Get the selected file details 502 | var fi = new FileInfo(ofDialog.FileName); 503 | _currentFileDirectory = fi.Directory; 504 | _fileName = fi.Name; 505 | _stream = fi.Open(FileMode.Open, FileAccess.Read, fShare); 506 | _pkg = Package.Open(_stream); 507 | 508 | // Setup a quick look up for easier package validation 509 | var quickPicks = new Dictionary>(3) 510 | { 511 | { docxIdUri, WordprocessingDocument.Open }, 512 | { xlsxIdUri, SpreadsheetDocument.Open }, 513 | { pptxIdUri, PresentationDocument.Open } 514 | }; 515 | 516 | foreach (var qp in quickPicks) 517 | { 518 | if (_pkg.PartExists(new Uri(qp.Key, UriKind.Relative))) 519 | { 520 | _oPkg = qp.Value.Invoke(_pkg); 521 | break; 522 | } 523 | } 524 | 525 | // Make sure that a valid package was found before proceeding. 526 | if (_oPkg == null) 527 | { 528 | throw new InvalidDataException("Selected file is not a known/valid OpenXml document"); 529 | } 530 | 531 | // Wrap it up 532 | var mainItem = new OpenXmlPackageTreeViewItem(_oPkg) { Header = _fileName }; 533 | _treeData.Clear(); 534 | _treeData.Add(mainItem); 535 | } 536 | 537 | /// 538 | /// Refreshes the controls 539 | /// in the main window. 540 | /// 541 | /// 542 | /// The currently selected by the user. 543 | /// 544 | /// 545 | /// Passing as the will cause 546 | /// the controls to clear their 547 | /// contents. 548 | /// 549 | private void RefreshSourceCodeWindows(OpenXmlTreeViewItem item) 550 | { 551 | if (item is null) 552 | { 553 | CodeDocument.FileName = null; 554 | XmlSourceDocument.FileName = null; 555 | CodeDocumentText = String.Empty; 556 | XmlSourceDocumentText = String.Empty; 557 | ToggleSyntaxHighlighting(false); 558 | } 559 | else 560 | { 561 | var randName = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()); 562 | CodeDocument.FileName = randName + "." + SelectedLanguage.Provider.FileExtension; 563 | XmlSourceDocument.FileName = randName + ".xml"; 564 | 565 | CodeDocumentText = item.BuildCodeDomTextDocument(SelectedLanguage.Provider); 566 | XmlSourceDocumentText = item.BuildXmlTextDocument(); 567 | 568 | ToggleSyntaxHighlighting(HighlightSyntax); 569 | } 570 | IsOpenXmlElement = !String.IsNullOrWhiteSpace(XmlSourceDocumentText); 571 | FireChangeEvent(nameof(IsOpenXmlElement)); 572 | } 573 | 574 | /// 575 | /// Enables or disables syntax highlighting in the code windows. 576 | /// 577 | /// 578 | /// to turn on syntax highlighting; 579 | /// to turn it off. 580 | /// 581 | private void ToggleSyntaxHighlighting(bool enable) 582 | { 583 | CodeDocumentSyntax = enable ? SelectedLanguage.Highlighting : null; 584 | XmlSourceDocumentSyntax = enable ? _defaultXmlDefinition : null; 585 | } 586 | 587 | #endregion 588 | } 589 | } 590 | --------------------------------------------------------------------------------