KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > tools > xjc > runtime > Util


1 package com.sun.tools.xjc.runtime;
2
3 import javax.xml.bind.ValidationEvent;
4 import javax.xml.bind.helpers.PrintConversionEventImpl;
5 import javax.xml.bind.helpers.ValidationEventImpl;
6 import javax.xml.bind.helpers.ValidationEventLocatorImpl;
7
8 import org.xml.sax.SAXException JavaDoc;
9
10 import com.sun.xml.bind.Messages;
11 import com.sun.xml.bind.serializer.AbortSerializationException;
12 import com.sun.xml.bind.util.ValidationEventLocatorExImpl;
13
14 /**
15  *
16  * @author
17  * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
18  */

19 public class Util {
20 // META-IF(M|V)
21
/**
22      * Reports a print conversion error while marshalling.
23      */

24     public static void handlePrintConversionException(
25         Object JavaDoc caller, Exception JavaDoc e, XMLSerializer serializer ) throws SAXException JavaDoc {
26         
27         if( e instanceof SAXException JavaDoc )
28             // assume this exception is not from application.
29
// (e.g., when a marshaller aborts the processing, this exception
30
// will be thrown)
31
throw (SAXException JavaDoc)e;
32         
33         String JavaDoc message = e.getMessage();
34         if(message==null) {
35             message = e.toString();
36         }
37         
38         ValidationEvent ve = new PrintConversionEventImpl(
39             ValidationEvent.ERROR, message,
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 JavaDoc parentObject, String JavaDoc fieldName, Object JavaDoc childObject ) throws AbortSerializationException {
49         
50          ValidationEvent ve = new ValidationEventImpl(
51             ValidationEvent.ERROR, // maybe it should be a fatal error.
52
Messages.format(Messages.ERR_TYPE_MISMATCH,
53                 getUserFriendlyTypeName(parentObject),
54                 fieldName,
55                 getUserFriendlyTypeName(childObject) ),
56             new ValidationEventLocatorExImpl(parentObject,fieldName) );
57          
58         serializer.reportError(ve);
59     }
60     
61     private static String JavaDoc getUserFriendlyTypeName( Object JavaDoc o ) {
62 // META-IF(V)
63
if( o instanceof ValidatableObject )
64             return ((ValidatableObject)o).getPrimaryInterface().getName();
65         else
66 // META-ENDIF
67
return o.getClass().getName();
68     }
69 // META-ENDIF
70
}
71
Popular Tags