Skip to main content
summaryrefslogtreecommitdiffstats
blob: f3f1514d22884bd0aee29a15670a52bcddce2f50 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
<%@jet package="org.eclipse.jet.internal.compiler.templates" 
       class="JET2TransformGenerator" 
               %>
<%-- @header
Copyright (c) 2006, 2009 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
--%>
<%@jet imports="java.util.Iterator"%>
<%@jet imports="java.util.Map"%>
<%@jet imports="java.util.SortedMap"%>
<%@jet imports="java.util.TreeMap"%>
<%@jet imports="org.eclipse.jet.internal.JavaUtil"%>
<%@jet imports="org.eclipse.jet.internal.compiler.LoaderGenerationParameters"%>
<%
LoaderGenerationParameters parms = (LoaderGenerationParameters)context.getVariable("args"); //$NON-NLS-1$
SortedMap templateMap = new TreeMap(parms.getTemplateMap());
boolean useJava5 = context.hasVariable("useJava5") ? Boolean.valueOf((String)context.getVariable("useJava5")).booleanValue() : false; //$NON-NLS-1$  //$NON-NLS-2$  
%>
package <%= parms.getPackageName() %>;

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

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

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

    private JET2TemplateLoader delegate = null;

<% if(useJava5){ %>
    private static Map<String, Integer> pathToTemplateOrdinalMap = null;
<% } else { %>
    private static Map pathToTemplateOrdinalMap = null;
<% } %>

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jet.JET2TemplateLoaderExtension#getDelegateLoader()
     */
    public JET2TemplateLoader getDelegateLoader() {
        return delegate;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jet.JET2TemplateLoader#getTemplate(java.lang.String)
     */
    public JET2Template getTemplate(final String templatePath) {
        if (pathToTemplateOrdinalMap == null) {
            initPathToFactoryMap();
        }
<% if(useJava5) { %>
        final Integer ordinal = pathToTemplateOrdinalMap.get(templatePath);
<% } else { %>
        final Integer ordinal = (Integer)pathToTemplateOrdinalMap.get(templatePath);
<% } %>
        switch (ordinal != null ? ordinal.intValue() : -1) {
<% int ordinal = 0;
   for(Iterator i=templateMap.entrySet().iterator(); i.hasNext(); ordinal++) { 
       Map.Entry entry = (Map.Entry)i.next(); %>
        case <%= ordinal %>: // <%= entry.getKey() %>
            return new <%= entry.getValue() %>();
<% } %>
        default:
            return delegate != null ? delegate.getTemplate(templatePath) : null;
        }
    }

    private static synchronized void initPathToFactoryMap() {
        if (pathToTemplateOrdinalMap == null) {
<% if(useJava5) { %>
            pathToTemplateOrdinalMap = new HashMap<String,Integer>(<%=templateMap.size()%>);
<% } else { %>
            pathToTemplateOrdinalMap = new HashMap(<%=templateMap.size()%>);
<% } %>
<% ordinal = 0;
   for(Iterator i=templateMap.keySet().iterator(); i.hasNext(); ordinal++) { 
       String templatePath = (String)i.next(); %>
            pathToTemplateOrdinalMap.put(<%= JavaUtil.asJavaQuotedString(templatePath) %>,
                    new Integer(<%= ordinal %>));
<% } %>
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.jet.JET2TemplateLoaderExtension#setDelegateLoader(org.eclipse
     * .jet.JET2TemplateLoader)
     */
    public void setDelegateLoader(final JET2TemplateLoader loader) {
        this.delegate = loader;
    }
}

Back to the top