Browse Source

Fix file resolution when running XSLTs.

pull/374/merge
Matt Ward 11 years ago
parent
commit
37cf805fea
  1. 2
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/RunXslTransformCommand.cs
  2. 16
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs
  3. 42
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/XslTransformUrlResolver.cs
  4. 1
      src/AddIns/DisplayBindings/XmlEditor/Project/XmlEditor.csproj

2
src/AddIns/DisplayBindings/XmlEditor/Project/Src/RunXslTransformCommand.cs

@ -58,7 +58,7 @@ namespace ICSharpCode.XmlEditor @@ -58,7 +58,7 @@ namespace ICSharpCode.XmlEditor
if (xmlView.StylesheetFileName != null) {
try {
xmlView.RunXslTransform(GetStylesheetContent(xmlView.StylesheetFileName));
xmlView.RunXslTransform(GetStylesheetContent(xmlView.StylesheetFileName), xmlView.StylesheetFileName);
} catch (Exception ex) {
MessageService.ShowException(ex);
}

16
src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs

@ -461,7 +461,7 @@ namespace ICSharpCode.XmlEditor @@ -461,7 +461,7 @@ namespace ICSharpCode.XmlEditor
/// <summary>
/// Applys the stylesheet to the xml and displays the resulting output.
/// </summary>
public void RunXslTransform(string xsl)
public void RunXslTransform(string xsl, string transformFileName)
{
try {
SD.Workbench.GetPad(typeof(CompilerMessageView)).BringPadToFront();
@ -472,9 +472,9 @@ namespace ICSharpCode.XmlEditor @@ -472,9 +472,9 @@ namespace ICSharpCode.XmlEditor
if (editor == null) return;
if (IsWellFormed) {
if (IsValidXsl(xsl)) {
if (IsValidXsl(xsl, transformFileName)) {
try {
string transformedXml = Transform(editor.Document.Text, xsl);
string transformedXml = Transform(editor.Document.Text, xsl, transformFileName);
ShowTransformOutput(transformedXml);
} catch (XsltException ex) {
AddTask(GetFileNameFromInnerException(ex, StylesheetFileName), GetInnerExceptionErrorMessage(ex), ex.LineNumber, ex.LinePosition, TaskType.Error);
@ -507,7 +507,7 @@ namespace ICSharpCode.XmlEditor @@ -507,7 +507,7 @@ namespace ICSharpCode.XmlEditor
/// <param name="input">The input xml to transform.</param>
/// <param name="transform">The transform xml.</param>
/// <returns>The output of the transform.</returns>
static string Transform(string input, string transform)
static string Transform(string input, string transform, string transformFileName)
{
StringReader inputString = new StringReader(input);
XmlTextReader sourceDocument = new XmlTextReader(inputString);
@ -516,7 +516,7 @@ namespace ICSharpCode.XmlEditor @@ -516,7 +516,7 @@ namespace ICSharpCode.XmlEditor
XPathDocument transformDocument = new XPathDocument(transformString);
XslCompiledTransform xslTransform = new XslCompiledTransform();
xslTransform.Load(transformDocument, XsltSettings.TrustedXslt, new XmlUrlResolver());
xslTransform.Load(transformDocument, XsltSettings.TrustedXslt, new XslTransformUrlResolver(transformFileName));
MemoryStream outputStream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(outputStream, Encoding.UTF8);
@ -531,7 +531,7 @@ namespace ICSharpCode.XmlEditor @@ -531,7 +531,7 @@ namespace ICSharpCode.XmlEditor
/// <summary>
/// Validates the given xsl string,.
/// </summary>
bool IsValidXsl(string xml)
bool IsValidXsl(string xml, string transformFileName)
{
try {
SD.Workbench.GetPad(typeof(CompilerMessageView)).BringPadToFront();
@ -540,13 +540,13 @@ namespace ICSharpCode.XmlEditor @@ -540,13 +540,13 @@ namespace ICSharpCode.XmlEditor
XPathDocument doc = new XPathDocument(reader);
XslCompiledTransform xslTransform = new XslCompiledTransform();
xslTransform.Load(doc, XsltSettings.Default, new XmlUrlResolver());
xslTransform.Load(doc, XsltSettings.Default, new XslTransformUrlResolver(transformFileName));
return true;
} catch(XsltCompileException ex) {
AddTask(StylesheetFileName, GetInnerExceptionErrorMessage(ex), ex.LineNumber, ex.LinePosition, TaskType.Error);
} catch(XsltException ex) {
AddTask(StylesheetFileName, ex.Message, ex.LinePosition, ex.LineNumber, TaskType.Error);
AddTask(StylesheetFileName, GetInnerExceptionErrorMessage(ex), ex.LinePosition, ex.LineNumber, TaskType.Error);
} catch(XmlException ex) {
AddTask(StylesheetFileName, ex.Message, ex.LinePosition, ex.LineNumber, TaskType.Error);
}

42
src/AddIns/DisplayBindings/XmlEditor/Project/Src/XslTransformUrlResolver.cs

@ -0,0 +1,42 @@ @@ -0,0 +1,42 @@
// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.IO;
using System.Xml;
namespace ICSharpCode.XmlEditor
{
public class XslTransformUrlResolver : XmlUrlResolver
{
readonly Uri baseDirectory;
public XslTransformUrlResolver(string fileName)
{
this.baseDirectory = new Uri(Path.GetDirectoryName(fileName) + Path.DirectorySeparatorChar, UriKind.Absolute);
}
public override Uri ResolveUri(Uri baseUri, string relativeUri)
{
if (baseUri != null) {
return base.ResolveUri(baseUri, relativeUri);
}
return base.ResolveUri(baseDirectory, relativeUri);
}
}
}

1
src/AddIns/DisplayBindings/XmlEditor/Project/XmlEditor.csproj

@ -68,6 +68,7 @@ @@ -68,6 +68,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Src\XslTransformUrlResolver.cs" />
<None Include="XmlEditor.addin">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>

Loading…
Cancel
Save