in src/XmlNotepad/XsltControl.cs [455:616]
public string DisplayXsltResults(XmlDocument context, string xsltfilename, string outpath = null, bool userSpecifiedOutput = false)
{
if (!this._webInitialized)
{
return null;
}
_previousTransform = new Context()
{
document = context,
xsltfilename = xsltfilename,
outpath = outpath,
userSpecifiedOutput = userSpecifiedOutput
};
this.CleanupTempFile();
Uri resolved = null;
try
{
XslCompiledTransform transform;
if (string.IsNullOrEmpty(xsltfilename))
{
transform = GetDefaultStylesheet();
this._usingDefaultXslt = true;
if (this._settings.GetBoolean("DisableDefaultXslt"))
{
context = new XmlDocument();
context.LoadXml("<Note>Default styling of your XML documents is disabled in your Options</Note>");
}
}
else
{
resolved = new Uri(_baseUri, xsltfilename);
if (resolved != this._xsltUri || IsModified())
{
_xslt = new XslCompiledTransform();
this._loaded = DateTime.Now;
var settings = new XsltSettings(true, this.EnableScripts);
settings.EnableScript = (_trusted.ContainsKey(resolved));
var rs = new XmlReaderSettings();
rs.DtdProcessing = this.IgnoreDTD ? DtdProcessing.Ignore : DtdProcessing.Parse;
rs.XmlResolver = _resolver;
using (XmlReader r = XmlReader.Create(resolved.AbsoluteUri, rs))
{
_xslt.Load(r, settings, _resolver);
}
// the XSLT DOM is also handy to have around for GetOutputMethod
this._xsltdoc = new XmlDocument();
this._xsltdoc.Load(resolved.AbsoluteUri);
}
transform = _xslt;
this._usingDefaultXslt = false;
}
if (string.IsNullOrEmpty(outpath))
{
if (!DisableOutputFile)
{
if (!string.IsNullOrEmpty(xsltfilename))
{
outpath = this.GetXsltOutputFileName(xsltfilename);
}
else
{
// default stylesheet produces html
this._tempFile = outpath = GetWritableFileName("DefaultXsltOutput.htm");
}
}
}
else if (!userSpecifiedOutput)
{
var ext = GetDefaultOutputExtension();
var basePath = Path.Combine(Path.GetDirectoryName(outpath), Path.GetFileNameWithoutExtension(outpath));
outpath = basePath + ext;
outpath = GetWritableFileName(outpath);
}
else
{
outpath = GetWritableFileName(outpath);
}
if (null != transform)
{
var dir = Path.GetDirectoryName(outpath);
if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
var settings = new XmlReaderSettings();
settings.XmlResolver = new XmlProxyResolver(this._site);
settings.DtdProcessing = this.IgnoreDTD ? DtdProcessing.Ignore : DtdProcessing.Parse;
var xmlReader = XmlIncludeReader.CreateIncludeReader(context, settings, GetBaseUri().AbsoluteUri);
if (string.IsNullOrEmpty(outpath))
{
using (StringWriter writer = new StringWriter())
{
transform.Transform(xmlReader, null, writer);
this._xsltUri = resolved;
Display(writer.ToString());
}
}
else
{
bool noBom = false;
Settings appSettings = (Settings)this._site.GetService(typeof(Settings));
if (appSettings != null)
{
noBom = (bool)appSettings["NoByteOrderMark"];
}
if (noBom)
{
// cache to an inmemory stream so we can strip the BOM.
using (MemoryStream ms = new MemoryStream())
{
transform.Transform(xmlReader, null, ms);
ms.Seek(0, SeekOrigin.Begin);
EncodingHelpers.WriteFileWithoutBOM(ms, outpath);
}
}
else
{
using (FileStream writer = new FileStream(outpath, FileMode.OpenOrCreate, FileAccess.Write))
{
Stopwatch watch = new Stopwatch();
watch.Start();
transform.Transform(xmlReader, null, writer);
watch.Stop();
this._info = new PerformanceInfo();
this._info.XsltMilliseconds = watch.ElapsedMilliseconds;
Debug.WriteLine("Transform in {0} milliseconds", watch.ElapsedMilliseconds);
this._xsltUri = resolved;
}
}
DisplayFile(outpath);
}
}
}
catch (System.Xml.Xsl.XsltException x)
{
if (x.Message.Contains("XsltSettings"))
{
if (!_trusted.ContainsKey(resolved) &&
MessageBox.Show(this, SR.XslScriptCodePrompt, SR.XslScriptCodeCaption,
MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
{
_trusted[resolved] = true;
return DisplayXsltResults(context, xsltfilename, outpath);
}
}
WriteError(x);
}
catch (Exception x)
{
WriteError(x);
}
this._previousOutputFile = outpath;
return outpath;
}