view src/kryshen/tema/Context.java @ 2:6c41a0b43e58

Tema 0.3 (imported from CVS).
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 19 Feb 2008 20:32:17 +0300
parents
children e9d13c7ffeb1
line source
1 /*
2 * Copyright 2006-2008 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 *
20 * $Id: Context.java,v 1.11 2008/02/19 16:21:00 mikhail Exp $
21 */
23 package kryshen.tema;
25 import java.io.File;
26 import java.util.HashMap;
27 import java.util.Map;
29 /**
30 * Tema template context.
31 *
32 * @author Mikhail Kryshen
33 */
34 public class Context {
35 private Map<String, Object> definitions = new HashMap<String, Object>();
36 private Context superContext;
37 private File baseDir;
39 public Context() {
40 this(null, null);
41 }
43 public Context(Context superContext) {
44 this(superContext, null);
45 }
47 public Context(File baseDir) {
48 this(null, baseDir);
49 }
51 public Context(Context superContext, File baseDir) {
52 this.superContext = superContext;
53 this.baseDir = baseDir;
55 definitions.put("this", this);
57 if (superContext != null)
58 definitions.put("super", superContext);
59 }
61 public void clear() {
62 definitions.clear();
63 }
65 public boolean export(String name) {
66 Object value = definitions.get(name);
68 if (value == null)
69 return false;
71 export(name, value);
72 return true;
73 }
75 public void export(String name, Object value) {
76 if (superContext == null) {
77 set(name, value);
78 return;
79 }
81 definitions.remove(name);
82 superContext.export(name, value);
83 }
85 public Object get(String name) throws TemplateException {
86 Object value = definitions.get(name);
88 if (value == null && superContext != null)
89 return superContext.get(name);
91 return value;
92 }
94 /**
95 * Set the definition value.
96 *
97 * @param name Variable name.
98 * @param value New variable value.
99 */
100 public void set(String name, Object value) {
101 definitions.put(name, value);
102 }
104 /**
105 * Remove the definition recursively.
106 *
107 * @param name Definition name.
108 * @return true if the definition was found and removed.
109 */
110 public boolean unset(String name) {
111 if (definitions.remove(name) != null)
112 return true;
114 if (superContext != null)
115 return superContext.unset(name);
117 return false;
118 }
120 /**
121 * Returns base directory which should be used to resolve
122 * relative path names in template.
123 */
124 public File getBaseDirectory() {
125 if (baseDir == null && superContext != null)
126 return superContext.getBaseDirectory();
128 return baseDir;
129 }
130 }