1 20 21 27 28 package com.sun.org.apache.xalan.internal.xsltc.trax; 29 30 import java.util.Iterator ; 31 32 import javax.xml.stream.XMLStreamException; 33 import javax.xml.stream.XMLStreamWriter; 34 import javax.xml.stream.XMLEventWriter; 35 36 import org.xml.sax.Attributes ; 37 import org.xml.sax.SAXException ; 38 import org.xml.sax.ext.Locator2 ; 39 40 43 44 public class SAX2StAXStreamWriter extends SAX2StAXBaseWriter { 45 46 47 private XMLStreamWriter writer; 48 49 private boolean needToCallStartDocument = false; 50 51 public SAX2StAXStreamWriter() { 52 53 } 54 55 public SAX2StAXStreamWriter(XMLStreamWriter writer) { 56 57 this.writer = writer; 58 59 } 60 61 62 public XMLStreamWriter getStreamWriter() { 63 64 return writer; 65 66 } 67 68 69 public void setStreamWriter(XMLStreamWriter writer) { 70 71 this.writer = writer; 72 73 } 74 75 public void startDocument() throws SAXException { 76 77 super.startDocument(); 78 needToCallStartDocument = true; 82 } 83 84 public void endDocument() throws SAXException { 85 86 try { 87 88 writer.writeEndDocument(); 89 90 } catch (XMLStreamException e) { 91 92 throw new SAXException (e); 93 94 } 95 96 super.endDocument(); 97 98 } 99 100 public void startElement(String uri, String localName, String qName, 101 Attributes attributes) throws SAXException { 102 103 if (needToCallStartDocument) { 104 try { 105 if (docLocator == null) 106 writer.writeStartDocument(); 107 else { 108 try{ 109 writer.writeStartDocument(((Locator2 )docLocator).getXMLVersion()); 110 }catch(ClassCastException e){ 111 writer.writeStartDocument(); 112 } 113 } 114 115 } catch (XMLStreamException e) { 116 117 throw new SAXException (e); 118 119 } 120 needToCallStartDocument = false; 121 } 122 123 try { 124 125 String [] qname = {null, null}; 126 parseQName(qName, qname); 127 writer.writeStartElement(qName); 131 132 133 150 151 for (int i = 0, s = attributes.getLength(); i < s; i++) { 153 154 parseQName(attributes.getQName(i), qname); 155 156 String attrPrefix = qname[0]; 157 String attrLocal = qname[1]; 158 159 String attrQName = attributes.getQName(i); 160 String attrValue = attributes.getValue(i); 161 String attrURI = attributes.getURI(i); 162 163 if ("xmlns".equals(attrPrefix) || "xmlns".equals(attrQName)) { 164 165 168 if (attrLocal.length() == 0) { 169 170 writer.setDefaultNamespace(attrValue); 171 172 } else { 173 174 writer.setPrefix(attrLocal, attrValue); 175 176 } 177 178 writer.writeNamespace(attrLocal, attrValue); 179 180 } else if (attrPrefix.length() > 0) { 181 182 writer.writeAttribute(attrPrefix, attrURI, attrLocal, 183 attrValue); 184 185 } else { 186 writer.writeAttribute(attrQName, attrValue); 187 } 188 189 } 190 191 } catch (XMLStreamException e) { 192 throw new SAXException (e); 193 194 } finally { 195 196 super.startElement(uri, localName, qName, attributes); 197 198 } 199 200 } 201 202 public void endElement(String uri, String localName, String qName) 203 throws SAXException { 204 205 try { 206 207 writer.writeEndElement(); 208 209 } catch (XMLStreamException e) { 210 211 throw new SAXException (e); 212 213 } finally { 214 215 super.endElement(uri, localName, qName); 216 217 } 218 219 } 220 221 public void comment(char[] ch, int start, int length) throws SAXException { 222 223 super.comment(ch, start, length); 224 try { 225 226 writer.writeComment(new String (ch, start, length)); 227 228 } catch (XMLStreamException e) { 229 230 throw new SAXException (e); 231 232 } 233 234 } 235 236 public void characters(char[] ch, int start, int length) 237 throws SAXException { 238 239 super.characters(ch, start, length); 240 try { 241 242 if (!isCDATA) { 243 244 writer.writeCharacters(ch, start, length); 245 246 } 247 248 } catch (XMLStreamException e) { 249 250 throw new SAXException (e); 251 252 } 253 254 } 255 256 public void endCDATA() throws SAXException { 257 258 try { 259 260 writer.writeCData(CDATABuffer.toString()); 261 262 } catch (XMLStreamException e) { 263 264 throw new SAXException (e); 265 266 } 267 268 super.endCDATA(); 269 270 } 271 272 public void ignorableWhitespace(char[] ch, int start, int length) 273 throws SAXException { 274 275 super.ignorableWhitespace(ch, start, length); 276 try { 277 278 writer.writeCharacters(ch, start, length); 279 280 } catch (XMLStreamException e) { 281 282 throw new SAXException (e); 283 284 } 285 286 } 287 288 public void processingInstruction(String target, String data) 289 throws SAXException { 290 291 super.processingInstruction(target, data); 292 try { 293 294 writer.writeProcessingInstruction(target, data); 295 296 } catch (XMLStreamException e) { 297 298 throw new SAXException (e); 299 300 } 301 302 } 303 304 } 305 | Popular Tags |