Skip to content

Commit

Permalink
Improvements to ImportInfo class. Started to break it out to
Browse files Browse the repository at this point in the history
Bridge class (so user can configure it more easily and more specifically).


git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/batik/trunk@742654 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Thomas E. DeWeese committed Feb 9, 2009
1 parent f456a42 commit f91fe2c
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 25 deletions.
4 changes: 3 additions & 1 deletion sources/org/apache/batik/bridge/BridgeContext.java
Expand Up @@ -560,7 +560,9 @@ public Interpreter getInterpreter(String language) {
Interpreter interpreter = (Interpreter)interpreterMap.get(language);
if (interpreter == null) {
try {
interpreter = interpreterPool.createInterpreter(document, language);
interpreter = interpreterPool.createInterpreter(document,
language,
null);
interpreterMap.put(language, interpreter);
} catch (Exception e) {
if (userAgent != null) {
Expand Down
91 changes: 74 additions & 17 deletions sources/org/apache/batik/script/ImportInfo.java
Expand Up @@ -26,23 +26,37 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import java.io.Reader;
import java.net.URL;

import java.util.List;
import java.util.LinkedList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
* This class represents a list of classes/packages to import.
* This class represents a list of Java classes/packages to import
* into a scripting environment.
*
* It initializes it's self by reading a file from the classpath.
* It can initializes it's self by reading a file,
* from the classpath (META_INF/imports/script.xt).
*
* The format of the file is as follows:
*
* Anything after a '#' on a line is ignored.
*
* The first space delimited token on a line must be either 'class' or
* 'package'.
*
* The remainder of a line is whitespace delimited, fully qualified,
* Java class/package name (i.e. java.lang.System).
*
* @author <a href="mailto:deweese@apache.org>deweese</a>
* @version $Id: skel.el,v 1.1 2003/05/13 21:04:46 deweese Exp $
*/
public class ImportInfo {
/** Default file to read imports from, can be overridden
* by setting the 'org.apache.batik.script.imports' System
* property*/
static final String defaultFile = "META-INF/imports/script.txt";
static final String classStr = "class";
static final String packageStr = "package";

static String importFile;
static {
Expand All @@ -57,6 +71,12 @@ public class ImportInfo {

static ImportInfo defaultImports=null;

/**
* Returns the default ImportInfo instance.
*
* This instance is initialized by reading the file
* identified by 'importFile'.
*/
static public ImportInfo getImports() {
if (defaultImports == null)
defaultImports = readImports();
Expand Down Expand Up @@ -93,24 +113,61 @@ static ImportInfo readImports() {
}


protected List classes;
protected List packages;
protected Set classes;
protected Set packages;

/**
* Construct an empty ImportInfo instance
*/
public ImportInfo() {
classes = new LinkedList();
packages = new LinkedList();
classes = new HashSet();
packages = new HashSet();
}
public Iterator getClasses() { return classes.iterator(); }
public Iterator getPackages() { return packages.iterator(); }

public void addClass(String cls) {
classes.add(cls);
/**
* Return an unmodifiable iterator over the list of classes
*/
public Iterator getClasses() {
return Collections.unmodifiableSet(classes).iterator();
}

public void addPackage(String pkg) {
packages.add(pkg);
/**
* Return an unmodifiable iterator over the list of packages
*/
public Iterator getPackages() {
return Collections.unmodifiableSet(packages).iterator();
}

/**
* Add a class to the set of classes to import (must be
* a fully qualified classname - "java.lang.System").
*/
public void addClass (String cls) { classes.add(cls); }
/**
* Add a package to the set of packages to import (must be
* a fully qualified package - "java.lang").
*/
public void addPackage(String pkg) { packages.add(pkg); }

/**
* Remove a class from the set of classes to import (must be
* a fully qualified classname - "java.lang.System").
* @return true if the class was present.
*/
public boolean removeClass(String cls) { return classes.remove(cls); }
/**
* Remove a package from the set of packages to import (must be
* a fully qualified package - "java.lang").
* @return true if the package was present.
*/
public boolean removePackage(String pkg) { return packages.remove(pkg); }


static final String classStr = "class";
static final String packageStr = "package";
/**
* Add imports read from a URL to this ImportInfo instance.
* See the class documentation for the expected format of the file.
*/
public void addImports(URL src) throws IOException
{
InputStream is = null;
Expand Down
12 changes: 12 additions & 0 deletions sources/org/apache/batik/script/InterpreterFactory.java
Expand Up @@ -34,6 +34,18 @@ public interface InterpreterFactory {
*/
String[] getMimeTypes();

/**
* This method should create an instance of <code>Interpreter</code>
* interface implementation.
*
* @param documentURL the url for the document which will be scripted
* @param svg12 whether the document is an SVG 1.2 document
* @param imports The set of classes/packages to import (if
* the interpreter supports that), may be null.
*/
Interpreter createInterpreter(URL documentURL, boolean svg12,
ImportInfo imports);

/**
* This method should create an instance of <code>Interpreter</code>
* interface implementation.
Expand Down
30 changes: 27 additions & 3 deletions sources/org/apache/batik/script/InterpreterPool.java
Expand Up @@ -87,15 +87,39 @@ public InterpreterPool() {
* @param document the document that needs the interpreter
* @param language the scripting language
*/
public Interpreter createInterpreter(Document document, String language) {
InterpreterFactory factory = (InterpreterFactory)factories.get(language);
public Interpreter createInterpreter(Document document,
String language) {
return createInterpreter(document, language, null);
}

/**
* Creates a new interpreter for the specified document and
* according to the specified language. This method can return
* null if no interpreter has been found for the specified
* language.
*
* @param document the document that needs the interpreter
* @param language the scripting language
* @param imports The set of classes/packages to import (if
* the interpreter supports that).
*/
public Interpreter createInterpreter(Document document,
String language,
ImportInfo imports) {
InterpreterFactory factory;
factory = (InterpreterFactory)factories.get(language);

if (factory == null) return null;

if (imports == null)
imports = ImportInfo.getImports();

Interpreter interpreter = null;
SVGOMDocument svgDoc = (SVGOMDocument) document;
try {
URL url = new URL(svgDoc.getDocumentURI());
interpreter = factory.createInterpreter(url, svgDoc.isSVG12());
interpreter = factory.createInterpreter(url, svgDoc.isSVG12(),
imports);
} catch (MalformedURLException e) {
}

Expand Down
13 changes: 13 additions & 0 deletions sources/org/apache/batik/script/jacl/JaclInterpreterFactory.java
Expand Up @@ -57,4 +57,17 @@ public String[] getMimeTypes() {
public Interpreter createInterpreter(URL documentURL, boolean svg12) {
return new JaclInterpreter();
}

/**
* Creates an instance of <code>JaclInterpreter</code> class.
*
* @param documentURL the url for the document which will be scripted
* @param svg12 whether the document is an SVG 1.2 document
* @param imports The set of classes/packages to import (if
* the interpreter supports that), may be null.
*/
public Interpreter createInterpreter(URL documentURL, boolean svg12,
ImportInfo imports) {
return new JaclInterpreter();
}
}
Expand Up @@ -57,4 +57,17 @@ public String[] getMimeTypes() {
public Interpreter createInterpreter(URL documentURL, boolean svg12) {
return new JPythonInterpreter();
}

/**
* Creates an instance of <code>JPythonInterpreter</code> class.
*
* @param documentURL the url for the document which will be scripted
* @param svg12 whether the document is an SVG 1.2 document
* @param imports The set of classes/packages to import (if
* the interpreter supports that), may be null.
*/
public Interpreter createInterpreter(URL documentURL, boolean svg12,
ImportInfo imports) {
return new JPythonInterpreter();
}
}
26 changes: 24 additions & 2 deletions sources/org/apache/batik/script/rhino/RhinoInterpreter.java
Expand Up @@ -141,6 +141,27 @@ public class RhinoInterpreter implements Interpreter {
* @see org.apache.batik.script.InterpreterPool
*/
public RhinoInterpreter(URL documentURL) {
init(documentURL, null);
}

/**
* Build a <code>Interpreter</code> for ECMAScript using Rhino.
*
* @param documentURL the URL for the document which references
* @param imports the set of Java classes/packages to import
* into the scripting enviornment.
*
* @see org.apache.batik.script.Interpreter
* @see org.apache.batik.script.InterpreterPool
*/
public RhinoInterpreter(URL documentURL,
ImportInfo imports) {
init(documentURL, imports);
}

protected void init(URL documentURL,
final ImportInfo imports)
{
try {
rhinoClassLoader = new RhinoClassLoader
(documentURL, getClass().getClassLoader());
Expand All @@ -155,8 +176,9 @@ public Object run(Context cx) {
ClassCache cache = ClassCache.get(globalObject);
cache.setCachingEnabled(rhinoClassLoader != null);

ImportInfo ii = ImportInfo.getImports();

ImportInfo ii = imports;
if (ii == null) ii = ImportInfo.getImports();

// import Java lang package & DOM Level 3 & SVG DOM packages
StringBuffer sb = new StringBuffer();
Iterator iter;
Expand Down
18 changes: 16 additions & 2 deletions sources/org/apache/batik/script/rhino/RhinoInterpreterFactory.java
Expand Up @@ -20,6 +20,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more

import java.net.URL;

import org.apache.batik.script.ImportInfo;
import org.apache.batik.script.Interpreter;
import org.apache.batik.script.InterpreterFactory;
import org.apache.batik.script.rhino.svg12.SVG12RhinoInterpreter;
Expand Down Expand Up @@ -63,9 +64,22 @@ public String[] getMimeTypes() {
* @param svg12 whether the document is an SVG 1.2 document
*/
public Interpreter createInterpreter(URL documentURL, boolean svg12) {
return createInterpreter(documentURL, svg12, null);
}

/**
* Creates an instance of <code>RhinoInterpreter</code> class.
*
* @param documentURL the url for the document which will be scripted
* @param svg12 whether the document is an SVG 1.2 document
* @param imports The set of classes/packages to import (if
* the interpreter supports that), may be null.
*/
public Interpreter createInterpreter(URL documentURL, boolean svg12,
ImportInfo imports) {
if (svg12) {
return new SVG12RhinoInterpreter(documentURL);
return new SVG12RhinoInterpreter(documentURL, imports);
}
return new RhinoInterpreter(documentURL);
return new RhinoInterpreter(documentURL, imports);
}
}
Expand Up @@ -20,6 +20,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more

import java.net.URL;

import org.apache.batik.script.ImportInfo;
import org.apache.batik.script.rhino.RhinoInterpreter;

import org.mozilla.javascript.Context;
Expand All @@ -41,6 +42,14 @@ public SVG12RhinoInterpreter(URL documentURL) {
super(documentURL);
}

/**
* Creates an SVG12RhinoInterpreter object.
*/
public SVG12RhinoInterpreter(URL documentURL,
ImportInfo imports) {
super(documentURL, imports);
}

/**
* Defines the class for the global object.
*/
Expand Down

0 comments on commit f91fe2c

Please sign in to comment.