1 /* 2 * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.org.apache.xalan.internal.xsltc.trax; 27 28 import java.util.Iterator; 29 30 import javax.xml.stream.XMLStreamException; 31 import javax.xml.stream.XMLStreamWriter; 32 import javax.xml.stream.XMLEventWriter; 33 34 import org.xml.sax.Attributes; 35 import org.xml.sax.SAXException; 36 import org.xml.sax.ext.Locator2; 37 38 /** 39 * @author Sunitha Reddy 40 */ 41 42 public class SAX2StAXStreamWriter extends SAX2StAXBaseWriter { 43 44 45 private XMLStreamWriter writer; 46 47 private boolean needToCallStartDocument = false; 48 49 public SAX2StAXStreamWriter() { 50 51 } 52 53 public SAX2StAXStreamWriter(XMLStreamWriter writer) { 54 55 this.writer = writer; 56 57 } 58 59 60 public XMLStreamWriter getStreamWriter() { 61 62 return writer; 63 64 } 65 66 67 public void setStreamWriter(XMLStreamWriter writer) { 68 69 this.writer = writer; 70 71 } 72 73 public void startDocument() throws SAXException { 74 75 super.startDocument(); 76 // Encoding and version info will be available only after startElement 77 // is called for first time. So, defer START_DOCUMENT event of StAX till 78 // that point of time. 79 needToCallStartDocument = true; 80 } 81 82 public void endDocument() throws SAXException { 83 84 try { 85 86 writer.writeEndDocument(); 87 88 } catch (XMLStreamException e) { 89 90 throw new SAXException(e); 91 92 } 93 94 super.endDocument(); 95 96 } 97 98 public void startElement(String uri, String localName, String qName, 99 Attributes attributes) throws SAXException { 100 101 if (needToCallStartDocument) { 102 try { 103 if (docLocator == null) 104 writer.writeStartDocument(); 105 else { 106 try{ 107 writer.writeStartDocument(((Locator2)docLocator).getXMLVersion()); 108 }catch(ClassCastException e){ 109 writer.writeStartDocument(); 110 } 111 } 112 113 } catch (XMLStreamException e) { 114 115 throw new SAXException(e); 116 117 } 118 needToCallStartDocument = false; 119 } 120 121 try { 122 123 String[] qname = {null, null}; 124 parseQName(qName, qname); 125 //Do not call writeStartElement with prefix and namespaceURI, as it writes out 126 //namespace declaration. 127 //writer.writeStartElement(qname[0], qname[1], uri); 128 writer.writeStartElement(qName); 129 130 131 // No need to write namespaces, as they are written as part of attributes. 132 /*if (namespaces != null) { 133 134 final int nDecls = namespaces.size(); 135 for (int i = 0; i < nDecls; i++) { 136 final String prefix = (String) namespaces.elementAt(i); 137 if (prefix.length() == 0) { 138 writer.setDefaultNamespace((String)namespaces.elementAt(++i)); 139 } else { 140 writer.setPrefix(prefix, (String) namespaces.elementAt(++i)); 141 } 142 143 writer.writeNamespace(prefix, (String)namespaces.elementAt(i)); 144 } 145 146 147 }*/ 148 149 // write attributes 150 for (int i = 0, s = attributes.getLength(); i < s; i++) { 151 152 parseQName(attributes.getQName(i), qname); 153 154 String attrPrefix = qname[0]; 155 String attrLocal = qname[1]; 156 157 String attrQName = attributes.getQName(i); 158 String attrValue = attributes.getValue(i); 159 String attrURI = attributes.getURI(i); 160 161 if ("xmlns".equals(attrPrefix) || "xmlns".equals(attrQName)) { 162 163 // namespace declaration disguised as an attribute. 164 // write it as an namespace 165 166 if (attrLocal.length() == 0) { 167 168 writer.setDefaultNamespace(attrValue); 169 170 } else { 171 172 writer.setPrefix(attrLocal, attrValue); 173 174 } 175 176 writer.writeNamespace(attrLocal, attrValue); 177 178 } else if (attrPrefix.length() > 0) { 179 180 writer.writeAttribute(attrPrefix, attrURI, attrLocal, 181 attrValue); 182 183 } else { 184 writer.writeAttribute(attrQName, attrValue); 185 } 186 187 } 188 189 } catch (XMLStreamException e) { 190 throw new SAXException(e); 191 192 } finally { 193 194 super.startElement(uri, localName, qName, attributes); 195 196 } 197 198 } 199 200 public void endElement(String uri, String localName, String qName) 201 throws SAXException { 202 203 try { 204 205 writer.writeEndElement(); 206 207 } catch (XMLStreamException e) { 208 209 throw new SAXException(e); 210 211 } finally { 212 213 super.endElement(uri, localName, qName); 214 215 } 216 217 } 218 219 public void comment(char[] ch, int start, int length) throws SAXException { 220 221 super.comment(ch, start, length); 222 try { 223 224 writer.writeComment(new String(ch, start, length)); 225 226 } catch (XMLStreamException e) { 227 228 throw new SAXException(e); 229 230 } 231 232 } 233 234 public void characters(char[] ch, int start, int length) 235 throws SAXException { 236 237 super.characters(ch, start, length); 238 try { 239 240 if (!isCDATA) { 241 242 writer.writeCharacters(ch, start, length); 243 244 } 245 246 } catch (XMLStreamException e) { 247 248 throw new SAXException(e); 249 250 } 251 252 } 253 254 public void endCDATA() throws SAXException { 255 256 try { 257 258 writer.writeCData(CDATABuffer.toString()); 259 260 } catch (XMLStreamException e) { 261 262 throw new SAXException(e); 263 264 } 265 266 super.endCDATA(); 267 268 } 269 270 public void ignorableWhitespace(char[] ch, int start, int length) 271 throws SAXException { 272 273 super.ignorableWhitespace(ch, start, length); 274 try { 275 276 writer.writeCharacters(ch, start, length); 277 278 } catch (XMLStreamException e) { 279 280 throw new SAXException(e); 281 282 } 283 284 } 285 286 public void processingInstruction(String target, String data) 287 throws SAXException { 288 289 super.processingInstruction(target, data); 290 try { 291 292 writer.writeProcessingInstruction(target, data); 293 294 } catch (XMLStreamException e) { 295 296 throw new SAXException(e); 297 298 } 299 300 } 301 302 }