KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > servlets > ssi > ExprParser


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.servlets.ssi;
31
32 import com.caucho.vfs.Path;
33
34 /**
35  * Parses an SSI expression
36  */

37 public class ExprParser {
38   private String JavaDoc _expr;
39   private int _index;
40   private Path _path;
41
42   private StringBuilder JavaDoc _sb = new StringBuilder JavaDoc();
43
44   private ExprParser(String JavaDoc expr, Path path)
45   {
46     _expr = expr;
47     _path = path;
48   }
49   
50   /**
51    * parse a string.
52    */

53   public static SSIExpr parseString(String JavaDoc expr, Path path)
54   {
55     return new ExprParser(expr, path).parseString();
56   }
57
58   private SSIExpr parseString()
59   {
60     int ch;
61
62     SSIExpr expr = null;
63     
64     while ((ch = read()) >= 0) {
65       if (ch == '$') {
66     if (_sb.length() > 0)
67       expr = ConcatExpr.create(expr, new StringExpr(_sb.toString()));
68     _sb.setLength(0);
69
70     ch = read();
71     if (ch == '{') {
72       for (ch = read(); ch >= 0 && ch != '}'; ch = read()) {
73         _sb.append((char) ch);
74       }
75     }
76     else {
77       for (;
78            'a' <= ch && ch <= 'z' ||
79          'A' <= ch && ch <= 'Z' ||
80          '0' <= ch && ch <= '9' ||
81          ch == '_';
82            ch = read()) {
83         _sb.append((char) ch);
84       }
85
86       unread();
87     }
88
89     expr = ConcatExpr.create(expr, new VarExpr(_sb.toString(), _path));
90     _sb.setLength(0);
91       }
92       else if (ch == '\\') {
93     ch = read();
94
95     if (ch == '$')
96       _sb.append((char) ch);
97     else if (ch == '\\')
98       _sb.append((char) ch);
99     else {
100       _sb.append('\\');
101       unread();
102     }
103       }
104       else
105     _sb.append((char) ch);
106     }
107
108     if (_sb.length() > 0)
109       expr = ConcatExpr.create(expr, new StringExpr(_sb.toString()));
110
111     return expr;
112   }
113
114   private int read()
115   {
116     if (_index < _expr.length())
117       return _expr.charAt(_index++);
118     else {
119       _index++;
120       return -1;
121     }
122   }
123
124   private void unread()
125   {
126     _index--;
127   }
128 }
129
Popular Tags