Creating a PublishCompare Plugin in C#
This example shows how to create a C# .NET PublishCompare plugin using the out-of-the-box implementation as starting point.
Before you begin
Important: The information and the procedure herein are provided as is without warranty of any kind, either express or implied. Check out the software requirements for compatible version numbers.
The following products need to be already installed on your machine:
- Microsoft .NET FrameWork
- Microsoft Visual Studio
Procedure
- Create a Visual Studio Project. In this example, the project is called
MyCustomComparePluginLibrary, and its type is Class Library. - Add a reference to a plugin SDK (Trisoft.InfoShare.Plugins.SDK.dll). For example, you can copy the file from \Applications\Common\Trisoft.InfoShare.Plugins.SDK.dll.
- Browse to the project, then right-click References, and select Add Reference.
- To select the reference you want to add, use the Browse button.
- Select Trisoft.InfoShare.Plugins.SDK.dll, then Add.
- To be able to view the code documentation of the Trisoft.InfoShare.Plugins.SDK.dll, also copy Trisoft.InfoShare.Plugins.SDK.xml.
- Add a reference to a plugin common helper (Trisoft.InfoShare.Plugins.Common.dll). For example, you can copy the file from \Applications\Plugins\Trisoft.InfoShare.Plugins.Common.dll.
- Browse to the project, then right-click References, and select Add Reference.
- To select the reference you want to add, use the Browse button.
- Select Trisoft.InfoShare.Plugins.Common.dll, then Add.
- To be able to view the code documentation of the Trisoft.InfoShare.Plugins.Common.dll, also copy Trisoft.InfoShare.Plugins.Common.xml.
- Add Trisoft.Utilities.ChangeTracker.dll
- Browse to the project, then right-click References, and select Add Reference.
- To select the reference you want to add, use the Browse button.
- Select Trisoft.Utilities.ChangeTracker.dll, then Add.
- Add a reference to the System.ComponentModel.Composition assembly.
- Create a new
MyCustomComparePluginclass.The name of the plugin corresponds to the value assigned to ExportAttribute, and it needs to be unique.using System; using System.ComponentModel.Composition; using Trisoft.InfoShare.Plugins.SDK; using Trisoft.InfoShare.Plugins.SDK.Publish; using Trisoft.Utilities.ChangeTracker; namespace MyCustomComparePluginLibrary { /// <summary> /// This plugin will compare 2 content or metadata XML files and produce an XML result file with insert_begin, insert_end, /// remove_begin and remove_end processing instructions /// </summary> [Export("MyCustomCompare", typeof(IPublishComparePlugin))] [PartCreationPolicy(CreationPolicy.NonShared)] public class MyCustomComparePlugin : IPublishComparePlugin { #region Private fields /// <summary> /// ChangeTracker compare instance /// </summary> private Trisoft.Utilities.ChangeTracker.Compare _changeTracker; #endregion #region Public methods /// <summary> /// Initializes the plugin /// </summary> /// <param name="configuration">The configuration containing all necessary information to run the plugin</param> public void Initialize(IPublishCompareConfiguration configuration) { // Initialize will be called only once per publish // parameters could be read from configuration instead of being hercoded var changeTrackerSettings = new Utilities.ChangeTracker.Settings(new string[] {"entry", "stentry"}, new string[] {"image", "topicref"}, new string[] {"navtitle", "ishcondition"}); _changeTracker = new Utilities.ChangeTracker.Compare(configuration.XmlResolver, changeTrackerSettings); } /// <summary> /// Compares 2 XML metadata files /// </summary> /// <param name="ishType">Type of the objects to compare</param> /// <param name="compareVersionMetadataFilePath">File path of the version metadata file to compare with</param> /// <param name="currentVersionMetadataFilePath">File path of the current version metadata file</param> /// <param name="resultMetadataFilePath">File path of the result metadata file</param> /// <returns>PublishCompareResult</returns> public PublishCompareResult CompareMetadata(IshType ishType, string compareVersionMetadataFilePath, string currentVersionMetadataFilePath, string resultMetadataFilePath) { return DoCompareXml(ishType, compareVersionMetadataFilePath, currentVersionMetadataFilePath, resultMetadataFilePath); } /// <summary> /// Compares 2 XML content files /// </summary> /// <param name="ishType">Type of the objects to compare</param> /// <param name="compareVersionFilePath">File path of the version to compare with</param> /// <param name="currentVersionFilePath">File path of the current version</param> /// <param name="resultFilePath">File path of the result file</param> /// <returns>PublishCompareResult</returns> public PublishCompareResult CompareXml(IshType ishType, string compareVersionFilePath, string currentVersionFilePath, string resultFilePath) { return DoCompareXml(ishType, compareVersionFilePath, currentVersionFilePath, resultFilePath); } #endregion #region Private methods /// <summary> /// Compares 2 XML content files /// </summary> /// <param name="ishType">Type of the objects to compare</param> /// <param name="compareVersionFilePath">File path of the version to compare with</param> /// <param name="currentVersionFilePath">File path of the current version</param> /// <param name="resultFilePath">File path of the result file</param> /// <returns>PublishCompareResult</returns> private PublishCompareResult DoCompareXml(IshType ishType, string compareVersionFilePath, string currentVersionFilePath, string resultFilePath) { XMLCompareOptions changeTrackerCompareOptions = XMLCompareOptions.IgnoreSpace | XMLCompareOptions.TrimSpace | XMLCompareOptions.IgnoreConditionAttributes; // As one of the filepaths might be empty, check which ones are filled in bool compareProvided = !String.IsNullOrEmpty(compareVersionFilePath); bool currentProvided = !String.IsNullOrEmpty(currentVersionFilePath); // The XmlFileDiff will save it's result to resultFilePath. // If there is only a current version file, the result file will contain all XML in insert_begin, insert_end processing instructions // If there is only a compare version file, the result file will contain all XML in remove_begin, remove_end processing instructions // If both files exist and have differences, the result file can contain a mixure of insert and remove processing attributes // If both files exist and do not have differences, the result file will not contain insert and remove processing attributes if (_changeTracker.XmlFileDiff(compareVersionFilePath, currentVersionFilePath, changeTrackerCompareOptions, resultFilePath)) { // change detected if (compareProvided && currentProvided) { // Both files were provided => Changed return PublishCompareResult.Changed; } else if (currentProvided) { // Only present in the current version = New return PublishCompareResult.New; } else if (compareProvided) { // Only present in the Compare version => Changed return PublishCompareResult.Deleted; } } return PublishCompareResult.Unchanged; } #endregion } }