1
2
3
4
5
6
7
8 package de.nierbeck.timeTrack.model.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 import com.sun.xml.bind.util.ValidationEventLocatorExImpl;
20
21 /***
22 *
23 * @author 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(Object caller,
30 Exception e, XMLSerializer serializer) throws SAXException {
31
32 if (e instanceof SAXException)
33
34
35
36 throw (SAXException) e;
37
38 String message = e.getMessage();
39 if (message == null) {
40 message = e.toString();
41 }
42
43 ValidationEvent ve = new PrintConversionEventImpl(
44 ValidationEvent.ERROR, message, new ValidationEventLocatorImpl(
45 caller), e);
46 serializer.reportError(ve);
47 }
48
49 /***
50 * Reports that the type of an object in a property is unexpected.
51 */
52 public static void handleTypeMismatchError(XMLSerializer serializer,
53 Object parentObject, String fieldName, Object childObject)
54 throws AbortSerializationException {
55
56 ValidationEvent ve = new ValidationEventImpl(ValidationEvent.ERROR,
57
58
59
60
61
62 Messages.format(Messages.ERR_TYPE_MISMATCH,
63 getUserFriendlyTypeName(parentObject), fieldName,
64 getUserFriendlyTypeName(childObject)),
65 new ValidationEventLocatorExImpl(parentObject, fieldName));
66
67 serializer.reportError(ve);
68 }
69
70 private static String getUserFriendlyTypeName(Object o) {
71 if (o instanceof ValidatableObject)
72 return ((ValidatableObject) o).getPrimaryInterface().getName();
73 else
74 return o.getClass().getName();
75 }
76 }