Exemplo n.º 1
0
        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.ProhibitDtd = false;
                        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.ProhibitDtd = false;
                    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);
            }
        }
        public void ExpandIncludes()
        {
            if (this.Document != null)
            {
                this.dirty = true;
                XmlReaderSettings s = new XmlReaderSettings();
                s.DtdProcessing = GetSettingBoolean("IgnoreDTD") ? DtdProcessing.Ignore : DtdProcessing.Parse;
                s.XmlResolver   = new XmlProxyResolver(this.site);
                using (XmlReader r = XmlIncludeReader.CreateIncludeReader(this.Document, s, this.FileName)) {
                    this.Document = loader.Load(r);
                }

                // calling this event will cause the XmlTreeView to populate
                FireModelChanged(ModelChangeType.Reloaded, this.doc);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Run an XSLT transform and show the results.
        /// </summary>
        /// <param name="context">The document to transform</param>
        /// <param name="xsltfilename">The xslt file to use</param>
        /// <param name="outpath">Output file name hint.</param>
        /// <returns>The output file name or null if DisableOutputFile is true</returns>
        public string DisplayXsltResults(XmlDocument context, string xsltfilename, string outpath = null)
        {
            if (!this._webInitialized)
            {
                return(null);
            }

            this.CleanupTempFile();
            Uri resolved = null;

            try
            {
                XslCompiledTransform transform;
                if (string.IsNullOrEmpty(xsltfilename))
                {
                    transform = GetDefaultStylesheet();
                }
                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;
                }

                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
                {
                    var ext      = GetDefaultOutputExtension();
                    var basePath = Path.Combine(Path.GetDirectoryName(outpath), Path.GetFileNameWithoutExtension(outpath));
                    outpath = basePath + ext;
                    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);
                                Utilities.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);
        }
Exemplo n.º 4
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);
            }
        }
Exemplo n.º 5
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);
            }
        }