Hi Sandeep,
You'd basically just want to change the structure of the XML handled by
the code to use a separate child element (rather than an attribute) for
the key value. I don't have the time to run through the details of this,
but if you look through the code it shouldn't be too hard to understand.
http://jibx.sourceforge.net/tutorial/binding-custom.html#marunmar has an
example of using the code as supplied.
- Dennis
Sandeep Khanna wrote:
Hi All,
Referring to
http://jibx.sourceforge.net/api/org/jibx/extras/HashMapperStringToComplex.html
What would be the:
1. sample Jibx mapping snippet
2. Code changes
to achieve marshall/unmarshall the following XML representation:
<?xml version="1.0" encoding="UTF-8"?>
<map-name size="3">
<entry>
<key>38193</key>
<value state="WA" zip="98059">
<name first-name="John" last-name="Smith"/>
<street>12345 Happy Lane</street>
<city>Plunk</city>
</value>
</entry>
<entry>
<key>39122</key>
<value state="WA" zip="98094">
<name first-name="Sally" last-name="Port"/>
<street>932 Easy Street</street>
<city>Fort Lewis</city>
</value>
</entry>
<entry>
<key>83132</key>
<value state="WA" zip="98059">
<name first-name="Mary" last-name="Smith"/>
<street>12345 Happy Lane</street>
<city>Plunk</city>
</value>
</entry>
</map-name>
Not that I fully understand every bit in the following code but here is
what I have so far:
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.log4j.Logger;
import org.jibx.runtime.IAliasable;
import org.jibx.runtime.IMarshallable;
import org.jibx.runtime.IMarshaller;
import org.jibx.runtime.IMarshallingContext;
import org.jibx.runtime.IUnmarshaller;
import org.jibx.runtime.IUnmarshallingContext;
import org.jibx.runtime.JiBXException;
import org.jibx.runtime.impl.MarshallingContext;
import org.jibx.runtime.impl.UnmarshallingContext;
public class JibxHashMapHandler implements IMarshaller, IUnmarshaller,
IAliasable {
private Logger logger = Logger.getRootLogger();
private static final String SIZE_ATTRIBUTE_NAME = "size";
private static final String VALUE_ELEMENT_NAME = "value";
private static final String KEY_ELEMENT_NAME = "key";
private static final int DEFAULT_SIZE = 10;
private String m_uri;
private int m_index;
private String m_name;
public JibxHashMapHandler() {
logger.debug( "in here" );
m_uri = null;
m_index = 0;
m_name = "hashmap";
}
public JibxHashMapHandler( String uri, int index, String name ) {
logger.debug( "new JibxHashMapHandler( " + uri + ", " + index + ",
" + name + " )" );
m_uri = uri;
m_index = index;
m_name = name;
}
/**
* Check if marshaller represents an extension mapping. This is used
by the
* framework in generated code to verify compatibility of objects
being
* marshalled using an abstract mapping.
*
* @param index
* abstract mapping index to be checked
* @return <code>true</code> if this mapping is an extension of the
* abstract mapping, <code>false</code> if not
*/
public boolean isExtension( int arg0 ) {
logger.debug( "in here" );
return false;
}
/**
* Check if instance present in XML. This method can be called when
the
* unmarshalling context is positioned at or just before the start of
the
* data corresponding to an instance of this mapping. It verifies
that the
* expected data is present.
*
* @param ctx
* unmarshalling context
* @return <code>true</code> if expected parse data found,
* <code>false</code> if not
* @throws JiBXException
* on error in unmarshalling process
*/
public boolean isPresent( IUnmarshallingContext ctx ) throws
JiBXException {
logger.debug( "in here" );
return ctx.isAt(m_uri, m_name);
}
/**
* Marshal instance of handled class. This method call is responsible
for
* all handling of the marshalling of an object to XML text. It is
called at
* the point where the start tag for the associated element should be
* generated.
*
* @param obj
* object to be marshalled (may be <code>null</code> if
* property is not optional)
* @param ctx
* XML text output context
* @throws JiBXException
* on error in marshalling process
*/
public void marshal( Object obj, IMarshallingContext ictx ) throws
JiBXException {
logger.debug( "Marshalling ..." );
// make sure the parameters are as expected
if( !( obj instanceof HashMap ) ) {
throw new JiBXException( "Invalid object type for
marshaller" );
}
else if( !( ictx instanceof MarshallingContext ) ) {
throw new JiBXException( "Invalid object type for
marshaller" );
}
else {
// start by generating start tag for container
MarshallingContext ctx = ( MarshallingContext )ictx;
HashMap map = ( HashMap )obj;
ctx.startTagAttributes( m_index, m_name ).attribute( m_index,
SIZE_ATTRIBUTE_NAME, map.size() ).closeStartContent();
// loop through all entries in hashmap
Iterator iter = map.entrySet().iterator();
while( iter.hasNext() ) {
Map.Entry entry = ( Map.Entry )iter.next();
if( entry.getKey() != null ) {
ctx.element( m_index, KEY_ELEMENT_NAME,
entry.getKey().toString() );
}
if( entry.getValue() instanceof IMarshallable ) {
( ( IMarshallable )entry.getValue() ).marshal( ctx );
ctx.endTag( m_index, VALUE_ELEMENT_NAME );
}
else {
throw new JiBXException( "Mapped value is not
marshallable" );
}
}
// finish with end tag for container element
ctx.endTag( m_index, m_name );
}
}
/**
* Unmarshal instance of handled class. This method call is
responsible for
* all handling of the unmarshalling of an object from XML text,
including
* creating the instance of the handled class if an instance is not
* supplied. When it is called the unmarshalling context is always
* positioned at or just before the start tag corresponding to the
start of
* the class data.
*
* @param obj
* object to be unmarshalled (may be <code>null</code>)
* @param ctx
* unmarshalling context
* @return unmarshalled object (may be <code>null</code>)
* @throws JiBXException
* on error in unmarshalling process
*/
public Object unmarshal( Object obj, IUnmarshallingContext ictx )
throws JiBXException {
logger.debug( "Unmarshalling ..." );
// make sure we're at the appropriate start tag
UnmarshallingContext ctx = ( UnmarshallingContext )ictx;
if( !ctx.isAt( m_uri, m_name ) ) {
ctx.throwStartTagNameError( m_uri, m_name );
}
// create new hashmap if needed
int size = ctx.attributeInt( m_uri, SIZE_ATTRIBUTE_NAME,
DEFAULT_SIZE );
HashMap map = ( HashMap )obj;
if( map == null ) {
map = new HashMap( size );
}
// process all entries present in document
ctx.parsePastStartTag( m_uri, m_name );
while( ctx.isAt( m_uri, VALUE_ELEMENT_NAME ) ) {
Object key = ctx.attributeText( m_uri, KEY_ELEMENT_NAME,
null );
ctx.parsePastStartTag( m_uri, VALUE_ELEMENT_NAME );
Object value = ctx.unmarshalElement();
map.put( key, value );
ctx.parsePastEndTag( m_uri, VALUE_ELEMENT_NAME );
}
ctx.parsePastEndTag( m_uri, m_name );
return map;
}
}
Any help would be appreciated.
--Sandeep Khanna
-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems? Stop! Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
jibx-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jibx-users
-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems? Stop! Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
jibx-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jibx-users