1 package com.thoughtworks.xstream.io.xml; 2 3 import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 4 import com.thoughtworks.xstream.io.StreamException; 5 6 import javax.xml.namespace.QName ; 7 import javax.xml.stream.XMLStreamException; 8 import javax.xml.stream.XMLStreamWriter; 9 10 16 public class StaxWriter implements HierarchicalStreamWriter { 17 private final QNameMap qnameMap; 18 private final XMLStreamWriter out; 19 private final boolean writeEnclosingDocument; 20 private boolean namespaceRepairingMode; 21 22 private int tagDepth; 23 24 public StaxWriter(QNameMap qnameMap, XMLStreamWriter out) throws XMLStreamException { 25 this(qnameMap, out, true, true); 26 } 27 28 36 public StaxWriter(QNameMap qnameMap, XMLStreamWriter out, boolean writeEnclosingDocument, boolean namespaceRepairingMode) throws XMLStreamException { 37 this.qnameMap = qnameMap; 38 this.out = out; 39 this.writeEnclosingDocument = writeEnclosingDocument; 40 this.namespaceRepairingMode = namespaceRepairingMode; 41 if (writeEnclosingDocument) { 42 out.writeStartDocument(); 43 } 44 } 45 46 public void flush() { 47 try { 48 out.close(); 49 } 50 catch (XMLStreamException e) { 51 throw new StreamException(e); 52 } 53 } 54 55 58 public void close() { 59 try { 60 out.close(); 61 } 62 catch (XMLStreamException e) { 63 throw new StreamException(e); 64 } 65 } 66 67 public void addAttribute(String name, String value) { 68 try { 69 out.writeAttribute(name, value); 70 } 71 catch (XMLStreamException e) { 72 throw new StreamException(e); 73 } 74 } 75 76 public void endNode() { 77 try { 78 tagDepth--; 79 out.writeEndElement(); 80 if (tagDepth == 0 && writeEnclosingDocument) { 81 out.writeEndDocument(); 82 } 83 } 84 catch (XMLStreamException e) { 85 throw new StreamException(e); 86 } 87 } 88 89 public void setValue(String text) { 90 try { 91 out.writeCharacters(text); 92 } 93 catch (XMLStreamException e) { 94 throw new StreamException(e); 95 } 96 } 97 98 public void startNode(String name) { 99 try { 100 QName qname = qnameMap.getQName(name); 101 String prefix = qname.getPrefix(); 102 String uri = qname.getNamespaceURI(); 103 104 107 boolean hasPrefix = prefix != null && prefix.length() > 0; 108 boolean hasURI = uri != null && uri.length() > 0; 109 boolean writeNamespace = false; 110 111 if (hasURI) { 112 if (hasPrefix) { 113 String currentNamespace = out.getNamespaceContext().getNamespaceURI(prefix); 114 if (currentNamespace == null || !currentNamespace.equals(uri)) { 115 writeNamespace = true; 116 } 117 } 118 else { 119 String defaultNamespace = out.getNamespaceContext().getNamespaceURI(""); 120 if (defaultNamespace == null || !defaultNamespace.equals(uri)) { 121 writeNamespace = true; 122 } 123 } 124 } 125 126 if (hasPrefix) { 127 out.setPrefix(prefix, uri); 128 } 129 else if (hasURI) { 130 if (writeNamespace) { 131 out.setDefaultNamespace(uri); 132 } 133 } 134 out.writeStartElement(prefix, qname.getLocalPart(), uri); 135 if (hasURI && writeNamespace && !isNamespaceRepairingMode()) { 136 if (hasPrefix) { 137 out.writeNamespace(prefix, uri); 138 } 139 else { 140 out.writeDefaultNamespace(uri); 141 } 142 } 143 tagDepth++; 144 } 145 catch (XMLStreamException e) { 146 throw new StreamException(e); 147 } 148 } 149 150 public HierarchicalStreamWriter underlyingWriter() { 151 return this; 152 } 153 154 159 public boolean isNamespaceRepairingMode() { 160 return namespaceRepairingMode; 161 } 162 163 } 164 | Popular Tags |