KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > script > InterpreterPool


1 /*
2
3    Copyright 2000-2004 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.script;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.batik.dom.svg.SVGOMDocument;
25 import org.apache.batik.util.Service;
26
27 import org.w3c.dom.Document JavaDoc;
28
29 /**
30  * A class allowing to create/query an {@link
31  * org.apache.batik.script.Interpreter} corresponding to a particular
32  * <tt>Document</tt> and scripting language.
33  *
34  * <p>By default, it is able to create interpreters for ECMAScript,
35  * Python and Tcl scripting languages if you provide the right jar
36  * files in your CLASSPATH (i.e. Rhino, JPython and Jacl jar
37  * files).</p>
38  *
39  * @author <a HREF="mailto:cjolif@ilog.fr">Christophe Jolif</a>
40  * @version $Id: InterpreterPool.java,v 1.19 2004/08/27 00:42:06 deweese Exp $
41  */

42 public class InterpreterPool {
43
44     /** The InterpreterFactory classname for Rhino. */
45     private static final String JavaDoc RHINO =
46         "org.apache.batik.script.rhino.RhinoInterpreterFactory";
47
48     /** The InterpreterFactory classname for JPython. */
49     private static final String JavaDoc JPYTHON =
50         "org.apache.batik.script.jpython.JPythonInterpreterFactory";
51
52     /** The InterpreterFactory classname for Jacl. */
53     private static final String JavaDoc JACL =
54         "org.apache.batik.script.jacl.JaclInterpreterFactory";
55
56     /**
57      * Name of the "document" object when referenced by scripts
58      */

59     public static final String JavaDoc BIND_NAME_DOCUMENT = "document";
60
61     /**
62      * The default InterpreterFactory map.
63      */

64     protected static Map JavaDoc defaultFactories = new HashMap JavaDoc(7);
65
66     /**
67      * The InterpreterFactory map.
68      */

69     protected Map JavaDoc factories = new HashMap JavaDoc(7);
70
71     static {
72         Iterator JavaDoc iter = Service.providers(InterpreterFactory.class);
73         while (iter.hasNext()) {
74             InterpreterFactory factory = null;
75             factory = (InterpreterFactory)iter.next();
76             // System.err.println("Factory : " + factory);
77
defaultFactories.put(factory.getMimeType(), factory);
78         }
79     }
80
81     /**
82      * Constructs a new <tt>InterpreterPool</tt>.
83      */

84     public InterpreterPool() {
85         factories.putAll(defaultFactories);
86     }
87
88     /**
89      * Creates a new interpreter for the specified document and
90      * according to the specified language. This method can return
91      * null if no interpreter has been found for the specified
92      * language.
93      *
94      * @param document the document that needs the interpreter
95      * @param language the scripting language
96      */

97     public Interpreter createInterpreter(Document document, String JavaDoc language) {
98         InterpreterFactory factory = (InterpreterFactory)factories.get(language);
99         Interpreter interpreter = null;
100         if (factory != null)
101             interpreter = factory.createInterpreter
102                 (((SVGOMDocument)document).getURLObject());
103         if (document != null) {
104             interpreter.bindObject(BIND_NAME_DOCUMENT, document);
105         }
106
107         return interpreter;
108     }
109
110     /**
111      * Adds for the specified language, the specified Interpreter factory.
112      *
113      * @param language the language for which the factory is registered
114      * @param factory the <code>InterpreterFactory</code> to register
115      */

116     public void putInterpreterFactory(String JavaDoc language,
117                                       InterpreterFactory factory) {
118         factories.put(language, factory);
119     }
120
121     /**
122      * Removes the InterpreterFactory associated to the specified language.
123      *
124      * @param language the language for which the factory should be removed.
125      */

126     public void removeInterpreterFactory(String JavaDoc language) {
127         factories.remove(language);
128     }
129 }
130
131
Popular Tags