KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > dv > DatatypeException


1 /*
2  * Copyright 2001, 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.impl.dv;
18
19 import java.util.ResourceBundle JavaDoc;
20 import java.util.PropertyResourceBundle JavaDoc;
21 import java.util.MissingResourceException JavaDoc;
22
23 /**
24  * Base class for datatype exceptions. For DTD types, the exception can be
25  * created from an error message. For Schema types, it needs an error code
26  * (as defined in Appendix C of the structure spec), plus an array of arguents,
27  * for error message substitution.
28  *
29  * @xerces.internal
30  *
31  * @author Sandy Gao, IBM
32  *
33  * @version $Id: DatatypeException.java,v 1.7 2004/10/06 14:56:50 mrglavas Exp $
34  */

35 public class DatatypeException extends Exception JavaDoc {
36
37     /** Serialization version. */
38     static final long serialVersionUID = 1940805832730465578L;
39     
40     // used to store error code and error substitution arguments
41
protected String JavaDoc key;
42     protected Object JavaDoc[] args;
43
44     /**
45      * Create a new datatype exception by providing an error code and a list
46      * of error message substitution arguments.
47      *
48      * @param key error code
49      * @param args error arguments
50      */

51     public DatatypeException(String JavaDoc key, Object JavaDoc[] args) {
52         super(key);
53         this.key = key;
54         this.args = args;
55     }
56
57     /**
58      * Return the error code
59      *
60      * @return error code
61      */

62     public String JavaDoc getKey() {
63         return key;
64     }
65
66     /**
67      * Return the list of error arguments
68      *
69      * @return error arguments
70      */

71     public Object JavaDoc[] getArgs() {
72         return args;
73     }
74     
75     /**
76      * Overrides this method to get the formatted&localized error message.
77      *
78      * REVISIT: the system locale is used to load the property file.
79      * do we want to allow the appilcation to specify a
80      * different locale?
81      */

82     public String JavaDoc getMessage() {
83         ResourceBundle JavaDoc resourceBundle = null;
84         resourceBundle = PropertyResourceBundle.getBundle("org.apache.xerces.impl.msg.XMLSchemaMessages");
85         if (resourceBundle == null)
86             throw new MissingResourceException JavaDoc("Property file not found!", "org.apache.xerces.impl.msg.XMLSchemaMessages", key);
87
88         String JavaDoc msg = resourceBundle.getString(key);
89         if (msg == null) {
90             msg = resourceBundle.getString("BadMessageKey");
91             throw new MissingResourceException JavaDoc(msg, "org.apache.xerces.impl.msg.XMLSchemaMessages", key);
92         }
93
94         if (args != null) {
95             try {
96                 msg = java.text.MessageFormat.format(msg, args);
97             } catch (Exception JavaDoc e) {
98                 msg = resourceBundle.getString("FormatFailed");
99                 msg += " " + resourceBundle.getString(key);
100             }
101         }
102
103         return msg;
104     }
105 }
106
Popular Tags