Skip to main content
summaryrefslogtreecommitdiffstats
blob: b52e10b4fb059296b56b0b2ff29138298ef2621f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<%@jet package="org.eclipse.jet.internal.compiler.templates" 
       class="JET2TransformGenerator" 
               %>
<%@jet imports="java.util.Iterator"%>
<%@jet imports="java.util.Map"%>
<%@jet imports="org.eclipse.jet.internal.compiler.LoaderGenerationParameters"%>
<%
LoaderGenerationParameters parms = (LoaderGenerationParameters)context.getVariable("args"); //$NON-NLS-1$
Map templateMap = (Map) parms.getTemplateMap();;
%>
package <%= parms.getPackageName() %>;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jet.JET2TemplateLoader;
import org.eclipse.jet.JET2TemplateLoaderExtension;
import org.eclipse.jet.JET2Template;

public class <%= parms.getClassName() %> implements JET2TemplateLoader, JET2TemplateLoaderExtension {

	private static final class TemplateFactoryMethod {

		private final String templatePath;
		private final String templateClass;

		public TemplateFactoryMethod(String templatePath, String templateClass) {
			this.templatePath = templatePath;
			this.templateClass = templateClass;
			
		}
		
		public String getTemplateClass() {
			return templateClass;
		}

		public String getTemplatePath() {
			return templatePath;
		}

		public JET2Template newTemplate() {
			JET2Template template = null;
			try {
				template = (JET2Template) Class.forName(templateClass).newInstance();
			} catch (InstantiationException e) {
			} catch (IllegalAccessException e) {
			} catch (ClassNotFoundException e) {
			}
			return template;
		}
		
		
	}

	private Map pathToFactoryMap = null;
	
	private JET2TemplateLoader delegate = null;

	public <%= parms.getClassName() %>() {
		super();
	}

	public JET2Template getTemplate(String templatePath) {
	    if(pathToFactoryMap == null) {
   	        initPathToFactoryMap();
        }
        TemplateFactoryMethod factory = (TemplateFactoryMethod)pathToFactoryMap.get(templatePath);
		return factory == null ? delegate == null ? null : delegate.getTemplate(templatePath) : factory.newTemplate();
	}

	private void addTemplate(String templatePath, String templateClass) {
		pathToFactoryMap.put(templatePath, //$NON-NLS-1$
		    new TemplateFactoryMethod(templatePath, templateClass));
	}
	
    private void initPathToFactoryMap() {
	        pathToFactoryMap = new HashMap(<%= templateMap.size() %>);
<% for(Iterator i = templateMap.entrySet().iterator(); i.hasNext();){ 
		Map.Entry entry = (Map.Entry)i.next();
%>
		    addTemplate("<%= entry.getKey() %>", "<%= entry.getValue() %>" ); //$NON-NLS-1$  //$NON-NLS-2$
<% } %>
	}

    public void setDelegateLoader(JET2TemplateLoader loader) {
        this.delegate = loader;
    }
    
    public JET2TemplateLoader getDelegateLoader() {
        return delegate;
    }
}

Back to the top