1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/org/documents/epl-v10.php
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.ide.eclipse.adt.internal.wizards.templates;
18 
19 import static com.android.tools.lint.detector.api.LintUtils.assertionsEnabled;
20 
21 import com.android.annotations.NonNull;
22 import com.android.annotations.Nullable;
23 import com.android.ide.eclipse.adt.AdtPlugin;
24 
25 import freemarker.cache.TemplateLoader;
26 import freemarker.template.Configuration;
27 import freemarker.template.DefaultObjectWrapper;
28 import freemarker.template.Template;
29 
30 import java.io.IOException;
31 import java.io.Reader;
32 import java.io.StringReader;
33 import java.io.StringWriter;
34 import java.util.List;
35 import java.util.Map;
36 
37 /**
38  * A template handler which can evaluate simple strings. Used to evaluate
39  * parameter constraints during UI wizard value editing.
40  * <p>
41  * Unlike the more general {@link TemplateHandler} which is used to instantiate
42  * full template files (from resources, merging into existing files etc) this
43  * evaluator supports only simple strings, referencing only values from the
44  * provided map (and builtin functions).
45  */
46 class StringEvaluator implements TemplateLoader {
47     private Map<String, Object> mParameters;
48     private Configuration mFreemarker;
49     private String mCurrentExpression;
50 
StringEvaluator()51     StringEvaluator() {
52         mParameters = TemplateHandler.createBuiltinMap();
53 
54         mFreemarker = new Configuration();
55         mFreemarker.setObjectWrapper(new DefaultObjectWrapper());
56         mFreemarker.setTemplateLoader(this);
57     }
58 
59     /** Evaluates the given expression, with the given set of parameters */
60     @Nullable
evaluate(@onNull String expression, @NonNull List<Parameter> parameters)61     String evaluate(@NonNull String expression, @NonNull List<Parameter> parameters) {
62         // Render the instruction list template.
63         for (Parameter parameter : parameters) {
64             mParameters.put(parameter.id, parameter.value);
65         }
66         try {
67             mCurrentExpression = expression;
68             Template inputsTemplate = mFreemarker.getTemplate(expression);
69             StringWriter out = new StringWriter();
70             inputsTemplate.process(mParameters, out);
71             out.flush();
72             return out.toString();
73         } catch (Exception e) {
74             if (assertionsEnabled()) {
75                 AdtPlugin.log(e, null);
76             }
77             return null;
78         }
79     }
80 
81     // ---- Implements TemplateLoader ----
82 
83     @Override
findTemplateSource(String name)84     public Object findTemplateSource(String name) throws IOException {
85         return mCurrentExpression;
86     }
87 
88     @Override
getLastModified(Object templateSource)89     public long getLastModified(Object templateSource) {
90         return 0;
91     }
92 
93     @Override
getReader(Object templateSource, String encoding)94     public Reader getReader(Object templateSource, String encoding) throws IOException {
95         return new StringReader(mCurrentExpression);
96     }
97 
98     @Override
closeTemplateSource(Object templateSource)99     public void closeTemplateSource(Object templateSource) throws IOException {
100     }
101 }
102