KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > database > resource > DException


1 package com.daffodilwoods.database.resource;
2
3 import java.util.*;
4 import java.text.*;
5 import java.sql.SQLException JavaDoc;
6
7 public class DException extends Exception JavaDoc {
8
9    String JavaDoc dseCode;
10    protected Object JavaDoc[] objarray;
11
12    protected DException prevException;
13
14    public DException getPreviousException() {
15       return (prevException == null) ? null : prevException;
16    }
17
18    public DException(String JavaDoc dsecode, Object JavaDoc[] objarray, DException temp) {
19       this.prevException = temp;
20       this.dseCode = dsecode;
21       this.objarray = objarray;
22    }
23
24    public DException(String JavaDoc dsecode, Object JavaDoc[] objarray) {
25       this.dseCode = dsecode;
26       this.objarray = objarray;
27    }
28
29    public String JavaDoc getMessage() {
30       return getMessage(null);
31    }
32
33    public String JavaDoc getMessage(Locale locale) {
34       String JavaDoc prevMessage = null;
35       if (prevException != null)
36          prevMessage = prevException.getMessage(locale);
37       if (dseCode == null)
38          return super.getMessage();
39       ResourceBundle manager = null;
40       if (locale != null)
41          manager = ResourceBundle.getBundle("com.daffodilwoods.database.resource.daffodilwoodsLocale", locale);
42       else
43          manager = ResourceBundle.getBundle("com.daffodilwoods.database.resource.daffodilwoodsLocale");
44       try {
45          MessageFormat mf = new MessageFormat(manager.getString(dseCode));
46          return (prevMessage != null) ? "\n\t" + mf.format(getParametersAsString()) + "\t" + prevMessage : "\n\t" + mf.format(getParametersAsString());
47       } catch (Exception JavaDoc ex) {
48          return (prevMessage != null) ? "\n\t" + "Code \"" + dseCode + "\" is not defined" + prevMessage
49              : "\n\t" + "Code \"" + dseCode + "\" is not defined";
50       }
51    }
52
53    public String JavaDoc getDseCode() {
54       return dseCode;
55    }
56
57    public Object JavaDoc[] getParameters() {
58       return objarray;
59    }
60
61    public void printStackTrace() {
62       if (prevException != null)
63          prevException.printStackTrace();
64       else
65          super.printStackTrace();
66    }
67
68    public SQLException JavaDoc getSqlException(Locale locale) {
69       if (dseCode == null)
70          return new SQLException JavaDoc();
71       try {
72          ResourceBundle manager = ResourceBundle.getBundle("com.daffodilwoods.database.resource.DseToSQL");
73          int cose = 0;
74          try {
75             String JavaDoc as = dseCode.substring(3);
76             cose = Integer.parseInt(as);
77          } catch (NumberFormatException JavaDoc ex) {
78          }
79          return new SQLException JavaDoc(getMessage(locale), manager.getString(dseCode), cose);
80       } catch (java.util.MissingResourceException JavaDoc mre) {
81          return new SQLException JavaDoc(getMessage(locale));
82       }
83    }
84
85    public void setParameters(Object JavaDoc[] parameters0) {
86       objarray = parameters0;
87    }
88
89    private String JavaDoc[] getParametersAsString() {
90       if (objarray == null)
91          return null;
92       int length = objarray.length;
93       String JavaDoc[] str = new String JavaDoc[length];
94       for (int i = 0; i < length; i++) {
95          String JavaDoc temp = "" + objarray[i];
96          temp = temp.trim();
97          if (temp.startsWith("the keyword")) {
98             String JavaDoc asd = temp.substring(12);
99             asd = asd.trim();
100             str[i] = "the keyword '" + asd + "'";
101          } else
102
103             str[i] = "'" + temp + "'";
104       }
105       return str;
106    }
107
108    public String JavaDoc getSQLCode() {
109       if (dseCode == null)
110          return "";
111       try {
112          ResourceBundle manager =
113              ResourceBundle.getBundle("com.daffodilwoods.database.resource.DseToSQL");
114          return manager.getString(dseCode);
115       } catch (java.util.MissingResourceException JavaDoc mre) {
116          return "";
117       }
118    }
119
120    public String JavaDoc toString(){
121       return getMessage().trim();
122    }
123 }
124
Popular Tags