1 28 29 package org.jibx.extras; 30 31 import java.io.IOException ; 32 33 import org.dom4j.Element; 34 import org.jibx.runtime.IAliasable; 35 import org.jibx.runtime.IMarshaller; 36 import org.jibx.runtime.IMarshallingContext; 37 import org.jibx.runtime.IUnmarshaller; 38 import org.jibx.runtime.IUnmarshallingContext; 39 import org.jibx.runtime.JiBXException; 40 import org.jibx.runtime.impl.UnmarshallingContext; 41 42 56 57 public class Dom4JElementMapper extends Dom4JMapperBase 58 implements IMarshaller, IUnmarshaller, IAliasable 59 { 60 61 private final String m_uri; 62 63 64 private final int m_index; 65 66 67 private final String m_name; 68 69 72 73 public Dom4JElementMapper() { 74 m_uri = null; 75 m_index = -1; 76 m_name = null; 77 } 78 79 89 90 public Dom4JElementMapper(String uri, int index, String name) { 91 92 m_uri = uri; 94 m_index = index; 95 m_name = name; 96 } 97 98 101 102 public boolean isExtension(int index) { 103 return false; 104 } 105 106 110 111 public void marshal(Object obj, IMarshallingContext ictx) 112 throws JiBXException { 113 114 if (!(obj instanceof Element)) { 116 throw new JiBXException("Mapped object not an org.dom4j.Element"); 117 } else { 118 try { 119 120 m_xmlWriter = ictx.getXmlWriter(); 122 m_xmlWriter.indent(); 123 int indent = ictx.getIndent(); 124 ictx.setIndent(-1); 125 m_defaultNamespaceURI = null; 126 marshalElement((Element)obj); 127 ictx.setIndent(indent); 128 129 } catch (IOException e) { 130 throw new JiBXException("Error writing to document", e); 131 } 132 } 133 } 134 135 138 139 public boolean isPresent(IUnmarshallingContext ctx) throws JiBXException { 140 if (m_name == null) { 141 if (!(ctx instanceof UnmarshallingContext)) { 142 throw new JiBXException 143 ("Unmarshalling context not of expected type"); 144 } else { 145 return !((UnmarshallingContext)ctx).isEnd(); 146 } 147 } else { 148 return ctx.isAt(m_uri, m_name); 149 } 150 } 151 152 156 157 public Object unmarshal(Object obj, IUnmarshallingContext ictx) 158 throws JiBXException { 159 160 if (!(ictx instanceof UnmarshallingContext)) { 162 throw new JiBXException 163 ("Unmarshalling context not of expected type"); 164 } else if (m_name != null && !ictx.isAt(m_uri, m_name)) { 165 ((UnmarshallingContext)ictx).throwStartTagNameError(m_uri, m_name); 166 } 167 168 m_unmarshalContext = (UnmarshallingContext)ictx; 170 m_unmarshalContext.toStart(); 171 172 try { 174 return unmarshalElement(); 175 } catch (IOException e) { 176 throw new JiBXException("Error reading from document", e); 177 } 178 } 179 } | Popular Tags |