1 //
2 // Authors:
3 //   Marek Habersack <grendel@twistedcode.net>
4 //
5 // (C) 2011 Novell, Inc (http://novell.com/)
6 //
7 
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 
29 using System;
30 using System.Collections.Generic;
31 using System.Text;
32 
33 namespace System.Web
34 {
35 	abstract class ExceptionPageTemplate
36 	{
37 		public const string Template_PageTopName = "PageTop";
38 		public const string Template_PageBottomName = "PageBottom";
39 		public const string Template_PageStandardName = "PageStandard";
40 		public const string Template_PageCustomErrorDefaultName = "PageCustomErrorDefault";
41 		public const string Template_PageHtmlizedExceptionName = "PageHtmlizedException";
42 		public const string Template_PageTitleName = "Title";
43 		public const string Template_ExceptionTypeName = "ExceptionType";
44 		public const string Template_ExceptionMessageName = "ExceptionMessage";
45 		public const string Template_DescriptionName = "Description";
46 		public const string Template_DetailsName = "Details";
47 		public const string Template_RuntimeVersionInformationName = "RuntimeVersionInformation";
48 		public const string Template_AspNetVersionInformationName = "AspNetVersionInformation";
49 		public const string Template_StackTraceName = "StackTrace";
50 		public const string Template_FullStackTraceName = "FullStackTrace";
51 		public const string Template_HtmlizedExceptionOriginName = "HtmlizedExceptionOrigin";
52 		public const string Template_HtmlizedExceptionShortSourceName = "HtmlizedExceptionShortSource";
53 		public const string Template_HtmlizedExceptionLongSourceName = "HtmlizedExceptionLongSource";
54 		public const string Template_HtmlizedExceptionSourceFileName = "HtmlizedExceptionSourceFile";
55 		public const string Template_HtmlizedExceptionErrorLinesName = "HtmlizedExceptionErrorLines";
56 		public const string Template_HtmlizedExceptionCompilerOutputName = "HtmlizedExceptionCompilerOutput";
57 
58 		List <ExceptionPageTemplateFragment> fragments;
59 
60 		public List <ExceptionPageTemplateFragment> Fragments {
61 			get {
62 				if (fragments == null)
63 					fragments = new List <ExceptionPageTemplateFragment> ();
64 				return fragments;
65 			}
66 		}
67 
Init()68 		public abstract void Init ();
69 
InitFragments(ExceptionPageTemplateValues values)70 		void InitFragments (ExceptionPageTemplateValues values)
71 		{
72 			foreach (ExceptionPageTemplateFragment fragment in fragments) {
73 				if (fragment == null)
74 					continue;
75 
76 				fragment.Init (values);
77 			}
78 		}
79 
Render(ExceptionPageTemplateValues values, ExceptionPageTemplateType pageType)80 		public string Render (ExceptionPageTemplateValues values, ExceptionPageTemplateType pageType)
81 		{
82 			if (values == null)
83 				throw new ArgumentNullException ("values");
84 			var sb = new StringBuilder ();
85 
86 			Render (values, pageType, (string text) => {
87 				sb.Append (text);
88 			});
89 
90 			return sb.ToString ();
91 		}
92 
Render(HttpResponse response, ExceptionPageTemplateValues values, ExceptionPageTemplateType pageType)93 		public void Render (HttpResponse response, ExceptionPageTemplateValues values, ExceptionPageTemplateType pageType)
94 		{
95 			if (response == null)
96 				return;
97 
98 			if (values == null)
99 				throw new ArgumentNullException ("values");
100 
101 			Render (values, pageType, (string text) => {
102 				response.Write (text);
103 			});
104 		}
105 
Render(ExceptionPageTemplateValues values, ExceptionPageTemplateType pageType, Action <string> writer)106 		void Render (ExceptionPageTemplateValues values, ExceptionPageTemplateType pageType, Action <string> writer)
107 		{
108 			if (fragments == null || fragments.Count == 0 || values.Count == 0)
109 				return;
110 
111 			InitFragments (values);
112 			string value;
113 			foreach (ExceptionPageTemplateFragment fragment in fragments) {
114 				if (fragment == null || (fragment.ValidForPageType & pageType) == 0)
115 					continue;
116 
117 				value = values.Get (fragment.Name);
118 				if (value == null || !fragment.Visible (values))
119 					continue;
120 
121 				writer (fragment.ReplaceMacros (value, values));
122 			}
123 		}
124 	}
125 }
126