Exemple #1
0
 public virtual string Render(TextWriter writer)
 {
     try
     {
         var xslt = XElement.Load(Xslt);
         var info = new RequestInfo(null, HttpStatic.Request);
         XPathWeb.XTransformToWriter(Xml, writer, xslt, info);
     }
     catch (Exception ex)
     {
         SegmentLang.Instance.LastException = ex;
         SegmentLang.Instance.IgnoreError(ex, HttpStatic.Request);
     }
     return string.Empty;
 }
        public virtual void RenderHtml(TextWriter writer, XPathDocument xmlDoc)
        {
            // Transform XLST validation
            xsltFileFull = Xslt;
            if (!xsltFileFull.Contains(@":\") && Context != null)
                xsltFileFull = Context.Server.MapPath(dirXslt + Xslt);

            Trace.Write("Transform ID=" + this.ID, "xslt=" + xsltFileFull);

            XsltArgumentList xslArg = new XsltArgumentList();
            XslCompiledTransform trans = new XslCompiledTransform();

            if (Context != null && Xslt.Length > 0 && File.Exists(xsltFileFull))
            {
#if WEB
                string serverUrl = Context.Request.Url.Scheme + "://" + Context.Request.Url.Authority + "/";
#else
                string serverUrl = Context.Request.Url.Scheme + "://" + Context.Request.Url.Authority + "/";
                // serverUrl = Context.Request.QueryString;
#endif
                XsltIncludeResolver resolver = new XsltIncludeResolver(serverUrl);  // for <xsl:include>
                trans.Load(xsltFileFull, XsltSettings.TrustedXslt, resolver as XmlUrlResolver);
            }
            else
            {
                var reader = XmlReader.Create(System.IO.File.OpenRead(xsltFileFull));
                trans.Load(stylesheet: reader);
            }

            Trace.Write("Transform ID=" + this.ID, "XSLT Parse Test OK");

            // Add an object to convert
            RequestInfo info = null;
            if (Context != null)
                info = new RequestInfo(this, Context.Request);
            else
                info = new RequestInfo(this, null);

            xslArg.AddExtensionObject("urn:request-info", info);

            if (isDebug <= 1)
                trans.Transform(xmlDoc, xslArg, writer);
        }
        protected override void Render(HtmlTextWriter writer)
        {
            TraceWrite(string.Format("V{0} sqlxml.ID={1}", Bin.Version, this.ID)
                      , string.Format("proc={0} xslt={1}", this.SqlProc, this.Xslt));

            HttpRequest Request = Context.Request;
            if (Request == null)
            {
                TraceWrite("sqlxml=" + ID, "<no Request>");
                writer.Write("Error: No request");
                return;
            }

            TraceContext Trace = Context.Trace;

            // General client request info values
            Trace.Write("IPAddress", Request.UserHostAddress);
            if (Request.UrlReferrer != null) Trace.Write("Referrer", Request.UrlReferrer.ToString());
            Trace.Write("Url", Request.Url.ToString());

            if (keyParam != null) keyParam.Clear();
            XPathDocument xmlDoc = SqlXmlDoc.SqlProcExecute(this, this.SqlParam
                                        , listParam, listParamNum, prmAdd, prmApp
                                        , formParam, formParamNum
                                        , "Root");
            // Debug: xmlDoc.CreateNavigator().OuterXml

            DateTime tStart = DateTime.Now;

            // Create argument list to pass to XSLT
            XsltArgumentList xslArg = new XsltArgumentList();
            XslCompiledTransform trans = new XslCompiledTransform();

            // Transform XLST validation
            string xsltFileFull = Context.Server.MapPath(dirXslt + Xslt);

            try
            {
                Trace.Write("Transform ID=" + this.ID, "xslt=" + xsltFileFull);

                if (Xslt.Length > 0 && File.Exists(xsltFileFull))
                {
                    string serverUrl = Request.Url.Scheme + "://" + Request.Url.Authority + "/";
                    XsltIncludeResolver resolver = new XsltIncludeResolver(serverUrl);  // for <xsl:include>
                    trans.Load(xsltFileFull, XsltSettings.TrustedXslt, resolver as XmlUrlResolver);
                }

                Trace.Write("Transform ID=" + this.ID, "XSLT Parse Test OK");
            }
            catch (Exception exp)
            {
                Log.Write("SqlProcRender." + Bin.Version + " exception : " + exp.Message
                        + "\n Url=" + Request.Url + "\n ip=" + Request.UserHostAddress
                        + " sqlxml.id=" + this.ID + " XSLT=" + dirXslt + Xslt);
                Trace.Write("XSLT ID=" + this.ID, "Error: " + exp.Message
                        + " XSLT=" + dirXslt + Xslt + " )");
                writer.Write("</br><span class=\"error\">Render." + Bin.Version
                        + " Xml Error: " + exp.Message
                        + " (ID=" + this.ID + " XSLT=" + dirXslt + Xslt + " )</span>");
                return;
            }

            if (xmlDoc != null && trans != null)
            {
                try
                {
                    // Add an object to convert
                    RequestInfo info = new RequestInfo(this, Request);

                    xslArg.AddExtensionObject("urn:request-info", info);
                    if (isDebug <= 1)
                        trans.Transform(xmlDoc, xslArg, writer);
                }
                catch (Exception exp)
                {
                    Trace.Write("Render." + Bin.Version
                            , " Transform error: " + exp.Message);
                    Log.Write("sqlxml transform : " + exp.Message
                            + "\n Url=" + Request.Url + "\n ip=" + Request.UserHostAddress
                            + " sqlxml.id=" + this.ID + " XSLT=" + dirXslt + Xslt);
                    writer.Write("</br><span class=\"error\">Render." + Bin.Version
                            + " Transform error: " + exp.Message);
                    return;
                }

                if (isDebug > 0)
                {
                    writer.Write("<br/>");

                    writer.Write("<br/>xmlDoc<br/><code><pre>");
                    if (isDebug == 3)      // for Chrome/Mozilla browser output
                        writer.Write(xmlDoc.CreateNavigator().OuterXml);
                    else                   // IE debug output
                        writer.Write(HttpUtility.HtmlEncode(xmlDoc.CreateNavigator().OuterXml));

                    writer.Write("</pre></code>");
                    writer.Write("<br/>xsltFileFull=" + xsltFileFull + "<br/><code><pre>");
                    if (isDebug == 3)
                        writer.Write(File.ReadAllText(xsltFileFull).ToString());
                    else                   // IE debug output
                        writer.Write(HttpUtility.HtmlEncode(File.ReadAllText(xsltFileFull).ToString()));
                    writer.Write("</code></pre>");
                }

                Trace.Write("V" + Bin.Version + " SqmlXml.ID=" + this.ID, "Finish Render");
            }
        }
        public virtual void AddRequestInfo(XsltArgumentList xslArg)
        {
            // Add an object to convert
            RequestInfo info = null;
            if (Context != null)
                info = new RequestInfo(this, Context.Request);
            else
                info = new RequestInfo(this, null);

            xslArg.AddExtensionObject("urn:request-info", info);
        }