1 38 39 40 package com.sun.xml.fastinfoset.stax; 41 42 import com.sun.xml.fastinfoset.QualifiedName; 43 import java.util.ArrayList ; 44 import javax.xml.stream.XMLStreamException; 45 import javax.xml.stream.XMLStreamWriter; 46 import org.xml.sax.Attributes ; 47 import org.xml.sax.Locator ; 48 import org.xml.sax.SAXException ; 49 import org.xml.sax.ext.LexicalHandler ; 50 import org.xml.sax.helpers.DefaultHandler ; 51 52 public class SAX2StAXWriter extends DefaultHandler implements LexicalHandler { 53 54 57 XMLStreamWriter _writer; 58 59 62 ArrayList _namespaces = new ArrayList (); 63 64 public SAX2StAXWriter(XMLStreamWriter writer) { 65 _writer = writer; 66 } 67 68 public XMLStreamWriter getWriter() { 69 return _writer; 70 } 71 72 public void startDocument() throws SAXException { 73 try { 74 _writer.writeStartDocument(); 75 } 76 catch (XMLStreamException e) { 77 throw new SAXException (e); 78 } 79 } 80 81 public void endDocument() throws SAXException { 82 try { 83 _writer.writeEndDocument(); 84 _writer.flush(); 85 } 86 catch (XMLStreamException e) { 87 throw new SAXException (e); 88 } 89 } 90 91 public void characters(char[] ch, int start, int length) 92 throws SAXException 93 { 94 try { 95 _writer.writeCharacters(ch, start, length); 96 } 97 catch (XMLStreamException e) { 98 throw new SAXException (e); 99 } 100 } 101 102 public void startElement(String namespaceURI, String localName, 103 String qName, Attributes atts) throws SAXException 104 { 105 try { 106 int k = qName.indexOf(':'); 107 String prefix = (k > 0) ? qName.substring(0, k) : ""; 108 _writer.writeStartElement(prefix, localName, namespaceURI); 109 110 int length = _namespaces.size(); 111 for (int i = 0; i < length; i++) { 112 QualifiedName nsh = (QualifiedName) _namespaces.get(i); 113 _writer.writeNamespace(nsh.prefix, nsh.namespaceName); 114 } 115 _namespaces.clear(); 116 117 length = atts.getLength(); 118 for (int i = 0; i < length; i++) { 119 _writer.writeAttribute(atts.getURI(i), 120 atts.getLocalName(i), 121 atts.getValue(i)); 122 } 123 } 124 catch (XMLStreamException e) { 125 throw new SAXException (e); 126 } 127 } 128 129 public void endElement(String namespaceURI, String localName, 130 String qName) throws SAXException 131 { 132 try { 133 _writer.writeEndElement(); 134 } 135 catch (XMLStreamException e) { 136 e.printStackTrace(); 137 throw new SAXException (e); 138 } 139 } 140 141 public void startPrefixMapping(String prefix, String uri) 142 throws SAXException 143 { 144 try { 145 _writer.setPrefix(prefix, uri); 146 _namespaces.add(new QualifiedName(prefix, uri)); 147 } 148 catch (XMLStreamException e) { 149 throw new SAXException (e); 150 } 151 } 152 153 public void endPrefixMapping(String prefix) throws SAXException { 154 } 155 156 public void ignorableWhitespace(char[] ch, int start, int length) 157 throws SAXException 158 { 159 characters(ch, start, length); 160 } 161 162 public void processingInstruction(String target, String data) 163 throws SAXException 164 { 165 try { 166 _writer.writeProcessingInstruction(target, data); 167 } 168 catch (XMLStreamException e) { 169 throw new SAXException (e); 170 } 171 } 172 173 public void setDocumentLocator(Locator locator) { 174 } 175 176 public void skippedEntity(String name) throws SAXException { 177 } 178 179 public void comment(char[] ch, int start, int length) 180 throws SAXException 181 { 182 try { 183 _writer.writeComment(new String (ch, start, length)); 184 } 185 catch (XMLStreamException e) { 186 throw new SAXException (e); 187 } 188 } 189 190 public void endCDATA() throws SAXException { 191 } 192 193 public void endDTD() throws SAXException { 194 } 195 196 public void endEntity(String name) throws SAXException { 197 } 198 199 public void startCDATA() throws SAXException { 200 } 201 202 public void startDTD(String name, String publicId, String systemId) throws SAXException { 203 } 204 205 public void startEntity(String name) throws SAXException { 206 } 207 208 } 209 210 | Popular Tags |