01: /*
02: * Copyright 2007 Gerd Ziegler (www.gerdziegler.de)
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: * http://www.apache.org/licenses/LICENSE-2.0
07: * Unless required by applicable law or agreed to in writing,
08: * software distributed under the License is distributed on an
09: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
10: * either express or implied. See the License for the specific
11: * language governing permissions and limitations under the License.
12: * @author www.gerdziegler.de
13: */
14: package org.ztemplates.actions.expression;
15:
16: import org.apache.log4j.Logger;
17: import org.ztemplates.actions.util.ZFormatUtil;
18:
19: public class ZOptionalExpression implements ZTerm {
20: private static Logger log = Logger
21: .getLogger(ZOptionalExpression.class);
22:
23: private ZExpression optionalExpression;
24:
25: public String parse(String s) throws ZParserException {
26: if (s.charAt(0) != '[') {
27: throw new ZParserException(s,
28: "optional expression must start with '['");
29: }
30:
31: optionalExpression = new ZExpression();
32: String rest = optionalExpression.parse(s.substring(1));
33: if (rest.length() == 0) {
34: throw new ZParserException(s,
35: "optional expression must end with with ']'");
36: }
37: if (rest.charAt(0) != ']') {
38: throw new ZParserException(s,
39: "optional expression must end with with ']', not '"
40: + rest.charAt(0));
41: }
42: return rest.substring(1);
43: }
44:
45: public String toString() {
46: StringBuffer sb = new StringBuffer();
47: sb.append("<optional>\n");
48: sb.append(optionalExpression);
49: sb.append("\n</optional>");
50: return sb.toString();
51: }
52:
53: public void toXml(StringBuffer sb, int depth) {
54: ZFormatUtil.indent(sb, depth);
55: sb.append("<optional>");
56: optionalExpression.toXml(sb, depth + 1);
57: ZFormatUtil.indent(sb, depth);
58: sb.append("</optional>");
59: }
60:
61: public String toDefinition() {
62: return "[" + optionalExpression.toDefinition() + "]";
63: }
64:
65: public ZExpression getOptionalExpression() {
66: return optionalExpression;
67: }
68: }
|