view src/kryshen/tema/FunctionDataParser.java @ 1:548a93c24e55

Tema 0.1jk - Javakonkurs edition (imported from CVS).
author Mikhail Kryshen <mikhail@kryshen.net>
date Thu, 14 Dec 2006 23:22:05 +0300
parents 1d2fe61a3a62
children 6c41a0b43e58
line source
1 /*
2 * Copyright (C) 2006 Mikhail A. Kryshen
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * $Id: FunctionDataParser.java,v 1.5 2006/12/14 14:39:22 mikhail Exp $
19 */
21 package kryshen.tema;
23 import java.io.*;
24 import java.util.*;
26 import static kryshen.tema.TemplateParser.Result;
27 import static kryshen.tema.TemplateParser.FunctionData;
28 import static kryshen.tema.TemplateParser.Terminator;
30 /**
31 * Parser for a function data.
32 *
33 * @author Mikhail A. Kryshen
34 */
35 public class FunctionDataParser {
36 static final char[] ARG_SEPARATORS = TemplateParser.LIST_SEPARATORS;
38 private TemplateParser tp;
39 private FunctionData fd;
40 private TemplateReader in;
42 private boolean available = true;
43 private boolean first = true;
45 FunctionDataParser(TemplateParser tp, FunctionData fd,
46 TemplateReader in) {
47 this.tp = tp;
48 this.fd = fd;
49 this.in = in;
50 }
52 private TemplateParser.Result parseData(Writer out, boolean argument)
53 throws IOException, TemplateException {
55 if (!available)
56 throw new TemplateException
57 ("Unexpected end of instruction data.", in);
59 TemplateParser.Result r;
61 if (fd == FunctionData.RECURSIVE) {
62 r = tp.parse(in, out, true,
63 argument ? ARG_SEPARATORS : null,
64 (argument || !first) ? ARG_SEPARATORS : null);
65 } else if (fd == FunctionData.NONRECURSIVE) {
66 r = tp.parse(in, out, false,
67 argument ? ARG_SEPARATORS : null,
68 (argument || !first) ? ARG_SEPARATORS : null);
69 } else if (fd == FunctionData.SUBFUNCTION && argument) {
70 // Subfunction can pass a list of arguments
71 StringWriter sw = new StringWriter();
72 tp.parseFunction(in, sw);
73 sw.close();
74 in = new TemplateReader(new StringReader(sw.toString()), in);
75 fd = FunctionData.NONRECURSIVE;
76 r = parseData(out, true);
77 } else if (fd == FunctionData.SUBFUNCTION) {
78 r = new Result(null, tp.parseFunction(in, out), false);
79 } else {
80 throw new IllegalArgumentException("Invalid FunctiodData: " + fd);
81 }
83 if (r.terminator != TemplateParser.Terminator.SEPARATOR)
84 available = false; // No more function data available.
86 first = false;
87 return r;
88 }
90 public int parseData(Writer out) throws IOException, TemplateException {
91 return parseData(out, false).retCode;
92 }
94 public String getData() throws IOException, TemplateException {
95 StringWriter sw = new StringWriter();
96 parseData(sw);
97 sw.close();
98 return sw.toString();
99 }
101 public int parseNextArg(Writer out) throws IOException, TemplateException {
102 TemplateParser.Result r = parseData(out, true);
104 // Ignore empty arguments.
105 while (r.empty && available) {
106 r = parseData(out, true);
107 }
109 return r.retCode;
110 }
112 public String getNextArg() throws IOException, TemplateException {
113 StringWriter sw = new StringWriter();
114 parseNextArg(sw);
115 sw.close();
116 return sw.toString();
117 }
119 public List<String> getArgs() throws IOException, TemplateException {
120 List<String> l = new ArrayList<String>();
122 while (available) {
123 l.add(getNextArg());
124 }
126 return l;
127 }
129 public boolean hasMoreData() {
130 return available;
131 }
133 public TemplateParser getTemplateParser() {
134 return tp;
135 }
137 public TemplateReader getTemplateReader() {
138 return in;
139 }
140 }