2010-06-21 Atsushi Enomoto <atsushi@ximian.com>
[mcs.git] / class / System.XML / Mono.Xml.Xsl / MSXslScriptManager.cs
blobbf5e98df2011729228648566b3a2630978048005
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 //
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.
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;
46 namespace Mono.Xml.Xsl {
48 // FIXME: Correct evidence handling; test other than simple string case
49 internal class MSXslScriptManager {
50 Hashtable scripts = new Hashtable ();
52 public MSXslScriptManager () {}
54 public void AddScript (Compiler c)
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));
63 enum ScriptingLanguage {
64 JScript,
65 VisualBasic,
66 CSharp
69 public object GetExtensionObject (string ns)
71 if (!scripts.ContainsKey (ns))
72 return null;
73 return Activator.CreateInstance ((Type) scripts [ns]);
76 class MSXslScript {
77 ScriptingLanguage language = ScriptingLanguage.JScript; // default = JScript.
78 string implementsPrefix = null;
79 string code = null;
80 Evidence evidence;
82 public MSXslScript (XPathNavigator nav, Evidence evidence)
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);
105 break;
106 case "implements-prefix":
107 implementsPrefix = nav.Value;
108 break;
110 } while (nav.MoveToNextAttribute ());
111 nav.MoveToParent ();
114 if (implementsPrefix == null)
115 throw new XsltException ("need implements-prefix attr", null);
118 public ScriptingLanguage Language {
119 get { return language; }
122 public string ImplementsPrefix {
123 get { return implementsPrefix; }
126 public string Code {
127 get { return code; }
130 public object Compile (XPathNavigator node)
132 #if TARGET_JVM || MONOTOUCH
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");
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;
149 #endif