1 28 29 package org.jibx.extras; 30 31 import java.io.IOException ; 32 33 import org.jibx.runtime.IAliasable; 34 import org.jibx.runtime.IMarshaller; 35 import org.jibx.runtime.IMarshallingContext; 36 import org.jibx.runtime.IUnmarshaller; 37 import org.jibx.runtime.IUnmarshallingContext; 38 import org.jibx.runtime.JiBXException; 39 import org.jibx.runtime.impl.UnmarshallingContext; 40 import org.w3c.dom.Element ; 41 42 55 56 public class DomElementMapper extends DomMapperBase 57 implements IMarshaller, IUnmarshaller, IAliasable 58 { 59 60 private final String m_uri; 61 62 63 private final int m_index; 64 65 66 private final String m_name; 67 68 73 74 public DomElementMapper() throws JiBXException { 75 m_uri = null; 76 m_index = -1; 77 m_name = null; 78 } 79 80 91 92 public DomElementMapper(String uri, int index, String name) 93 throws JiBXException { 94 95 m_uri = uri; 97 m_index = index; 98 m_name = name; 99 } 100 101 104 105 public boolean isExtension(int index) { 106 return false; 107 } 108 109 113 114 public void marshal(Object obj, IMarshallingContext ictx) 115 throws JiBXException { 116 117 if (!(obj instanceof Element )) { 119 throw new JiBXException("Mapped object not an org.w3c.dom.Element"); 120 } else { 121 try { 122 123 m_xmlWriter = ictx.getXmlWriter(); 125 m_xmlWriter.indent(); 126 int indent = ictx.getIndent(); 127 ictx.setIndent(-1); 128 m_defaultNamespaceURI = null; 129 marshalElement((Element )obj); 130 ictx.setIndent(indent); 131 132 } catch (IOException e) { 133 throw new JiBXException("Error writing to document", e); 134 } 135 } 136 } 137 138 141 142 public boolean isPresent(IUnmarshallingContext ctx) throws JiBXException { 143 if (m_name == null) { 144 if (!(ctx instanceof UnmarshallingContext)) { 145 throw new JiBXException 146 ("Unmarshalling context not of expected type"); 147 } else { 148 return !((UnmarshallingContext)ctx).isEnd(); 149 } 150 } else { 151 return ctx.isAt(m_uri, m_name); 152 } 153 } 154 155 159 160 public Object unmarshal(Object obj, IUnmarshallingContext ictx) 161 throws JiBXException { 162 163 if (!(ictx instanceof UnmarshallingContext)) { 165 throw new JiBXException 166 ("Unmarshalling context not of expected type"); 167 } else if (m_name != null && !ictx.isAt(m_uri, m_name)) { 168 ((UnmarshallingContext)ictx).throwStartTagNameError(m_uri, m_name); 169 } 170 171 m_unmarshalContext = (UnmarshallingContext)ictx; 173 m_unmarshalContext.toStart(); 174 175 try { 177 return unmarshalElement(); 178 } catch (IOException e) { 179 throw new JiBXException("Error reading from document", e); 180 } 181 } 182 } | Popular Tags |