public new static string Serialize(object value)
        {
            StringBuilder stringBuilder = new StringBuilder();

            using (EcmaScriptWriter ecmaScriptWriter = new EcmaScriptWriter(stringBuilder))
            {
                ecmaScriptWriter.Write(value);
            }
            return(stringBuilder.ToString());
        }
示例#2
0
        public new static string Serialize(object value)
        {
            StringBuilder output = new StringBuilder();

            using (EcmaScriptWriter writer = new EcmaScriptWriter(output))
            {
                writer.Write(value);
            }
            return(output.ToString());
        }
示例#3
0
        public void Render(TextWriter writer)
        {
            this.ProcessDirectives();

            // add JSLINT directives
            string globals = this.GetGlobals();
            if (!String.IsNullOrEmpty(globals))
            {
                writer.WriteLine("/*global {0} */", globals);
            }

            if (!EcmaScriptWriter.WriteNamespaceDeclaration(writer, this.JbstName, null, true))
            {
                writer.Write("var ");
            }

            // wrap with ctor and assign
            writer.Write(this.JbstName);
            writer.WriteLine(" = JsonML.BST(");

            // render root node of content (null is OK)
            EcmaScriptWriter jsWriter = new EcmaScriptWriter(writer);
            jsWriter.Settings.PrettyPrint = true;
            jsWriter.Write(this.JbstParseTree);

            writer.WriteLine(");");

            // render any declarations
            if (this.Declarations.HasCode)
            {
                this.Declarations.OwnerName = this.JbstName;
                jsWriter.Write(this.Declarations);
            }
        }
		/// <summary>
		/// A helper method for serializing an object to EcmaScript
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static new string Serialize(object value)
		{
			StringBuilder output = new StringBuilder();

			using (EcmaScriptWriter writer = new EcmaScriptWriter(output))
			{
				writer.Write(value);
			}

			return output.ToString();
		}
        void IHttpHandler.ProcessRequest(HttpContext context)
        {
            if (this.IsDebug)
            {
                context.Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
            }
            else
            {
                // TODO: make this configurable (default to min-value YSlow! considers useful)
                // Note: Google Page Speed wants 1 month
                context.Response.ExpiresAbsolute = DateTime.UtcNow.AddDays(3);
            }

            string userCulture = context.Request.QueryString[ResourceHandler.GlobalizationQuery];
            if (userCulture != null && userCulture.Length > 1)
            {
                try
                {
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo(userCulture);
                }
                catch { }
                try
                {
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(userCulture);
                }
                catch { }
            }

            // get the target
            string targetPath = context.Request.AppRelativeCurrentExecutionFilePath;

            IGlobalizedBuildResult target = BuildManager.CreateInstanceFromVirtualPath(targetPath, typeof(IGlobalizedBuildResult)) as IGlobalizedBuildResult;
            if (target == null)
            {
                return;
            }

            IDictionary<string, object> res = this.GetResourceStrings(target.GlobalizationKeys, targetPath);

            if (!this.IsDebug)
            {
                // TODO: provide a mechanism for disabling compression?
                ResourceHandler.EnableStreamCompression(context);
            }

            HttpResponse response = context.Response;
            response.ContentType = ScriptResourceCodeProvider.MimeType;

            response.AppendHeader(
                "Content-Disposition",
                "inline;filename="+Path.GetFileNameWithoutExtension(targetPath)+".js");

            if (this.IsDebug)
            {
                response.Write(JslintDirective);
            }

            if (res.Count < 1)
            {
                // don't output call
                return;
            }

            response.Write(ResStart);

            EcmaScriptWriter writer = new EcmaScriptWriter(response.Output);
            writer.Settings.PrettyPrint = this.IsDebug;
            writer.Write(res);

            response.Write(",");

            writer = new EcmaScriptWriter(response.Output);
            writer.Settings.PrettyPrint = this.IsDebug;
            writer.Write(Thread.CurrentThread.CurrentCulture.Name);

            response.Write(ResEnd);
        }
        /// <summary>
        /// Renders the JBST control reference and any stored data to be used.
        /// </summary>
        /// <param name="writer">output</param>
        /// <param name="data">data to be bound as an object which will be serialized</param>
        /// <param name="index">the data index</param>
        /// <param name="count">the total data count</param>
        /// <param name="inner">a callback for writing inner placeholder content</param>
        internal void Write(TextWriter writer, object data, int index, int count, InnerCallback inner)
        {
            if (String.IsNullOrEmpty(this.JbstName))
            {
                throw new ArgumentNullException("JBST Name must be specified.");
            }

            // generate an ID for controls which do not have explicit
            if (String.IsNullOrEmpty(this.ID))
            {
                // happens with no parents
                this.ID = "_"+Guid.NewGuid().ToString("n");
            }

            bool hasInner = (inner != null);
            string placeholder = hasInner ? "div" : "noscript";

            // render the placeholder hook
            writer.Write('<');
            writer.Write(placeholder);
            writer.Write(" id=\"");
            writer.Write(this.ID);
            writer.Write("\">");

            if (hasInner)
            {
                // render inner as loading/error markup
                inner(writer);
            }

            string inlineData = null;
            if (data != null && !(data is EcmaScriptIdentifier) && this.AutoMarkup == AutoMarkupType.Data)
            {
                if (hasInner)
                {
                    writer.Write("<noscript>");
                }

                // serialize InlineData as a JavaScript literal
                StringBuilder builder = new StringBuilder();

                JsonMarkupWriter jsWriter = new JsonMarkupWriter(builder, writer);
                if (this.IsDebug)
                {
                    jsWriter.Settings.PrettyPrint = true;
                    jsWriter.Settings.NewLine = Environment.NewLine;
                    jsWriter.Settings.Tab = "\t";
                }
                jsWriter.Write(data);

                if (hasInner)
                {
                    writer.Write("</noscript>");
                }

                inlineData = builder.ToString();
            }

            writer.Write("</");
            writer.Write(placeholder);
            writer.Write('>');

            // render the binding script
            writer.Write("<script type=\"text/javascript\">");

            writer.Write(this.JbstName);
            writer.Write(".replace(\"");
            writer.Write(this.ID);
            writer.Write("\",");

            if (!String.IsNullOrEmpty(inlineData))
            {
                writer.Write(inlineData);
            }
            else if (data != null)
            {
                // serialize InlineData as a JavaScript literal
                EcmaScriptWriter jsWriter = new EcmaScriptWriter(writer);
                if (this.IsDebug)
                {
                    jsWriter.Settings.PrettyPrint = true;
                    jsWriter.Settings.NewLine = Environment.NewLine;
                    jsWriter.Settings.Tab = "\t";
                }
                jsWriter.Write(data);
            }
            else
            {
                // smallest most innocuous default data
                writer.Write("{}");
            }

            if (index >= 0)
            {
                writer.Write(",(");
                writer.Write(index);
                writer.Write(')');

                if (count >= 0)
                {
                    writer.Write(",(");
                    writer.Write(count);
                    writer.Write(')');
                }
            }
            writer.Write(");");

            writer.Write("</script>");
        }