Hi,
I hope this email isn't too big!! :-)
I'm trying to marshal/unmarshal a Map where the values are string[]'s. The string[]'s could be empty when marshalled, and I would expect that when I ask for a value of a key that had an empty array when marshalled, i'd get back an empty array when unmarshalling, correct? Well the marshalling works and writes out the file, but the program blows up when unmarshalling with a stacktrace.
I've included the stacktrace, the sample main method of the Test class, the Root.java source, and the mapping file and the result file for you to try out on your own.. Am I misunderstanding something??? I'm using castor-0.9.6-xml.jar...
// BEGIN STACKTRACE unable to resolve reference: [EMAIL PROTECTED]: [not available]; line: 2; column: 257} at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:671) at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:565) at Test.main(Test.java:58) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39 ) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl .java:25) at java.lang.reflect.Method.invoke(Method.java:324) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:78) Caused by: ValidationException: unable to resolve reference: [EMAIL PROTECTED] at org.exolab.castor.xml.UnmarshalHandler.endElement(UnmarshalHandler.java:862) at org.exolab.castor.xml.UnmarshalHandler.endElement(UnmarshalHandler.java:1073 ) at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatc her.dispatch(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XMLParser.parse(Unknown Source) at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source) at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:657) ... 7 more Caused by: ValidationException: unable to resolve reference: [EMAIL PROTECTED] at org.exolab.castor.xml.UnmarshalHandler.endElement(UnmarshalHandler.java:862) at org.exolab.castor.xml.UnmarshalHandler.endElement(UnmarshalHandler.java:1073 ) at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatc her.dispatch(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XMLParser.parse(Unknown Source) at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source) at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:657) at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:565) at Test.main(Test.java:58) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39 ) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl .java:25) at java.lang.reflect.Method.invoke(Method.java:324) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:78) Caused by: ValidationException: unable to resolve reference: [EMAIL PROTECTED] at org.exolab.castor.xml.UnmarshalHandler.endElement(UnmarshalHandler.java:862) at org.exolab.castor.xml.UnmarshalHandler.endElement(UnmarshalHandler.java:1073 ) at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatc her.dispatch(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XMLParser.parse(Unknown Source) at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source) at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:657) at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:565) at Test.main(Test.java:58) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39 ) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl .java:25) at java.lang.reflect.Method.invoke(Method.java:324) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:78)
// END STACKTRACE
// SAMPLE CODE
public static void main(String[] args) {
try { // Set the mapping
Mapping map = new Mapping();
URL url =
Thread.currentThread().getContextClassLoader().getResource("mapping.xml");
map.loadMapping(url); // Create a root object
Root root = new Root(); // Create the locales
Locale[] locales = new Locale[2];
locales[0] = new Locale("en", "US");
locales[1] = new Locale("fr", "FR"); // Set the locales
root.setLocales(locales); Map mappings = new HashMap();
mappings.put("Everyone", new String[0]);
mappings.put("Admins", new String[] {"1", "2", "3"}); root.setRoleMappings(mappings);
System.out.println("Marshalling:");
System.out.println(root); Writer w = new FileWriter("locales.xml");
Marshaller m = new Marshaller(w);
m.setMapping(map);
m.marshal(root);
w.close(); // Read the file back
System.out.println("Unmarshalling:");
System.out.println(); Reader reader = new FileReader("locales.xml");
Unmarshaller unmarshaller = new Unmarshaller(Root.class);
unmarshaller.setMapping(map);
root = (Root)unmarshaller.unmarshal(reader);
reader.close(); System.out.println("Results: " + root);} catch (Exception e) {
e.printStackTrace();
}
}
// END SAMPLE
// BEGIN MAPPING FILE <?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 file for Configuration Object</description> <class name="Root"> <map-to xml="root-object"/>
<!-- Map the locales information -->
<field name="locales" type="locale" collection="array">
<bind-xml name="locale" location="locales"/>
</field> <field name="roleMappings" collection="map">
<bind-xml name="mapping" location="role-mappings">
<class name="org.exolab.castor.mapping.MapItem">
<field name="key" type="string">
<bind-xml name="name" node="attribute"/>
</field>
<field name="value" type="string" collection="array">
<bind-xml node="element"/>
</field>
</class>
</bind-xml>
</field>
</class>
</mapping>// END MAPPING FILE
// BEGIN SAMPLE FILE <?xml version="1.0" encoding="UTF-8"?> <root-object> <locales> <locale country="US" language="en"/> <locale country="FR" language="fr"/> </locales> <role-mappings> <mapping name="Admins"> <value>1</value> <value>2</value> <value>3</value> </mapping> <mapping name="Everyone"/> </role-mappings> </root-object> // END SAMPLE FILE
// ROOT.JAVA
import java.util.*;
public class Root {private Locale[] _locales; private Map roleMappings;
public Map getRoleMappings() {
return roleMappings;
} public void setRoleMappings(Map roleMappings) {
this.roleMappings = roleMappings;
} public Locale[] getLocales() {
return _locales;
} public void setLocales(Locale[] locales) {
_locales = locales;
}
public String toString() { final StringBuffer buf = new StringBuffer(); buf.append("Root"); buf.append("{_locales=").append(_locales == null ? "null" : Arrays.asList(_locales).toString()); buf.append(",roleMappings=").append(roleMappings); buf.append('}'); return buf.toString(); } } //-- class: Root // END ROOT.JAVA
Tim Mull� Senior Software Developer Lighthammer
(610)-903-8000 x117 [EMAIL PROTECTED]
----------------------------------------------------------- If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-user
