summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2007-05-16 12:44:13 (EDT)
committerpelder2007-05-16 12:44:13 (EDT)
commit3db18f9fad9f966b5b85c67364b98d14774e9edb (patch)
tree0f97482154744a953c493869e87205eadc38fb28
parent5bf4bce6937837c9687a6a94e853455954b4d9be (diff)
downloadorg.eclipse.jet-3db18f9fad9f966b5b85c67364b98d14774e9edb.zip
org.eclipse.jet-3db18f9fad9f966b5b85c67364b98d14774e9edb.tar.gz
org.eclipse.jet-3db18f9fad9f966b5b85c67364b98d14774e9edb.tar.bz2
[187298] Allow dynamic loading of JET1 templates as an alternative to org.eclipse.emf.codegen.JETEmitter
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/JET1TemplateManager.java167
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/JET2TemplateManager.java159
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/builder/JET2Builder.java5
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/PreferenceValueUtil.java73
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/SimplifiedCompiler.java42
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/templates/CodeGenTemplateLoader.java3
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/templates/JETTemplateMapGenerator.java39
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/runtime/JET2TemplateLoaderImpl.java178
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/runtime/JETBundleManager.java22
-rw-r--r--plugins/org.eclipse.jet/templates/jetTemplateMap.properties.jet19
10 files changed, 693 insertions, 14 deletions
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/JET1TemplateManager.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/JET1TemplateManager.java
new file mode 100644
index 0000000..57daa98
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/JET1TemplateManager.java
@@ -0,0 +1,167 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2007 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM - Initial API and implementation
+ *
+ * </copyright>
+ *
+ * $Id: JET1TemplateManager.java,v 1.1 2007/05/16 16:44:13 pelder Exp $
+ */
+package org.eclipse.jet;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.SubProgressMonitor;
+import org.eclipse.jet.transform.IJETBundleManager;
+import org.osgi.framework.BundleException;
+
+/**
+ * Mananger for operations that dynamically invoke JET1 templates.
+ * Manager for invoking dymanic JET1 templates. A replacement for (and simplification of) {@link org.eclipse.emf.codegen.jet.JETEmitter}.
+ * Operations implement {@link ITemplateOperation} and are run via {@link #run(String[], org.eclipse.jet.JET1TemplateManager.ITemplateOperation, IProgressMonitor)}.
+ * The operation may then dynamically load and execute templates found in the provided plug-ins via the passed {@link ITemplateRunner}.
+ * <p>
+ * Loads JET1 templates dynamically from Eclipse plug-ins and/or in workspace plug-in projects, so long
+ * as the projects were compiled with the org.eclipse.jet compiler (rather than org.eclipse.emf.codegen JET compiler).
+ * <p>
+ * Provisional API. This API is subject to breaking change or removal.
+ * </p>
+ */
+public class JET1TemplateManager
+{
+
+ /**
+ * Defines the protocol for an operation that dynamically invokes JET1 templates.
+ *
+ * @see JET1TemplateManager#run(String[], org.eclipse.jet.JET1TemplateManager.ITemplateOperation, IProgressMonitor)
+ */
+ public interface ITemplateRunner {
+ /**
+ * Execute a JET1 template
+ * @param templatePath the template path
+ * @param argument the template argument
+ * @return the template result
+ * @throws IllegalAccessException if templatePath cannot be resolved.
+ */
+ String generate(String templatePath, Object argument);
+
+ }
+
+
+ /**
+ * Implementation of {@link ITemplateRunner}.
+ */
+ private static class JET1TemplateRunnerImpl implements ITemplateRunner {
+
+ private final JET2TemplateLoader[] loaders;
+ private final JET2Context context = new JET2Context(null);
+ public JET1TemplateRunnerImpl(JET2TemplateLoader[] loaders)
+ {
+ this.loaders = loaders;
+
+ }
+ public String generate(String templatePath, Object argument)
+ {
+ JET2Template template = null;
+ for (int i = 0; i < loaders.length && template == null; i++)
+ {
+ template = loaders[i].getTemplate(templatePath);
+ }
+ if(template == null) {
+ throw new IllegalArgumentException();
+ }
+
+ BodyContentWriter writer = new BodyContentWriter();
+ template.generate(context, writer);
+ return writer.getContent();
+ }
+
+ }
+
+ /**
+ * Protocol for a dynamic template operation.
+ *
+ * @see JET1TemplateManager#run(String[], org.eclipse.jet.JET1TemplateManager.ITemplateOperation, IProgressMonitor)
+ */
+ public interface ITemplateOperation {
+ /**
+ * Run the template operation. The passed template runner provides the operation with access t
+ * @param templateRunner
+ */
+ void run(final ITemplateRunner templateRunner);
+ }
+
+ private JET1TemplateManager() {
+ // prevent instantiation
+ }
+
+ /**
+ * @param pluginIDs
+ */
+ private static void checkPlugins(String[] pluginIDs)
+ {
+ if(pluginIDs == null) {
+ throw new NullPointerException();
+ }
+ if(pluginIDs.length == 0) {
+ throw new IllegalArgumentException();
+ }
+ for (int i = 0; i < pluginIDs.length; i++)
+ {
+ if(pluginIDs[i] == null) {
+ throw new IllegalArgumentException();
+ }
+ }
+ }
+
+ /**
+ * Execute a JET templates that are loaded dynamically from the provided Eclipse plug-ins.
+ * Each plug-in ID must be the ID of an already loaded plug-in, or of a plug-in in the
+ * Eclipse workspace. The plug-in list is search in order until a template is found.
+ * @param pluginIDs a non-empty list of Eclipse plug-in ids.
+ * @param operation an operation that will consume the templates
+ * @param monitor a progress monitor
+ * @throws BundleException if one of the plug-ins cannot be loaded
+ * @throws NullPointerException if pluginIDs is <code>null</code>, or if any element is <code>null</code>
+ * @throws IllegalArgumentException if pluginIDs has zero length
+ */
+ public static void run(String[] pluginIDs, ITemplateOperation operation, IProgressMonitor monitor) throws BundleException
+ {
+ checkPlugins(pluginIDs);
+
+ IJETBundleManager bundleManager = JET2Platform.getJETBundleManager();
+ List connectedBundles = new ArrayList(pluginIDs.length);
+ JET2TemplateLoader[] templateLoaders = new JET2TemplateLoader[pluginIDs.length];
+ try {
+ for (int i = 0; i < pluginIDs.length; i++)
+ {
+ String id = pluginIDs[i];
+ bundleManager.connect(id, new SubProgressMonitor(monitor, 1));
+ connectedBundles.add(id);
+ templateLoaders[i] = bundleManager.getTemplateLoader(id);
+ }
+ ITemplateRunner templateRunner = new JET1TemplateRunnerImpl(templateLoaders);
+
+ operation.run(templateRunner);
+
+ } finally {
+ for (Iterator i = connectedBundles.iterator(); i.hasNext();)
+ {
+ String id = (String)i.next();
+ bundleManager.disconnect(id);
+ }
+ }
+ }
+
+
+}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/JET2TemplateManager.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/JET2TemplateManager.java
new file mode 100644
index 0000000..0d96694
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/JET2TemplateManager.java
@@ -0,0 +1,159 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2007 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM - Initial API and implementation
+ *
+ * </copyright>
+ *
+ * $Id: JET2TemplateManager.java,v 1.1 2007/05/16 16:44:13 pelder Exp $
+ */
+package org.eclipse.jet;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.jet.transform.IJETBundleManager;
+import org.osgi.framework.BundleException;
+
+/**
+ * Mananger for operations that dynamically invoke JET2 templates.
+ * Operations implement {@link ITemplateOperation} and are run via {@link #run(String[], org.eclipse.jet.JET2TemplateManager.ITemplateOperation)}.
+ * The operation may then dynamically load and execute templates found in the provided plug-ins via the passed {@link ITemplateRunner}.
+ * <p>
+ * Provisional API. This API is subject to breaking change or removal.
+ * </p>
+ */
+public class JET2TemplateManager
+{
+
+ /**
+ * Defines the protocol for an operation that dynamically invokes JET2 templates.
+ *
+ * @see JET2TemplateManager#run(String[], JET2TemplateManager.ITemplateOperation)
+ */
+ public interface ITemplateRunner {
+ /**
+ * Execute a JET2 template
+ * @param templatePath the template path
+ * @param context the JET invocation context
+ * @param out the JET2 writer
+ * @return the template result
+ * @throws IllegalAccessException if templatePath cannot be resolved.
+ */
+ void generate(String templatePath, JET2Context context, JET2Writer out);
+
+ }
+
+
+ /**
+ * Implementation of {@link ITemplateRunner}.
+ */
+ private static class TemplateRunnerImpl implements ITemplateRunner {
+
+ private final JET2TemplateLoader[] loaders;
+ public TemplateRunnerImpl(JET2TemplateLoader[] loaders)
+ {
+ this.loaders = loaders;
+
+ }
+ public void generate(String templatePath, JET2Context context, JET2Writer out)
+ {
+ JET2Template template = null;
+ for (int i = 0; i < loaders.length && template == null; i++)
+ {
+ template = loaders[i].getTemplate(templatePath);
+ }
+ if(template == null) {
+ throw new IllegalArgumentException();
+ }
+
+ template.generate(context, out);
+ }
+
+ }
+
+ /**
+ * Protocol for a dynamic template operation.
+ *
+ * @see JET2TemplateManager#run(String[], JET2TemplateManager.ITemplateOperation)
+ */
+ public interface ITemplateOperation {
+ /**
+ * Run the template operation. The passed template runner provides the operation with access t
+ * @param templateRunner
+ */
+ void run(final ITemplateRunner templateRunner);
+ }
+
+ private JET2TemplateManager() {
+ // prevent instantiation
+ }
+
+ /**
+ * @param pluginIDs
+ */
+ private static void checkPlugins(String[] pluginIDs)
+ {
+ if(pluginIDs == null) {
+ throw new NullPointerException();
+ }
+ if(pluginIDs.length == 0) {
+ throw new IllegalArgumentException();
+ }
+ for (int i = 0; i < pluginIDs.length; i++)
+ {
+ if(pluginIDs[i] == null) {
+ throw new IllegalArgumentException();
+ }
+ }
+ }
+
+ /**
+ * Execute a JET templates that are loaded dynamically from the provided Eclipse plug-ins.
+ * Each plug-in ID must be the ID of an already loaded plug-in, or of a plug-in in the
+ * Eclipse workspace. The plug-in list is search in order until a template is found.
+ * @param pluginIDs a non-empty list of Eclipse plug-in ids.
+ * @param operation an operation that will consume the templates
+ * @throws BundleException if one of the plug-ins cannot be loaded
+ * @throws NullPointerException if pluginIDs is <code>null</code>, or if any element is <code>null</code>
+ * @throws IllegalArgumentException if pluginIDs has zero length
+ */
+ public static void run(String[] pluginIDs, ITemplateOperation operation) throws BundleException
+ {
+ checkPlugins(pluginIDs);
+
+ IJETBundleManager bundleManager = JET2Platform.getJETBundleManager();
+ List connectedBundles = new ArrayList(pluginIDs.length);
+ JET2TemplateLoader[] templateLoaders = new JET2TemplateLoader[pluginIDs.length];
+ try {
+ for (int i = 0; i < pluginIDs.length; i++)
+ {
+ String id = pluginIDs[i];
+ bundleManager.connect(id, new NullProgressMonitor());
+ connectedBundles.add(id);
+ templateLoaders[i] = bundleManager.getTemplateLoader(id);
+ }
+ ITemplateRunner templateRunner = new TemplateRunnerImpl(templateLoaders);
+
+ operation.run(templateRunner);
+
+ } finally {
+ for (Iterator i = connectedBundles.iterator(); i.hasNext();)
+ {
+ String id = (String)i.next();
+ bundleManager.disconnect(id);
+ }
+ }
+ }
+
+
+}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/builder/JET2Builder.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/builder/JET2Builder.java
index f8dfa05..710b93a 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/builder/JET2Builder.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/builder/JET2Builder.java
@@ -207,7 +207,10 @@ public class JET2Builder extends IncrementalProjectBuilder implements SavedState
compiler.compile(templatePath);
monitor.worked(1);
}
- compiler.finish();
+ if(templatesToCompile.size() > 0 || templatesToRemove.size() > 0)
+ {
+ compiler.finish();
+ }
savedState = compiler.getMemento();
return computeReferencedProjects(compiler.getTagLibaryDependencies());
}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/PreferenceValueUtil.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/PreferenceValueUtil.java
new file mode 100644
index 0000000..8d98f21
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/PreferenceValueUtil.java
@@ -0,0 +1,73 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2007 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM - Initial API and implementation
+ *
+ * </copyright>
+ *
+ * $Id: PreferenceValueUtil.java,v 1.1 2007/05/16 16:44:13 pelder Exp $
+ */
+package org.eclipse.jet.internal.compiler;
+
+import java.text.CharacterIterator;
+import java.text.StringCharacterIterator;
+
+/**
+ * Utility class for propertly escaping Java preference values.
+ */
+public class PreferenceValueUtil
+{
+
+ public static String encode(String value)
+ {
+ StringBuffer buffer = new StringBuffer(value.length());
+
+ CharacterIterator ci = new StringCharacterIterator(value);
+ for(char c = ci.first(); c != CharacterIterator.DONE; c = ci.next())
+ {
+ switch(c) {
+ case '\\':
+ case '\t':
+ case '\r':
+ case '\n':
+ case '\f':
+ case ' ':
+ case '#':
+ case '!':
+ case '=':
+ buffer.append('\\').append(c);
+ break;
+ default:
+ if(0x0020 <= c && c <= 0x007e)
+ {
+ buffer.append(c);
+ }
+ else
+ {
+ buffer.append("\\u").append(hexValue(c)); //$NON-NLS-1$
+ }
+ }
+ }
+
+
+ return buffer.toString();
+ }
+
+ private static final char[] hexDigits = "0123456789abcdef".toCharArray(); //$NON-NLS-1$
+ private static char[] hexValue(char c)
+ {
+ char[] result = new char[4];
+ result[0] = hexDigits[(c >> 12) & 0xf];
+ result[1] = hexDigits[(c >> 8) & 0xf ];
+ result[2] = hexDigits[(c >> 4) & 0xf ];
+ result[3] = hexDigits[c & 0xf ];
+ return result;
+ }
+}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/SimplifiedCompiler.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/SimplifiedCompiler.java
index 847a5b4..c6c972e 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/SimplifiedCompiler.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/SimplifiedCompiler.java
@@ -12,7 +12,7 @@
*
* </copyright>
*
- * $Id: SimplifiedCompiler.java,v 1.1 2007/04/04 14:59:32 pelder Exp $
+ * $Id: SimplifiedCompiler.java,v 1.2 2007/05/16 16:44:13 pelder Exp $
*/
package org.eclipse.jet.internal.compiler;
@@ -55,6 +55,8 @@ import org.eclipse.jet.taglib.TagLibraryReference;
public final class SimplifiedCompiler implements IJETCompiler
{
+ private static final String JET_TEMPLATE_MAP_PROPERTIES = "jetTemplateMap.properties"; //$NON-NLS-1$
+
private static final class NullTemplateResolver implements ITemplateResolver
{
public URI[] getBaseLocations()
@@ -469,18 +471,30 @@ public final class SimplifiedCompiler implements IJETCompiler
*/
public void finish()
{
- if(templateLoaderClassName == null) return;
-
- final LoaderGenerationParameters args = new LoaderGenerationParameters(templateLoaderPackage,
- templateLoaderClassName, uniqueNameGenerator.getPathToFQNMap());
-
- // run the code generator
TemplateRunner templateRunner = new TemplateRunner();
- String code = templateRunner.generate("templates/jet2transform.jet", //$NON-NLS-1$
- Collections.singletonMap("args", args)); //$NON-NLS-1$
-
- compilerOutput.writeOutput(outputPath(templateLoaderPackage, templateLoaderClassName),
- NewLineUtil.setLineTerminator(code, NL), null);
+
+ if(jetSpec == JETAST.JET_SPEC_V1)
+ {
+ // enable dynamic loading of JET1 templates
+ String mapContents = templateRunner.generate("templates/jetTemplateMap.properties.jet", //$NON-NLS-1$
+ Collections.singletonMap("args",uniqueNameGenerator.getPathToFQNMap())); //$NON-NLS-1$
+ compilerOutput.writeOutput(JET_TEMPLATE_MAP_PROPERTIES,
+ NewLineUtil.setLineTerminator(mapContents, NL), null);
+ }
+
+ if(templateLoaderClassName != null)
+ {
+
+ final LoaderGenerationParameters args = new LoaderGenerationParameters(templateLoaderPackage,
+ templateLoaderClassName, uniqueNameGenerator.getPathToFQNMap());
+
+ // run the code generator
+ String code = templateRunner.generate("templates/jet2transform.jet", //$NON-NLS-1$
+ Collections.singletonMap("args", args)); //$NON-NLS-1$
+
+ compilerOutput.writeOutput(outputPath(templateLoaderPackage, templateLoaderClassName),
+ NewLineUtil.setLineTerminator(code, NL), null);
+ }
}
@@ -499,6 +513,10 @@ public final class SimplifiedCompiler implements IJETCompiler
if(templateLoaderClassName != null)
{
compilerOutput.removeOutput(outputPath(templateLoaderPackage, templateLoaderClassName));
+ if(jetSpec == JETAST.JET_SPEC_V1)
+ {
+ compilerOutput.removeOutput(JET_TEMPLATE_MAP_PROPERTIES);
+ }
}
// forget all our internal state
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/templates/CodeGenTemplateLoader.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/templates/CodeGenTemplateLoader.java
index ee55276..41cd8b5 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/templates/CodeGenTemplateLoader.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/templates/CodeGenTemplateLoader.java
@@ -67,9 +67,10 @@ public class CodeGenTemplateLoader implements JET2TemplateLoader, JET2TemplateLo
}
private void initPathToFactoryMap() {
- this.pathToFactoryMap = new HashMap(10);
+ this.pathToFactoryMap = new HashMap(11);
addTemplate("templates/jet2java.jet", "org.eclipse.jet.internal.compiler.templates.JET2JavaGenerator" ); //$NON-NLS-1$ //$NON-NLS-2$
addTemplate("templates/jet2transform.jet", "org.eclipse.jet.internal.compiler.templates.JET2TransformGenerator" ); //$NON-NLS-1$ //$NON-NLS-2$
+ addTemplate("templates/jetTemplateMap.properties.jet", "org.eclipse.jet.internal.compiler.templates.JETTemplateMapGenerator" ); //$NON-NLS-1$ //$NON-NLS-2$
addTemplate("templates/main.jet", "org.eclipse.jet.internal.compiler.templates.DummyMain" ); //$NON-NLS-1$ //$NON-NLS-2$
addTemplate("templates/tagDataDeclarations.jet", "org.eclipse.jet.internal.compiler.templates.TagDataDeclGenerator" ); //$NON-NLS-1$ //$NON-NLS-2$
addTemplate("templates/v1/jet2java.jet", "org.eclipse.jet.internal.compiler.templates.v1.JET1JavaGenerator" ); //$NON-NLS-1$ //$NON-NLS-2$
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/templates/JETTemplateMapGenerator.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/templates/JETTemplateMapGenerator.java
new file mode 100644
index 0000000..31a37dc
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/templates/JETTemplateMapGenerator.java
@@ -0,0 +1,39 @@
+package org.eclipse.jet.internal.compiler.templates;
+
+import org.eclipse.jet.JET2Context;
+import org.eclipse.jet.JET2Template;
+import org.eclipse.jet.JET2Writer;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import org.eclipse.jet.internal.compiler.PreferenceValueUtil;
+
+public class JETTemplateMapGenerator implements JET2Template {
+
+ public JETTemplateMapGenerator() {
+ super();
+ }
+
+ private static final String NL = System.getProperties().getProperty("line.separator"); //$NON-NLS-1$
+
+ public void generate(final JET2Context context, final JET2Writer __out) {
+ JET2Writer out = __out;
+
+Map templateMap = (Map)context.getVariable("args"); //$NON-NLS-1$
+List templatePaths = new ArrayList(templateMap.keySet());
+Collections.sort(templatePaths);
+
+
+ for(Iterator i = templatePaths.iterator(); i.hasNext();){
+ String key = (String)i.next();
+ String value = (String)templateMap.get(key);
+
+ out.write( PreferenceValueUtil.encode(key) );
+ out.write(" = "); //$NON-NLS-1$
+ out.write( PreferenceValueUtil.encode(value) );
+ out.write(NL);
+ }
+ }
+}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/runtime/JET2TemplateLoaderImpl.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/runtime/JET2TemplateLoaderImpl.java
new file mode 100644
index 0000000..e6d4b6a
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/runtime/JET2TemplateLoaderImpl.java
@@ -0,0 +1,178 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2007 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM - Initial API and implementation
+ *
+ * </copyright>
+ *
+ * $Id: JET2TemplateLoaderImpl.java,v 1.1 2007/05/16 16:44:13 pelder Exp $
+ */
+package org.eclipse.jet.internal.runtime;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.net.URL;
+import java.util.Map;
+import java.util.Properties;
+
+import org.eclipse.jet.JET2Context;
+import org.eclipse.jet.JET2Template;
+import org.eclipse.jet.JET2TemplateLoader;
+import org.eclipse.jet.JET2TemplateLoaderExtension;
+import org.eclipse.jet.JET2Writer;
+import org.osgi.framework.Bundle;
+
+/**
+ * An implementation of {@link JET2TemplateLoader} that loads template information
+ * from a properties file.
+ */
+public class JET2TemplateLoaderImpl implements JET2TemplateLoader, JET2TemplateLoaderExtension
+{
+
+ private final Map map;
+ private final Bundle bundle;
+ private JET2TemplateLoader delegate;
+
+ public JET2TemplateLoaderImpl(Bundle bundle, URL templateMap) throws IOException
+ {
+ this.bundle = bundle;
+ this.map = getTemplateMap(bundle, templateMap);
+ }
+
+ private Map getTemplateMap(Bundle bundle, URL mapURL) throws IOException
+ {
+ InputStream inputStream = null;
+ try {
+ inputStream = mapURL.openStream();
+ Properties map = new Properties();
+ map.load(inputStream);
+ return map;
+ } finally {
+ ensureClosed(inputStream);
+ }
+ }
+
+ /**
+ * @param inputStream
+ */
+ private void ensureClosed(InputStream inputStream)
+ {
+ if(inputStream != null) {
+ try
+ {
+ inputStream.close();
+ }
+ catch (IOException e)
+ {
+ // nothing to do
+ }
+ }
+ }
+
+
+ private static final class JET1TemplateWrapper implements JET2Template
+ {
+ private static final String JET1_ARGUMENT_CONTEXT_VAR = "argument"; //$NON-NLS-1$
+ private final Object template;
+ private final Method generateMethod;
+
+ public JET1TemplateWrapper(Class templateClass) throws SecurityException, NoSuchMethodException, InstantiationException, IllegalAccessException
+ {
+ template = templateClass.newInstance();
+ generateMethod = templateClass.getMethod("generate", new Class[] {Object.class}); //$NON-NLS-1$
+ }
+
+ public void generate(JET2Context context, JET2Writer out)
+ {
+
+ Object arg = context.hasVariable(JET1_ARGUMENT_CONTEXT_VAR) ? context.getVariable(JET1_ARGUMENT_CONTEXT_VAR) : null;
+ try
+ {
+ Object result = generateMethod.invoke(template, new Object[] {arg});
+ out.write(result != null ? result.toString() : ""); //$NON-NLS-1$
+ }
+ catch (IllegalArgumentException e)
+ {
+ context.logError(e.getCause());
+ }
+ catch (IllegalAccessException e)
+ {
+ context.logError(e.getCause());
+ }
+ catch (InvocationTargetException e)
+ {
+ context.logError(e.getCause());
+ }
+ }
+
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.JET2TemplateLoader#getTemplate(java.lang.String)
+ */
+ public JET2Template getTemplate(String templatePath)
+ {
+ String className = (String)map.get(templatePath);
+
+ if(className != null) {
+ try
+ {
+ Class templateClass = bundle.loadClass(className);
+
+ if(JET2Template.class.isAssignableFrom(templateClass)) {
+ return (JET2Template)templateClass.newInstance();
+ } else {
+ return new JET1TemplateWrapper(templateClass);
+ }
+
+ }
+ catch (ClassNotFoundException e)
+ {
+ // do nothing
+ }
+ catch (InstantiationException e)
+ {
+ // do nothing
+ }
+ catch (IllegalAccessException e)
+ {
+ // do nothing
+ }
+ catch (SecurityException e)
+ {
+ // do nothing
+ }
+ catch (NoSuchMethodException e)
+ {
+ // do nothing
+ }
+ }
+ return delegate != null ? delegate.getTemplate(templatePath) : null;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.JET2TemplateLoaderExtension#getDelegateLoader()
+ */
+ public JET2TemplateLoader getDelegateLoader()
+ {
+ return delegate;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.JET2TemplateLoaderExtension#setDelegateLoader(org.eclipse.jet.JET2TemplateLoader)
+ */
+ public void setDelegateLoader(JET2TemplateLoader loader)
+ {
+ this.delegate = loader;
+ }
+
+}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/runtime/JETBundleManager.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/runtime/JETBundleManager.java
index 532e45d..9d0bc7f 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/runtime/JETBundleManager.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/runtime/JETBundleManager.java
@@ -410,6 +410,16 @@ public class JETBundleManager implements IJETBundleManager
}
}
+ private URL getTemplateMapURL(String id)
+ {
+ final BundleWrapper wrappedBundle = (BundleWrapper)bundlesById.get(id);
+ if(wrappedBundle != null) {
+ return wrappedBundle.getBundle().getResource("jetTemplateMap.properties"); //$NON-NLS-1$
+ } else {
+ return null;
+ }
+ }
+
public JET2TemplateLoader getTemplateLoader(String id) throws BundleException
{
synchronized(bundlesById)
@@ -419,6 +429,18 @@ public class JETBundleManager implements IJETBundleManager
{
try
{
+ URL mapURL = getTemplateMapURL(id);
+ if(mapURL != null) {
+ try
+ {
+ JET2TemplateLoaderImpl loader = new JET2TemplateLoaderImpl(wrappedBundle.getBundle(), mapURL);
+ return loader;
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ }
+ }
final Class loaderClass = wrappedBundle.getBundle().loadClass(wrappedBundle.getProvider().getDescriptor(id).getTemplateLoaderClass());
final Object newInstance = loaderClass.newInstance();
if(newInstance instanceof JET2TemplateLoader)
diff --git a/plugins/org.eclipse.jet/templates/jetTemplateMap.properties.jet b/plugins/org.eclipse.jet/templates/jetTemplateMap.properties.jet
new file mode 100644
index 0000000..c87790a
--- /dev/null
+++ b/plugins/org.eclipse.jet/templates/jetTemplateMap.properties.jet
@@ -0,0 +1,19 @@
+<%@jet package="org.eclipse.jet.internal.compiler.templates" class="JETTemplateMapGenerator" %>
+<%@jet imports="java.util.ArrayList"%>
+<%@jet imports="java.util.Collections"%>
+<%@jet imports="java.util.Iterator"%>
+<%@jet imports="java.util.List"%>
+<%@jet imports="java.util.Map"%>
+<%@jet imports="org.eclipse.jet.internal.compiler.PreferenceValueUtil"%>
+<%
+Map templateMap = (Map)context.getVariable("args"); //$NON-NLS-1$
+List templatePaths = new ArrayList(templateMap.keySet());
+Collections.sort(templatePaths);
+%>
+<%
+ for(Iterator i = templatePaths.iterator(); i.hasNext();){
+ String key = (String)i.next();
+ String value = (String)templateMap.get(key);
+%>
+<%= PreferenceValueUtil.encode(key) %> = <%= PreferenceValueUtil.encode(value) %>
+<% } %>