Exemplo n.º 1
0
        /// <summary>
        /// Serialisierung aller Properties ins HTML Format
        /// </summary>
        public static string ToHtml(this object obj, HtmlOutputFormat format)
        {
            string surroundingTag;
            string innerTag  = "li";
            var    innerHtml = new StringBuilder();

            switch (format)
            {
            case HtmlOutputFormat.SortedList:
                surroundingTag = "ol";
                break;

            case HtmlOutputFormat.UnsortetList:
                surroundingTag = "ul";
                break;

            default:
                surroundingTag = null;
                break;
            }

            Type t = obj.GetType();

            // Get the public properties.
            PropertyInfo[] propInfos = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            var            cnt       = propInfos.Length;


            // Display information for all properties.
            foreach (var propInfo in propInfos)
            {
                var value = t.GetProperty(propInfo.Name).GetValue(obj, null);
                innerHtml.AppendFormat("<{0}>{1}: {2}</{0}>\n", innerTag, propInfo.Name, value);
            }


            return(String.Format("<{0}>{1}</{0}>", surroundingTag, innerHtml));
        }
Exemplo n.º 2
0
 /// <summary>
 /// 将对象所表达的 Html 内容输出至流。
 /// </summary>
 /// <param name="s"></param>
 public abstract void WriteToStream(Stream s, HtmlOutputFormat format, int indent);
Exemplo n.º 3
0
 /// <summary>
 /// 返回对象所表达的 Html 内容。
 /// </summary>
 /// <returns></returns>
 public string ToString(HtmlOutputFormat format)
 {
     using (var ms = new MemoryStream()) {
         this.WriteToStream(ms, format, 0);
         return Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Length);
     }
 }
Exemplo n.º 4
0
 public override void WriteToStream(System.IO.Stream s, HtmlOutputFormat format, int indent)
 {
     var buffer = Encoding.UTF8.GetBytes(format == HtmlOutputFormat.Indent ? (InnerHtml + Environment .NewLine) : InnerHtml);
     s.Write(buffer, 0, buffer.Length);
     s.Flush();
 }
Exemplo n.º 5
0
 public override void WriteToStream(System.IO.Stream s, HtmlOutputFormat format, int indent)
 {
     content.WriteToStream(s, format, indent);
 }
Exemplo n.º 6
0
        public override void WriteToStream(System.IO.Stream s, HtmlOutputFormat format, int indent)
        {
            var writer = new StreamWriter(s);
            writer.Write(format == HtmlOutputFormat.Indent ? html_header_before_content_type : html_header_before_content_type_zip);
            writer.Write(ContentType);
            writer.Write(format == HtmlOutputFormat.Indent ? html_header_before_title : html_header_before_title_zip);
            writer.Write(HttpUtil.EncodeHtml(this.Title));
            writer.Write("</title>");
            if (format == HtmlOutputFormat.Indent)
                writer.WriteLine();
            writer.Flush();

            // 输出脚本和 CSS
            var writeScriptAndCss = new Action<HtmlPart>(x => {
                foreach (var item in x.Scripts) {
                    item.WriteToStream(s, format, 4);
                }
                foreach (var item in x.Styles) {
                    item.WriteToStream(s, format, 4);
                }
            });

            // 添加用来支持 Bigpipe 的 JS 脚本
            if (UseBigPipe) {
                this.Scripts.Add(new Script("function showPart(part) {try{console.debug('received part ' + part.id)}catch(e){} document.getElementById(part.id).innerHTML=part.content;}", null));
            }
            writeScriptAndCss(this);

            foreach (var parts in this.Parts) {
                writeScriptAndCss(parts);
            }

            writer.Write("</head>");
            if (format == HtmlOutputFormat.Indent)
                writer.WriteLine();
            writer.Write("<body>");
            if (format == HtmlOutputFormat.Indent)
                writer.WriteLine();
            writer.Flush(); // 让客户端可以先下载 CSS
            s.Flush();

            if (UseBigPipe) {
                // 输出内容占位符
                foreach (var part in this.Parts) {
                    (new HtmlElement("div") { ID = part.ID }).WriteToStream(s, format, 4);
                    s.Flush();
                }
            }

            // 输出正文
            var partStatic = this.Parts.FindAll(x => !(x is AsyncHtmlPart));
            var partDynamic = this.Parts.FindAll(x => x is AsyncHtmlPart).Cast<AsyncHtmlPart>().ToArray();

            // 先输出静态内容
            foreach (var part in partStatic) {
                writePart(s, part, format);
            }

            // 再输出动态内容
            List<Task> tasks = new List<Task>();
            foreach (var part in partDynamic) {
                var task = part.PrepareStreamAsync();
                tasks.Add(task);
            }

            bool[] isFinish = new bool[partDynamic.Length];
            isFinish.Fill(false);
            var remainCount = partDynamic.Length;
            while (remainCount != 0) {
                var i = Task.WaitAny(tasks.WithIndex().FindAll(x => !isFinish[x.Index]).Select(y => y.Value).ToArray());
                remainCount--;
                isFinish[i] = true;
                //partDynamic[i].WriteToStream(s, format, 4);
                writePart(s, partDynamic[i], format);
            }

            // 输出页尾
            (new Script("try{finishDownload();}catch(e){}", null)).WriteToStream(s, format, 4);
            writer.Write("</body>");
            if (format == HtmlOutputFormat.Indent)
                writer.WriteLine();
            writer.Write("</html>");
            if (format == HtmlOutputFormat.Indent)
                writer.WriteLine();

            writer.Flush();
        }
Exemplo n.º 7
0
 private void writePart(Stream s, HtmlPart part, HtmlOutputFormat format)
 {
     if (UseBigPipe) {
         var c = HttpUtil.SerializeAsyncPart(part);
         s.Write(c, true);
     }
     else {
         (new HtmlElement("div") { ID = part.ID, InnerHtml = part }).WriteToStream(s, format, 4);
         s.Flush();
     }
 }