1 16 package org.apache.axis2.om.impl.llom.serialize; 17 18 import org.apache.axis2.om.OMOutput; 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.xml.sax.Attributes ; 22 import org.xml.sax.ContentHandler ; 23 import org.xml.sax.Locator ; 24 import org.xml.sax.SAXException ; 25 26 import javax.xml.stream.XMLStreamException; 27 import javax.xml.stream.XMLStreamWriter; 28 29 32 public class StreamWriterToContentHandlerConverter implements ContentHandler { 33 36 private Log log = LogFactory.getLog(getClass()); 37 38 41 private XMLStreamWriter writer; 42 43 48 public StreamWriterToContentHandlerConverter(OMOutput omOutput) { 49 this.writer = omOutput.getXmlStreamWriter(); 50 } 51 52 57 public void endDocument() throws SAXException { 58 59 } 61 62 67 public void startDocument() throws SAXException { 68 69 } 71 72 80 public void characters(char ch[], int start, int length) 81 throws SAXException { 82 try { 83 writer.writeCharacters(ch, start, length); 84 } catch (XMLStreamException e) { 85 throw new SAXException (e); 86 } 87 } 88 89 97 public void ignorableWhitespace(char ch[], int start, int length) 98 throws SAXException { 99 100 } 102 103 109 public void endPrefixMapping(String prefix) throws SAXException { 110 111 } 113 114 120 public void skippedEntity(String name) throws SAXException { 121 122 } 124 125 130 public void setDocumentLocator(Locator locator) { 131 132 } 134 135 142 public void processingInstruction(String target, String data) 143 throws SAXException { 144 145 } 147 148 155 public void startPrefixMapping(String prefix, String uri) 156 throws SAXException { 157 try { 158 writer.writeNamespace(prefix, uri); 159 writer.setPrefix(prefix, uri); 160 } catch (XMLStreamException e) { 161 throw new SAXException (e); 162 } 163 } 164 165 173 public void endElement(String namespaceURI, String localName, String qName) 174 throws SAXException { 175 try { 176 writer.writeEndElement(); 177 } catch (XMLStreamException e) { 178 throw new SAXException (e); 179 } 180 } 181 182 188 private String getPrefix(String qName) { 189 if (qName != null) { 190 return qName.substring(0, qName.indexOf(":")); 191 } 192 return null; 193 } 194 195 204 public void startElement( 205 String namespaceURI, String localName, String qName, Attributes atts) 206 throws SAXException { 207 try { 208 log.info("writing element {" + namespaceURI + '}' + localName 209 + " directly to stream "); 210 String prefix = getPrefix(qName); 211 212 if (prefix == null) { 215 writer.writeStartElement(namespaceURI, localName); 216 } else { 217 writer.writeStartElement(prefix, localName, namespaceURI); 218 } 219 if (atts != null) { 220 int attCount = atts.getLength(); 221 for (int i = 0; i < attCount; i++) { 222 writer.writeAttribute(atts.getURI(i), localName, 223 atts.getValue(i)); 224 } 225 } 226 } catch (XMLStreamException e) { 227 throw new SAXException (e); 228 } 229 } 230 } 231 | Popular Tags |