示例#1
0
        private void WriteMethod(TextWriter writer, JsonMethodDescription method)
        {
            if (EcmaScriptIdentifier.IsValidIdentifier(method.Name, false))
            {
                writer.Write(this.formatter.MethodBeginFormat, this.ProxyNamespace, method.Name, this.ConvertParamType(method.Return.Type));
            }
            else
            {
                writer.Write(this.formatter.SafeMethodBeginFormat, this.ProxyNamespace, EcmaScriptWriter.Serialize(method.Name), this.ConvertParamType(method.Return.Type));
            }

            foreach (JsonNamedParameterDescription param in method.Params)
            {
                this.WriteParameter(writer, param);
            }

            writer.Write(this.formatter.MethodMiddleFormat, EcmaScriptWriter.Serialize(method.Name));

            if (method.Params.Length > 0)
            {
                string[] args = new string[method.Params.Length];
                for (int i = 0; i < method.Params.Length; i++)
                {
                    args[i] = method.Params[i].Name;
                }
                writer.Write(this.formatter.ArgsFormat, String.Join(",", args));
            }
            else
            {
                writer.Write("null");
            }

            writer.Write(this.formatter.MethodEndFormat);
        }
示例#2
0
        private void WriteNamespaces(TextWriter writer)
        {
            writer.Write(this.formatter.GlobalsFormat);

            if (!String.IsNullOrEmpty(this.ProxyNamespace))
            {
                EcmaScriptWriter.WriteNamespaceDeclaration(writer, this.ProxyNamespace, null, this.formatter.IsDebug);
            }
        }
示例#3
0
    public new static string Serialize(object value)
    {
        StringBuilder stringBuilder = new StringBuilder();

        using (EcmaScriptWriter ecmaScriptWriter = new EcmaScriptWriter(stringBuilder))
        {
            ecmaScriptWriter.Write(value);
        }
        return(stringBuilder.ToString());
    }
            /// <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());
            }
示例#5
0
 private void WriteProperty(TextWriter writer, string name, object value)
 {
     if (EcmaScriptIdentifier.IsValidIdentifier(name, false))
     {
         writer.Write(this.formatter.PropertyFormat, this.ProxyNamespace, name, EcmaScriptWriter.Serialize(value));
     }
     else
     {
         writer.Write(this.formatter.SafePropertyFormat, this.ProxyNamespace, EcmaScriptWriter.Serialize(name), EcmaScriptWriter.Serialize(value));
     }
 }
            protected override void Write(object value, bool isProperty)
            {
                if (value is Regex)
                {
                    if (isProperty && this.Settings.PrettyPrint)
                    {
                        this.TextWriter.Write(' ');
                    }
                    EcmaScriptWriter.WriteEcmaScriptRegExp(this, (Regex)value);
                    return;
                }

                base.Write(value, isProperty);
            }
示例#7
0
        /// <summary>
        /// Processes each argument allowing string literals to code expressions to function calls.
        /// </summary>
        /// <param name="defaultValue">the default value if none was supplied</param>
        /// <param name="keys">an ordered list of keys to check</param>
        /// <returns>the resulting expression</returns>
        protected string ProcessArgument(string defaultValue, params string[] keys)
        {
            object argument = null;

            foreach (string key in keys)
            {
                if (this.Attributes.ContainsKey(key))
                {
                    argument = this.Attributes[key];
                    break;
                }
            }

            string value;

            if (argument == null)
            {
                value = defaultValue;
            }
            else if (argument is string)
            {
                // directly use as inline expression
                value = (string)argument;
            }
            else if (argument is JbstExpressionBlock)
            {
                // convert to inline expression
                value = ((JbstExpressionBlock)argument).Code;
            }
            else if (argument is JbstCodeBlock)
            {
                // convert to anonymous function expression
                value = String.Format(
                    FunctionEvalExpression,
                    EcmaScriptWriter.Serialize(argument));
            }
            else
            {
                // convert to literal expression
                value = EcmaScriptWriter.Serialize(argument);
            }

            return((value ?? String.Empty).Trim());
        }
示例#8
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>
 /// Outputs a .NET Regex as an ECMAScript RegExp literal.
 /// Defaults to global matching off.
 /// </summary>
 /// <param name="writer"></param>
 /// <param name="regex"></param>
 /// <remarks>
 /// http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
 /// </remarks>
 public static void WriteEcmaScriptRegExp(JsonWriter writer, Regex regex)
 {
     EcmaScriptWriter.WriteEcmaScriptRegExp(writer, regex, false);
 }
 /// <summary>
 /// Writes dates as ECMAScript Date constructors
 /// </summary>
 /// <param name="value"></param>
 public override void Write(DateTime value)
 {
     EcmaScriptWriter.WriteEcmaScriptDate(this, value);
 }
示例#11
0
        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);
        }
示例#12
0
        /// <summary>
        /// Renders the data items as a block of JavaScript
        /// </summary>
        /// <param name="writer"></param>
        public void Write(TextWriter writer, IDictionary <string, object> data)
        {
            if (data == null || data.Count < 1)
            {
                // emit nothing when empty
                return;
            }

            List <string> namespaces = new List <string>();

            StringWriter     markup;
            EcmaScriptWriter jsWriter;

            if (this.AutoMarkup == AutoMarkupType.Data)
            {
                markup   = new StringWriter();
                jsWriter = new JsonMarkupWriter(writer, markup);
            }
            else
            {
                markup   = null;
                jsWriter = new EcmaScriptWriter(writer);
            }

            if (this.IsDebug)
            {
                jsWriter.Settings.PrettyPrint = true;
                jsWriter.Settings.NewLine     = Environment.NewLine;
                jsWriter.Settings.Tab         = "\t";
            }

            writer.Write(DataBlockWriter.ScriptOpen);

            foreach (string key in data.Keys)
            {
                if (markup != null)
                {
                    if (this.IsDebug)
                    {
                        markup.WriteLine();
                    }
                    markup.Write("<div title=\"");
                    HttpUtility.HtmlAttributeEncode(key, markup);
                    markup.Write("\">");
                }

                string declaration;
                if (!EcmaScriptWriter.WriteNamespaceDeclaration(writer, key, namespaces, this.IsDebug))
                {
                    declaration = "var " + key;
                }
                else
                {
                    declaration = key;
                }

                if (this.IsDebug)
                {
                    writer.Write(DataBlockWriter.VarAssignmentDebug, declaration);
                    if (data[key] != null &&
                        data[key].GetType().IsClass)
                    {
                        writer.WriteLine();
                    }
                }
                else
                {
                    writer.Write(DataBlockWriter.VarAssignment, declaration);
                }

                // emit the value as JSON
                jsWriter.Write(data[key]);
                writer.Write(DataBlockWriter.VarAssignmentEnd);

                if (markup != null)
                {
                    markup.Write("</div>");
                }

                if (this.IsDebug)
                {
                    writer.WriteLine();
                }
            }

            writer.Write(DataBlockWriter.ScriptClose);

            if (markup != null)
            {
                writer.Write(DataBlockWriter.NoScriptOpen);
                writer.Write(markup.ToString());
                writer.Write(DataBlockWriter.NoScriptClose);
            }
        }
示例#13
0
        /// <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>");
        }
示例#14
0
        public void OutputProxy(TextWriter writer, bool prettyPrint)
        {
            lock (this.SyncLock)
            {
                // locking because changing Formatter based upon debug switch

                if (prettyPrint)
                {
                    this.formatter = new DebugJsonServiceProxyFormat();
                }
                else
                {
                    this.formatter = new JsonServiceProxyFormat();
                }

                this.WriteNamespaces(writer);

                writer.Write(this.formatter.ProxyInstantiationFormat, this.ProxyNamespace, EcmaScriptWriter.Serialize(this.service.Address));

                foreach (JsonMethodDescription method in this.Service.Methods)
                {
                    this.WriteMethod(writer, method);
                }

                if (prettyPrint)
                {
                    this.WriteProperty(writer, "isDebug", true);
                }
            }
        }