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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jsp;
030:
031: import com.caucho.vfs.Vfs;
032: import com.caucho.xml.QName;
033:
034: import org.xml.sax.Attributes;
035: import org.xml.sax.Locator;
036: import org.xml.sax.SAXException;
037: import org.xml.sax.helpers.DefaultHandler;
038:
039: /**
040: * Generates the nodes for JSP code.
041: */
042: public class JspContentHandler extends DefaultHandler {
043: private JspBuilder _builder;
044: private Locator _locator;
045:
046: public JspContentHandler(JspBuilder builder) {
047: _builder = builder;
048: }
049:
050: /**
051: * Sets the document locator
052: */
053: public void setDocumentLocator(Locator locator) {
054: _locator = locator;
055: }
056:
057: /**
058: * Starts the document.
059: */
060: public void startDocument() throws SAXException {
061: try {
062: setLocation();
063: _builder.startDocument();
064: } catch (JspParseException e) {
065: throw new SAXException(e);
066: }
067: }
068:
069: /**
070: * Ends the document.
071: */
072: public void endDocument() throws SAXException {
073: try {
074: setLocation();
075: _builder.endDocument();
076: } catch (JspParseException e) {
077: throw new SAXException(e);
078: }
079: }
080:
081: /**
082: * Adds characters.
083: */
084: public void characters(char[] buf, int offset, int length)
085: throws SAXException {
086: try {
087: setLocation();
088:
089: if (_builder.getGenerator().isELIgnore()) {
090: String s = new String(buf, offset, length);
091:
092: _builder.text(s);
093: } else
094: addText(buf, offset, length);
095: } catch (JspParseException e) {
096: throw new SAXException(e);
097: }
098: }
099:
100: /**
101: * Adds text, checking for JSP-EL
102: */
103: private void addText(char[] buf, int offset, int length)
104: throws JspParseException {
105: int end = offset + length;
106: int begin = offset;
107:
108: while (offset < end) {
109: if (buf[offset] != '$')
110: offset++;
111: else if (end <= offset + 1)
112: offset++;
113: else if (buf[offset + 1] != '{')
114: offset++;
115: else {
116: if (begin < offset)
117: _builder
118: .text(new String(buf, begin, offset - begin));
119:
120: begin = offset;
121: offset += 2;
122: while (offset < end && buf[offset] != '}') {
123: if (buf[offset] == '\'') {
124: for (offset++; offset < end
125: && buf[offset] != '\''; offset++) {
126: }
127:
128: if (offset < end)
129: offset++;
130: } else if (buf[offset] == '"') {
131: for (offset++; offset < end
132: && buf[offset] != '"'; offset++) {
133: }
134:
135: if (offset < end)
136: offset++;
137: } else
138: offset++;
139: }
140:
141: if (offset < end)
142: offset++;
143:
144: String value = new String(buf, begin, offset - begin);
145:
146: QName qname = new QName("resin-c", "out",
147: JspParser.JSTL_CORE_URI);
148:
149: _builder.startElement(qname);
150: _builder.attribute(new QName("value"), value);
151: _builder.attribute(new QName("escapeXml"), "false");
152: _builder.endAttributes();
153: _builder.endElement("resin-c:out");
154:
155: begin = offset;
156: }
157: }
158:
159: if (begin < offset)
160: _builder.text(new String(buf, begin, offset - begin));
161: }
162:
163: /**
164: * Starts a prefix mapping.
165: */
166: public void startPrefixMapping(String prefix, String uri)
167: throws SAXException {
168: try {
169: _builder.startPrefixMapping(prefix, uri);
170: } catch (JspParseException e) {
171: throw new SAXException(e);
172: }
173:
174: /*
175: _builder.getParseState().pushNamespace(prefix, uri);
176: ParseTagManager manager = _builder.getTagManager();
177: */
178:
179: /*
180: try {
181: manager.addTaglib(prefix, uri);
182: } catch (JspParseException e) {
183: throw new SAXException(e);
184: }
185: */
186: }
187:
188: /**
189: * Ends a prefix mapping.
190: */
191: public void endPrefixMapping(String prefix) throws SAXException {
192: _builder.getParseState().popNamespace(prefix);
193: ParseTagManager manager = _builder.getTagManager();
194:
195: /*
196: try {
197: manager.addTaglib(prefix, uri);
198: } catch (JspParseException e) {
199: throw new SAXException(e);
200: }
201: */
202: }
203:
204: /**
205: * Starts an element.
206: */
207: public void startElement(String uri, String localName,
208: String qName, Attributes atts) throws SAXException {
209: try {
210: setLocation();
211: _builder.startElement(new QName(qName, uri));
212:
213: for (int i = 0; i < atts.getLength(); i++) {
214: setLocation();
215: _builder.attribute(new QName(atts.getQName(i), atts
216: .getURI(i)), atts.getValue(i));
217: }
218:
219: setLocation();
220: _builder.endAttributes();
221: } catch (JspParseException e) {
222: throw new SAXException(e);
223: }
224: }
225:
226: /**
227: * Ends an element.
228: */
229: public void endElement(String uri, String localName, String qName)
230: throws SAXException {
231: try {
232: setLocation();
233: _builder.endElement(qName);
234: } catch (JspParseException e) {
235: throw new SAXException(e);
236: }
237: }
238:
239: /**
240: * Adds a processing instruction
241: */
242: public void processingInstruction(String key, String value)
243: throws SAXException {
244: try {
245: _builder.text("<?" + key + " " + value + "?>");
246: } catch (JspParseException e) {
247: throw new SAXException(e);
248: }
249: }
250:
251: /**
252: * Sets the location.
253: */
254: private void setLocation() {
255: if (_locator != null) {
256: _builder.setLocation(Vfs.lookup(_locator.getSystemId()),
257: _locator.getSystemId(), _locator.getLineNumber());
258: }
259: }
260: }
|