1 28 29 package org.jibx.extras; 30 31 import org.jibx.runtime.IAliasable; 32 import org.jibx.runtime.IMarshaller; 33 import org.jibx.runtime.IMarshallingContext; 34 import org.jibx.runtime.IUnmarshaller; 35 import org.jibx.runtime.IUnmarshallingContext; 36 import org.jibx.runtime.JiBXException; 37 import org.jibx.runtime.impl.MarshallingContext; 38 import org.jibx.runtime.impl.UnmarshallingContext; 39 40 56 57 public abstract class IdRefMapperBase 58 implements IMarshaller, IUnmarshaller, IAliasable { 59 60 private String m_uri; 61 private int m_index; 62 private String m_name; 63 64 74 public IdRefMapperBase(String uri, int index, String name) { 75 m_uri = uri; 76 m_index = index; 77 m_name = name; 78 } 79 80 85 protected abstract String getIdValue(Object item); 86 87 91 protected String getAttributeName() { 92 return "ref"; 93 } 94 95 98 public boolean isExtension(int index) { 99 return false; 100 } 101 102 106 public void marshal(Object obj, IMarshallingContext ictx) 107 throws JiBXException { 108 109 if (obj == null) { 111 return; 112 } else if (!(ictx instanceof MarshallingContext)) { 113 throw new JiBXException("Invalid context type for marshaller"); 114 } else { 115 116 MarshallingContext ctx = (MarshallingContext)ictx; 118 ctx.startTagAttributes(m_index, m_name); 119 120 ctx.attribute(0, getAttributeName(), getIdValue(obj)); 122 123 ctx.closeStartEmpty(); 125 } 126 } 127 128 131 public boolean isPresent(IUnmarshallingContext ctx) throws JiBXException { 132 return ctx.isAt(m_uri, m_name); 133 } 134 135 139 public Object unmarshal(Object obj, IUnmarshallingContext ictx) 140 throws JiBXException { 141 142 UnmarshallingContext ctx = (UnmarshallingContext)ictx; 144 if (!ctx.isAt(m_uri, m_name)) { 145 return null; 146 } 147 148 obj = ctx.attributeExistingIDREF(null, getAttributeName(), 0); 150 151 ctx.parsePastEndTag(m_uri, m_name); 153 return obj; 154 } 155 } | Popular Tags |