1 38 39 package com.sun.xml.fastinfoset.stax; 40 41 import java.util.Iterator ; 42 import javax.xml.namespace.QName ; 43 import javax.xml.namespace.NamespaceContext ; 44 import javax.xml.stream.XMLStreamConstants; 45 import javax.xml.stream.XMLEventWriter; 46 import javax.xml.stream.XMLEventReader; 47 import javax.xml.stream.XMLStreamWriter; 48 import javax.xml.stream.XMLStreamException; 49 import javax.xml.stream.events.*; 50 import com.sun.xml.fastinfoset.util.Util; 51 import com.sun.xml.fastinfoset.CommonResourceBundle; 52 53 public class StAXEventWriter implements XMLEventWriter { 54 55 private XMLStreamWriter _streamWriter ; 56 60 public StAXEventWriter(XMLStreamWriter streamWriter){ 61 _streamWriter = streamWriter; 62 } 63 64 68 public void flush() throws XMLStreamException { 69 _streamWriter.flush(); 70 } 71 75 public void close() throws javax.xml.stream.XMLStreamException { 76 _streamWriter.close(); 77 } 78 79 84 public void add(XMLEventReader eventReader) throws XMLStreamException { 85 if(eventReader == null) throw new XMLStreamException(CommonResourceBundle.getInstance().getString("message.nullEventReader")); 86 while(eventReader.hasNext()){ 87 add(eventReader.nextEvent()); 88 } 89 } 90 91 99 public void add(XMLEvent event) throws XMLStreamException { 100 int type = event.getEventType(); 101 switch(type){ 102 case XMLEvent.DTD:{ 103 DTD dtd = (DTD)event ; 104 _streamWriter.writeDTD(dtd.getDocumentTypeDeclaration()); 105 break; 106 } 107 case XMLEvent.START_DOCUMENT :{ 108 StartDocument startDocument = (StartDocument)event ; 109 _streamWriter.writeStartDocument(startDocument.getCharacterEncodingScheme(), startDocument.getVersion()); 110 break; 111 } 112 case XMLEvent.START_ELEMENT :{ 113 StartElement startElement = event.asStartElement() ; 114 QName qname = startElement.getName(); 115 _streamWriter.writeStartElement(qname.getPrefix(), qname.getLocalPart(), qname.getNamespaceURI()); 116 117 Iterator iterator = startElement.getNamespaces(); 118 while(iterator.hasNext()){ 119 Namespace namespace = (Namespace)iterator.next(); 120 _streamWriter.writeNamespace(namespace.getPrefix(), namespace.getNamespaceURI()); 121 } 122 123 Iterator attributes = startElement.getAttributes(); 124 while(attributes.hasNext()){ 125 Attribute attribute = (Attribute)attributes.next(); 126 QName name = attribute.getName(); 127 _streamWriter.writeAttribute(name.getPrefix(), name.getNamespaceURI(), 128 name.getLocalPart(),attribute.getValue()); 129 } 130 break; 131 } 132 case XMLEvent.NAMESPACE:{ 133 Namespace namespace = (Namespace)event; 134 _streamWriter.writeNamespace(namespace.getPrefix(), namespace.getNamespaceURI()); 135 break ; 136 } 137 case XMLEvent.COMMENT: { 138 Comment comment = (Comment)event ; 139 _streamWriter.writeComment(comment.getText()); 140 break; 141 } 142 case XMLEvent.PROCESSING_INSTRUCTION:{ 143 ProcessingInstruction processingInstruction = (ProcessingInstruction)event ; 144 _streamWriter.writeProcessingInstruction(processingInstruction.getTarget(), processingInstruction.getData()); 145 break; 146 } 147 case XMLEvent.CHARACTERS:{ 148 Characters characters = event.asCharacters(); 149 if(characters.isCData()){ 151 _streamWriter.writeCData(characters.getData()); 152 } 153 else{ 154 _streamWriter.writeCharacters(characters.getData()); 155 } 156 break; 157 } 158 case XMLEvent.ENTITY_REFERENCE:{ 159 EntityReference entityReference = (EntityReference)event ; 160 _streamWriter.writeEntityRef(entityReference.getName()); 161 break; 162 } 163 case XMLEvent.ATTRIBUTE:{ 164 Attribute attribute = (Attribute)event; 165 QName qname = attribute.getName(); 166 _streamWriter.writeAttribute(qname.getPrefix(), qname.getNamespaceURI(), qname.getLocalPart(),attribute.getValue()); 167 break; 168 } 169 case XMLEvent.CDATA:{ 170 Characters characters = (Characters)event; 173 if(characters.isCData()){ 174 _streamWriter.writeCData(characters.getData()); 175 } 176 break; 177 } 178 179 case XMLEvent.END_ELEMENT:{ 180 _streamWriter.writeEndElement(); 181 break; 182 } 183 case XMLEvent.END_DOCUMENT:{ 184 _streamWriter.writeEndDocument(); 185 break; 186 } 187 default: 188 throw new XMLStreamException(CommonResourceBundle.getInstance().getString("message.eventTypeNotSupported", new Object []{Util.getEventTypeString(type)})); 189 }; 191 192 } 193 194 199 public String getPrefix(String uri) throws XMLStreamException { 200 return _streamWriter.getPrefix(uri); 201 } 202 203 204 208 public NamespaceContext getNamespaceContext() { 209 return _streamWriter.getNamespaceContext(); 210 } 211 212 213 222 public void setDefaultNamespace(String uri) throws XMLStreamException { 223 _streamWriter.setDefaultNamespace(uri); 224 } 225 226 236 public void setNamespaceContext(NamespaceContext namespaceContext) throws XMLStreamException { 237 _streamWriter.setNamespaceContext(namespaceContext); 238 } 239 248 public void setPrefix(String prefix, String uri) throws XMLStreamException { 249 _streamWriter.setPrefix(prefix, uri); 250 } 251 252 } 253 | Popular Tags |