public void marshal(Object obj, IMarshallingContext ictx)
throws JiBXException {
// make sure the parameters are as expected
if (!(obj instanceof Map)) {
throw new JiBXException("Invalid object type for marshaller");
} else if (!(ictx instanceof MarshallingContext)) {
throw new JiBXException("Invalid object type for marshaller");
} else {
// start by setting up added namespaces
MarshallingContext ctx = (MarshallingContext)ictx;
IXMLWriter xwrite = ctx.getXmlWriter();
int ixsi = xwrite.getNamespaces().length;
String[][] extens = xwrite.getExtensionNamespaces();
if (extens != null) {
for (int i = 0; i < extens.length; i++) {
ixsi += extens[i].length;
}
}
xwrite.pushExtensionNamespaces(SCHEMA_NAMESPACE_URIS);
// generate start tag for containing element
Map map = (Map)obj;
ctx.startTagNamespaces(m_index, m_name, new int[] { ixsi, ixsi+1 },
SCHEMA_NAMESPACE_PREFIXES).
attribute(m_index, SIZE_ATTRIBUTE_NAME, map.size()).
closeStartContent();
// loop through all entries in hashmap
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
// first make sure we have a value
Map.Entry entry = (Map.Entry)iter.next();
Object value = entry.getValue();
if (value != null) {
// write element with key attribute
ctx.startTagAttributes(m_index, ENTRY_ELEMENT_NAME);
ctx.attribute(m_index, KEY_ATTRIBUTE_NAME,
entry.getKey().toString());
// translate value object class to schema type
String tname = value.getClass().getName();
int type = s_javaTypesEnum.getValue(tname);
if (type < 0) {
throw new JiBXException("Value of type " + tname +
" with key " + entry.getKey() +
" is not a supported type");
}
// generate xsi:type attribute for value