1
2
3
4
5
6
7
8 package org.jdtaus.mojo.resource.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
24
25
26 public class Util {
27
28
29
30 public static void handlePrintConversionException(
31 Object caller, Exception e, XMLSerializer serializer ) throws SAXException {
32
33 if( e instanceof SAXException )
34
35
36
37 throw (SAXException)e;
38
39 String message = e.getMessage();
40 if(message==null) {
41 message = e.toString();
42 }
43
44 ValidationEvent ve = new PrintConversionEventImpl(
45 ValidationEvent.ERROR, message,
46 new ValidationEventLocatorImpl(caller), e );
47 serializer.reportError(ve);
48 }
49
50
51
52
53 public static void handleTypeMismatchError( XMLSerializer serializer,
54 Object parentObject, String fieldName, Object childObject ) throws AbortSerializationException {
55
56 ValidationEvent ve = new ValidationEventImpl(
57 ValidationEvent.ERROR,
58 Messages.format(Messages.ERR_TYPE_MISMATCH,
59 getUserFriendlyTypeName(parentObject),
60 fieldName,
61 getUserFriendlyTypeName(childObject) ),
62 new ValidationEventLocatorExImpl(parentObject,fieldName) );
63
64 serializer.reportError(ve);
65 }
66
67 private static String getUserFriendlyTypeName( Object o ) {
68 if( o instanceof ValidatableObject )
69 return ((ValidatableObject)o).getPrimaryInterface().getName();
70 else
71 return o.getClass().getName();
72 }
73 }