HtmlFormatter.cs :  » Script » IronRuby » Chiron » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Script » IronRuby 
IronRuby » Chiron » HtmlFormatter.cs
/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 *
 * ***************************************************************************/

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Chiron{
    static class HtmlFormatter {

#region HTML templates

        public const string Style =
@"body {font-family:""Verdana"";font-weight:normal;font-size: 8pt;color:black;} 
p {font-family:""Verdana"";font-weight:normal;color:black;margin-top: -5px}
b {font-family:""Verdana"";font-weight:bold;color:black;margin-top: -5px}
h1 { font-family:""Verdana"";font-weight:normal;font-size:18pt;color:red }
h2 { font-family:""Verdana"";font-weight:normal;font-size:14pt;color:maroon }
pre {font-family:""Lucida Console"";font-size: 9pt}
.marker {font-weight: bold; color: black;text-decoration: none;}
.version {color: gray;}
.error {margin-bottom: 10px;}
.expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
";

        const string ErrorFormat =
@"<html>
<head>
<title>{0}</title>
<link rel=""stylesheet"" type=""text/css"" href=""/style.css!"" />
</head>
<body bgcolor=""white"">
<span><h1><img src=""/sl.png!"" />&nbsp;Server Error<hr width=100% size=1 color=silver></h1>
<h2><i>HTTP Error {1} - {2}.</i> </h2></span>
<hr width=100% size=1 color=silver>
<b>Version Information:</b>&nbsp;Chiron/{3}
</body>
</html>
";

        const string DirListingFormat =
@"<html>
<head>
<title>Directory Listing -- {0}</title>
<link rel=""stylesheet"" type=""text/css"" href=""/style.css!"" />
</head>
<body bgcolor=""white"">
<h2><img src=""/sl.png!"" />&nbsp;<i>Directory Listing -- {1}</i> </h2></span>
<hr width=100% size=1 color=silver>
<pre>
{2}
</pre>
<hr width=100% size=1 color=silver>
<b>Version Information:</b>&nbsp;Chiron/{3}
</body>
</html>
";

        const string XapListingFormat =
@"<html>
<head>
<title>XAP Archive Listing -- {0}</title>
<link rel=""stylesheet"" type=""text/css"" href=""/style.css!"" />
</head>
<body bgcolor=""white"">
<h2><img src=""/sl.png!"" />&nbsp;<i>XAP Archive Listing -- {1}</i> </h2></span>
<hr width=100% size=1 color=silver>
<pre>
{2}
</pre>
<hr width=100% size=1 color=silver>
<b>Version Information:</b>&nbsp;Chiron/{3}
</body>
</html>
";

#endregion

        public static string GenerateErrorBody(int statusCode, string statusText, string message) {
            string body = string.Format(ErrorFormat, statusText, statusCode, statusText,
                typeof(Chiron).Assembly.GetName().Version.ToString());
            if (!string.IsNullOrEmpty(message))
                body += "\r\n<!--\r\n" + message + "\r\n-->";
            return body;
        }

        public static string FormatDirectoryListing(string dirPath, string parentPath, IList<FileSystemInfo> elements) {
            StringBuilder sb = new StringBuilder();

            if (parentPath != null) {
                if (!parentPath.EndsWith("/"))
                    parentPath += "/";
                sb.AppendFormat("<a href=\"{0}\">[To Parent Directory]</a>\r\n", parentPath);
            }

            foreach (FileSystemInfo e in elements) {
                if (e is FileInfo) {
                    if (e.Name.EndsWith(".xap", StringComparison.OrdinalIgnoreCase)) {
                        // special handling of XAP files to allow listing requests
                        sb.AppendFormat(
@"{0,38:dddd, MMMM dd, yyyy hh:mm tt} {1,12:n0} <a href=""{2}/"">{3}</a>&nbsp;<a href=""{4}""><img border=""0"" src=""/slx.png!"" title=""Download XAP file"" /></a>
", e.LastWriteTime, ((FileInfo)e).Length, e.Name, e.Name, e.Name);
                    }
                    else if (string.IsNullOrEmpty(HttpSocket.GetMimeType(e.Name))) {
                        sb.AppendFormat(
@"{0,38:dddd, MMMM dd, yyyy hh:mm tt} {1,12:n0} {2}</a>
", e.LastWriteTime, ((FileInfo)e).Length, e.Name);
                    }
                    else {
                        sb.AppendFormat(
@"{0,38:dddd, MMMM dd, yyyy hh:mm tt} {1,12:n0} <a href=""{2}"">{3}</a>
", e.LastWriteTime, ((FileInfo)e).Length, e.Name, e.Name);
                    }
                }
                else if (e is DirectoryInfo) {
                    sb.AppendFormat(
@"{0,38:dddd, MMMM dd, yyyy hh:mm tt}        [dir] <a href=""{1}/"">{2}</a>&nbsp;<a href=""{3}.xap""><img border=""0"" src=""/slx.png!"" title=""Create XAP file from directory contents"" /></a>
",
e.LastWriteTime, e.Name, e.Name, e.Name);
                }
            }

            return string.Format(DirListingFormat, dirPath, dirPath, sb.ToString(),
                typeof(Chiron).Assembly.GetName().Version.ToString());
        }

        public static string FormatXapListing(string xapPath, IList<ZipArchiveFile> elements) {
            StringBuilder sb = new StringBuilder();

            foreach (ZipArchiveFile f in elements) {
                if (string.IsNullOrEmpty(HttpSocket.GetMimeType(f.Name))) {
                    sb.AppendFormat(
@"{0,38:dddd, MMMM dd, yyyy hh:mm tt} {1,12:n0} {2}</a>
", f.LastWriteTime, f.Length, f.Name);
                }
                else {
                    sb.AppendFormat(
@"{0,38:dddd, MMMM dd, yyyy hh:mm tt} {1,12:n0} <a href=""{2}?{3}"">{4}</a>
", f.LastWriteTime, f.Length, xapPath, f.Name, f.Name);
                }
            }

            return string.Format(XapListingFormat, xapPath, xapPath, sb.ToString(),
                typeof(Chiron).Assembly.GetName().Version.ToString());
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.