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