2 // ServiceHostParser.cs
5 // Ankit Jain (jankit@novell.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // Copyright (C) 2006 Novell, Inc. http://www.novell.com
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections;
31 using System.Collections.Specialized;
35 using System.Web.Caching;
36 using System.Web.Configuration;
38 namespace System.ServiceModel.Channels {
40 class ServiceHostParser
48 bool got_default_directive;
49 string program; // If program == null, we have to get the requested 'type_name' from the assemblies in bin
53 public ServiceHostParser (string file, string url, HttpContext context)
57 assemblies = new ArrayList ();
58 assemblies.Add ("System.ServiceModel");
59 this.context = context;
60 CompilationSection section = (CompilationSection) context.GetSection ("system.web/compilation");
61 language = section.DefaultLanguage;
64 public HttpContext Context {
65 get { return context; }
68 public string Filename {
72 public string TypeName {
73 get { return type_name; }
80 public string Program {
81 get { return program; }
84 public ArrayList Assemblies {
85 get { return assemblies; }
88 public string Factory {
89 get { return factory; }
92 public string Language {
93 get { return language; }
98 using (StreamReader reader = new StreamReader (file)) {
100 bool directive_found = false;
101 StringBuilder content = new StringBuilder ();
103 while ((line = reader.ReadLine ()) != null) {
104 string trimmed = line.Trim ();
105 if (!directive_found && trimmed == String.Empty)
108 if (trimmed.StartsWith ("<%@")) {
109 ParseDirective (trimmed);
110 directive_found = true;
114 content.Append (line + "\n");
115 content.Append (reader.ReadToEnd ());
118 if (!got_default_directive)
119 throw new Exception ("No @ServiceHost directive found");
121 this.program = content.ToString ().Trim ();
122 if (this.program.Trim () == "")
126 if (String.IsNullOrEmpty (Language))
127 throw new Exception ("Language not specified.");
130 void ParseDirective (string line)
132 StringDictionary attributes = Split (line);
135 if (String.Compare (attributes ["directive"], "ServiceHost", true) == 0) {
136 got_default_directive = true;
138 if (!attributes.ContainsKey ("SERVICE"))
139 throw new Exception ("Service attribute not present in @ServiceHost directive.");
141 type_name = attributes ["SERVICE"];
143 if (attributes.ContainsKey ("LANGUAGE"))
144 language = attributes ["LANGUAGE"];
146 if (attributes.ContainsKey ("FACTORY"))
147 factory = attributes ["FACTORY"];
149 if (attributes.ContainsKey ("DEBUG")) {
150 if (String.Compare (attributes ["DEBUG"], "TRUE", true) == 0)
152 else if (String.Compare (attributes ["DEBUG"], "FALSE", true) == 0)
155 throw new Exception (String.Format (
156 "Invalid value for debug attribute : '{0}'", attributes ["DEBUG"]));
160 //FIXME: Other attributes,
163 //FIXME: Other directives? Documentation doesn't mention any other
165 throw new Exception (String.Format ("Cannot handle directive : '{0}'", attributes ["directive"]));
168 StringDictionary Split (string line)
171 int end_pos = line.LastIndexOf ("%>");
173 throw new Exception ("Directive must end with '%>'");
175 StringDictionary table = new StringDictionary ();
176 string content = line.Substring (3, end_pos - 3).Trim ();
177 if (content.Length == 0)
178 throw new Exception ("No directive found");
180 int len = content.Length;
183 while (pos < len && content [pos] != ' ')
187 table ["directive"] = content;
191 table ["directive"] = content.Substring (0, pos);
193 content = content.Substring (pos);
195 len = content.Length;
199 while (content [pos] == ' ' && pos < len)
202 int eq_pos = content.IndexOf ('=', pos);
203 string key = content.Substring (pos, eq_pos - pos).Trim ();
206 int start_quote = content.IndexOf ('"', pos);
207 int end_quote = content.IndexOf ('"', start_quote + 1);
209 string val = content.Substring (start_quote + 1, end_quote - start_quote - 1).Trim ();
212 table [key.ToUpper ()] = val;