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: JXTGStringTemplateParser.java 449189 2006-09-23 06:52:29Z crossley $
25: */
26: public class JXTGStringTemplateParser extends
27: AbstractStringTemplateParser {
28:
29: public final static String JXPATH = "jxpath";
30: public final static String JEXL = "jexl";
31: public final static String JAVASCRIPT = "js";
32:
33: /**
34: * @see org.apache.cocoon.template.expression.StringTemplateParser#parseSubstitutions(java.io.Reader)
35: */
36: public List parseSubstitutions(Reader in) throws Exception {
37: LinkedList substitutions = new LinkedList();
38: StringBuffer buf = new StringBuffer();
39: buf.setLength(0);
40: int ch;
41: boolean inExpr = false;
42: String lang = null;
43: top: while ((ch = in.read()) != -1) {
44: // column++;
45: char c = (char) ch;
46: processChar: while (true) {
47: if (inExpr) {
48: if (c == '\\') {
49: ch = in.read();
50: buf.append(ch == -1 ? '\\' : (char) ch);
51: } else if (c == '}') {
52: String str = buf.toString();
53: substitutions.add(compile(str, lang));
54: buf.setLength(0);
55: inExpr = false;
56: } else {
57: buf.append(c);
58: }
59: } else if (c == '$' || c == '#' || c == '@') {
60: ch = in.read();
61: if (ch == '{') {
62:
63: lang = (c == '#') ? JXPATH : ((c == '$') ? JEXL
64: : JAVASCRIPT);
65: inExpr = true;
66: if (buf.length() > 0) {
67: substitutions.add(new Literal(buf
68: .toString()));
69: buf.setLength(0);
70: }
71: continue top;
72: }
73: buf.append(c);
74: if (ch != -1) {
75: c = (char) ch;
76: continue processChar;
77: }
78: } else {
79: buf.append(c);
80: }
81: break;
82: }
83: }
84: if (inExpr)
85: throw new Exception("Unterminated {");
86:
87: if (buf.length() > 0)
88: substitutions.add(new Literal(buf.toString()));
89: return substitutions;
90: }
91:
92: }
|