001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.servlets.ssi;
031:
032: import com.caucho.vfs.Path;
033:
034: /**
035: * Parses an SSI expression
036: */
037: public class ExprParser {
038: private String _expr;
039: private int _index;
040: private Path _path;
041:
042: private StringBuilder _sb = new StringBuilder();
043:
044: private ExprParser(String expr, Path path) {
045: _expr = expr;
046: _path = path;
047: }
048:
049: /**
050: * parse a string.
051: */
052: public static SSIExpr parseString(String expr, Path path) {
053: return new ExprParser(expr, path).parseString();
054: }
055:
056: private SSIExpr parseString() {
057: int ch;
058:
059: SSIExpr expr = null;
060:
061: while ((ch = read()) >= 0) {
062: if (ch == '$') {
063: if (_sb.length() > 0)
064: expr = ConcatExpr.create(expr, new StringExpr(_sb
065: .toString()));
066: _sb.setLength(0);
067:
068: ch = read();
069: if (ch == '{') {
070: for (ch = read(); ch >= 0 && ch != '}'; ch = read()) {
071: _sb.append((char) ch);
072: }
073: } else {
074: for (; 'a' <= ch && ch <= 'z' || 'A' <= ch
075: && ch <= 'Z' || '0' <= ch && ch <= '9'
076: || ch == '_'; ch = read()) {
077: _sb.append((char) ch);
078: }
079:
080: unread();
081: }
082:
083: expr = ConcatExpr.create(expr, new VarExpr(_sb
084: .toString(), _path));
085: _sb.setLength(0);
086: } else if (ch == '\\') {
087: ch = read();
088:
089: if (ch == '$')
090: _sb.append((char) ch);
091: else if (ch == '\\')
092: _sb.append((char) ch);
093: else {
094: _sb.append('\\');
095: unread();
096: }
097: } else
098: _sb.append((char) ch);
099: }
100:
101: if (_sb.length() > 0)
102: expr = ConcatExpr.create(expr, new StringExpr(_sb
103: .toString()));
104:
105: return expr;
106: }
107:
108: private int read() {
109: if (_index < _expr.length())
110: return _expr.charAt(_index++);
111: else {
112: _index++;
113: return -1;
114: }
115: }
116:
117: private void unread() {
118: _index--;
119: }
120: }
|