view src/kryshen/tema/FunctionDataParser.java @ 17:5ebf123f3486

Documentation update and code cleanup.
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 24 Mar 2009 19:36:09 +0300
parents e9d13c7ffeb1
children 7b11f5174e29
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 kryshen.tema.TemplateParser.DataFormat;
30 import static kryshen.tema.TemplateParser.Terminator;
31 import kryshen.tema.io.CopyWriter;
32 import kryshen.tema.io.NullWriter;
33 import kryshen.tema.io.TemplateReader;
35 /**
36 * Parser for a function data.
37 *
38 * @author Mikhail Kryshen
39 */
40 public class FunctionDataParser {
41 static final char[] ARG_SEPARATORS = TemplateParser.LIST_SEPARATORS;
43 private TemplateParser tp;
44 private DataFormat fd;
45 private TemplateReader in;
47 private boolean available = true;
49 private int lastReturnCode = 0;
51 FunctionDataParser(TemplateParser tp, DataFormat fd, TemplateReader in)
52 throws IOException, TemplateException {
53 this.tp = tp;
54 this.fd = fd;
55 this.in = in;
57 // Check for empty function data.
58 checkAvailable();
59 }
61 private void checkAvailable() throws IOException, TemplateException {
62 if (tp.checkCloseBracket(in)) {
63 // Complete parsing the instruction.
64 skipData();
65 }
66 }
68 private int parseData(Writer out, boolean argument, boolean skip)
69 throws IOException, TemplateException {
71 if (!available)
72 throw new TemplateException
73 ("Unexpected end of instruction data.", in);
75 DataFormat fd = skip ? this.fd.noCall() : this.fd;
77 TemplateParser.Result r;
79 // if (fd.subfunction && argument) {
80 // // Allow subfunction to pass a list of arguments
81 // StringWriter sw = new StringWriter();
82 // tp.parse(in, sw, fd);
83 // sw.close();
84 // in = new TemplateReader(new StringReader(sw.toString()), in);
85 // this.fd = DataFormat.VERBATIM;
86 // r = parseData(out, true, false);
87 // } else
88 if (fd.subfunction) {
89 r = tp.parse(in, out, fd);
90 } else if (argument) {
91 // Skip duplicate separators before argument.
92 tp.skip(in, ARG_SEPARATORS);
94 r = tp.parse(in, out, fd, ARG_SEPARATORS);
96 // Skip duplicate separators after argument.
97 if (r.terminator == Terminator.SEPARATOR) {
98 tp.skip(in, ARG_SEPARATORS);
99 checkAvailable();
100 }
101 } else {
102 r = tp.parse(in, out, fd, null);
103 }
105 if (r.terminator != Terminator.SEPARATOR)
106 available = false; // No more function data available.
108 lastReturnCode = r.retCode;
109 return r.retCode;
110 }
112 /**
113 * Parse function data.
114 */
115 public int parseData(Writer out) throws IOException, TemplateException {
116 return parseData(out, false, false);
117 }
119 /**
120 * Skip function data (do not call any functions).
121 */
122 public void skipData() throws IOException, TemplateException {
123 parseData(NullWriter.INSTANCE, false, true);
124 }
126 /**
127 * Get function data as string.
128 */
129 public String getData() throws IOException, TemplateException {
130 StringWriter sw = new StringWriter();
131 parseData(sw);
132 sw.close();
133 return sw.toString();
134 }
136 /**
137 * Parse function data into specified <code>Writer</code>.
138 * Returns copy of the parsed data as string.
139 */
140 public String getData(Writer out) throws IOException, TemplateException {
141 StringWriter sw = new StringWriter();
142 parseData(new CopyWriter(sw, out));
143 sw.close();
144 return sw.toString();
145 }
147 public int parseNextArg(Writer out) throws IOException, TemplateException {
148 return parseData(out, true, false);
149 }
151 public void skipNextArg() throws IOException, TemplateException {
152 parseData(NullWriter.INSTANCE, true, true);
153 }
155 public String getNextArg() throws IOException, TemplateException {
156 StringWriter sw = new StringWriter();
157 parseNextArg(sw);
158 sw.close();
159 return sw.toString();
160 }
162 public List<String> getArgs() throws IOException, TemplateException {
163 List<String> l = new ArrayList<String>();
165 while (available) {
166 l.add(getNextArg());
167 }
169 return l;
170 }
172 public boolean hasMoreData() {
173 return available;
174 }
176 public int getLastReturnCode() {
177 return lastReturnCode;
178 }
180 public TemplateParser getTemplateParser() {
181 return tp;
182 }
184 public Context getContext() {
185 return tp.getContext();
186 }
188 public TemplateReader getTemplateReader() {
189 return in;
190 }
192 public File createFile(String path) {
193 File file = new File(path);
195 if (file.isAbsolute())
196 return file;
198 return new File(tp.getContext().getBaseDirectory(), path);
199 }
200 }