1 17 package org.apache.servicemix.jbi.jaxp; 18 19 import javax.xml.XMLConstants ; 20 import javax.xml.namespace.QName ; 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 34 public class XMLStreamHelper implements XMLStreamConstants { 35 36 47 public static void copy( XMLStreamReader reader, XMLStreamWriter writer ) 48 throws XMLStreamException 49 { 50 int read = 0; int event = reader.getEventType(); 52 53 while ( reader.hasNext() ) 54 { 55 switch( event ) 56 { 57 case XMLStreamConstants.START_ELEMENT: 58 read++; 59 writeStartElement( reader, writer ); 60 break; 61 case XMLStreamConstants.END_ELEMENT: 62 writer.writeEndElement(); 63 read--; 64 if ( read <= 0 ) 65 return; 66 break; 67 case XMLStreamConstants.CHARACTERS: 68 writer.writeCharacters( reader.getText() ); 69 break; 70 case XMLStreamConstants.START_DOCUMENT: 71 case XMLStreamConstants.END_DOCUMENT: 72 case XMLStreamConstants.ATTRIBUTE: 73 case XMLStreamConstants.NAMESPACE: 74 break; 75 default: 76 break; 77 } 78 event = reader.next(); 79 } 80 } 81 82 private static void writeStartElement(XMLStreamReader reader, XMLStreamWriter writer) 83 throws XMLStreamException 84 { 85 String local = reader.getLocalName(); 86 String uri = reader.getNamespaceURI(); 87 String prefix = reader.getPrefix(); 88 if (prefix == null) 89 { 90 prefix = ""; 91 } 92 if (uri == null) 93 { 94 uri = ""; 95 } 96 97 String boundPrefix = writer.getPrefix(uri); 98 boolean writeElementNS = false; 99 if ( boundPrefix == null || !prefix.equals(boundPrefix) ) 100 { 101 writeElementNS = true; 102 } 103 104 if (uri != null) 106 { 107 if (prefix.length() == 0) 108 { 109 110 writer.writeStartElement(local); 111 writer.setDefaultNamespace(uri); 112 113 } 114 else 115 { 116 writer.writeStartElement(prefix, local, uri); 117 writer.setPrefix(prefix, uri); 118 } 119 } 120 else 121 { 122 writer.writeStartElement( local ); 123 } 124 125 for ( int i = 0; i < reader.getNamespaceCount(); i++ ) 127 { 128 String nsURI = reader.getNamespaceURI(i); 129 String nsPrefix = reader.getNamespacePrefix(i); 130 if (nsPrefix == null) nsPrefix = ""; 131 132 if ( nsPrefix.length() == 0 ) 133 { 134 writer.writeDefaultNamespace(nsURI); 135 } 136 else 137 { 138 writer.writeNamespace(nsPrefix, nsURI); 139 } 140 141 if (nsURI.equals(uri) && nsPrefix.equals(prefix)) 142 { 143 writeElementNS = false; 144 } 145 } 146 147 if (writeElementNS) 151 { 152 if ( prefix == null || prefix.length() == 0 ) 153 { 154 writer.writeDefaultNamespace(uri); 155 } 156 else 157 { 158 writer.writeNamespace(prefix, uri); 159 } 160 } 161 162 for ( int i = 0; i < reader.getAttributeCount(); i++ ) 164 { 165 String ns = reader.getAttributeNamespace(i); 166 String nsPrefix = reader.getAttributePrefix(i); 167 if ( ns == null || ns.length() == 0 ) 168 { 169 writer.writeAttribute( 170 reader.getAttributeLocalName(i), 171 reader.getAttributeValue(i)); 172 } 173 else if (nsPrefix == null || nsPrefix.length() == 0) 174 { 175 writer.writeAttribute( 176 reader.getAttributeNamespace(i), 177 reader.getAttributeLocalName(i), 178 reader.getAttributeValue(i)); 179 } 180 else 181 { 182 writer.writeAttribute(reader.getAttributePrefix(i), 183 reader.getAttributeNamespace(i), 184 reader.getAttributeLocalName(i), 185 reader.getAttributeValue(i)); 186 } 187 188 189 } 190 } 191 192 200 public static void writeStartElement( XMLStreamWriter writer, String uri, String local, String prefix ) 201 throws XMLStreamException 202 { 203 if (prefix == null) 204 { 205 prefix = ""; 206 } 207 if (uri == null) 208 { 209 uri = ""; 210 } 211 212 String boundPrefix = writer.getPrefix(uri); 213 boolean writeElementNS = false; 214 if ( boundPrefix == null || !prefix.equals(boundPrefix) ) 215 { 216 writeElementNS = true; 217 } 218 219 if (uri != null) 221 { 222 if (prefix.length() == 0) 223 { 224 225 writer.writeStartElement(local); 226 writer.setDefaultNamespace(uri); 227 228 } 229 else 230 { 231 writer.writeStartElement(prefix, local, uri); 232 writer.setPrefix(prefix, uri); 233 } 234 } 235 else 236 { 237 writer.writeStartElement( local ); 238 } 239 240 if (writeElementNS) 244 { 245 if ( prefix.length() == 0 ) 246 { 247 writer.writeDefaultNamespace(uri); 248 } 249 else 250 { 251 writer.writeNamespace(prefix, uri); 252 } 253 } 254 } 255 256 266 public static void writeStartElement(XMLStreamWriter writer, QName name) throws XMLStreamException { 267 String prefix = choosePrefix(writer, name, false); 268 writeStartElement(writer, name.getNamespaceURI(), name.getLocalPart(), prefix); 269 } 270 271 277 public static void writeTextQName(XMLStreamWriter out, QName name) throws XMLStreamException { 278 String prefix = choosePrefix(out, name, true); 279 if (XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)) { 280 out.writeCharacters(name.getLocalPart()); 281 } else { 282 out.writeCharacters(prefix + ":" + name.getLocalPart()); 283 } 284 } 285 286 protected static String choosePrefix(XMLStreamWriter out, QName name, boolean declare) throws XMLStreamException { 287 String uri = name.getNamespaceURI(); 288 if (uri == null || XMLConstants.NULL_NS_URI.equals(uri)) { 290 if (!XMLConstants.NULL_NS_URI.equals(out.getNamespaceContext().getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX))) { 291 out.setPrefix(XMLConstants.DEFAULT_NS_PREFIX, XMLConstants.NULL_NS_URI); 292 } 293 return XMLConstants.DEFAULT_NS_PREFIX; 294 } else { 296 String defPrefix = name.getPrefix(); 297 if (defPrefix != null && !XMLConstants.DEFAULT_NS_PREFIX.equals(defPrefix)) { 299 if (!uri.equals(out.getNamespaceContext().getNamespaceURI(defPrefix))) { 301 if (out.getNamespaceContext().getPrefix(uri) != null) { 303 defPrefix = out.getNamespaceContext().getPrefix(uri); 304 } else if (out.getPrefix(uri) != null) { 306 defPrefix = out.getPrefix(uri); 307 } else if (declare) { 309 out.setPrefix(defPrefix, uri); 310 out.writeNamespace(defPrefix, uri); 311 } 312 } 313 } else { 315 if (out.getNamespaceContext().getPrefix(uri) != null) { 317 defPrefix = out.getNamespaceContext().getPrefix(uri); 318 } else if (out.getPrefix(uri) != null) { 320 defPrefix = out.getPrefix(uri); 321 } else { 323 defPrefix = getUniquePrefix(out); 324 if (declare) { 325 out.setPrefix(defPrefix, uri); 326 out.writeNamespace(defPrefix, uri); 327 } 328 } 329 } 330 return defPrefix; 331 } 332 } 333 334 protected static String getUniquePrefix(XMLStreamWriter writer) { 335 int n = 1; 336 while (true) { 337 String nsPrefix = "ns" + n; 338 if (writer.getNamespaceContext().getNamespaceURI(nsPrefix) == null) { 339 return nsPrefix; 340 } 341 n++; 342 } 343 } 344 345 } 346 | Popular Tags |