public static string Build(string xmlpath, string xslpath, IPrincipal user, bool isFlyout, string userInfo) { var xslargs = new XsltArgumentList(); xslargs.AddParam("isFlyout", "", isFlyout); xslargs.AddParam("userInfo", "", userInfo); XElement xml = XElement.Load(xmlpath); xml.Descendants().ToList().ForEach((xe) => { if (xe.Attributes().Any(xe_ => xe_.Name == "roles")) { var roles = xe.Attributes().First(a_ => a_.Name == "roles").Value; if (roles != "*" && (user == null || !user.IsInRole(roles))) { xe.Remove(); } } }); var xsl = new XmlDocument(); xsl.Load(xslpath); var xslt = new XmlTransformer(xml.ToString(SaveOptions.DisableFormatting), xsl.OuterXml); return(xslt.Transform(xslargs)); }
public void TestXmlTransform() { string sourceFile = this.WriteTextToTempFile(TestUtilities.Source01); string transformFile = this.WriteTextToTempFile(TestUtilities.Transform01); string expectedResultFile = this.WriteTextToTempFile(TestUtilities.Result01); string destFile = this.GetTempFilename(true); ITransformer transformer = new XmlTransformer(); transformer.Transform(sourceFile, transformFile, destFile); Assert.True(File.Exists(sourceFile)); Assert.True(File.Exists(transformFile)); Assert.True(File.Exists(destFile)); string actualResult = File.ReadAllText(destFile); string expectedResult = File.ReadAllText(expectedResultFile); Assert.Equal(expectedResult.Trim(), actualResult.Trim()); }
/// <summary> /// Shows a preview of the transformation in a temporary file. /// </summary> /// <param name="hier">Current IVsHierarchy</param> /// <param name="sourceFile">Full path to the file to be trasnformed</param> /// <param name="transformFile">Full path to the transformation file</param> private void PreviewTransform(IVsHierarchy hier, string sourceFile, string transformFile) { if (string.IsNullOrWhiteSpace(sourceFile)) { throw new ArgumentNullException("sourceFile"); } if (string.IsNullOrWhiteSpace(transformFile)) { throw new ArgumentNullException("transformFile"); } if (!File.Exists(sourceFile)) { throw new FileNotFoundException(string.Format(CultureInfo.CurrentCulture, Resources.Resources.Error_SourceFileNotFound, sourceFile), sourceFile); } if (!File.Exists(transformFile)) { throw new FileNotFoundException(string.Format(CultureInfo.CurrentCulture, Resources.Resources.Error_TransformFileNotFound, transformFile), transformFile); } // Get our options using (OptionsDialogPage optionsPage = new OptionsDialogPage()) { optionsPage.LoadSettingsFromStorage(); this.LogMessageWriteLineFormat("SlowCheetah PreviewTransform"); FileInfo sourceFileInfo = new FileInfo(sourceFile); // dest file string destFile = PackageUtilities.GetTempFilename(true, sourceFileInfo.Extension); this.TempFilesCreated.Add(destFile); // perform the transform and then display the result into the diffmerge tool that comes with VS. // If for some reason we can't find it, we just open it in an editor window this.errorListProvider.Tasks.Clear(); ITransformationLogger logger = new TransformationPreviewLogger(this.errorListProvider, hier); ITransformer transformer = new XmlTransformer(logger, false); if (!transformer.Transform(sourceFile, transformFile, destFile)) { throw new TransformFailedException(Resources.Resources.TransformPreview_ErrorMessage); } // Does the customer want a preview? if (optionsPage.EnablePreview == false) { ProjectUtilities.GetDTE().ItemOperations.OpenFile(destFile); } else { // If the diffmerge service is available (dev11) and no diff tool is specified, or diffmerge.exe is specifed we use the service IVsDifferenceService diffService = this.GetService(typeof(SVsDifferenceService)) as IVsDifferenceService; if (diffService != null && (string.IsNullOrEmpty(optionsPage.PreviewToolExecutablePath) || optionsPage.PreviewToolExecutablePath.EndsWith(@"\diffmerge.exe", StringComparison.OrdinalIgnoreCase))) { string sourceName = Path.GetFileName(sourceFile); string leftLabel = string.Format(CultureInfo.CurrentCulture, Resources.Resources.TransformPreview_LeftLabel, sourceName); string rightLabel = string.Format(CultureInfo.CurrentCulture, Resources.Resources.TransformPreview_RightLabel, sourceName, Path.GetFileName(transformFile)); string caption = string.Format(CultureInfo.CurrentCulture, Resources.Resources.TransformPreview_Caption, sourceName); string tooltip = string.Format(CultureInfo.CurrentCulture, Resources.Resources.TransformPreview_ToolTip, sourceName); diffService.OpenComparisonWindow2(sourceFile, destFile, caption, tooltip, leftLabel, rightLabel, null, null, (uint)__VSDIFFSERVICEOPTIONS.VSDIFFOPT_RightFileIsTemporary); } else if (string.IsNullOrEmpty(optionsPage.PreviewToolExecutablePath)) { throw new FileNotFoundException(Resources.Resources.Error_NoPreviewToolSpecified); } else if (!File.Exists(optionsPage.PreviewToolExecutablePath)) { throw new FileNotFoundException(string.Format(Resources.Resources.Error_CantFindPreviewTool, optionsPage.PreviewToolExecutablePath), optionsPage.PreviewToolExecutablePath); } else { // Quote the filenames... ProcessStartInfo psi = new ProcessStartInfo(optionsPage.PreviewToolExecutablePath, string.Format(optionsPage.PreviewToolCommandLine, "\"" + sourceFile + "\"", "\"" + destFile + "\"")) { CreateNoWindow = true, UseShellExecute = false }; System.Diagnostics.Process.Start(psi); } } } // TODO: Instead of creating a file and then deleting it later we could instead do this // http://matthewmanela.com/blog/the-problem-with-the-envdte-itemoperations-newfile-method/ // http://social.msdn.microsoft.com/Forums/en/vsx/thread/eb032063-eb4d-42e0-84e8-dec64bf42abf }
/// <summary> /// Shows a preview of the transformation in a temporary file. /// </summary> /// <param name="hier">Current IVsHierarchy</param> /// <param name="sourceFile">Full path to the file to be trasnformed</param> /// <param name="transformFile">Full path to the transformation file</param> private void PreviewTransform(IVsHierarchy hier, string sourceFile, string transformFile) { if (string.IsNullOrWhiteSpace(sourceFile)) { throw new ArgumentNullException("sourceFile"); } if (string.IsNullOrWhiteSpace(transformFile)) { throw new ArgumentNullException("transformFile"); } if (!File.Exists(sourceFile)) { throw new FileNotFoundException(string.Format(CultureInfo.CurrentCulture, Resources.Resources.Error_SourceFileNotFound, sourceFile), sourceFile); } if (!File.Exists(transformFile)) { throw new FileNotFoundException(string.Format(CultureInfo.CurrentCulture, Resources.Resources.Error_TransformFileNotFound, transformFile), transformFile); } // Get our options using (OptionsDialogPage optionsPage = new OptionsDialogPage()) { optionsPage.LoadSettingsFromStorage(); this.LogMessageWriteLineFormat("SlowCheetah PreviewTransform"); FileInfo sourceFileInfo = new FileInfo(sourceFile); // dest file string destFile = PackageUtilities.GetTempFilename(true, sourceFileInfo.Extension); this.TempFilesCreated.Add(destFile); // perform the transform and then display the result into the diffmerge tool that comes with VS. // If for some reason we can't find it, we just open it in an editor window this.errorListProvider.Tasks.Clear(); ITransformationLogger logger = new TransformationPreviewLogger(this.errorListProvider, hier); ITransformer transformer = new XmlTransformer(logger, false); if (!transformer.Transform(sourceFile, transformFile, destFile)) { throw new TransformFailedException(Resources.Resources.TransformPreview_ErrorMessage); } // Does the customer want a preview? if (optionsPage.EnablePreview == false) { ProjectUtilities.GetDTE().ItemOperations.OpenFile(destFile); } else { // If the diffmerge service is available (dev11) and no diff tool is specified, or diffmerge.exe is specifed we use the service if (this.GetService(typeof(SVsDifferenceService)) is IVsDifferenceService diffService && (string.IsNullOrEmpty(optionsPage.PreviewToolExecutablePath) || optionsPage.PreviewToolExecutablePath.EndsWith(@"\diffmerge.exe", StringComparison.OrdinalIgnoreCase))) { string sourceName = Path.GetFileName(sourceFile); string leftLabel = string.Format(CultureInfo.CurrentCulture, Resources.Resources.TransformPreview_LeftLabel, sourceName); string rightLabel = string.Format(CultureInfo.CurrentCulture, Resources.Resources.TransformPreview_RightLabel, sourceName, Path.GetFileName(transformFile)); string caption = string.Format(CultureInfo.CurrentCulture, Resources.Resources.TransformPreview_Caption, sourceName); string tooltip = string.Format(CultureInfo.CurrentCulture, Resources.Resources.TransformPreview_ToolTip, sourceName); diffService.OpenComparisonWindow2(sourceFile, destFile, caption, tooltip, leftLabel, rightLabel, null, null, (uint)__VSDIFFSERVICEOPTIONS.VSDIFFOPT_RightFileIsTemporary); }