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