From 29dc52e5aecc1b2174a26476b781450930ab2faa Mon Sep 17 00:00:00 2001 From: Matt Ward Date: Wed, 24 Sep 2008 18:12:36 +0000 Subject: [PATCH] Fixed SD2-1350. XSLT document function now supported when running transforms. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@3554 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../XmlEditor/Project/Src/XmlView.cs | 52 ++++++++++++++----- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs b/src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs index 5564f1f253..1d29cc6fa5 100644 --- a/src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs +++ b/src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs @@ -394,13 +394,16 @@ namespace ICSharpCode.XmlEditor if (IsWellFormed) { if (IsValidXsl(xsl)) { - string transformedXml = Transform(Text, xsl); - ShowTransformOutput(transformedXml); + try { + string transformedXml = Transform(Text, xsl); + ShowTransformOutput(transformedXml); + } catch (XsltException ex) { + AddTask(GetFileNameFromInnerException(ex, StylesheetFileName), GetInnerExceptionErrorMessage(ex), ex.LineNumber - 1, ex.LinePosition - 1, TaskType.Error); + } } } - ShowErrorList(); - + ShowErrorList(); } catch (Exception ex) { MessageService.ShowError(ex); } @@ -942,7 +945,7 @@ namespace ICSharpCode.XmlEditor XPathDocument transformDocument = new XPathDocument(transformString); XslCompiledTransform xslTransform = new XslCompiledTransform(); - xslTransform.Load(transformDocument, XsltSettings.Default, new XmlUrlResolver()); + xslTransform.Load(transformDocument, XsltSettings.TrustedXslt, new XmlUrlResolver()); MemoryStream outputStream = new MemoryStream(); XmlTextWriter writer = new XmlTextWriter(outputStream, Encoding.UTF8); @@ -1030,15 +1033,7 @@ namespace ICSharpCode.XmlEditor return true; } catch(XsltCompileException ex) { - string message = String.Empty; - - if(ex.InnerException != null) { - message = ex.InnerException.Message; - } else { - message = ex.ToString(); - } - - AddTask(StylesheetFileName, message, ex.LineNumber - 1, ex.LinePosition - 1, TaskType.Error); + AddTask(StylesheetFileName, GetInnerExceptionErrorMessage(ex), ex.LineNumber - 1, ex.LinePosition - 1, TaskType.Error); } catch(XsltException ex) { AddTask(StylesheetFileName, ex.Message, ex.LinePosition - 1, ex.LineNumber - 1, TaskType.Error); } catch(XmlException ex) { @@ -1048,6 +1043,17 @@ namespace ICSharpCode.XmlEditor return false; } + /// + /// Returns the inner exception message if there is one otherwise returns the exception's error message. + /// + static string GetInnerExceptionErrorMessage(Exception ex) + { + if(ex.InnerException != null) { + return ex.InnerException.Message; + } + return ex.Message; + } + /// /// Validates the XML in the editor against all the schemas in the /// schema manager. @@ -1275,5 +1281,23 @@ namespace ICSharpCode.XmlEditor { return AddInTree.BuildItems(editActionsPath, this, false).ToArray(); } + + /// + /// Tries to get the filename from the inner exception otherwise returns the default filename. + /// + /// + /// + /// + static string GetFileNameFromInnerException(Exception ex, string defaultFileName) + { + XmlException innerException = ex.InnerException as XmlException; + if (innerException != null) { + string fileName = innerException.SourceUri.Replace("file:///", String.Empty); + if (!String.IsNullOrEmpty(fileName)) { + return fileName; + } + } + return defaultFileName; + } } }