1 29 30 package com.caucho.xml.stream; 31 32 import javax.xml.namespace.NamespaceContext ; 33 import javax.xml.namespace.QName ; 34 import javax.xml.stream.XMLEventReader; 35 import javax.xml.stream.XMLEventWriter; 36 import javax.xml.stream.XMLStreamConstants; 37 import javax.xml.stream.XMLStreamException; 38 import javax.xml.stream.XMLStreamWriter; 39 import javax.xml.stream.events.*; 40 41 public class XMLEventWriterImpl implements XMLEventWriter, XMLStreamConstants { 42 private XMLStreamWriter _out; 43 44 public XMLEventWriterImpl(XMLStreamWriter out) 45 { 46 _out = out; 47 } 48 49 public void add(XMLEvent event) throws XMLStreamException 50 { 51 if (event instanceof Namespace) { 53 Namespace namespace = (Namespace) event; 54 55 if (namespace.isDefaultNamespaceDeclaration()) 56 _out.writeDefaultNamespace(namespace.getNamespaceURI()); 57 else 58 _out.writeNamespace(namespace.getPrefix(), 59 namespace.getNamespaceURI()); 60 61 } 62 else if (event instanceof Attribute) { 63 Attribute attribute = (Attribute) event; 64 QName name = attribute.getName(); 65 66 if (name.getPrefix() != null && ! "".equals(name.getPrefix())) { 67 _out.writeAttribute(name.getPrefix(), name.getNamespaceURI(), 68 name.getLocalPart(), attribute.getValue()); 69 } 70 else if (name.getNamespaceURI() != null && 71 ! "".equals(name.getNamespaceURI())) { 72 _out.writeAttribute(name.getNamespaceURI(), name.getLocalPart(), 73 attribute.getValue()); 74 } 75 else 76 _out.writeAttribute(name.getLocalPart(), attribute.getValue()); 77 } 78 else if (event instanceof Characters) { 79 Characters characters = (Characters) event; 80 81 switch (characters.getEventType()) { 82 case CDATA: 83 _out.writeCData(characters.getData()); 84 break; 85 86 case SPACE: 87 case CHARACTERS: 88 default: 89 _out.writeCharacters(characters.getData()); 90 break; 91 } 92 } 93 else if (event instanceof Comment) { 94 Comment comment = (Comment) event; 95 96 _out.writeComment(comment.getText()); 97 } 98 else if (event instanceof DTD) { 99 DTD dtd = (DTD) event; 100 101 _out.writeDTD(dtd.getDocumentTypeDeclaration()); 102 } 103 else if (event instanceof EndDocument) { 104 _out.writeEndDocument(); 105 } 106 else if (event instanceof EndElement) { 107 _out.writeEndElement(); 108 } 109 else if (event instanceof EntityDeclaration) { 110 throw new UnsupportedOperationException (); 111 } 112 else if (event instanceof EntityReference) { 113 throw new UnsupportedOperationException (); 114 } 115 else if (event instanceof NotationDeclaration) { 116 throw new UnsupportedOperationException (); 117 } 118 else if (event instanceof ProcessingInstruction) { 119 ProcessingInstruction pi = (ProcessingInstruction) event; 120 121 if (pi.getData() == null || "".equals(pi.getData())) 122 _out.writeProcessingInstruction(pi.getTarget()); 123 else 124 _out.writeProcessingInstruction(pi.getTarget(), pi.getData()); 125 126 } 127 else if (event instanceof StartDocument) { 128 StartDocument startDocument = (StartDocument) event; 129 130 if (startDocument.encodingSet()) { 131 _out.writeStartDocument(startDocument.getCharacterEncodingScheme(), 132 startDocument.getVersion()); 133 } 134 else if (startDocument.getVersion() != null && 135 ! "".equals(startDocument.getVersion())) { 136 _out.writeStartDocument(startDocument.getVersion()); 137 } 138 else 139 _out.writeStartDocument(); 140 } 141 else if (event instanceof StartElement) { 142 StartElement startElement = (StartElement) event; 143 QName name = startElement.getName(); 144 145 if (name.getPrefix() != null && ! "".equals(name.getPrefix())) { 146 _out.writeStartElement(name.getPrefix(), name.getNamespaceURI(), 147 name.getLocalPart()); 148 } 149 else if (name.getNamespaceURI() != null && 150 ! "".equals(name.getNamespaceURI())) { 151 _out.writeStartElement(name.getNamespaceURI(), name.getLocalPart()); 152 } 153 else 154 _out.writeStartElement(name.getLocalPart()); 155 } 156 else 157 throw new XMLStreamException(); 158 } 159 160 public void add(XMLEventReader reader) throws XMLStreamException 161 { 162 while (reader.hasNext()) 163 add(reader.nextEvent()); 164 } 165 166 public void close() throws XMLStreamException 167 { 168 _out.close(); 169 } 170 171 public void flush() throws XMLStreamException 172 { 173 _out.flush(); 174 } 175 176 public NamespaceContext getNamespaceContext() 177 { 178 return _out.getNamespaceContext(); 179 } 180 181 public String getPrefix(String uri) throws XMLStreamException 182 { 183 return _out.getPrefix(uri); 184 } 185 186 public void setDefaultNamespace(String uri) throws XMLStreamException 187 { 188 _out.setDefaultNamespace(uri); 189 } 190 191 public void setNamespaceContext(NamespaceContext context) 192 throws XMLStreamException 193 { 194 _out.setNamespaceContext(context); 195 } 196 197 public void setPrefix(String prefix, String uri) throws XMLStreamException 198 { 199 _out.setPrefix(prefix, uri); 200 } 201 202 } 203 | Popular Tags |