1 28 29 package com.caucho.xml; 30 31 import com.caucho.util.CharBuffer; 32 33 import org.xml.sax.ContentHandler ; 34 import org.xml.sax.Locator ; 35 import org.xml.sax.SAXException ; 36 import org.xml.sax.XMLReader ; 37 38 import java.io.IOException ; 39 40 43 public class SAXBuilder implements XMLWriter, Locator { 44 private XMLReader _saxReader; 45 private ContentHandler _contentHandler; 46 47 private QAttributes _attributes; 48 private CharBuffer _text; 49 private boolean _escapeText; 50 51 private boolean _hasElement; 52 53 private String _elementUri; 54 private String _elementLocalName; 55 private String _elementQName; 56 57 private String _filename; 58 private int _line; 59 60 private String _elementSystemId; 61 private int _elementLine; 62 63 private Locator _locator; 64 65 public SAXBuilder() 66 { 67 _text = new CharBuffer(); 68 _attributes = new QAttributes(); 69 } 70 71 public SAXBuilder(XMLReader saxReader) 72 { 73 this(); 74 75 init(saxReader); 76 } 77 78 public void init(XMLReader saxReader) 79 { 80 _saxReader = saxReader; 81 82 _contentHandler = saxReader.getContentHandler(); 83 84 init(); 85 } 86 87 public void init() 88 { 89 _text.clear(); 90 _attributes.clear(); 91 _hasElement = false; 92 93 _filename = null; 94 _line = 0; 95 } 96 97 100 public void setContentHandler(ContentHandler handler) 101 { 102 _contentHandler = handler; 103 } 104 105 public void setDocumentLocator(Locator locator) 106 { 107 _locator = locator; 108 } 109 110 public void startDocument() 111 throws IOException , SAXException 112 { 113 _contentHandler.setDocumentLocator(this); 114 _contentHandler.startDocument(); 115 } 116 117 public void endDocument() 118 throws IOException , SAXException 119 { 120 pop(); 121 122 _contentHandler.endDocument(); 123 } 124 125 public void setLocation(String filename, int line, int column) 126 { 127 _filename = filename; 128 _line = line; 129 } 130 131 134 public String getSystemId() 135 { 136 if (_elementSystemId != null) 137 return _elementSystemId; 138 else if (_locator != null) 139 return _locator.getSystemId(); 140 else 141 return _filename; 142 } 143 144 147 public String getPublicId() 148 { 149 if (_locator != null) 150 return _locator.getPublicId(); 151 else 152 return null; 153 } 154 155 158 public int getLineNumber() 159 { 160 if (_elementSystemId != null) 161 return _elementLine; 162 else if (_locator != null) 163 return _locator.getLineNumber(); 164 else 165 return 0; 166 } 167 168 171 public int getColumnNumber() 172 { 173 if (_locator != null) 174 return _locator.getColumnNumber(); 175 else 176 return 0; 177 } 178 179 186 public void startElement(String uri, String localName, String qName) 187 throws IOException , SAXException 188 { 189 pop(); 190 191 _elementUri = uri; 192 _elementLocalName = localName; 193 _elementQName = qName; 194 _hasElement = true; 195 196 if (_locator != null) { 197 _elementSystemId = _locator.getSystemId(); 198 _elementLine = _locator.getLineNumber(); 199 } 200 } 201 202 public void startPrefixMapping(String prefix, String uri) 203 throws IOException , SAXException 204 { 205 _contentHandler.startPrefixMapping(prefix, uri); 206 } 207 208 public void endPrefixMapping(String prefix) 209 throws IOException , SAXException 210 { 211 _contentHandler.endPrefixMapping(prefix); 212 } 213 214 public void attribute(String uri, String localName, String qName, 215 String value) 216 throws IOException , SAXException 217 { 218 QName name = new QName(qName, uri); 219 220 _attributes.add(name, value); 221 } 222 223 public void endElement(String uri, String localName, String qName) 224 throws IOException , SAXException 225 { 226 pop(); 227 228 if (uri != null) 229 _contentHandler.endElement(uri, localName, qName); 230 else 231 _contentHandler.endElement("", null, qName); 232 } 233 234 public void processingInstruction(String name, String data) 235 throws IOException , SAXException 236 { 237 pop(); 238 239 _contentHandler.processingInstruction(name, data); 240 } 241 242 public void comment(String data) 243 throws IOException , SAXException 244 { 245 pop(); 246 } 247 248 public boolean getEscapeText() 249 { 250 return _escapeText; 251 } 252 253 public void setEscapeText(boolean isEscaped) 254 { 255 _escapeText = isEscaped; 256 } 257 258 public void text(String text) 259 throws IOException , SAXException 260 { 261 popElement(); 262 263 _text.append(text); 264 } 265 266 public void text(char []buffer, int offset, int length) 267 throws IOException , SAXException 268 { 269 popElement(); 270 271 _text.append(buffer, offset, length); 272 } 273 274 public void cdata(String text) 275 throws IOException , SAXException 276 { 277 popElement(); 278 279 text(text); 280 } 281 282 public void cdata(char []buffer, int offset, int length) 283 throws IOException , SAXException 284 { 285 popElement(); 286 287 text(buffer, offset, length); 288 } 289 290 private void pop() 291 throws IOException , SAXException 292 { 293 popElement(); 294 295 if (_text.length() == 0) 296 return; 297 298 _contentHandler.characters(_text.getBuffer(), 0, _text.getLength()); 299 300 _text.clear(); 301 } 302 303 306 private void popElement() 307 throws IOException , SAXException 308 { 309 if (_hasElement) { 310 if (_elementUri == null) 311 _contentHandler.startElement("", null, _elementQName, _attributes); 312 else 313 _contentHandler.startElement(_elementUri, _elementLocalName, 314 _elementQName, _attributes); 315 316 _hasElement = false; 317 _elementSystemId = null; 318 _attributes.clear(); 319 } 320 } 321 } 322 | Popular Tags |