view src/kryshen/tema/io/TemplateReader.java @ 15:e9d13c7ffeb1

Update header comment.
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 24 Mar 2009 18:51:47 +0300
parents a20217d78068
children bdd1c8d6b560
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.io;
23 import java.io.IOException;
24 import java.io.Reader;
25 import java.io.LineNumberReader;
26 import java.io.PushbackReader;
27 import kryshen.tema.TemplateParser;
29 /**
30 * Reader for Tema templates. Stores data source name (commonly
31 * filename) and tracks line numbers for error reporting.
32 *
33 * @author Mikhail Kryshen
34 */
35 public class TemplateReader extends PushbackReader {
36 static final int UNREAD_BUFFER_SIZE;
38 /* Calculate UNREAD_BUFFER_SIZE value. */
39 static {
40 int max = 0;
42 for (String s : TemplateParser.BRACKET_OPEN) {
43 max = Math.max(max, s.length());
44 }
46 for (String s : TemplateParser.BRACKET_CLOSE) {
47 max = Math.max(max, s.length());
48 }
50 max = Math.max(max, TemplateParser.ESCAPE_NEWLINE.length() + 2);
51 max = Math.max(max, TemplateParser.ESCAPE_WHITESPACE.length());
53 UNREAD_BUFFER_SIZE = max;
54 }
56 private final String source;
57 private final LineNumberReader lnReader;
58 private final TemplateReader parentReader;
60 public TemplateReader(Reader in) {
61 this(new LineNumberReader(in));
62 }
64 public TemplateReader(Reader in, String source) {
65 this(new LineNumberReader(in), source);
66 }
68 public TemplateReader(LineNumberReader in) {
69 this(in, "");
70 }
72 public TemplateReader(LineNumberReader in, String source) {
73 super(in, UNREAD_BUFFER_SIZE);
75 this.parentReader = null;
76 this.lnReader = in;
77 this.source = source;
78 }
80 public TemplateReader(Reader in, TemplateReader parent) {
81 super(in, UNREAD_BUFFER_SIZE);
83 this.parentReader = parent;
84 this.lnReader = null;
85 this.source = null;
86 }
88 public String getSource() {
89 if (source != null)
90 return source;
92 return parentReader.getSource();
93 }
95 public int getLineNumber() {
96 if (lnReader != null)
97 return lnReader.getLineNumber();
99 return parentReader.getLineNumber();
100 }
102 public void unread(String s) throws IOException {
103 unread(s.toCharArray());
104 }
105 }