1
2
3
4
5
6
7
8 package net.sf.jour.config.impl.runtime;
9
10 import javax.xml.bind.ValidationEvent;
11 import javax.xml.bind.helpers.PrintConversionEventImpl;
12 import javax.xml.bind.helpers.ValidationEventImpl;
13 import javax.xml.bind.helpers.ValidationEventLocatorImpl;
14
15 import org.xml.sax.SAXException;
16
17 import com.sun.xml.bind.Messages;
18 import com.sun.xml.bind.serializer.AbortSerializationException;
19
20 /***
21 *
22 * @author
23 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
24 */
25 public class Util {
26 /***
27 * Reports a print conversion error while marshalling.
28 */
29 public static void handlePrintConversionException(
30 Object caller, Exception e, XMLSerializer serializer ) throws SAXException {
31
32 if( e instanceof SAXException )
33
34
35
36 throw (SAXException)e;
37
38 ValidationEvent ve = new PrintConversionEventImpl(
39 ValidationEvent.ERROR, e.getMessage(),
40 new ValidationEventLocatorImpl(caller), e );
41 serializer.reportError(ve);
42 }
43
44 /***
45 * Reports that the type of an object in a property is unexpected.
46 */
47 public static void handleTypeMismatchError( XMLSerializer serializer,
48 Object parentObject, String fieldName, Object childObject ) throws AbortSerializationException {
49
50 ValidationEvent ve = new ValidationEventImpl(
51 ValidationEvent.ERROR,
52 Messages.format(Messages.ERR_TYPE_MISMATCH,
53 getUserFriendlyTypeName(parentObject),
54 fieldName,
55 getUserFriendlyTypeName(childObject) ),
56 new ValidationEventLocatorImpl(parentObject) );
57
58 serializer.reportError(ve);
59 }
60
61 private static String getUserFriendlyTypeName( Object o ) {
62 if( o instanceof ValidatableObject )
63 return ((ValidatableObject)o).getPrimaryInterface().getName();
64 else
65 return o.getClass().getName();
66 }
67 }