2010-06-05 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.Compilation / SimpleBuildProvider.cs
1 //
2 // System.Web.Compilation.SimpleBuildProvider
3 //
4 // Authors:
5 //   Marek Habersack (mhabersack@novell.com)
6 //
7 // (C) 2008 Novell, Inc
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.CodeDom;
33 using System.CodeDom.Compiler;
34 using System.Collections;
35 using System.Collections.Generic;
36 using System.IO;
37 using System.Reflection;
38 using System.Web;
39 using System.Web.UI;
40
41 namespace System.Web.Compilation
42 {
43         abstract class SimpleBuildProvider : GenericBuildProvider <SimpleWebHandlerParser>
44         {
45                 bool _parsed;
46                 bool _needLoadFromBin;
47                 
48                 protected override SimpleWebHandlerParser Parse ()
49                 {
50                         SimpleWebHandlerParser parser = Parser;
51                         
52                         if (_parsed)
53                                 return parser;
54                         
55                         _parsed = true;
56                         return parser;
57                 }
58
59                 protected override void GenerateCode (AssemblyBuilder assemblyBuilder, SimpleWebHandlerParser parser, BaseCompiler compiler)
60                 {
61                         if (assemblyBuilder == null || parser == null)
62                                 return;
63                         
64                         string programCode = parser.Program.Trim ();
65                         if (String.IsNullOrEmpty (programCode)) {
66                                 _needLoadFromBin = true;
67                                 return;
68                         }
69                         
70                         _needLoadFromBin = false;
71                         using (TextWriter writer = assemblyBuilder.CreateCodeFile (this))
72                                 writer.WriteLine (programCode);
73                 }
74
75                 protected override Type LoadTypeFromBin (BaseCompiler compiler, SimpleWebHandlerParser parser)
76                 {
77                         return parser.GetTypeFromBin (parser.ClassName);
78                 }
79                 
80                 protected override string GetClassType (BaseCompiler compiler, SimpleWebHandlerParser parser)
81                 {
82                         if (parser != null)
83                                 return parser.ClassName;
84
85                         return null;
86                 }
87                 
88                 protected override ICollection GetParserDependencies (SimpleWebHandlerParser parser)
89                 {
90                         if (parser != null)
91                                 return parser.Dependencies;
92
93                         return null;
94                 }
95                 
96                 protected override string GetParserLanguage (SimpleWebHandlerParser parser)
97                 {
98                         if (parser != null)
99                                 return parser.Language;
100
101                         return null;
102                 }
103                 
104                 protected override string GetCodeBehindSource (SimpleWebHandlerParser parser)
105                 {
106                         return null;
107                 }
108                 
109                 protected override AspGenerator CreateAspGenerator (SimpleWebHandlerParser parser)
110                 {
111                         return null;
112                 }
113
114                 protected override BaseCompiler CreateCompiler (SimpleWebHandlerParser parser)
115                 {
116                         return new WebServiceCompiler (parser);
117                 }
118
119                 protected override List <string> GetReferencedAssemblies (SimpleWebHandlerParser parser)
120                 {
121                         if (parser == null)
122                                 return null;
123                         
124                         ArrayList al = parser.Assemblies;
125                         if (al == null || al.Count == 0)
126                                 return null;
127
128                         List <string> ret = new List <string> ();
129                         string loc;
130                         
131                         foreach (object o in al) {
132                                 loc = o as string;
133                                 if (loc == null)
134                                         continue;
135
136                                 if (ret.Contains (loc))
137                                         continue;
138
139                                 ret.Add (loc);
140                         }
141
142                         return ret;
143                 }
144                 
145                 protected override bool NeedsConstructType {
146                         get { return false; }
147                 }
148
149                 protected override bool NeedsLoadFromBin {
150                         get { return _needLoadFromBin; }
151                 }
152         }
153 }