1 package org.apache.axis2.om.impl.llom; 2 3 import org.apache.axis2.om.OMAttribute; 4 import org.apache.axis2.om.OMNamespace; 5 import org.apache.axis2.om.OMNode; 6 import org.apache.axis2.om.OMOutput; 7 import org.apache.axis2.om.impl.llom.serialize.StreamingOMSerializer; 8 9 import javax.xml.stream.XMLStreamException; 10 import javax.xml.stream.XMLStreamWriter; 11 import java.util.Iterator ; 12 13 30 public class OMSerializerUtil { 31 32 38 static void serializeEndpart(OMOutput omOutput) 39 throws XMLStreamException { 40 omOutput.getXmlStreamWriter().writeEndElement(); 41 } 42 43 50 static void serializeAttribute(OMAttribute attr, OMOutput omOutput) 51 throws XMLStreamException { 52 53 OMNamespace ns = attr.getNamespace(); 55 String prefix = null; 56 String namespaceName = null; 57 XMLStreamWriter writer = omOutput.getXmlStreamWriter(); 58 if (ns != null) { 59 60 prefix = ns.getPrefix(); 62 namespaceName = ns.getName(); 63 if (prefix != null) { 64 writer.writeAttribute(prefix, namespaceName, 65 attr.getLocalName(), attr.getValue()); 66 } else { 67 writer.writeAttribute(namespaceName, attr.getLocalName(), 68 attr.getValue()); 69 } 70 } else { 71 writer.writeAttribute(attr.getLocalName(), attr.getValue()); 72 } 73 } 74 75 82 static void serializeNamespace(OMNamespace namespace,OMOutput omOutput) 83 throws XMLStreamException { 84 85 if (namespace != null) { 86 XMLStreamWriter writer = omOutput.getXmlStreamWriter(); 87 String uri = namespace.getName(); 88 String prefix = writer.getPrefix(uri); 89 String ns_prefix = namespace.getPrefix(); 90 if (prefix == null) { 91 writer.writeNamespace(ns_prefix, namespace.getName()); 92 writer.setPrefix(ns_prefix, uri); 93 } 94 } 95 } 96 97 98 104 static void serializeStartpart(OMElementImpl element,OMOutput omOutput) 105 throws XMLStreamException { 106 String nameSpaceName = null; 107 String writer_prefix = null; 108 String prefix = null; 109 XMLStreamWriter writer = omOutput.getXmlStreamWriter(); 110 if (element.ns != null) { 111 nameSpaceName = element.ns.getName(); 112 writer_prefix = writer.getPrefix(nameSpaceName); 113 prefix = element.ns.getPrefix(); 114 if (nameSpaceName != null) { 115 if (writer_prefix != null) { 116 writer.writeStartElement(nameSpaceName, 117 element.getLocalName()); 118 } else { 119 if (prefix != null) { 120 writer.writeStartElement(prefix, element.getLocalName(), 121 nameSpaceName); 122 writer.writeNamespace(prefix, nameSpaceName); 123 writer.setPrefix(prefix, nameSpaceName); 124 } else { 125 writer.writeStartElement(nameSpaceName, 126 element.getLocalName()); 127 writer.writeDefaultNamespace(nameSpaceName); 128 writer.setDefaultNamespace(nameSpaceName); 129 } 130 } 131 } else { 132 writer.writeStartElement(element.getLocalName()); 133 } 136 } else { 137 writer.writeStartElement(element.getLocalName()); 138 } 141 142 serializeAttributes(element,omOutput); 144 145 serializeNamespaces(element, omOutput); 147 } 148 149 public static void serializeNamespaces(OMElementImpl element, OMOutput omOutput) throws XMLStreamException { 150 Iterator namespaces = element.getAllDeclaredNamespaces(); 151 if (namespaces != null) { 152 while (namespaces.hasNext()) { 153 serializeNamespace((OMNamespace) namespaces.next(), omOutput); 154 } 155 } 156 } 157 158 public static void serializeAttributes(OMElementImpl element, OMOutput omOutput) throws XMLStreamException { 159 if (element.getAttributes() != null) { 160 Iterator attributesList = element.getAttributes(); 161 while (attributesList.hasNext()) { 162 serializeAttribute((OMAttribute) attributesList.next(), omOutput); 163 } 164 } 165 } 166 167 168 175 static void serializeNormal(OMElementImpl element,OMOutput omOutput, boolean cache) 176 throws XMLStreamException { 177 178 if (cache){ 179 element.build(); 180 } 181 182 serializeStartpart(element,omOutput); 183 OMNode firstChild = element.firstChild; 184 if (firstChild != null) { 185 if (cache){ 186 firstChild.serializeWithCache(omOutput); 187 }else{ 188 firstChild.serialize(omOutput); 189 } 190 } 191 serializeEndpart(omOutput); 192 } 193 194 static void serializeByPullStream(OMElementImpl element,OMOutput omOutput) throws XMLStreamException { 195 StreamingOMSerializer streamingOMSerializer = new StreamingOMSerializer(); 196 streamingOMSerializer.serialize(element.getXMLStreamReaderWithoutCaching(), 197 omOutput); 198 return; 199 } 200 } 201 | Popular Tags |