1 20 21 27 28 package com.sun.org.apache.xalan.internal.xsltc.trax; 29 30 import java.util.ArrayList ; 31 import java.util.Collection ; 32 import java.util.Collections ; 33 import java.util.HashMap ; 34 import java.util.Iterator ; 35 import java.util.List ; 36 import java.util.Map ; 37 38 import javax.xml.stream.XMLEventFactory; 39 import javax.xml.stream.XMLStreamException; 40 import javax.xml.stream.events.*; 41 import javax.xml.stream.XMLEventWriter; 42 43 import org.xml.sax.Attributes ; 44 import org.xml.sax.SAXException ; 45 import org.xml.sax.ext.Locator2 ; 46 47 50 public class SAX2StAXEventWriter extends SAX2StAXBaseWriter { 51 52 53 private XMLEventWriter writer; 54 55 56 private XMLEventFactory eventFactory; 57 58 59 private List namespaceStack = new ArrayList (); 60 61 62 private boolean needToCallStartDocument = false; 63 64 65 public SAX2StAXEventWriter() { 66 67 eventFactory = XMLEventFactory.newInstance(); 68 69 } 70 71 72 public SAX2StAXEventWriter(XMLEventWriter writer) { 73 74 this.writer = writer; 75 eventFactory = XMLEventFactory.newInstance(); 76 77 } 78 79 public SAX2StAXEventWriter(XMLEventWriter writer, 80 XMLEventFactory factory) { 81 82 this.writer = writer; 83 if (factory != null) { 84 85 this.eventFactory = factory; 86 87 } else { 88 89 eventFactory = XMLEventFactory.newInstance(); 90 91 } 92 93 } 94 95 public XMLEventWriter getEventWriter() { 96 97 return writer; 98 99 } 100 101 102 public void setEventWriter(XMLEventWriter writer) { 103 104 this.writer = writer; 105 106 } 107 108 109 public XMLEventFactory getEventFactory() { 110 111 return eventFactory; 112 113 } 114 115 116 public void setEventFactory(XMLEventFactory factory) { 117 118 this.eventFactory = factory; 119 120 } 121 122 public void startDocument() throws SAXException { 123 124 super.startDocument(); 125 126 namespaceStack.clear(); 127 128 eventFactory.setLocation(getCurrentLocation()); 129 130 needToCallStartDocument = true; 134 135 } 136 137 public void endDocument() throws SAXException { 138 139 eventFactory.setLocation(getCurrentLocation()); 140 141 try { 142 143 writer.add(eventFactory.createEndDocument()); 144 145 } catch (XMLStreamException e) { 146 147 throw new SAXException (e); 148 149 } 150 151 super.endDocument(); 152 153 namespaceStack.clear(); 155 156 } 157 158 public void startElement(String uri, String localName, String qName, 159 Attributes attributes) throws SAXException { 160 161 if (needToCallStartDocument){ 162 try { 163 164 if (docLocator == null) 165 writer.add(eventFactory.createStartDocument()); 166 else { 167 try{ 168 writer.add(eventFactory.createStartDocument(((Locator2 )docLocator).getEncoding(),((Locator2 )docLocator).getXMLVersion())); 169 }catch(ClassCastException e){ 170 writer.add(eventFactory.createStartDocument()); 171 } 172 } 173 174 } catch (XMLStreamException e) { 175 176 throw new SAXException (e); 177 178 } 179 needToCallStartDocument = false; 180 } 181 182 eventFactory.setLocation(getCurrentLocation()); 184 185 Collection [] events = {null, null}; 187 createStartEvents(attributes, events); 188 189 190 namespaceStack.add(events[0]); 191 192 try { 193 194 String [] qname = {null, null}; 195 parseQName(qName, qname); 196 197 writer.add(eventFactory.createStartElement(qname[0], uri, 198 qname[1], events[1].iterator(), events[0].iterator())); 199 200 } catch (XMLStreamException e) { 201 202 throw new SAXException (e); 203 204 } finally { 205 206 super.startElement(uri, localName, qName, attributes); 207 208 } 209 210 } 211 212 public void endElement(String uri, String localName, String qName) 213 throws SAXException { 214 215 super.endElement(uri, localName, qName); 216 217 eventFactory.setLocation(getCurrentLocation()); 218 219 String [] qname = {null, null}; 221 parseQName(qName, qname); 222 223 Collection nsList = (Collection ) namespaceStack.remove(namespaceStack.size() - 1); 225 Iterator nsIter = nsList.iterator(); 226 227 try { 228 229 writer.add(eventFactory.createEndElement(qname[0], uri, qname[1], 230 nsIter)); 231 232 } catch (XMLStreamException e) { 233 234 throw new SAXException (e); 235 236 } 237 238 } 239 240 public void comment(char[] ch, int start, int length) throws SAXException { 241 242 super.comment(ch, start, length); 243 244 eventFactory.setLocation(getCurrentLocation()); 245 try { 246 247 writer.add(eventFactory.createComment(new String (ch, start, 248 length))); 249 250 } catch (XMLStreamException e) { 251 252 throw new SAXException (e); 253 254 } 255 256 } 257 258 public void characters(char[] ch, int start, int length) 259 throws SAXException { 260 261 super.characters(ch, start, length); 262 263 try { 264 265 if (!isCDATA) { 266 267 eventFactory.setLocation(getCurrentLocation()); 268 writer.add(eventFactory.createCharacters(new String (ch, 269 start, length))); 270 271 } 272 273 } catch (XMLStreamException e) { 274 275 throw new SAXException (e); 276 277 } 278 279 } 280 281 public void ignorableWhitespace(char[] ch, int start, int length) 282 throws SAXException { 283 284 super.ignorableWhitespace(ch, start, length); 285 characters(ch, start, length); 286 287 } 288 289 public void processingInstruction(String target, String data) 290 throws SAXException { 291 292 super.processingInstruction(target, data); 293 try { 294 295 writer.add(eventFactory.createProcessingInstruction(target, data)); 296 297 } catch (XMLStreamException e) { 298 299 throw new SAXException (e); 300 301 } 302 303 } 304 305 public void endCDATA() throws SAXException { 306 307 eventFactory.setLocation(getCurrentLocation()); 308 try { 309 310 writer.add(eventFactory.createCData(CDATABuffer.toString())); 311 312 } catch (XMLStreamException e) { 313 314 throw new SAXException (e); 315 316 } 317 318 super.endCDATA(); 319 320 } 321 322 323 protected void createStartEvents(Attributes attributes, Collection [] events) { 324 325 Map nsMap = null; 326 List attrs = null; 327 328 if (namespaces != null) { 330 331 final int nDecls = namespaces.size(); 332 for (int i = 0; i < nDecls; i++) { 333 final String prefix = (String ) namespaces.elementAt(i); 334 String uri = (String ) namespaces.elementAt(i++); 335 Namespace ns = createNamespace(prefix, uri); 336 if (nsMap == null) { 337 338 nsMap = new HashMap (); 339 340 } 341 nsMap.put(prefix, ns); 342 343 344 } 345 346 } 347 348 String [] qname = {null, null}; 350 for (int i = 0, s = attributes.getLength(); i < s; i++) { 351 352 parseQName(attributes.getQName(i), qname); 353 354 String attrPrefix = qname[0]; 355 String attrLocal = qname[1]; 356 357 String attrQName = attributes.getQName(i); 358 String attrValue = attributes.getValue(i); 359 String attrURI = attributes.getURI(i); 360 361 if ("xmlns".equals(attrQName) || "xmlns".equals(attrPrefix)) { 362 363 367 if (!nsMap.containsKey(attrPrefix)) { 368 369 Namespace ns = createNamespace(attrPrefix, attrValue); 370 if (nsMap == null) { 371 372 nsMap = new HashMap (); 373 374 } 375 nsMap.put(attrPrefix, ns); 376 377 } 378 379 } else { 380 381 Attribute attribute; 382 if (attrPrefix.length() > 0) { 383 384 attribute = eventFactory.createAttribute(attrPrefix, 385 attrURI, attrLocal, attrValue); 386 387 } else { 388 389 attribute = eventFactory.createAttribute(attrLocal, 390 attrValue); 391 392 } 393 394 if (attrs == null) { 395 396 attrs = new ArrayList (); 397 398 } 399 attrs.add(attribute); 400 401 } 402 403 } 404 405 events[0] = (nsMap == null ? Collections.EMPTY_LIST : nsMap.values()); 406 events[1] = (attrs == null ? Collections.EMPTY_LIST : attrs); 407 408 } 409 410 protected Namespace createNamespace(String prefix, String uri) { 411 412 if (prefix == null || prefix.length() == 0) { 413 414 return eventFactory.createNamespace(uri); 415 416 } else { 417 418 return eventFactory.createNamespace(prefix, uri); 419 420 } 421 422 } 423 424 } 425 | Popular Tags |