1 28 29 package org.jibx.extras; 30 31 import java.io.IOException ; 32 import java.util.ArrayList ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 36 import org.jibx.runtime.IMarshaller; 37 import org.jibx.runtime.IMarshallingContext; 38 import org.jibx.runtime.IUnmarshaller; 39 import org.jibx.runtime.IUnmarshallingContext; 40 import org.jibx.runtime.JiBXException; 41 import org.jibx.runtime.impl.UnmarshallingContext; 42 import org.w3c.dom.Node ; 43 44 58 59 public class DomListMapper extends DomMapperBase 60 implements IMarshaller, IUnmarshaller 61 { 62 67 public DomListMapper() throws JiBXException { 68 super(); 69 } 70 71 74 75 public boolean isExtension(int index) { 76 return false; 77 } 78 79 83 84 public void marshal(Object obj, IMarshallingContext ictx) 85 throws JiBXException { 86 87 if (!(obj instanceof List )) { 89 throw new JiBXException("Mapped object not a java.util.List"); 90 } else { 91 try { 92 93 m_xmlWriter = ictx.getXmlWriter(); 95 int indent = ictx.getIndent(); 96 ictx.setIndent(-1); 97 m_defaultNamespaceURI = null; 98 for (Iterator iter = ((List )obj).iterator(); iter.hasNext();) { 99 Object item = iter.next(); 100 if (item instanceof Node ) { 101 marshalNode((Node )item); 102 } else { 103 throw new JiBXException 104 ("List item is not an org.w3c.dom.Node"); 105 } 106 } 107 ictx.setIndent(indent); 108 109 } catch (IOException e) { 110 throw new JiBXException("Error writing to document", e); 111 } 112 } 113 } 114 115 118 119 public boolean isPresent(IUnmarshallingContext ctx) throws JiBXException { 120 if (!(ctx instanceof UnmarshallingContext)) { 121 throw new JiBXException 122 ("Unmarshalling context not of expected type"); 123 } else { 124 return ((UnmarshallingContext)ctx).currentEvent() != 125 UnmarshallingContext.END_TAG; 126 } 127 } 128 129 133 134 public Object unmarshal(Object obj, IUnmarshallingContext ictx) 135 throws JiBXException { 136 137 boolean created = false; 139 List list = null; 140 if (obj == null) { 141 list = new ArrayList (); 142 created = true; 143 } else if (obj instanceof List ) { 144 list = (List )obj; 145 } else { 146 throw new JiBXException("Supplied object is not a java.util.List"); 147 } 148 if (!(ictx instanceof UnmarshallingContext)) { 149 throw new JiBXException 150 ("Unmarshalling context not of expected type"); 151 } 152 153 m_unmarshalContext = (UnmarshallingContext)ictx; 155 try { 156 Node node; 157 while ((node = unmarshalNode()) != null) { 158 list.add(node); 159 } 160 if (created && list.isEmpty()) { 161 return null; 162 } else { 163 return list; 164 } 165 } catch (IOException e) { 166 throw new JiBXException("Error reading from document", e); 167 } 168 } 169 } | Popular Tags |