1 28 29 package org.jibx.extras; 30 31 import java.io.IOException ; 32 33 import org.jibx.runtime.IMarshaller; 34 import org.jibx.runtime.IMarshallingContext; 35 import org.jibx.runtime.IUnmarshaller; 36 import org.jibx.runtime.IUnmarshallingContext; 37 import org.jibx.runtime.JiBXException; 38 import org.jibx.runtime.impl.UnmarshallingContext; 39 import org.w3c.dom.DocumentFragment ; 40 import org.w3c.dom.Node ; 41 42 55 56 public class DomFragmentMapper extends DomMapperBase 57 implements IMarshaller, IUnmarshaller 58 { 59 64 public DomFragmentMapper() throws JiBXException { 65 super(); 66 } 67 68 71 72 public boolean isExtension(int index) { 73 return false; 74 } 75 76 80 81 public void marshal(Object obj, IMarshallingContext ictx) 82 throws JiBXException { 83 84 if (!(obj instanceof DocumentFragment )) { 86 throw new JiBXException 87 ("Mapped object not an org.w3c.dom.DocumentFragment"); 88 } else { 89 try { 90 91 m_xmlWriter = ictx.getXmlWriter(); 93 int indent = ictx.getIndent(); 94 ictx.setIndent(-1); 95 m_defaultNamespaceURI = null; 96 marshalContent(((DocumentFragment )obj).getChildNodes()); 97 ictx.setIndent(indent); 98 99 } catch (IOException e) { 100 throw new JiBXException("Error writing to document", e); 101 } 102 } 103 } 104 105 108 109 public boolean isPresent(IUnmarshallingContext ctx) throws JiBXException { 110 if (!(ctx instanceof UnmarshallingContext)) { 111 throw new JiBXException 112 ("Unmarshalling context not of expected type"); 113 } else { 114 return ((UnmarshallingContext)ctx).currentEvent() != 115 UnmarshallingContext.END_TAG; 116 } 117 } 118 119 123 124 public Object unmarshal(Object obj, IUnmarshallingContext ictx) 125 throws JiBXException { 126 127 boolean created = false; 129 DocumentFragment frag = null; 130 if (obj == null) { 131 frag = m_document.createDocumentFragment(); 132 created = true; 133 } else if (obj instanceof DocumentFragment ) { 134 frag = (DocumentFragment )obj; 135 } else { 136 throw new JiBXException 137 ("Supplied object is not an org.w3c.dom.DocumentFragment"); 138 } 139 if (!(ictx instanceof UnmarshallingContext)) { 140 throw new JiBXException 141 ("Unmarshalling context not of expected type"); 142 } 143 144 m_unmarshalContext = (UnmarshallingContext)ictx; 146 try { 147 Node node; 148 while ((node = unmarshalNode()) != null) { 149 frag.appendChild(node); 150 } 151 if (created && !frag.hasChildNodes()) { 152 return null; 153 } else { 154 return frag; 155 } 156 } catch (IOException e) { 157 throw new JiBXException("Error reading from document", e); 158 } 159 } 160 } | Popular Tags |