view src/kryshen/tema/Context.java @ 15:e9d13c7ffeb1

Update header comment.
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 24 Mar 2009 18:51:47 +0300
parents 6c41a0b43e58
children 54539dff18ca
line source
1 /*
2 * Copyright 2006-2009 Mikhail Kryshen
3 *
4 * This file is part of Tema.
5 *
6 * Tema is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * Tema is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the
17 * GNU Lesser General Public License along with Tema.
18 * If not, see <http://www.gnu.org/licenses/>.
19 */
21 package kryshen.tema;
23 import java.io.File;
24 import java.util.HashMap;
25 import java.util.Map;
27 /**
28 * Tema template context.
29 *
30 * @author Mikhail Kryshen
31 */
32 public class Context {
33 private Map<String, Object> definitions = new HashMap<String, Object>();
34 private Context superContext;
35 private File baseDir;
37 public Context() {
38 this(null, null);
39 }
41 public Context(Context superContext) {
42 this(superContext, null);
43 }
45 public Context(File baseDir) {
46 this(null, baseDir);
47 }
49 public Context(Context superContext, File baseDir) {
50 this.superContext = superContext;
51 this.baseDir = baseDir;
53 definitions.put("this", this);
55 if (superContext != null)
56 definitions.put("super", superContext);
57 }
59 public void clear() {
60 definitions.clear();
61 }
63 public boolean export(String name) {
64 Object value = definitions.get(name);
66 if (value == null)
67 return false;
69 export(name, value);
70 return true;
71 }
73 public void export(String name, Object value) {
74 if (superContext == null) {
75 set(name, value);
76 return;
77 }
79 definitions.remove(name);
80 superContext.export(name, value);
81 }
83 public Object get(String name) throws TemplateException {
84 Object value = definitions.get(name);
86 if (value == null && superContext != null)
87 return superContext.get(name);
89 return value;
90 }
92 /**
93 * Set the definition value.
94 *
95 * @param name Variable name.
96 * @param value New variable value.
97 */
98 public void set(String name, Object value) {
99 definitions.put(name, value);
100 }
102 /**
103 * Remove the definition recursively.
104 *
105 * @param name Definition name.
106 * @return true if the definition was found and removed.
107 */
108 public boolean unset(String name) {
109 if (definitions.remove(name) != null)
110 return true;
112 if (superContext != null)
113 return superContext.unset(name);
115 return false;
116 }
118 /**
119 * Returns base directory which should be used to resolve
120 * relative path names in template.
121 */
122 public File getBaseDirectory() {
123 if (baseDir == null && superContext != null)
124 return superContext.getBaseDirectory();
126 return baseDir;
127 }
128 }