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.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 Dom4JListMapper extends Dom4JMapperBase 58 implements IMarshaller, IUnmarshaller 59 { 60 63 64 public boolean isExtension(int index) { 65 return false; 66 } 67 68 72 73 public void marshal(Object obj, IMarshallingContext ictx) 74 throws JiBXException { 75 76 if (!(obj instanceof List )) { 78 throw new JiBXException("Mapped object not a java.util.List"); 79 } else { 80 try { 81 82 m_xmlWriter = ictx.getXmlWriter(); 84 int indent = ictx.getIndent(); 85 ictx.setIndent(-1); 86 m_defaultNamespaceURI = null; 87 marshalContent((List )obj); 88 ictx.setIndent(indent); 89 90 } catch (IOException e) { 91 throw new JiBXException("Error writing to document", e); 92 } 93 } 94 } 95 96 99 100 public boolean isPresent(IUnmarshallingContext ctx) throws JiBXException { 101 if (!(ctx instanceof UnmarshallingContext)) { 102 throw new JiBXException 103 ("Unmarshalling context not of expected type"); 104 } else { 105 return ((UnmarshallingContext)ctx).currentEvent() != 106 UnmarshallingContext.END_TAG; 107 } 108 } 109 110 114 115 public Object unmarshal(Object obj, IUnmarshallingContext ictx) 116 throws JiBXException { 117 118 boolean created = false; 120 List list = null; 121 if (obj == null) { 122 list = new ArrayList (); 123 created = true; 124 } else if (obj instanceof List ) { 125 list = (List )obj; 126 } else { 127 throw new JiBXException("Supplied object is not a java.util.List"); 128 } 129 if (!(ictx instanceof UnmarshallingContext)) { 130 throw new JiBXException 131 ("Unmarshalling context not of expected type"); 132 } 133 134 m_unmarshalContext = (UnmarshallingContext)ictx; 136 try { 137 unmarshalContent(list); 138 if (created && list.isEmpty()) { 139 return null; 140 } else { 141 return list; 142 } 143 } catch (IOException e) { 144 throw new JiBXException("Error reading from document", e); 145 } 146 } 147 } | Popular Tags |