view src/kryshen/tema/io/TemplateReader.java @ 3:a20217d78068

Tema current (imported from CVS).
author Mikhail Kryshen <mikhail@kryshen.net>
date Thu, 06 Nov 2008 23:30:18 +0300
parents 6c41a0b43e58
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: TemplateReader.java,v 1.9 2008/09/30 14:02:24 mikhail Exp $
21 */
23 package kryshen.tema.io;
25 import java.io.IOException;
26 import java.io.Reader;
27 import java.io.LineNumberReader;
28 import java.io.PushbackReader;
29 import kryshen.tema.TemplateParser;
31 /**
32 * Reader for Tema templates. Stores data source name (commonly
33 * filename) and tracks line numbers for error reporting.
34 *
35 * @author Mikhail Kryshen
36 */
37 public class TemplateReader extends PushbackReader {
38 static final int UNREAD_BUFFER_SIZE;
40 /* Calculate UNREAD_BUFFER_SIZE value. */
41 static {
42 int max = 0;
44 for (String s : TemplateParser.BRACKET_OPEN) {
45 max = Math.max(max, s.length());
46 }
48 for (String s : TemplateParser.BRACKET_CLOSE) {
49 max = Math.max(max, s.length());
50 }
52 max = Math.max(max, TemplateParser.ESCAPE_NEWLINE.length() + 2);
53 max = Math.max(max, TemplateParser.ESCAPE_WHITESPACE.length());
55 UNREAD_BUFFER_SIZE = max;
56 }
58 private final String source;
59 private final LineNumberReader lnReader;
60 private final TemplateReader parentReader;
62 public TemplateReader(Reader in) {
63 this(new LineNumberReader(in));
64 }
66 public TemplateReader(Reader in, String source) {
67 this(new LineNumberReader(in), source);
68 }
70 public TemplateReader(LineNumberReader in) {
71 this(in, "");
72 }
74 public TemplateReader(LineNumberReader in, String source) {
75 super(in, UNREAD_BUFFER_SIZE);
77 this.parentReader = null;
78 this.lnReader = in;
79 this.source = source;
80 }
82 public TemplateReader(Reader in, TemplateReader parent) {
83 super(in, UNREAD_BUFFER_SIZE);
85 this.parentReader = parent;
86 this.lnReader = null;
87 this.source = null;
88 }
90 public String getSource() {
91 if (source != null)
92 return source;
94 return parentReader.getSource();
95 }
97 public int getLineNumber() {
98 if (lnReader != null)
99 return lnReader.getLineNumber();
101 return parentReader.getLineNumber();
102 }
104 public void unread(String s) throws IOException {
105 unread(s.toCharArray());
106 }
107 }