view src/kryshen/tema/FunctionDataParser.java @ 15:e9d13c7ffeb1

Update header comment.
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 24 Mar 2009 18:51:47 +0300
parents 6c41a0b43e58
children 5ebf123f3486
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.StringWriter;
26 import java.io.Writer;
27 import java.util.ArrayList;
28 import java.util.List;
29 import static kryshen.tema.TemplateParser.Result;
30 import static kryshen.tema.TemplateParser.DataFormat;
31 import kryshen.tema.TemplateParser.DataFormat;
32 import static kryshen.tema.TemplateParser.Terminator;
33 import kryshen.tema.io.CopyWriter;
34 import kryshen.tema.io.NullWriter;
35 import kryshen.tema.io.TemplateReader;
37 /**
38 * Parser for a function data.
39 *
40 * @author Mikhail Kryshen
41 */
42 public class FunctionDataParser {
43 static final char[] ARG_SEPARATORS = TemplateParser.LIST_SEPARATORS;
45 private TemplateParser tp;
46 private DataFormat fd;
47 private TemplateReader in;
49 private boolean available = true;
51 private int lastReturnCode = 0;
53 FunctionDataParser(TemplateParser tp, DataFormat fd, TemplateReader in)
54 throws IOException, TemplateException {
55 this.tp = tp;
56 this.fd = fd;
57 this.in = in;
59 // Check for empty function data.
60 checkAvailable();
61 }
63 private void checkAvailable() throws IOException, TemplateException {
64 if (tp.checkCloseBracket(in)) {
65 // Complete parsing the instruction.
66 skipData();
67 }
68 }
70 private int parseData(Writer out, boolean argument, boolean skip)
71 throws IOException, TemplateException {
73 if (!available)
74 throw new TemplateException
75 ("Unexpected end of instruction data.", in);
77 DataFormat fd = skip ? this.fd.noCall() : this.fd;
79 TemplateParser.Result r;
81 // if (fd.subfunction && argument) {
82 // // Allow subfunction to pass a list of arguments
83 // StringWriter sw = new StringWriter();
84 // tp.parse(in, sw, fd);
85 // sw.close();
86 // in = new TemplateReader(new StringReader(sw.toString()), in);
87 // this.fd = DataFormat.VERBATIM;
88 // r = parseData(out, true, false);
89 // } else
90 if (fd.subfunction) {
91 r = tp.parse(in, out, fd);
92 } else if (argument) {
93 // Skip duplicate separators before argument.
94 tp.skip(in, ARG_SEPARATORS);
96 r = tp.parse(in, out, fd, ARG_SEPARATORS);
98 // Skip duplicate separators after argument.
99 if (r.terminator == Terminator.SEPARATOR) {
100 tp.skip(in, ARG_SEPARATORS);
101 checkAvailable();
102 }
103 } else {
104 r = tp.parse(in, out, fd, null);
105 }
107 if (r.terminator != Terminator.SEPARATOR)
108 available = false; // No more function data available.
110 lastReturnCode = r.retCode;
111 return r.retCode;
112 }
114 /**
115 * Parse function data.
116 */
117 public int parseData(Writer out) throws IOException, TemplateException {
118 return parseData(out, false, false);
119 }
121 /**
122 * Skip function data (do not call any functions).
123 */
124 public void skipData() throws IOException, TemplateException {
125 parseData(NullWriter.INSTANCE, false, true);
126 }
128 /**
129 * Get function data as string.
130 */
131 public String getData() throws IOException, TemplateException {
132 StringWriter sw = new StringWriter();
133 parseData(sw);
134 sw.close();
135 return sw.toString();
136 }
138 /**
139 * Parse function data into specified <code>Writer</code>.
140 * Returns copy of the parsed data as string.
141 */
142 public String getData(Writer out) throws IOException, TemplateException {
143 StringWriter sw = new StringWriter();
144 parseData(new CopyWriter(sw, out));
145 sw.close();
146 return sw.toString();
147 }
149 public int parseNextArg(Writer out) throws IOException, TemplateException {
150 return parseData(out, true, false);
151 }
153 public void skipNextArg() throws IOException, TemplateException {
154 parseData(NullWriter.INSTANCE, true, true);
155 }
157 public String getNextArg() throws IOException, TemplateException {
158 StringWriter sw = new StringWriter();
159 parseNextArg(sw);
160 sw.close();
161 return sw.toString();
162 }
164 public List<String> getArgs() throws IOException, TemplateException {
165 List<String> l = new ArrayList<String>();
167 while (available) {
168 l.add(getNextArg());
169 }
171 return l;
172 }
174 public boolean hasMoreData() {
175 return available;
176 }
178 public int getLastReturnCode() {
179 return lastReturnCode;
180 }
182 public TemplateParser getTemplateParser() {
183 return tp;
184 }
186 public Context getContext() {
187 return tp.getContext();
188 }
190 public TemplateReader getTemplateReader() {
191 return in;
192 }
194 public File createFile(String path) {
195 File file = new File(path);
197 if (file.isAbsolute())
198 return file;
200 return new File(tp.getContext().getBaseDirectory(), path);
201 }
202 }