I'm using castor to store xml files and started with the example given in the xml-mapping documentation. I have a simple situation with two classes (User and Contact). When I try to create an Unmarshaller, it chokes, and sends:
org.exolab.castor.mapping.MappingException: Nested error: org.exolab.castor.mapping.MappingException: Could not find the class User
org.exolab.castor.mapping.MappingException: Could not find the class User
at org.exolab.castor.mapping.loader.MappingLoader.createDescriptor(MappingLoader.java:341)
at org.exolab.castor.xml.XMLMappingLoader.createDescriptor(XMLMappingLoader.java:192)
at org.exolab.castor.mapping.loader.MappingLoader.loadMapping(MappingLoader.java:233)
at org.exolab.castor.mapping.Mapping.getResolver(Mapping.java:291)
at org.exolab.castor.mapping.Mapping.getResolver(Mapping.java:246)
at org.exolab.castor.xml.Unmarshaller.setMapping(Unmarshaller.java:410)
at org.exolab.castor.xml.Unmarshaller.<init>(Unmarshaller.java:227)
at main.main(main.java:22)
Although I'm using WebObjects and NSMutableArray in the WO collection, this is not the problem. I guess that my mapping.xml is the problem, but didn't find why... Of course, all files are in the same directory, so classpath is not the issue.
Got an idea for me?
Here are the files:
main.java (mostly the same as doc example):
import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.Marshaller;
import java.io.IOException;
import java.io.FileReader;
import java.io.OutputStreamWriter;
import org.xml.sax.InputSource;
public class main {
public static void main(String args[]) {
Mapping mapping = new Mapping();
try {
mapping.loadMapping( "mapping.xml" );
System.out.println("Mapping loaded");
Unmarshaller unmar = new Unmarshaller(mapping);
System.out.println("UnMarshaller created");
User myUser = (User)unmar.unmarshal(new InputSource(new FileReader("user1.xml")));
System.out.println(""+myUser.mail() + myUser.signature());
myUser.setMail("[EMAIL PROTECTED]");
Marshaller marshaller = new Marshaller(new OutputStreamWriter(System.out));
marshaller.setMapping(mapping);
marshaller.marshal(myUser);
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
return;
}
}
}
-----------------------------
mapping.xml
------------------------------
<?xml version="1.0"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Object Mapping DTD Version 1.0//EN"
"http://castor.exolab.org/mapping.dtd">
<mapping>
<description>Mapping of a user object to save preferences and contacts</description>
<class name="User">
<map-to xml="User"/>
<field name="mail" get-method="mail"
type="string">
<bind-xml name="mail_address" node="attribute"/>
</field>
<field name="signature" get-method="signature"
type="string">
<bind-xml name="signature" node="attribute"/>
</field>
<field name="autoreplyMessage" get-method="autoreplyMessage"
type="string">
<bind-xml name="autoReplyMessage" node="attribute"/>
</field>
<field name="popHost"
get-method="popHost"
type="string">
<bind-xml name="popHost" node="attribute"/>
</field>
<field name="popUser"
get-method="popUser"
type="string">
<bind-xml name="popUser" node="attribute"/>
</field>
<field name="popPassword"
get-method="popPassword"
type="string">
<bind-xml name="popPassword" node="attribute"/>
</field>
<field name="login"
get-method="login"
type="string">
<bind-xml name="login" node="attribute"/>
</field>
<field name="sentItemsDestination"
get-method="sentItemsDestination"
type="string">
<bind-xml name="sentItemsDestination" node="attribute"/>
</field>
<field name="deletedItemsDestination"
get-method="deletedItemsDestination"
type="string">
<bind-xml name="deletedItemsDestination" node="attribute"/>
</field>
<field name="sortOrder"
get-method="sortOrder"
type="string">
<bind-xml name="sortOrder" node="attribute"/>
</field>
<field name="autoReply"
get-method="autoReply"
type="boolean">
<bind-xml name="autoReply" node="attribute"/>
</field>
<field name="numberOfMessagesPerPage"
get-method="numberOfMessagesPerPage"
type="integer">
<bind-xml name="numberOfMessagesPerPage" node="attribute"/>
</field>
<field name="saveSentItems"
get-method="saveSentItems"
type="boolean">
<bind-xml name="saveSentItems" node="attribute"/>
</field>
<field name="saveDeletedItems"
get-method="saveDeletedItems"
type="boolean">
<bind-xml name="saveDeletedItems" node="attribute"/>
</field>
<field name="includeMessageInReply"
get-method="includeMessageInReply"
type="boolean">
<bind-xml name="includeMessageInReply" node="attribute"/>
</field>
<field name="importHost"
get-method="importHost"
type="boolean">
<bind-xml name="importHost" node="attribute"/>
</field>
<field name="orderAsc"
get-method="orderAsc"
type="boolean">
<bind-xml name="orderAsc" node="attribute"/>
</field>
<field name="Contact"
type="Contact"
get-method="contactsAsArray"
collection="array">
<bind-xml name="Contact"/>
</field>
</class>
<class name="Contact">
<map-to xml="Contact"/>
<field name="firstName" get-method="firstName"
type="string">
<bind-xml name="firstName" node="attribute"/>
</field>
<field name="lastName" get-method="lastName"
type="string">
<bind-xml name="lastName" node="attribute"/>
</field>
<field name="email" get-method="email"
type="string">
<bind-xml name="email" node="attribute"/>
</field>
<field name="alias" get-method="alias"
type="string">
<bind-xml name="alias" node="attribute"/>
</field>
</class>
</mapping>
----------------------------------
User.java
----------------------------------
import com.webobjects.foundation.*;
public class User {
private String _mail, _autoreplyMessage,
_popHost, _popUser, _popPassword, _signature,
_sentItemsDestination, _login, _sortOrder, _deletedItemsDestination;
private boolean _autoreply, _saveSentItems, _saveDeletedItems,
_includeMessageInReply, _importHost, _orderAsc;
private int _numberOfMessagesPerPage;
private NSMutableArray _contacts;
public User() {
super();
}
public String mail() {
return _mail;
}
public void setMail(String value) {
_mail=value;
}
public String signature() {
return _signature;
}
public void setSignature(String value) {
_signature=value;
}
public boolean autoreply() {
return _autoreply;
}
public void setAutoreply(boolean value) {
_autoreply=value;
}
public String autoreplyMessage() {
return _autoreplyMessage;
}
public void setAutoreplyMessage(String value) {
_autoreplyMessage=value;
}
public String popHost() {
return _popHost;
}
public void setPopHost(String value) {
_popHost=value;
}
public String popUser() {
return _popUser;
}
public void setPopUser(String value) {
_popUser=value;
}
public String popPassword() {
return _popPassword;
}
public void setPopPassword(String value) {
_popPassword=value;
}
public int numberOfMessagesPerPage() {
return _numberOfMessagesPerPage;
}
public void setNumberOfMessagesPerPage(int value) {
_numberOfMessagesPerPage=value;
}
public boolean saveSentItems() {
return _saveSentItems;
}
public void setSaveSentItems(boolean value) {
_saveSentItems=value;
}
public String sentItemsDestination() {
return _sentItemsDestination;
}
public void setSentItemsDestination(String value) {
_sentItemsDestination=value;
}
public boolean saveDeletedItems() {
return _saveDeletedItems;
}
public void setSaveDeletedItems(boolean value) {
_saveDeletedItems=value;
}
public String login() {
return _login;
}
public void setLogin(String value) {
_login=value;
}
public String deletedItemsDestination() {
return _deletedItemsDestination;
}
public void setDeletedItemsDestination(String value) {
_deletedItemsDestination=value;
}
public boolean includeMessageInReply() {
return _includeMessageInReply;
}
public void setIncludeMessageInReply(boolean value) {
_includeMessageInReply=value;
}
public boolean importHost() {
return _importHost;
}
public void setImportHost(boolean value) {
_importHost=value;
}
public String sortOrder() {
return _sortOrder;
}
public void setSortOrder(String value) {
_sortOrder=value;
}
public boolean orderAsc() {
return _orderAsc;
}
public void setOrderAsc(boolean value) {
_orderAsc=value;
}
public NSMutableArray contacts() {
return _contacts;
}
public Contact[] contactsAsArray() {
return (Contact[])_contacts.objects();
}
public void setContacts(Contact[] value) {
_contacts=new NSMutableArray(value);
}
public void addToContacts(Contact object) {
_contacts.addObject(object);
}
public void removeFromContacts(Contact object) {
NSMutableArray array = (NSMutableArray)contacts();
array.removeObject(object);
}
}
