1 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 ; 35 import org.xml.sax.Locator ; 36 import org.xml.sax.SAXException ; 37 import org.xml.sax.helpers.DefaultHandler ; 38 39 42 public class JspContentHandler extends DefaultHandler { 43 private JspBuilder _builder; 44 private Locator _locator; 45 46 public JspContentHandler(JspBuilder builder) 47 { 48 _builder = builder; 49 } 50 51 54 public void setDocumentLocator(Locator locator) 55 { 56 _locator = locator; 57 } 58 59 62 public void startDocument() 63 throws SAXException 64 { 65 try { 66 setLocation(); 67 _builder.startDocument(); 68 } catch (JspParseException e) { 69 throw new SAXException (e); 70 } 71 } 72 73 76 public void endDocument() 77 throws SAXException 78 { 79 try { 80 setLocation(); 81 _builder.endDocument(); 82 } catch (JspParseException e) { 83 throw new SAXException (e); 84 } 85 } 86 87 90 public void characters(char []buf, int offset, int length) 91 throws SAXException 92 { 93 try { 94 setLocation(); 95 96 if (_builder.getGenerator().isELIgnore()) { 97 String s = new String (buf, offset, length); 98 99 _builder.text(s); 100 } 101 else 102 addText(buf, offset, length); 103 } catch (JspParseException e) { 104 throw new SAXException (e); 105 } 106 } 107 108 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 (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 value = new String (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 (buf, begin, offset - begin)); 168 } 169 170 173 public void startPrefixMapping(String prefix, String uri) 174 throws SAXException 175 { 176 try { 177 _builder.startPrefixMapping(prefix, uri); 178 } catch (JspParseException e) { 179 throw new SAXException (e); 180 } 181 182 186 187 194 } 195 196 199 public void endPrefixMapping(String prefix) 200 throws SAXException 201 { 202 _builder.getParseState().popNamespace(prefix); 203 ParseTagManager manager = _builder.getTagManager(); 204 205 212 } 213 214 217 public void startElement(String uri, String localName, 218 String qName, Attributes atts) 219 throws SAXException 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 (e); 235 } 236 } 237 238 241 public void endElement (String uri, String localName, String qName) 242 throws SAXException 243 { 244 try { 245 setLocation(); 246 _builder.endElement(qName); 247 } catch (JspParseException e) { 248 throw new SAXException (e); 249 } 250 } 251 252 255 public void processingInstruction(String key, String value) 256 throws SAXException 257 { 258 try { 259 _builder.text("<?" + key + " " + value + "?>"); 260 } catch (JspParseException e) { 261 throw new SAXException (e); 262 } 263 } 264 265 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 |