1 /*
2
3 Licensed to the Apache Software Foundation (ASF) under one or more
4 contributor license agreements. See the NOTICE file distributed with
5 this work for additional information regarding copyright ownership.
6 The ASF licenses this file to You under the Apache License, Version 2.0
7 (the "License"); you may not use this file except in compliance with
8 the License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17
18 */
19 package org.apache.batik.script;
20
21 import java.net.MalformedURLException;
22 import java.net.URL;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.Map;
26
27 import org.apache.batik.anim.dom.SVGOMDocument;
28 import org.apache.batik.util.Service;
29
30 import org.w3c.dom.Document;
31
32 /**
33 * A class allowing to create/query an {@link
34 * org.apache.batik.script.Interpreter} corresponding to a particular
35 * <code>Document</code> and scripting language.
36 *
37 * <p>By default, it is able to create interpreters for ECMAScript,
38 * Python and Tcl scripting languages if you provide the right jar
39 * files in your CLASSPATH (i.e. Rhino, JPython and Jacl jar
40 * files).</p>
41 *
42 * @author <a href="mailto:cjolif@ilog.fr">Christophe Jolif</a>
43 * @version $Id: InterpreterPool.java 1804130 2017-08-04 14:41:11Z ssteiner $
44 */
45 public class InterpreterPool {
46
47 /**
48 * Name of the "document" object when referenced by scripts
49 */
50 public static final String BIND_NAME_DOCUMENT = "document";
51
52 /**
53 * The default InterpreterFactory map.
54 */
55 protected static Map defaultFactories = new HashMap(7);
56
57 /**
58 * The InterpreterFactory map.
59 */
60 protected Map factories = new HashMap(7);
61
62 static {
63 Iterator iter = Service.providers(InterpreterFactory.class);
64 while (iter.hasNext()) {
65 InterpreterFactory factory = null;
66 factory = (InterpreterFactory)iter.next();
67 String[] mimeTypes = factory.getMimeTypes();
68 for (String mimeType : mimeTypes) {
69 defaultFactories.put(mimeType, factory);
70 }
71 }
72 }
73
74 /**
75 * Constructs a new <code>InterpreterPool</code>.
76 */
77 public InterpreterPool() {
78 factories.putAll(defaultFactories);
79 }
80
81 /**
82 * Creates a new interpreter for the specified document and
83 * according to the specified language. This method can return
84 * null if no interpreter has been found for the specified
85 * language.
86 *
87 * @param document the document that needs the interpreter
88 * @param language the scripting language
89 */
90 public Interpreter createInterpreter(Document document,
91 String language) {
92 return createInterpreter(document, language, null);
93 }
94
95 /**
96 * Creates a new interpreter for the specified document and
97 * according to the specified language. This method can return
98 * null if no interpreter has been found for the specified
99 * language.
100 *
101 * @param document the document that needs the interpreter
102 * @param language the scripting language
103 * @param imports The set of classes/packages to import (if
104 * the interpreter supports that).
105 */
106 public Interpreter createInterpreter(Document document,
107 String language,
108 ImportInfo imports) {
109 InterpreterFactory factory;
110 factory = (InterpreterFactory)factories.get(language);
111
112 if (factory == null) return null;
113
114 if (imports == null)
115 imports = ImportInfo.getImports();
116
117 Interpreter interpreter = null;
118 SVGOMDocument svgDoc = (SVGOMDocument) document;
119 URL url = null;
120 try {
121 url = new URL(svgDoc.getDocumentURI());
122 } catch (MalformedURLException e) {
123 }
124 interpreter = factory.createInterpreter(url, svgDoc.isSVG12(),
125 imports);
126
127 if (interpreter == null) return null;
128
129 if (document != null)
130 interpreter.bindObject(BIND_NAME_DOCUMENT, document);
131
132 return interpreter;
133 }
134
135 /**
136 * Adds for the specified language, the specified Interpreter factory.
137 *
138 * @param language the language for which the factory is registered
139 * @param factory the <code>InterpreterFactory</code> to register
140 */
141 public void putInterpreterFactory(String language,
142 InterpreterFactory factory) {
143 factories.put(language, factory);
144 }
145
146 /**
147 * Removes the InterpreterFactory associated to the specified language.
148 *
149 * @param language the language for which the factory should be removed.
150 */
151 public void removeInterpreterFactory(String language) {
152 factories.remove(language);
153 }
154 }
155