01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.template.expression;
18:
19: import java.io.Reader;
20: import java.util.LinkedList;
21: import java.util.List;
22:
23: /**
24: * @version $Id: DefaultStringTemplateParser.java 449189 2006-09-23 06:52:29Z crossley $
25: */
26: public class DefaultStringTemplateParser extends
27: AbstractStringTemplateParser {
28:
29: /**
30: * @see org.apache.cocoon.template.expression.StringTemplateParser#parseSubstitutions(java.io.Reader)
31: */
32: public List parseSubstitutions(Reader in) throws Exception {
33: LinkedList substitutions = new LinkedList();
34: StringBuffer buf = new StringBuffer();
35: buf.setLength(0);
36: int ch;
37: boolean inExpr = false;
38: top: while ((ch = in.read()) != -1) {
39: // column++;
40: char c = (char) ch;
41: processChar: while (true) {
42: if (inExpr) {
43: if (c == '\\') {
44: ch = in.read();
45: buf.append(ch == -1 ? '\\' : (char) ch);
46: } else if (c == '}') {
47: String str = buf.toString();
48: substitutions.add(compile(str));
49: buf.setLength(0);
50: inExpr = false;
51: } else {
52: buf.append(c);
53: }
54: } else if (c == '{') {
55: ch = in.read();
56: if (ch != '{') {
57: inExpr = true;
58: if (buf.length() > 0) {
59: substitutions.add(new Literal(buf
60: .toString()));
61: buf.setLength(0);
62: }
63: buf.append((char) ch);
64: continue top;
65: }
66: buf.append(c);
67: if (ch != -1) {
68: c = (char) ch;
69: continue processChar;
70: }
71: } else {
72: buf.append(c);
73: }
74: break;
75: }
76: }
77: if (inExpr)
78: throw new Exception("Unterminated {");
79:
80: if (buf.length() > 0)
81: substitutions.add(new Literal(buf.toString()));
82: return substitutions;
83: }
84:
85: }
|