KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > exceptions > ConversionException


1 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
2
package oracle.toplink.essentials.exceptions;
3
4 import java.util.*;
5 import oracle.toplink.essentials.exceptions.i18n.ExceptionMessageGenerator;
6
7 /**
8  * <P><B>Purpose</B>: Conversion exceptions such as method or class not defined will raise this exception.
9  */

10 public class ConversionException extends TopLinkException {
11     protected Class JavaDoc classToConvertTo;
12     protected transient Object JavaDoc sourceObject;
13     public final static int COULD_NOT_BE_CONVERTED = 3001;
14     public final static int COULD_NOT_BE_CONVERTED_EXTENDED = 3002;
15     public final static int INCORRECT_DATE_FORMAT = 3003;
16     public final static int INCORRECT_TIME_FORMAT = 3004;
17     public final static int INCORRECT_TIMESTAMP_FORMAT = 3005;
18     public final static int COULD_NOT_CONVERT_TO_BYTE_ARRAY = 3006;
19     public final static int COULD_NOT_BE_CONVERTED_TO_CLASS = 3007;
20     public final static int INCORRECT_DATE_TIME_FORMAT = 3008;
21
22     /**
23      * INTERNAL:
24      * TopLink exceptions should only be thrown by TopLink.
25      */

26     protected ConversionException(String JavaDoc message, Object JavaDoc sourceObject, Class JavaDoc classToConvertTo, Exception JavaDoc exception) {
27         super(message, exception);
28         setSourceObject(sourceObject);
29         setClassToConvertTo(classToConvertTo);
30     }
31
32     // Couldn't find a way of simply changing the message on an existing exception.
33
// therefore, create a new exception with appropriate message and port existing
34
// info (stack trace and internal exception over)
35
public static ConversionException couldNotBeConverted(Object JavaDoc mapping, Object JavaDoc descriptor, ConversionException exception) {
36         Object JavaDoc sourceObject = exception.getSourceObject();
37         Class JavaDoc javaClass = exception.getClassToConvertTo();
38         Exception JavaDoc original = (Exception JavaDoc)exception.getInternalException();
39
40         Object JavaDoc[] args = { sourceObject, sourceObject.getClass(), mapping, descriptor, javaClass };
41         String JavaDoc message = ExceptionMessageGenerator.buildMessage(ConversionException.class, COULD_NOT_BE_CONVERTED_EXTENDED, args);
42         ConversionException conversionException = new ConversionException(message, sourceObject, javaClass, original);
43         conversionException.setStackTrace(exception.getStackTrace());
44         conversionException.setErrorCode(COULD_NOT_BE_CONVERTED_EXTENDED);
45         return conversionException;
46     }
47
48     public static ConversionException couldNotBeConverted(Object JavaDoc object, Class JavaDoc javaClass) {
49         Object JavaDoc[] args = { object, object.getClass(), javaClass };
50         String JavaDoc message = ExceptionMessageGenerator.buildMessage(ConversionException.class, COULD_NOT_BE_CONVERTED, args);
51         ConversionException conversionException = new ConversionException(message, object, javaClass, null);
52         conversionException.setErrorCode(COULD_NOT_BE_CONVERTED);
53         return conversionException;
54     }
55
56     public static ConversionException couldNotBeConverted(Object JavaDoc object, Class JavaDoc javaClass, Exception JavaDoc exception) {
57         Object JavaDoc[] args = { object, object.getClass(), javaClass };
58         String JavaDoc message = ExceptionMessageGenerator.buildMessage(ConversionException.class, COULD_NOT_BE_CONVERTED, args);
59         ConversionException conversionException = new ConversionException(message, object, javaClass, exception);
60         conversionException.setErrorCode(COULD_NOT_BE_CONVERTED);
61         return conversionException;
62     }
63
64     public static ConversionException couldNotBeConvertedToClass(Object JavaDoc object, Class JavaDoc javaClass, Exception JavaDoc exception) {
65         Object JavaDoc[] args = { object, object.getClass(), javaClass };
66         String JavaDoc message = ExceptionMessageGenerator.buildMessage(ConversionException.class, COULD_NOT_BE_CONVERTED_TO_CLASS, args);
67         ConversionException conversionException = new ConversionException(message, object, javaClass, exception);
68         conversionException.setErrorCode(COULD_NOT_BE_CONVERTED_TO_CLASS);
69         return conversionException;
70     }
71
72     public static ConversionException couldNotConvertToByteArray(Object JavaDoc object) {
73         Object JavaDoc[] args = { object };
74         String JavaDoc message = ExceptionMessageGenerator.buildMessage(ConversionException.class, COULD_NOT_CONVERT_TO_BYTE_ARRAY, args);
75         ConversionException conversionException = new ConversionException(message, object, byte[].class, null);
76         conversionException.setErrorCode(COULD_NOT_CONVERT_TO_BYTE_ARRAY);
77         return conversionException;
78     }
79
80     public static ConversionException incorrectDateFormat(String JavaDoc dateString) {
81         Object JavaDoc[] args = { dateString };
82         String JavaDoc message = ExceptionMessageGenerator.buildMessage(ConversionException.class, INCORRECT_DATE_FORMAT, args);
83         ConversionException conversionException = new ConversionException(message, dateString, java.sql.Date JavaDoc.class, null);
84         conversionException.setErrorCode(INCORRECT_DATE_FORMAT);
85         return conversionException;
86     }
87
88     public static ConversionException incorrectTimeFormat(String JavaDoc timeString) {
89         Object JavaDoc[] args = { timeString };
90         String JavaDoc message = ExceptionMessageGenerator.buildMessage(ConversionException.class, INCORRECT_TIME_FORMAT, args);
91         ConversionException conversionException = new ConversionException(message, timeString, java.sql.Time JavaDoc.class, null);
92         conversionException.setErrorCode(INCORRECT_TIME_FORMAT);
93         return conversionException;
94     }
95
96     public static ConversionException incorrectTimestampFormat(String JavaDoc timestampString) {
97         Object JavaDoc[] args = { timestampString };
98         String JavaDoc message = ExceptionMessageGenerator.buildMessage(ConversionException.class, INCORRECT_TIMESTAMP_FORMAT, args);
99         ConversionException conversionException = new ConversionException(message, timestampString, java.sql.Timestamp JavaDoc.class, null);
100         conversionException.setErrorCode(INCORRECT_TIMESTAMP_FORMAT);
101         return conversionException;
102     }
103
104     public static ConversionException incorrectDateTimeFormat(String JavaDoc dateTimeString) {
105         Object JavaDoc[] args = { dateTimeString };
106         String JavaDoc message = ExceptionMessageGenerator.buildMessage(ConversionException.class, INCORRECT_DATE_TIME_FORMAT, args);
107         ConversionException conversionException = new ConversionException(message, dateTimeString, Calendar.class, null);
108         conversionException.setErrorCode(INCORRECT_DATE_TIME_FORMAT);
109         return conversionException;
110     }
111
112     /**
113      * PUBLIC:
114      * Return the class to convert to.
115      */

116     public Class JavaDoc getClassToConvertTo() {
117         return classToConvertTo;
118     }
119
120     /**
121      * PUBLIC:
122      * Return the object for which the problem was detected.
123      */

124     public Object JavaDoc getSourceObject() {
125         return sourceObject;
126     }
127
128     /**
129      * INTERNAL:
130      * Set the class to convert to.
131      */

132     public void setClassToConvertTo(Class JavaDoc classToConvertTo) {
133         this.classToConvertTo = classToConvertTo;
134     }
135
136     /**
137      * INTERNAL:
138      * Set the object for which the problem was detected.
139      */

140     public void setSourceObject(Object JavaDoc sourceObject) {
141         this.sourceObject = sourceObject;
142     }
143 }
144
Popular Tags