view src/kryshen/tema/Context.java @ 30:54539dff18ca

Allow access to the calling (outer) context. Start parse_args implementation.
author Mikhail Kryshen <mikhail@kryshen.net>
date Fri, 15 May 2009 03:07:16 +0400
parents e9d13c7ffeb1
children
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.io.IOException;
25 import java.io.Writer;
26 import java.util.HashMap;
27 import java.util.Map;
28 import kryshen.tema.io.TemplateReader;
30 /**
31 * Tema template context.
32 *
33 * @author Mikhail Kryshen
34 */
35 public class Context {
36 private Map<String, Object> definitions = new HashMap<String, Object>();
37 private Context superContext;
38 private File baseDir;
40 public Context() {
41 this(null, null);
42 }
44 public Context(Context superContext) {
45 this(superContext, null);
46 }
48 public Context(File baseDir) {
49 this(null, baseDir);
50 }
52 public Context(Context superContext, File baseDir) {
53 this.superContext = superContext;
54 this.baseDir = baseDir;
56 definitions.put("this", this);
58 if (superContext != null)
59 definitions.put("super", superContext);
60 }
62 public void clear() {
63 definitions.clear();
64 }
66 public boolean export(String name) {
67 Object value = definitions.get(name);
69 if (value == null)
70 return false;
72 export(name, value);
73 return true;
74 }
76 public void export(String name, Object value) {
77 if (superContext == null) {
78 set(name, value);
79 return;
80 }
82 definitions.remove(name);
83 superContext.export(name, value);
84 }
86 public Object get(String name) throws TemplateException {
87 Object value = definitions.get(name);
89 if (value == null && superContext != null)
90 return superContext.get(name);
92 return value;
93 }
95 /**
96 * Set the definition value.
97 *
98 * @param name Variable name.
99 * @param value New variable value.
100 */
101 public void set(String name, Object value) {
102 definitions.put(name, value);
103 }
105 /**
106 * Remove the definition recursively.
107 *
108 * @param name Definition name.
109 * @return true if the definition was found and removed.
110 */
111 public boolean unset(String name) {
112 if (definitions.remove(name) != null)
113 return true;
115 if (superContext != null)
116 return superContext.unset(name);
118 return false;
119 }
121 int parseInContext(TemplateReader in, Writer out, Context outer)
122 throws IOException, TemplateException {
124 final String OUTER = "outer";
126 boolean hadOuter = definitions.containsKey(OUTER);
127 Object oldOuter = null;
128 if (hadOuter) {
129 oldOuter = definitions.get(OUTER);
130 }
132 definitions.put(OUTER, outer);
134 try {
135 TemplateParser parser = new TemplateParser(this);
136 return parser.parse(in, out);
137 } finally {
138 if (hadOuter) {
139 definitions.put(OUTER, oldOuter);
140 } else {
141 definitions.remove(OUTER);
142 }
143 }
144 }
146 /**
147 * Returns base directory which should be used to resolve
148 * relative path names in template.
149 */
150 public File getBaseDirectory() {
151 if (baseDir == null && superContext != null)
152 return superContext.getBaseDirectory();
154 return baseDir;
155 }
156 }