view src/kryshen/tema/FunctionDataParser.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 548a93c24e55
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: FunctionDataParser.java,v 1.27 2008/02/16 17:56:34 mikhail Exp $
21 */
23 package kryshen.tema;
25 import java.io.File;
26 import java.io.IOException;
27 import java.io.StringWriter;
28 import java.io.Writer;
29 import java.util.ArrayList;
30 import java.util.List;
31 import static kryshen.tema.TemplateParser.Result;
32 import static kryshen.tema.TemplateParser.DataFormat;
33 import kryshen.tema.TemplateParser.DataFormat;
34 import static kryshen.tema.TemplateParser.Terminator;
35 import kryshen.tema.io.CopyWriter;
36 import kryshen.tema.io.NullWriter;
37 import kryshen.tema.io.TemplateReader;
39 /**
40 * Parser for a function data.
41 *
42 * @author Mikhail Kryshen
43 */
44 public class FunctionDataParser {
45 static final char[] ARG_SEPARATORS = TemplateParser.LIST_SEPARATORS;
47 private TemplateParser tp;
48 private DataFormat fd;
49 private TemplateReader in;
51 private boolean available = true;
53 private int lastReturnCode = 0;
55 FunctionDataParser(TemplateParser tp, DataFormat fd, TemplateReader in)
56 throws IOException, TemplateException {
57 this.tp = tp;
58 this.fd = fd;
59 this.in = in;
61 // Check for empty function data.
62 checkAvailable();
63 }
65 private void checkAvailable() throws IOException, TemplateException {
66 if (tp.checkCloseBracket(in)) {
67 // Complete parsing the instruction.
68 skipData();
69 }
70 }
72 private int parseData(Writer out, boolean argument, boolean skip)
73 throws IOException, TemplateException {
75 if (!available)
76 throw new TemplateException
77 ("Unexpected end of instruction data.", in);
79 DataFormat fd = skip ? this.fd.noCall() : this.fd;
81 TemplateParser.Result r;
83 // if (fd.subfunction && argument) {
84 // // Allow subfunction to pass a list of arguments
85 // StringWriter sw = new StringWriter();
86 // tp.parse(in, sw, fd);
87 // sw.close();
88 // in = new TemplateReader(new StringReader(sw.toString()), in);
89 // this.fd = DataFormat.VERBATIM;
90 // r = parseData(out, true, false);
91 // } else
92 if (fd.subfunction) {
93 r = tp.parse(in, out, fd);
94 } else if (argument) {
95 // Skip duplicate separators before argument.
96 tp.skip(in, ARG_SEPARATORS);
98 r = tp.parse(in, out, fd, ARG_SEPARATORS);
100 // Skip duplicate separators after argument.
101 if (r.terminator == Terminator.SEPARATOR) {
102 tp.skip(in, ARG_SEPARATORS);
103 checkAvailable();
104 }
105 } else {
106 r = tp.parse(in, out, fd, null);
107 }
109 if (r.terminator != Terminator.SEPARATOR)
110 available = false; // No more function data available.
112 lastReturnCode = r.retCode;
113 return r.retCode;
114 }
116 /**
117 * Parse function data.
118 */
119 public int parseData(Writer out) throws IOException, TemplateException {
120 return parseData(out, false, false);
121 }
123 /**
124 * Skip function data (do not call any functions).
125 */
126 public void skipData() throws IOException, TemplateException {
127 parseData(NullWriter.INSTANCE, false, true);
128 }
130 /**
131 * Get function data as string.
132 */
133 public String getData() throws IOException, TemplateException {
134 StringWriter sw = new StringWriter();
135 parseData(sw);
136 sw.close();
137 return sw.toString();
138 }
140 /**
141 * Parse function data into specified <code>Writer</code>.
142 * Returns copy of the parsed data as string.
143 */
144 public String getData(Writer out) throws IOException, TemplateException {
145 StringWriter sw = new StringWriter();
146 parseData(new CopyWriter(sw, out));
147 sw.close();
148 return sw.toString();
149 }
151 public int parseNextArg(Writer out) throws IOException, TemplateException {
152 return parseData(out, true, false);
153 }
155 public void skipNextArg() throws IOException, TemplateException {
156 parseData(NullWriter.INSTANCE, true, true);
157 }
159 public String getNextArg() throws IOException, TemplateException {
160 StringWriter sw = new StringWriter();
161 parseNextArg(sw);
162 sw.close();
163 return sw.toString();
164 }
166 public List<String> getArgs() throws IOException, TemplateException {
167 List<String> l = new ArrayList<String>();
169 while (available) {
170 l.add(getNextArg());
171 }
173 return l;
174 }
176 public boolean hasMoreData() {
177 return available;
178 }
180 public int getLastReturnCode() {
181 return lastReturnCode;
182 }
184 public TemplateParser getTemplateParser() {
185 return tp;
186 }
188 public Context getContext() {
189 return tp.getContext();
190 }
192 public TemplateReader getTemplateReader() {
193 return in;
194 }
196 public File createFile(String path) {
197 File file = new File(path);
199 if (file.isAbsolute())
200 return file;
202 return new File(tp.getContext().getBaseDirectory(), path);
203 }
204 }