The problem with the second method you tried is a quirk of how JiBX structures the added code. If you reorder the two <mapping> items, so that the HashMap mapping comes after the HelpReference mapping, and then change the line:
IBindingFactory bfact = BindingDirectory.getFactory(HashMap.class);
to
IBindingFactory bfact = BindingDirectory.getFactory(HelpReference.class);
this should also work. I realize that's sort of a black magic solution. I'm trying to handle this more cleanly in the beta 4 code, where it'll avoid the ordering issue with <mapping> and give you an explicit error message when you pass a system class to the BindingDirectory.getFactory() method.
- Dennis
Gilles Devaux wrote:
Hi,
I don't know what's going wrong with your first approach but the second one should work if you add a mapping for the com.quest.wcf.core.services.HelpServices class :
<mapping name="help-services" class="com.quest.wcf.core.services.HelpServices>
<structure field="help"/> (I'm doing the same thing using get / set methods, using field should be OK)
</mapping>
the class of the "help" field if java.util.HashMap and will be marshalled / unmarshalled with what you defined in the <mapping> for this class.
------------------------------------------------------------------------
*De :* [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] *De la part de* Dan Cooperstock
*Envoy� :* mercredi 22 d�cembre 2004 19:18
*� :* [email protected]
*Objet :* [jibx-users] HashMap not working during jibxbind
I'm trying to map a class that has only one, static, element, which is a HashMap from String to another class.
I have the following binding file:
<binding name="help" value-style="attribute">
<mapping name="help-services" class="com.quest.wcf.core.services.HelpServices">
<structure field="help"
marshaller="org.jibx.extras.HashMapperStringToComplex"
unmarshaller="org.jibx.extras.HashMapperStringToComplex"/>
</mapping>
<mapping name="help-reference"
class="com.quest.wcf.core.services.HelpReference">
<value name="url" field="URL"/>
<value name="map-id" field="mapId"/>
</mapping>
</binding>
Here is the core part of the HelpServices class:
public class HelpServices {
private static HashMap help = new HashMap();
}
The mapped objects are HelpReferences, and the keys are Strings. Here is the core part of the HelpReference class:
public class HelpReference {
private String URL;
private int mapId;
}
When jibxbind runs, I'm getting the following error message:
java.lang.IllegalStateException: Internal error: Expected java.util.HashMap on stack, found com.quest.wcf.core.services.HelpServices
full stack:
0: com.quest.wcf.core.services.HelpServices
1: com.quest.wcf.core.services.HelpServices
Any bright ideas on what I'm doing wrong? Does it have anything to do with the fact that the HashMap element is static? It's really only the HashMap element that I want to marshall / unmarshall, since the class isn't even intended to have any instances.
WAIT ... I'm trying something different. Now my binding is as follows, and jibxbind works OK:
<binding name="help" value-style="attribute">
<mapping name="help" class="java.util.HashMap"
marshaller="org.jibx.extras.HashMapperStringToComplex"
unmarshaller="org.jibx.extras.HashMapperStringToComplex"/>
<mapping name="help-reference"
class="com.quest.wcf.core.services.HelpReference">
<value name="url" field="URL"/>
<value name="map-id" field="mapId"/>
</mapping>
</binding>
But when I run the following code:
private static HashMap help = new HashMap();
static {
try {
IBindingFactory bfact = BindingDirectory.getFactory(HashMap.class);
IMarshallingContext mctx = bfact.createMarshallingContext();
mctx.setIndent(4);
mctx.marshalDocument(help, "UTF-8", null,
new FileOutputStream("HelpReferences.xml"));
}
catch (JiBXException je) {
System.out.println("JiBXException marshalling HelpReferences: " +
je.getMessage());
}
catch (FileNotFoundException fnfe) {
System.out.println("FileNotFoundException marshalling HelpReferences: " +
fnfe.getMessage());
}
}
what happens is the JiBXException is thrown, with the message:
Unable to access binding information for class java.util.HashMap
Make sure the binding has been compiled
Any bright ideas about that? Which approach is more correct?
Thanks.
Dan Cooperstock, Senior Software Developer, Quest Software
[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> 416-933-5165
------------------------------------------------------- The SF.Net email is sponsored by: Beat the post-holiday blues Get a FREE limited edition SourceForge.net t-shirt from ThinkGeek. It's fun and FREE -- well, almost....http://www.thinkgeek.com/sfshirt _______________________________________________ jibx-users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/jibx-users
