Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit 37cf805

Browse files
committed
Fix file resolution when running XSLTs.
1 parent 86c2927 commit 37cf805

4 files changed

Lines changed: 52 additions & 9 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public override void Run()
5858

5959
if (xmlView.StylesheetFileName != null) {
6060
try {
61-
xmlView.RunXslTransform(GetStylesheetContent(xmlView.StylesheetFileName));
61+
xmlView.RunXslTransform(GetStylesheetContent(xmlView.StylesheetFileName), xmlView.StylesheetFileName);
6262
} catch (Exception ex) {
6363
MessageService.ShowException(ex);
6464
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ static void DisplayValidationWarning(string fileName, string message, int column
461461
/// <summary>
462462
/// Applys the stylesheet to the xml and displays the resulting output.
463463
/// </summary>
464-
public void RunXslTransform(string xsl)
464+
public void RunXslTransform(string xsl, string transformFileName)
465465
{
466466
try {
467467
SD.Workbench.GetPad(typeof(CompilerMessageView)).BringPadToFront();
@@ -472,9 +472,9 @@ public void RunXslTransform(string xsl)
472472
if (editor == null) return;
473473

474474
if (IsWellFormed) {
475-
if (IsValidXsl(xsl)) {
475+
if (IsValidXsl(xsl, transformFileName)) {
476476
try {
477-
string transformedXml = Transform(editor.Document.Text, xsl);
477+
string transformedXml = Transform(editor.Document.Text, xsl, transformFileName);
478478
ShowTransformOutput(transformedXml);
479479
} catch (XsltException ex) {
480480
AddTask(GetFileNameFromInnerException(ex, StylesheetFileName), GetInnerExceptionErrorMessage(ex), ex.LineNumber, ex.LinePosition, TaskType.Error);
@@ -507,7 +507,7 @@ void ShowTransformOutput(string xml)
507507
/// <param name="input">The input xml to transform.</param>
508508
/// <param name="transform">The transform xml.</param>
509509
/// <returns>The output of the transform.</returns>
510-
static string Transform(string input, string transform)
510+
static string Transform(string input, string transform, string transformFileName)
511511
{
512512
StringReader inputString = new StringReader(input);
513513
XmlTextReader sourceDocument = new XmlTextReader(inputString);
@@ -516,7 +516,7 @@ static string Transform(string input, string transform)
516516
XPathDocument transformDocument = new XPathDocument(transformString);
517517

518518
XslCompiledTransform xslTransform = new XslCompiledTransform();
519-
xslTransform.Load(transformDocument, XsltSettings.TrustedXslt, new XmlUrlResolver());
519+
xslTransform.Load(transformDocument, XsltSettings.TrustedXslt, new XslTransformUrlResolver(transformFileName));
520520

521521
MemoryStream outputStream = new MemoryStream();
522522
XmlTextWriter writer = new XmlTextWriter(outputStream, Encoding.UTF8);
@@ -531,7 +531,7 @@ static string Transform(string input, string transform)
531531
/// <summary>
532532
/// Validates the given xsl string,.
533533
/// </summary>
534-
bool IsValidXsl(string xml)
534+
bool IsValidXsl(string xml, string transformFileName)
535535
{
536536
try {
537537
SD.Workbench.GetPad(typeof(CompilerMessageView)).BringPadToFront();
@@ -540,13 +540,13 @@ bool IsValidXsl(string xml)
540540
XPathDocument doc = new XPathDocument(reader);
541541

542542
XslCompiledTransform xslTransform = new XslCompiledTransform();
543-
xslTransform.Load(doc, XsltSettings.Default, new XmlUrlResolver());
543+
xslTransform.Load(doc, XsltSettings.Default, new XslTransformUrlResolver(transformFileName));
544544

545545
return true;
546546
} catch(XsltCompileException ex) {
547547
AddTask(StylesheetFileName, GetInnerExceptionErrorMessage(ex), ex.LineNumber, ex.LinePosition, TaskType.Error);
548548
} catch(XsltException ex) {
549-
AddTask(StylesheetFileName, ex.Message, ex.LinePosition, ex.LineNumber, TaskType.Error);
549+
AddTask(StylesheetFileName, GetInnerExceptionErrorMessage(ex), ex.LinePosition, ex.LineNumber, TaskType.Error);
550550
} catch(XmlException ex) {
551551
AddTask(StylesheetFileName, ex.Message, ex.LinePosition, ex.LineNumber, TaskType.Error);
552552
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4+
// software and associated documentation files (the "Software"), to deal in the Software
5+
// without restriction, including without limitation the rights to use, copy, modify, merge,
6+
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7+
// to whom the Software is furnished to do so, subject to the following conditions:
8+
//
9+
// The above copyright notice and this permission notice shall be included in all copies or
10+
// substantial portions of the Software.
11+
//
12+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14+
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15+
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17+
// DEALINGS IN THE SOFTWARE.
18+
19+
using System;
20+
using System.IO;
21+
using System.Xml;
22+
23+
namespace ICSharpCode.XmlEditor
24+
{
25+
public class XslTransformUrlResolver : XmlUrlResolver
26+
{
27+
readonly Uri baseDirectory;
28+
29+
public XslTransformUrlResolver(string fileName)
30+
{
31+
this.baseDirectory = new Uri(Path.GetDirectoryName(fileName) + Path.DirectorySeparatorChar, UriKind.Absolute);
32+
}
33+
34+
public override Uri ResolveUri(Uri baseUri, string relativeUri)
35+
{
36+
if (baseUri != null) {
37+
return base.ResolveUri(baseUri, relativeUri);
38+
}
39+
return base.ResolveUri(baseDirectory, relativeUri);
40+
}
41+
}
42+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
</Reference>
6969
</ItemGroup>
7070
<ItemGroup>
71+
<Compile Include="Src\XslTransformUrlResolver.cs" />
7172
<None Include="XmlEditor.addin">
7273
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
7374
</None>

0 commit comments

Comments
 (0)