org.stjs.generator.plugin.GenerationPlugins Maven / Gradle / Ivy
package org.stjs.generator.plugin;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.stjs.generator.STJSRuntimeException;
import org.stjs.generator.check.CheckVisitor;
import org.stjs.generator.writer.WriterVisitor;
import org.stjs.javascript.annotation.UsePlugin;
import com.google.common.io.Closeables;
/**
* This class contains all the generation plugins defined by the users and the default ones
*
* @author acraciun
*
*/
public class GenerationPlugins {
private static final String STJS_PLUGINS_CONFIG_FILE = "META-INF/stjs.plugins";
private static final String JAVA_VERSION_ENTRY = "java.version";
private final Map> mandatoryPlugins = new HashMap>();
private final Map> optionalPlugins = new HashMap>();
private CheckVisitor checkVisitor = new CheckVisitor();
private WriterVisitor writerVisitor = new WriterVisitor();
public GenerationPlugins() {
MainGenerationPlugin mainPlugin = new MainGenerationPlugin();
mainPlugin.contributeCheckVisitor(checkVisitor);
mainPlugin.contributeWriteVisitor(writerVisitor);
mandatoryPlugins.put("default", mainPlugin);
Enumeration configFiles;
try {
configFiles = Thread.currentThread().getContextClassLoader().getResources(STJS_PLUGINS_CONFIG_FILE);
}
catch (IOException e) {
throw new STJSRuntimeException(e);
}
while (configFiles.hasMoreElements()) {
loadConfigFile(configFiles.nextElement());
}
}
private void loadConfigFile(URL configFile) {
InputStream input = null;
try {
input = configFile.openStream();
Properties props = new Properties();
props.load(input);
String javaVersion = props.getProperty(JAVA_VERSION_ENTRY);
String runningVersion = System.getProperty(JAVA_VERSION_ENTRY);
if (compareVersion(javaVersion, runningVersion) > 0) {
// this plugin is for a next version of java
return;
}
for (Map.Entry