remove svn:executable from .cs files
[mono.git] / mcs / class / System.XML / Mono.Xml.Xsl / MSXslScriptManager.cs
1 //
2 // MSXslScriptManager.cs
3 //
4 // Author:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //      Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
7 //
8 // (C)2003 Atsushi Enomoto
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31 using System;
32 using System.Diagnostics;
33 using System.Collections;
34 using System.Globalization;
35 using System.IO;
36 using System.Reflection;
37 using System.Security;
38 using System.Security.Policy;
39 using System.Xml;
40 using System.Xml.Schema;
41 using System.Xml.XPath;
42 using System.Xml.Xsl;
43
44 namespace Mono.Xml.Xsl {
45
46         [MonoTODO ("Correct evidence handling; test other than simple string case")]
47         internal class MSXslScriptManager {
48                 Hashtable scripts = new Hashtable ();
49
50                 public MSXslScriptManager () {}
51                 
52                 public void AddScript (Compiler c)
53                 {
54                         MSXslScript s = new MSXslScript (c.Input, c.Evidence);
55                         string ns = c.Input.GetNamespace (s.ImplementsPrefix);
56                         if (ns == null)
57                                 throw new XsltCompileException ("Specified prefix for msxsl:script was not found: " + s.ImplementsPrefix, null, c.Input);
58                         scripts.Add (ns, s.Compile (c.Input));
59                 }
60                 
61                 enum ScriptingLanguage {
62                         JScript,
63                         VisualBasic,
64                         CSharp
65                 }
66                 
67                 public object GetExtensionObject (string ns)
68                 {
69                         if (!scripts.ContainsKey (ns))
70                                 return null;
71                         return Activator.CreateInstance ((Type) scripts [ns]);
72                 }
73
74                 class MSXslScript {
75                         ScriptingLanguage language = ScriptingLanguage.JScript; // default = JScript.
76                         string implementsPrefix = null;
77                         string code = null;
78                         Evidence evidence;
79
80                         public MSXslScript (XPathNavigator nav, Evidence evidence)
81                         {
82                                 this.evidence = evidence;
83                                 code = nav.Value;
84                                 if (nav.MoveToFirstAttribute ()) {
85                                         do {
86                                                 switch (nav.LocalName) {
87                                                 case "language":
88                                                         switch (nav.Value.ToLower (CultureInfo.InvariantCulture)) {
89                                                         case "jscript":
90                                                         case "javascript":
91                                                                 language = ScriptingLanguage.JScript; break;
92                                                         case "vb":
93                                                         case "visualbasic":
94                                                                 language = ScriptingLanguage.VisualBasic;
95                                                                 break;
96                                                         case "c#":
97                                                         case "csharp":
98                                                                 language = ScriptingLanguage.CSharp;
99                                                                 break;
100                                                         default:
101                                                                 throw new XsltException ("Invalid scripting language!", null);
102                                                         }
103                                                         break;
104                                                 case "implements-prefix":
105                                                         implementsPrefix = nav.Value;
106                                                         break;
107                                                 }
108                                         } while (nav.MoveToNextAttribute ());
109                                         nav.MoveToParent ();
110                                 }
111                                 
112                                 if (implementsPrefix == null)
113                                         throw new XsltException ("need implements-prefix attr", null);
114                         }
115         
116                         public ScriptingLanguage Language {
117                                 get { return language; }
118                         }
119         
120                         public string ImplementsPrefix {
121                                 get { return implementsPrefix; }
122                         }
123         
124                         public string Code {
125                                 get { return code; }
126                         }
127                         
128                         public object Compile (XPathNavigator node)
129                         {
130 #if TARGET_JVM
131                                 throw new NotImplementedException ();
132 #else
133                                 string suffix = Guid.NewGuid ().ToString ("N");
134                                 switch (this.language) {
135                                 case ScriptingLanguage.CSharp:
136                                         return new CSharpCompilerInfo ().GetScriptClass (Code, suffix, node, evidence);
137                                 case ScriptingLanguage.JScript:
138                                         return new JScriptCompilerInfo ().GetScriptClass (Code, suffix, node, evidence);
139                                 case ScriptingLanguage.VisualBasic:
140                                         return new VBCompilerInfo ().GetScriptClass (Code, suffix, node, evidence);
141                                 default:
142                                         return null;
143                                 }
144 #endif
145                         }
146                 }
147         }
148 }
149