1 16 package org.apache.axis2.om.impl.llom.serialize; 17 18 import org.apache.axis2.om.OMOutput; 19 import org.apache.axis2.om.OMSerializer; 20 21 import javax.xml.stream.XMLStreamConstants; 22 import javax.xml.stream.XMLStreamException; 23 import javax.xml.stream.XMLStreamReader; 24 import javax.xml.stream.XMLStreamWriter; 25 26 29 public class StreamingOMSerializer implements XMLStreamConstants, OMSerializer { 30 31 37 38 41 private int depth = 0; 42 43 50 public void serialize(Object obj, OMOutput omOutput) 51 throws XMLStreamException { 52 if (!(obj instanceof XMLStreamReader)) { 53 throw new UnsupportedOperationException ( 54 "Unsupported input object. Must be of the the type XMLStreamReader"); 55 } 56 XMLStreamReader node = (XMLStreamReader) obj; 57 serializeNode(node, omOutput); 58 } 59 60 67 protected void serializeNode(XMLStreamReader reader, OMOutput omOutput) 68 throws XMLStreamException { 69 XMLStreamWriter writer = omOutput.getXmlStreamWriter(); 72 while (reader.hasNext()) { 73 int event = reader.next(); 74 if (event == START_ELEMENT) { 75 serializeElement(reader, writer); 76 depth++; 77 } else if (event == ATTRIBUTE) { 78 serializeAttributes(reader, writer); 79 } else if (event == CHARACTERS) { 80 serializeText(reader, writer); 81 } else if (event == COMMENT) { 82 serializeComment(reader, writer); 83 } else if (event == CDATA) { 84 serializeCData(reader, writer); 85 } else if (event == END_ELEMENT) { 86 serializeEndElement(writer); 87 depth--; 88 } else if (event == END_DOCUMENT) { 89 try { 90 serializeEndElement(writer); 91 } catch (Exception e) { 92 } 94 } 95 if (depth == 0) { 96 break; 97 } 98 } 99 } 100 101 106 protected void serializeElement( 107 XMLStreamReader reader, XMLStreamWriter writer) 108 throws XMLStreamException { 109 String prefix = reader.getPrefix(); 110 String nameSpaceName = reader.getNamespaceURI(); 111 String writer_prefix = writer.getPrefix(nameSpaceName); 112 if (nameSpaceName != null) { 113 if (writer_prefix != null) { 114 writer.writeStartElement(nameSpaceName, reader.getLocalName()); 115 } else { 116 if (prefix != null) { 117 writer.writeStartElement(prefix, reader.getLocalName(), 118 nameSpaceName); 119 writer.writeNamespace(prefix, nameSpaceName); 120 writer.setPrefix(prefix, nameSpaceName); 121 } else { 122 writer.writeStartElement(nameSpaceName, 123 reader.getLocalName()); 124 writer.writeDefaultNamespace(nameSpaceName); 125 writer.setDefaultNamespace(nameSpaceName); 126 } 127 } 128 } else { 129 } 132 133 serializeAttributes(reader, writer); 135 136 int count = reader.getNamespaceCount(); 138 for (int i = 0; i < count; i++) { 139 serializeNamespace(reader.getNamespacePrefix(i), 140 reader.getNamespaceURI(i), writer); 141 } 142 } 143 144 150 protected void serializeEndElement(XMLStreamWriter writer) 151 throws XMLStreamException { 152 writer.writeEndElement(); 153 } 154 155 160 protected void serializeText(XMLStreamReader reader, XMLStreamWriter writer) 161 throws XMLStreamException { 162 writer.writeCharacters(reader.getText()); 163 } 164 165 172 protected void serializeCData( 173 XMLStreamReader reader, XMLStreamWriter writer) 174 throws XMLStreamException { 175 writer.writeCData(reader.getText()); 176 } 177 178 185 protected void serializeComment( 186 XMLStreamReader reader, XMLStreamWriter writer) 187 throws XMLStreamException { 188 writer.writeComment(reader.getText()); 189 } 190 191 196 protected void serializeAttributes( 197 XMLStreamReader reader, XMLStreamWriter writer) 198 throws XMLStreamException { 199 int count = reader.getAttributeCount(); 200 String prefix = null; 201 String namespaceName = null; 202 for (int i = 0; i < count; i++) { 203 prefix = reader.getAttributePrefix(i); 204 namespaceName = reader.getAttributeNamespace(i); 205 if ((prefix != null) && !namespaceName.equals("")) { 206 writer.writeAttribute(prefix, namespaceName, 207 reader.getAttributeLocalName(i), 208 reader.getAttributeValue(i)); 209 } else { 210 writer.writeAttribute(reader.getAttributeLocalName(i), 211 reader.getAttributeValue(i)); 212 } 213 } 214 } 215 216 224 private void serializeNamespace( 225 String prefix, String URI, XMLStreamWriter writer) 226 throws XMLStreamException { 227 String prefix1 = writer.getPrefix(URI); 228 if (prefix1 == null) { 229 writer.writeNamespace(prefix, URI); 230 writer.setPrefix(prefix, URI); 231 } 232 } 233 } 234 | Popular Tags |