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 ZNestedExpression implements ZTerm {
20: private static Logger log = Logger
21: .getLogger(ZNestedExpression.class);
22:
23: private String name;
24:
25: public String parse(String s) throws ZParserException {
26: if (!s.startsWith("#{")) {
27: throw new ZParserException(s,
28: "nested expression must start with '#{', single '#' is not allowed");
29: }
30:
31: int idx = s.indexOf('}');
32: if (idx < 0) {
33: throw new ZParserException(s,
34: "nested expression not closed by '}'");
35: }
36: if (idx == 2) {
37: throw new ZParserException(s,
38: "nested expression name must not be empty");
39: }
40:
41: name = s.substring(2, idx);
42: for (int i = 0; i < name.length(); i++) {
43: char c = name.charAt(i);
44: if (!Character.isJavaIdentifierPart(c)) {
45: throw new ZParserException(s,
46: "nested expression name can contain only java identifier characters");
47: }
48:
49: if (c == '$') {
50: throw new ZParserException(s,
51: "nested expression name can not contain '$'");
52: }
53: }
54:
55: return s.substring(idx + 1);
56: }
57:
58: public String getName() {
59: return name;
60: }
61:
62: public void setName(String name) {
63: this .name = name;
64: }
65:
66: public String toString() {
67: StringBuffer sb = new StringBuffer();
68: sb.append("<nested>");
69: sb.append(getName());
70: sb.append("</nested>");
71: return sb.toString();
72: }
73:
74: public void toXml(StringBuffer sb, int depth) {
75: ZFormatUtil.indent(sb, depth);
76: sb.append("<nested>" + getName() + "</nested>");
77: }
78:
79: public String toDefinition() {
80: return "#{" + getName() + "}";
81: }
82: }
|