1 28 29 package org.jibx.extras; 30 31 import java.io.IOException ; 32 import java.util.ArrayList ; 33 import java.util.List ; 34 35 import org.dom4j.Attribute; 36 import org.dom4j.DocumentFactory; 37 import org.dom4j.Element; 38 import org.dom4j.Namespace; 39 import org.dom4j.Node; 40 import org.dom4j.ProcessingInstruction; 41 import org.dom4j.QName; 42 import org.jibx.runtime.JiBXException; 43 import org.jibx.runtime.impl.UnmarshallingContext; 44 45 53 54 public class Dom4JMapperBase extends DocumentModelMapperBase 55 { 56 57 private static DocumentFactory s_factory = DocumentFactory.getInstance(); 58 59 60 protected String m_defaultNamespaceURI; 61 62 63 protected int m_defaultNamespaceIndex; 64 65 72 73 private int findNamespaceIndex(Namespace ns) { 74 if (Namespace.NO_NAMESPACE.equals(ns)) { 75 return 0; 76 } else if (Namespace.XML_NAMESPACE.equals(ns)) { 77 return 1; 78 } else { 79 String prefix = ns.getPrefix(); 80 if (prefix == null || prefix.length() == 0) { 81 if (m_defaultNamespaceURI == null) { 82 int index = m_xmlWriter.getPrefixIndex(""); 83 if (index >= 0) { 84 m_defaultNamespaceURI = getNamespaceUri(index); 85 m_defaultNamespaceIndex = index; 86 if (m_defaultNamespaceURI.equals(ns.getURI())) { 87 return index; 88 } else { 89 return -1; 90 } 91 } else { 92 return -1; 93 } 94 } else { 95 return m_defaultNamespaceURI.equals(ns.getURI()) ? 96 m_defaultNamespaceIndex : -1; 97 } 98 } else { 99 int index = m_xmlWriter.getPrefixIndex(prefix); 100 if (index >= 0) { 101 return getNamespaceUri(index).equals(ns.getURI()) ? 102 index : -1; 103 } else { 104 return -1; 105 } 106 } 107 } 108 } 109 110 117 118 protected void marshalContent(List content) 119 throws JiBXException, IOException { 120 int size = content.size(); 121 for (int i = 0; i < size; i++) { 122 Node node = (Node)content.get(i); 123 switch (node.getNodeType()) { 124 125 case Node.CDATA_SECTION_NODE: 126 m_xmlWriter.writeCData(node.getText()); 127 break; 128 129 case Node.COMMENT_NODE: 130 m_xmlWriter.writeComment(node.getText()); 131 break; 132 133 case Node.ELEMENT_NODE: 134 marshalElement((Element)node); 135 break; 136 137 case Node.ENTITY_REFERENCE_NODE: 138 m_xmlWriter.writeEntityRef(node.getName()); 139 break; 140 141 case Node.PROCESSING_INSTRUCTION_NODE: 142 m_xmlWriter.writePI(((ProcessingInstruction)node). 143 getTarget(), node.getText()); 144 break; 145 146 case Node.TEXT_NODE: 147 m_xmlWriter.writeTextContent(node.getText()); 148 break; 149 150 default: 151 break; 152 } 153 } 154 } 155 156 163 164 protected void marshalElement(Element element) 165 throws JiBXException, IOException { 166 167 int size = element.nodeCount(); 169 Namespace ns = element.getNamespace(); 170 int nsi = findNamespaceIndex(ns); 171 ArrayList nss = null; 172 boolean hascontent = false; 173 int defind = -1; 174 String defuri = null; 175 for (int i = 0; i < size; i++) { 176 Node node = element.node(i); 177 if (node instanceof Namespace) { 178 Namespace dns = (Namespace)node; 179 if (findNamespaceIndex(dns) < 0) { 180 if (nss == null) { 181 nss = new ArrayList (); 182 } 183 nss.add(dns); 184 String prefix = dns.getPrefix(); 185 if (prefix == null || prefix.length() == 0) { 186 defind = nss.size() - 1; 187 defuri = dns.getURI(); 188 } 189 } 190 } else { 191 hascontent = true; 192 } 193 } 194 195 String [] uris = null; 197 if (nss == null) { 198 m_xmlWriter.startTagOpen(nsi, element.getName()); 199 } else { 200 int base = getNextNamespaceIndex(); 201 if (defind >= 0) { 202 m_defaultNamespaceIndex = base + defind; 203 m_defaultNamespaceURI = defuri; 204 } 205 uris = new String [nss.size()]; 206 int[] nums = new int[nss.size()]; 207 String [] prefs = new String [nss.size()]; 208 for (int i = 0; i < uris.length; i++) { 209 Namespace addns = (Namespace)nss.get(i); 210 uris[i] = addns.getURI(); 211 nums[i] = base + i; 212 prefs[i] = addns.getPrefix(); 213 if (nsi < 0 && ns.equals(addns)) { 214 nsi = base + i; 215 } 216 } 217 m_xmlWriter.pushExtensionNamespaces(uris); 218 m_xmlWriter.startTagNamespaces(nsi, element.getName(), nums, prefs); 219 if (defind >= 0) { 220 m_defaultNamespaceIndex = defind; 221 m_defaultNamespaceURI = defuri; 222 } 223 } 224 225 if (element.attributeCount() > 0) { 227 for (int i = 0; i < element.attributeCount(); i++) { 228 Attribute attr = element.attribute(i); 229 int index = findNamespaceIndex(attr.getNamespace()); 230 m_xmlWriter.addAttribute(index, attr.getName(), 231 attr.getValue()); 232 } 233 } 234 235 if (hascontent) { 237 m_xmlWriter.closeStartTag(); 238 marshalContent(element.content()); 239 m_xmlWriter.endTag(nsi, element.getName()); 240 } else { 241 m_xmlWriter.closeEmptyTag(); 242 } 243 244 if (nss != null) { 246 m_xmlWriter.popExtensionNamespaces(); 247 if (defind >= 0) { 248 m_defaultNamespaceURI = null; 249 } 250 } 251 } 252 253 262 263 protected void unmarshalContent(List content) 264 throws JiBXException, IOException { 265 266 loop: while (true) { 268 int cev = m_unmarshalContext.currentEvent(); 269 switch (cev) { 270 271 case UnmarshallingContext.CDSECT: 272 content.add(s_factory. 273 createCDATA(m_unmarshalContext.getText())); 274 break; 275 276 case UnmarshallingContext.COMMENT: 277 content.add(s_factory. 278 createComment(m_unmarshalContext.getText())); 279 break; 280 281 case UnmarshallingContext.END_TAG: 282 break loop; 283 284 case UnmarshallingContext.ENTITY_REF: 285 if (m_unmarshalContext.getText() == null) { 286 content.add(s_factory. 287 createEntity(m_unmarshalContext.getName(), null)); 288 break; 289 } else { 290 content.add(s_factory.createText(accumulateText())); 291 continue loop; 292 } 293 294 case UnmarshallingContext.PROCESSING_INSTRUCTION: 295 { 296 String text = m_unmarshalContext.getText(); 297 int index = 0; 298 while (++index < text.length() && 299 !isWhitespace(text.charAt(index))); 300 if (index < text.length()) { 301 String target = text.substring(0, index); 302 while (++index < text.length() && 303 isWhitespace(text.charAt(index))); 304 String data = text.substring(index); 305 content.add(s_factory. 306 createProcessingInstruction(target, data)); 307 } else { 308 content.add(s_factory. 309 createProcessingInstruction(text, "")); 310 } 311 } 312 break; 313 314 case UnmarshallingContext.START_TAG: 315 content.add(unmarshalElement()); 316 continue loop; 317 318 case UnmarshallingContext.TEXT: 319 content.add(s_factory.createText(accumulateText())); 320 continue loop; 321 322 } 323 m_unmarshalContext.nextToken(); 324 } 325 } 326 327 335 336 protected Element unmarshalElement() throws JiBXException, IOException { 337 338 QName qname = QName.get(m_unmarshalContext.getName(), 340 m_unmarshalContext.getPrefix(), m_unmarshalContext.getNamespace()); 341 Element element = s_factory.createElement(qname); 342 343 int ncount = m_unmarshalContext.getNamespaceCount(); 345 for (int i = 0; i < ncount; i++) { 346 String prefix = m_unmarshalContext.getNamespacePrefix(i); 347 String uri = m_unmarshalContext.getNamespaceUri(i); 348 element.addNamespace(prefix, uri); 349 } 350 351 int acount = m_unmarshalContext.getAttributeCount(); 353 for (int i = 0; i < acount; i++) { 354 String prefix = m_unmarshalContext.getAttributePrefix(i); 355 String uri = m_unmarshalContext.getAttributeNamespace(i); 356 String name = m_unmarshalContext.getAttributeName(i); 357 String value = m_unmarshalContext.getAttributeValue(i); 358 qname = QName.get(name, prefix, uri); 359 element.addAttribute(qname, value); 360 } 361 362 int event = m_unmarshalContext.nextToken(); 364 if (event != UnmarshallingContext.END_TAG) { 365 unmarshalContent(element.content()); 366 } 367 m_unmarshalContext.nextToken(); 368 return element; 369 } 370 } | Popular Tags |