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.util.ByteBuffer;
033: import com.caucho.vfs.Path;
034: import com.caucho.vfs.ReadStream;
035:
036: import java.io.IOException;
037: import java.util.ArrayList;
038: import java.util.HashMap;
039:
040: /**
041: * Serves server-side include files.
042: */
043: public class SSIParser {
044: private int _line;
045:
046: private final SSIFactory _factory;
047:
048: public SSIParser() {
049: _factory = new SSIFactory();
050: }
051:
052: public SSIParser(SSIFactory factory) {
053: _factory = factory;
054: }
055:
056: Statement parse(Path path) throws IOException {
057: ReadStream is = path.openRead();
058:
059: try {
060: ArrayList<Statement> statements = new ArrayList<Statement>();
061:
062: parse(is, statements);
063:
064: return new BlockStatement(statements);
065: } finally {
066: is.close();
067: }
068: }
069:
070: Statement parse(ReadStream is) throws IOException {
071: ArrayList<Statement> statements = new ArrayList<Statement>();
072:
073: parse(is, statements);
074:
075: return new BlockStatement(statements);
076: }
077:
078: /**
079: * Parses a list of statements from the ssi stream.
080: */
081: private void parse(ReadStream is, ArrayList<Statement> statements)
082: throws IOException {
083: ByteBuffer bb = new ByteBuffer();
084: int ch;
085:
086: while ((ch = is.read()) >= 0) {
087: if (ch != '<') {
088: if (ch == '\n')
089: _line++;
090:
091: bb.append(ch);
092: } else if ((ch = is.read()) != '!') {
093: bb.append('<');
094:
095: is.unread();
096: } else if ((ch = is.read()) != '-') {
097: bb.append('<');
098: bb.append('!');
099:
100: is.unread();
101: } else if ((ch = is.read()) != '-') {
102: bb.append('<');
103: bb.append('!');
104: bb.append('-');
105:
106: is.unread();
107: } else if ((ch = is.read()) != '#') {
108: bb.append('<');
109: bb.append('!');
110: bb.append('-');
111: bb.append('-');
112:
113: is.unread();
114: } else {
115: if (bb.getLength() > 0) {
116: TextStatement text;
117:
118: text = new TextStatement(bb.getBuffer(), 0, bb
119: .getLength());
120:
121: statements.add(text);
122: bb.clear();
123: }
124:
125: statements.add(parseCommand(is));
126: }
127: }
128:
129: if (bb.getLength() > 0) {
130: statements.add(new TextStatement(bb.getBuffer(), 0, bb
131: .getLength()));
132: bb.clear();
133: }
134: }
135:
136: private Statement parseCommand(ReadStream is) throws IOException {
137: StringBuilder sb = new StringBuilder();
138:
139: int ch;
140:
141: while (Character.isLetterOrDigit((ch = is.read()))) {
142: sb.append((char) ch);
143: }
144:
145: String cmd = sb.toString();
146:
147: HashMap<String, String> attr = parseAttributes(is);
148:
149: if ((ch = is.read()) != '-') {
150: } else if ((ch = is.read()) != '-') {
151: } else if ((ch = is.read()) != '>') {
152: }
153:
154: Statement statement = _factory.createStatement(cmd, attr, is
155: .getPath());
156:
157: if (statement == null)
158: statement = new ErrorStatement("['" + cmd
159: + "' is an unknown command.]");
160:
161: return statement;
162: }
163:
164: private HashMap<String, String> parseAttributes(ReadStream is)
165: throws IOException {
166: HashMap<String, String> attr = new HashMap<String, String>();
167:
168: while (true) {
169: int ch;
170:
171: while (Character.isWhitespace((ch = is.read()))) {
172: }
173:
174: StringBuilder key = new StringBuilder();
175:
176: for (; Character.isLetterOrDigit(ch); ch = is.read()) {
177: key.append((char) ch);
178: }
179:
180: for (; Character.isWhitespace(ch); ch = is.read()) {
181: }
182:
183: if (ch != '=')
184: return attr;
185:
186: for (ch = is.read(); Character.isWhitespace(ch); ch = is
187: .read()) {
188: }
189:
190: StringBuilder value = new StringBuilder();
191:
192: if (ch == '\'' || ch == '"') {
193: int end = ch;
194:
195: for (ch = is.read(); ch > 0 && ch != end; ch = is
196: .read()) {
197: if (ch == '\\') {
198: ch = is.read();
199:
200: if (ch == '\'' || ch == '\"')
201: value.append((char) ch);
202: else {
203: value.append('\\');
204: is.unread();
205: }
206: } else
207: value.append((char) ch);
208: }
209: } else {
210: for (; ch > 0 && !Character.isWhitespace(ch); ch = is
211: .read()) {
212: value.append((char) ch);
213: }
214: }
215:
216: attr.put(key.toString(), value.toString());
217: }
218: }
219: }
|