KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jsp > JspContentHandler


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jsp;
30
31 import com.caucho.vfs.Vfs;
32 import com.caucho.xml.QName;
33
34 import org.xml.sax.Attributes JavaDoc;
35 import org.xml.sax.Locator JavaDoc;
36 import org.xml.sax.SAXException JavaDoc;
37 import org.xml.sax.helpers.DefaultHandler JavaDoc;
38
39 /**
40  * Generates the nodes for JSP code.
41  */

42 public class JspContentHandler extends DefaultHandler JavaDoc {
43   private JspBuilder _builder;
44   private Locator JavaDoc _locator;
45
46   public JspContentHandler(JspBuilder builder)
47   {
48     _builder = builder;
49   }
50
51   /**
52    * Sets the document locator
53    */

54   public void setDocumentLocator(Locator JavaDoc locator)
55   {
56     _locator = locator;
57   }
58
59   /**
60    * Starts the document.
61    */

62   public void startDocument()
63     throws SAXException JavaDoc
64   {
65     try {
66       setLocation();
67       _builder.startDocument();
68     } catch (JspParseException e) {
69       throw new SAXException JavaDoc(e);
70     }
71   }
72
73   /**
74    * Ends the document.
75    */

76   public void endDocument()
77     throws SAXException JavaDoc
78   {
79     try {
80       setLocation();
81       _builder.endDocument();
82     } catch (JspParseException e) {
83       throw new SAXException JavaDoc(e);
84     }
85   }
86
87   /**
88    * Adds characters.
89    */

90   public void characters(char []buf, int offset, int length)
91     throws SAXException JavaDoc
92   {
93     try {
94       setLocation();
95
96       if (_builder.getGenerator().isELIgnore()) {
97     String JavaDoc s = new String JavaDoc(buf, offset, length);
98       
99     _builder.text(s);
100       }
101       else
102     addText(buf, offset, length);
103     } catch (JspParseException e) {
104       throw new SAXException JavaDoc(e);
105     }
106   }
107
108   /**
109    * Adds text, checking for JSP-EL
110    */

111   private void addText(char []buf, int offset, int length)
112     throws JspParseException
113   {
114     int end = offset + length;
115     int begin = offset;
116
117     while (offset < end) {
118       if (buf[offset] != '$')
119     offset++;
120       else if (end <= offset + 1)
121     offset++;
122       else if (buf[offset + 1] != '{')
123     offset++;
124       else {
125     if (begin < offset)
126       _builder.text(new String JavaDoc(buf, begin, offset - begin));
127
128     begin = offset;
129     offset += 2;
130     while (offset < end && buf[offset] != '}') {
131       if (buf[offset] == '\'') {
132         for (offset++; offset < end && buf[offset] != '\''; offset++) {
133         }
134
135         if (offset < end)
136           offset++;
137       }
138       else if (buf[offset] == '"') {
139         for (offset++; offset < end && buf[offset] != '"'; offset++) {
140         }
141
142         if (offset < end)
143           offset++;
144       }
145       else
146         offset++;
147     }
148
149     if (offset < end)
150       offset++;
151     
152     String JavaDoc value = new String JavaDoc(buf, begin, offset - begin);
153
154     QName qname = new QName("resin-c", "out", JspParser.JSTL_CORE_URI);
155
156     _builder.startElement(qname);
157     _builder.attribute(new QName("value"), value);
158     _builder.attribute(new QName("escapeXml"), "false");
159     _builder.endAttributes();
160     _builder.endElement("resin-c:out");
161
162     begin = offset;
163       }
164     }
165
166     if (begin < offset)
167       _builder.text(new String JavaDoc(buf, begin, offset - begin));
168   }
169
170   /**
171    * Starts a prefix mapping.
172    */

173   public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
174     throws SAXException JavaDoc
175   {
176     try {
177       _builder.startPrefixMapping(prefix, uri);
178     } catch (JspParseException e) {
179       throw new SAXException JavaDoc(e);
180     }
181     
182     /*
183     _builder.getParseState().pushNamespace(prefix, uri);
184     ParseTagManager manager = _builder.getTagManager();
185     */

186
187     /*
188     try {
189       manager.addTaglib(prefix, uri);
190     } catch (JspParseException e) {
191       throw new SAXException(e);
192     }
193     */

194   }
195
196   /**
197    * Ends a prefix mapping.
198    */

199   public void endPrefixMapping(String JavaDoc prefix)
200     throws SAXException JavaDoc
201   {
202     _builder.getParseState().popNamespace(prefix);
203     ParseTagManager manager = _builder.getTagManager();
204
205     /*
206     try {
207       manager.addTaglib(prefix, uri);
208     } catch (JspParseException e) {
209       throw new SAXException(e);
210     }
211     */

212   }
213
214   /**
215    * Starts an element.
216    */

217   public void startElement(String JavaDoc uri, String JavaDoc localName,
218                String JavaDoc qName, Attributes JavaDoc atts)
219     throws SAXException JavaDoc
220   {
221     try {
222       setLocation();
223       _builder.startElement(new QName(qName, uri));
224
225       for (int i = 0; i < atts.getLength(); i++) {
226     setLocation();
227     _builder.attribute(new QName(atts.getQName(i), atts.getURI(i)),
228                atts.getValue(i));
229       }
230       
231       setLocation();
232       _builder.endAttributes();
233     } catch (JspParseException e) {
234       throw new SAXException JavaDoc(e);
235     }
236   }
237
238   /**
239    * Ends an element.
240    */

241   public void endElement (String JavaDoc uri, String JavaDoc localName, String JavaDoc qName)
242     throws SAXException JavaDoc
243   {
244     try {
245       setLocation();
246       _builder.endElement(qName);
247     } catch (JspParseException e) {
248       throw new SAXException JavaDoc(e);
249     }
250   }
251
252   /**
253    * Adds a processing instruction
254    */

255   public void processingInstruction(String JavaDoc key, String JavaDoc value)
256     throws SAXException JavaDoc
257   {
258     try {
259       _builder.text("<?" + key + " " + value + "?>");
260     } catch (JspParseException e) {
261       throw new SAXException JavaDoc(e);
262     }
263   }
264
265   /**
266    * Sets the location.
267    */

268   private void setLocation()
269   {
270     if (_locator != null) {
271       _builder.setLocation(Vfs.lookup(_locator.getSystemId()),
272                _locator.getSystemId(),
273                _locator.getLineNumber());
274     }
275   }
276 }
277
Popular Tags