view src/kryshen/tema/io/TemplateReader.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 a20217d78068
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: TemplateReader.java,v 1.8 2008/02/19 00:20:48 mikhail Exp $
21 */
23 package kryshen.tema.io;
25 import java.io.Reader;
26 import java.io.LineNumberReader;
27 import java.io.PushbackReader;
28 import kryshen.tema.TemplateParser;
30 /**
31 * Reader for Tema templates. Stores data source name (commonly
32 * filename) and tracks line numbers for error reporting.
33 *
34 * @author Mikhail Kryshen
35 */
36 public class TemplateReader extends PushbackReader {
37 static final int UNREAD_BUFFER_SIZE;
39 /* Calculate UNREAD_BUFFER_SIZE value. */
40 static {
41 int max = 0;
43 for (String s : TemplateParser.BRACKET_OPEN) {
44 max = Math.max(max, s.length());
45 }
47 for (String s : TemplateParser.BRACKET_CLOSE) {
48 max = Math.max(max, s.length());
49 }
51 max = Math.max(max, TemplateParser.ESCAPE_NEWLINE.length() + 2);
52 max = Math.max(max, TemplateParser.ESCAPE_WHITESPACE.length());
54 UNREAD_BUFFER_SIZE = max;
55 }
57 private final String source;
58 private final LineNumberReader lnReader;
59 private final TemplateReader parentReader;
61 public TemplateReader(Reader in) {
62 this(new LineNumberReader(in));
63 }
65 public TemplateReader(Reader in, String source) {
66 this(new LineNumberReader(in), source);
67 }
69 public TemplateReader(LineNumberReader in) {
70 this(in, "");
71 }
73 public TemplateReader(LineNumberReader in, String source) {
74 super(in, UNREAD_BUFFER_SIZE);
76 this.parentReader = null;
77 this.lnReader = in;
78 this.source = source;
79 }
81 public TemplateReader(Reader in, TemplateReader parent) {
82 super(in, UNREAD_BUFFER_SIZE);
84 this.parentReader = parent;
85 this.lnReader = null;
86 this.source = null;
87 }
89 public String getSource() {
90 if (source != null)
91 return source;
93 return parentReader.getSource();
94 }
96 public int getLineNumber() {
97 if (lnReader != null)
98 return lnReader.getLineNumber();
100 return parentReader.getLineNumber();
101 }
102 }