public void DisplayXsltResults(XmlDocument context)
        {
            Uri resolved = null;

            try {
                XslCompiledTransform transform;
                string path = null;
                if (this.showFileStrip)
                {
                    path = this.SourceFileName.Text.Trim();
                }
                if (string.IsNullOrEmpty(path))
                {
                    transform = GetDefaultStylesheet();
                }
                else
                {
                    resolved = new Uri(baseUri, path);
                    if (resolved != this.xsltUri || IsModified())
                    {
                        this.loaded           = DateTime.Now;
                        settings.EnableScript = (trusted.ContainsKey(resolved));
                        XmlReaderSettings rs = new XmlReaderSettings();
                        rs.DtdProcessing = model.GetSettingBoolean("IgnoreDTD") ? DtdProcessing.Ignore : DtdProcessing.Parse;
                        rs.XmlResolver   = resolver;
                        using (XmlReader r = XmlReader.Create(resolved.AbsoluteUri, rs)) {
                            xslt.Load(r, settings, resolver);
                        }
                    }
                    transform = xslt;
                }
                if (null != transform)
                {
                    StringWriter      writer   = new StringWriter();
                    XmlReaderSettings settings = new XmlReaderSettings();
                    settings.XmlResolver   = new XmlProxyResolver(this.site);
                    settings.DtdProcessing = model.GetSettingBoolean("IgnoreDTD") ? DtdProcessing.Ignore : DtdProcessing.Parse;
                    transform.Transform(XmlIncludeReader.CreateIncludeReader(context, settings, GetBaseUri().AbsoluteUri), null, writer);
                    this.xsltUri = resolved;
                    Display(writer.ToString());
                }
            } 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;
                        DisplayXsltResults();
                        return;
                    }
                }
                WriteError(x);
            } catch (Exception x) {
                WriteError(x);
            }
        }
Ejemplo n.º 2
0
        public void DisplayXsltResults(XmlDocument context)
        {
            Uri resolved = null;

            try {
                XslCompiledTransform transform;
                string path = null;
                if (this.showFileStrip)
                {
                    path = this.SourceFileName.Text.Trim();
                }
                string outpath = null;
                if (this.showFileStrip)
                {
                    outpath = this.OutputFileName.Text.Trim();
                }
                if (string.IsNullOrEmpty(path))
                {
                    transform = GetDefaultStylesheet();
                }
                else
                {
                    resolved = new Uri(baseUri, path);
                    if (resolved != this.xsltUri || IsModified())
                    {
                        this.loaded           = DateTime.Now;
                        settings.EnableScript = (trusted.ContainsKey(resolved));
                        XmlReaderSettings rs = new XmlReaderSettings();
                        rs.DtdProcessing = model.GetSettingBoolean("IgnoreDTD") ? DtdProcessing.Ignore : DtdProcessing.Parse;
                        rs.XmlResolver   = resolver;
                        using (XmlReader r = XmlReader.Create(resolved.AbsoluteUri, rs)) {
                            xslt.Load(r, settings, resolver);
                        }
                    }

                    transform = xslt;
                }
                if (string.IsNullOrEmpty(outpath) && !DisableOutputFile)
                {
                    if (!string.IsNullOrEmpty(path))
                    {
                        // pick a good default filename ... this means we need to know the <xsl:output method> and unfortunately
                        // XslCompiledTransform doesn't give us that so we need to get it outselves.
                        var    method = GetOutputMethod(resolved);
                        string ext    = ".xml";
                        if (method.ToLower() == "html")
                        {
                            ext = ".htm";
                        }
                        else if (method.ToLower() == "text")
                        {
                            ext = ".txt";
                        }
                        outpath = Path.GetFileNameWithoutExtension(path) + "_output" + ext;

                        var safeUri = GetWritableBaseUri(outpath);
                        if (safeUri != this.baseUri)
                        {
                            this.OutputFileName.Text = new Uri(safeUri, outpath).LocalPath;
                        }
                        else
                        {
                            this.OutputFileName.Text = outpath;
                        }
                        outpath = new Uri(safeUri, outpath).LocalPath;
                    }
                }
                else if (!string.IsNullOrEmpty(outpath))
                {
                    outpath = new Uri(baseUri, outpath).LocalPath;
                }

                if (null != transform)
                {
                    if (DisableOutputFile || string.IsNullOrEmpty(path))
                    {
                        StringWriter      writer   = new StringWriter();
                        XmlReaderSettings settings = new XmlReaderSettings();
                        settings.XmlResolver   = new XmlProxyResolver(this.site);
                        settings.DtdProcessing = model.GetSettingBoolean("IgnoreDTD") ? DtdProcessing.Ignore : DtdProcessing.Parse;
                        transform.Transform(XmlIncludeReader.CreateIncludeReader(context, settings, GetBaseUri().AbsoluteUri), null, writer);
                        this.xsltUri = resolved;
                        Display(writer.ToString());
                    }
                    else
                    {
                        using (FileStream writer = new FileStream(outpath, FileMode.OpenOrCreate, FileAccess.Write))
                        {
                            XmlReaderSettings settings = new XmlReaderSettings();
                            settings.XmlResolver   = new XmlProxyResolver(this.site);
                            settings.DtdProcessing = model.GetSettingBoolean("IgnoreDTD") ? DtdProcessing.Ignore : DtdProcessing.Parse;
                            transform.Transform(XmlIncludeReader.CreateIncludeReader(context, settings, GetBaseUri().AbsoluteUri), null, writer);
                            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;
                        DisplayXsltResults();
                        return;
                    }
                }
                WriteError(x);
            } catch (Exception x) {
                WriteError(x);
            }
        }
Ejemplo n.º 3
0
        public void DisplayXsltResults(XmlDocument context)
        {
            Uri resolved = null;

            try {
                XslCompiledTransform transform;
                string path = null;
                if (this.showFileStrip)
                {
                    path = this.SourceFileName.Text.Trim();
                }
                string outpath = null;
                if (this.showFileStrip)
                {
                    outpath = this.OutputFileName.Text.Trim();
                }
                if (string.IsNullOrEmpty(path))
                {
                    transform = GetDefaultStylesheet();
                }
                else
                {
                    resolved = new Uri(baseUri, path);
                    if (resolved != this.xsltUri || IsModified())
                    {
                        this.loaded           = DateTime.Now;
                        settings.EnableScript = (trusted.ContainsKey(resolved));
                        XmlReaderSettings rs = new XmlReaderSettings();
                        rs.DtdProcessing = model.GetSettingBoolean("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;
                }

                if (string.IsNullOrEmpty(outpath) && !DisableOutputFile)
                {
                    if (!string.IsNullOrEmpty(path))
                    {
                        // pick a good default filename ... this means we need to know the <xsl:output method> and unfortunately
                        // XslCompiledTransform doesn't give us that so we need to get it outselves.

                        var ext = GetDefaultOutputExtension();
                        outpath = Path.GetFileNameWithoutExtension(path) + "_output" + ext;

                        var safeUri = GetWritableBaseUri(outpath);
                        if (safeUri != this.baseUri)
                        {
                            this.OutputFileName.Text = new Uri(safeUri, outpath).LocalPath;
                        }
                        else
                        {
                            this.OutputFileName.Text = outpath;
                        }
                        outpath = new Uri(safeUri, outpath).LocalPath;
                    }
                }
                else if (!string.IsNullOrEmpty(outpath))
                {
                    outpath = new Uri(baseUri, outpath).LocalPath;
                }

                if (null != transform)
                {
                    XmlReaderSettings settings = new XmlReaderSettings();
                    settings.XmlResolver   = new XmlProxyResolver(this.site);
                    settings.DtdProcessing = model.GetSettingBoolean("IgnoreDTD") ? DtdProcessing.Ignore : DtdProcessing.Parse;
                    var xmlReader = XmlIncludeReader.CreateIncludeReader(context, settings, GetBaseUri().AbsoluteUri);
                    if (DisableOutputFile || string.IsNullOrEmpty(outpath))
                    {
                        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.
                            MemoryStream ms = new MemoryStream();
                            transform.Transform(xmlReader, null, ms);

                            ms.Seek(0, SeekOrigin.Begin);
                            Utilities.WriteFileWithoutBOM(ms, outpath);
                        }
                        else
                        {
                            using (FileStream writer = new FileStream(outpath, FileMode.OpenOrCreate, FileAccess.Write))
                            {
                                transform.Transform(xmlReader, null, writer);
                                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;
                        DisplayXsltResults();
                        return;
                    }
                }
                WriteError(x);
            } catch (Exception x) {
                WriteError(x);
            }
        }