[Cleanup] Removed TARGET_JVM
[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.Cryptography;
39 using System.Security.Policy;
40 using System.Text;
41 using System.Xml;
42 using System.Xml.Schema;
43 using System.Xml.XPath;
44 using System.Xml.Xsl;
45
46 namespace Mono.Xml.Xsl {
47
48         // FIXME: Correct evidence handling; test other than simple string case
49         internal class MSXslScriptManager {
50                 Hashtable scripts = new Hashtable ();
51
52                 public MSXslScriptManager () {}
53                 
54                 public void AddScript (Compiler c)
55                 {
56                         MSXslScript s = new MSXslScript (c.Input, c.Evidence);
57                         string ns = c.Input.GetNamespace (s.ImplementsPrefix);
58                         if (ns == null)
59                                 throw new XsltCompileException ("Specified prefix for msxsl:script was not found: " + s.ImplementsPrefix, null, c.Input);
60                         scripts.Add (ns, s.Compile (c.Input));
61                 }
62                 
63                 enum ScriptingLanguage {
64                         JScript,
65                         VisualBasic,
66                         CSharp
67                 }
68                 
69                 public object GetExtensionObject (string ns)
70                 {
71                         if (!scripts.ContainsKey (ns))
72                                 return null;
73                         return Activator.CreateInstance ((Type) scripts [ns]);
74                 }
75
76                 class MSXslScript {
77                         ScriptingLanguage language = ScriptingLanguage.JScript; // default = JScript.
78                         string implementsPrefix = null;
79                         string code = null;
80                         Evidence evidence;
81
82                         public MSXslScript (XPathNavigator nav, Evidence evidence)
83                         {
84                                 this.evidence = evidence;
85                                 code = nav.Value;
86                                 if (nav.MoveToFirstAttribute ()) {
87                                         do {
88                                                 switch (nav.LocalName) {
89                                                 case "language":
90                                                         switch (nav.Value.ToLower (CultureInfo.InvariantCulture)) {
91                                                         case "jscript":
92                                                         case "javascript":
93                                                                 language = ScriptingLanguage.JScript; break;
94                                                         case "vb":
95                                                         case "visualbasic":
96                                                                 language = ScriptingLanguage.VisualBasic;
97                                                                 break;
98                                                         case "c#":
99                                                         case "csharp":
100                                                                 language = ScriptingLanguage.CSharp;
101                                                                 break;
102                                                         default:
103                                                                 throw new XsltException ("Invalid scripting language!", null);
104                                                         }
105                                                         break;
106                                                 case "implements-prefix":
107                                                         implementsPrefix = nav.Value;
108                                                         break;
109                                                 }
110                                         } while (nav.MoveToNextAttribute ());
111                                         nav.MoveToParent ();
112                                 }
113                                 
114                                 if (implementsPrefix == null)
115                                         throw new XsltException ("need implements-prefix attr", null);
116                         }
117         
118                         public ScriptingLanguage Language {
119                                 get { return language; }
120                         }
121         
122                         public string ImplementsPrefix {
123                                 get { return implementsPrefix; }
124                         }
125         
126                         public string Code {
127                                 get { return code; }
128                         }
129                         
130                         public object Compile (XPathNavigator node)
131                         {
132 #if MOBILE
133                                 throw new NotImplementedException ();
134 #else
135                                 string suffix = "";
136                                 foreach (byte b in MD5.Create ().ComputeHash (Encoding.Unicode.GetBytes (code))) {
137                                         suffix += b.ToString ("x2");
138                                 }
139                                 switch (this.language) {
140                                 case ScriptingLanguage.CSharp:
141                                         return new CSharpCompilerInfo ().GetScriptClass (Code, suffix, node, evidence);
142                                 case ScriptingLanguage.JScript:
143                                         return new JScriptCompilerInfo ().GetScriptClass (Code, suffix, node, evidence);
144                                 case ScriptingLanguage.VisualBasic:
145                                         return new VBCompilerInfo ().GetScriptClass (Code, suffix, node, evidence);
146                                 default:
147                                         return null;
148                                 }
149 #endif
150                         }
151                 }
152         }
153 }
154