1 9 10 package org.nanocontainer.script; 11 12 import java.io.File ; 13 import java.net.URL ; 14 import java.util.HashMap ; 15 import java.util.Map ; 16 17 24 public class ScriptBuilderResolver { 25 26 public static final String GROOVY = ".groovy"; 27 public static final String BEANSHELL = ".bsh"; 28 public static final String JAVASCRIPT = ".js"; 29 public static final String JYTHON = ".py"; 30 public static final String XML = ".xml"; 31 32 public static final String DEFAULT_GROOVY_BUILDER = "org.nanocontainer.script.groovy.GroovyContainerBuilder"; 33 public static final String DEFAULT_BEANSHELL_BUILDER = "org.nanocontainer.script.bsh.BeanShellContainerBuilder"; 34 public static final String DEFAULT_JAVASCRIPT_BUILDER = "org.nanocontainer.script.rhino.JavascriptContainerBuilder"; 35 public static final String DEFAULT_XML_BUILDER = "org.nanocontainer.script.xml.XMLContainerBuilder"; 36 public static final String DEFAULT_JYTHON_BUILDER = "org.nanocontainer.script.jython.JythonContainerBuilder"; 37 38 private final Map extensionToBuilders = new HashMap (); 39 40 41 42 public ScriptBuilderResolver() { 43 resetBuilders(); 44 } 45 46 47 52 public String getBuilderClassName(File compositionFile) { 53 String language = getExtension(compositionFile.getAbsolutePath()); 54 return getBuilderClassName(language); 55 } 56 57 58 59 60 65 public String getBuilderClassName(URL compositionURL) { 66 String language = getExtension(compositionURL.getFile()); 67 return getBuilderClassName(language); 68 } 69 70 81 public synchronized String getBuilderClassName(final String extension) throws UnsupportedScriptTypeException{ 82 String resultingBuilderClassName = null; 83 resultingBuilderClassName = (String ) extensionToBuilders.get(extension); 84 if (resultingBuilderClassName == null) { 85 throw new UnsupportedScriptTypeException(extension, this.getAllSupportedExtensions()); 86 } 87 return resultingBuilderClassName; 88 } 89 90 94 public synchronized void resetBuilders() { 95 extensionToBuilders.clear(); 96 97 registerBuilder(GROOVY, DEFAULT_GROOVY_BUILDER); 101 registerBuilder(BEANSHELL, DEFAULT_BEANSHELL_BUILDER); 102 registerBuilder(JAVASCRIPT, DEFAULT_JAVASCRIPT_BUILDER); 103 registerBuilder(XML, DEFAULT_XML_BUILDER); 104 registerBuilder(JYTHON, DEFAULT_JYTHON_BUILDER); 105 106 } 107 108 123 public synchronized void registerBuilder(final String extension, final String className) { 124 extensionToBuilders.put(extension, className); 125 } 126 127 131 public synchronized String [] getAllSupportedExtensions() { 132 return (String []) extensionToBuilders.keySet().toArray(new String [extensionToBuilders.size()]); 133 } 134 135 136 141 private String getExtension(String fileName) { 142 return fileName.substring(fileName.lastIndexOf(".")); 143 } 144 145 146 } 147 | Popular Tags |