KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > lib > sql > SQLErrorDocument


1 /*
2  * Copyright 1999-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  * $Id: SQLErrorDocument.java,v 1.8 2004/02/11 17:56:36 minchau Exp $
18  */

19
20 package org.apache.xalan.lib.sql;
21
22 import java.sql.SQLException JavaDoc;
23 import java.sql.SQLWarning JavaDoc;
24
25 import org.apache.xml.dtm.DTM;
26 import org.apache.xml.dtm.DTMManager;
27
28 /**
29  *
30  * A base class that will convert an exception into an XML stream
31  * that can be returned in place of the standard result. The XML
32  * format returned is a follows.
33  *
34  * <ext-error>
35  * <message> The Message for a generic error </message>
36  * <sql-error>
37  * <message> SQL Message from the Exception thrown </message>
38  * <code> SQL Error Code </stack>
39  * </sql-error>
40  * <ext-error>
41  *
42  */

43
44 /**
45  * The SQL Document is the main controlling class the executesa SQL Query
46  */

47 public class SQLErrorDocument extends DTMDocument
48 {
49   /**
50    */

51   private static final String JavaDoc S_EXT_ERROR = "ext-error";
52   /**
53    */

54   private static final String JavaDoc S_SQL_ERROR = "sql-error";
55   /**
56    */

57   private static final String JavaDoc S_MESSAGE = "message";
58   /**
59    */

60   private static final String JavaDoc S_CODE = "code";
61
62   /**
63    */

64   private static final String JavaDoc S_STATE = "state";
65
66   /**
67    */

68   private static final String JavaDoc S_SQL_WARNING = "sql-warning";
69
70   /**
71    */

72   private int m_ErrorExt_TypeID = DTM.NULL;
73   /**
74    */

75   private int m_Message_TypeID = DTM.NULL;
76   /**
77    */

78   private int m_Code_TypeID = DTM.NULL;
79
80   /**
81    */

82   private int m_State_TypeID = DTM.NULL;
83
84   /**
85    */

86   private int m_SQLWarning_TypeID = DTM.NULL;
87
88   /**
89    */

90   private int m_SQLError_TypeID = DTM.NULL;
91
92   /**
93    */

94   private int m_rootID = DTM.NULL;
95   /**
96    */

97   private int m_extErrorID = DTM.NULL;
98   /**
99    */

100   private int m_MainMessageID = DTM.NULL;
101
102   /**
103    * Build up an SQLErrorDocument that includes the basic error information
104    * along with the Extended SQL Error information.
105    * @param mgr
106    * @param ident
107    * @param error
108    */

109   public SQLErrorDocument( DTMManager mgr, int ident, SQLException JavaDoc error )
110   {
111     super(mgr, ident);
112
113     createExpandedNameTable();
114     buildBasicStructure(error);
115
116     int sqlError = addElement(2, m_SQLError_TypeID, m_extErrorID, m_MainMessageID);
117     int element = DTM.NULL;
118
119     element = addElementWithData(
120       new Integer JavaDoc(error.getErrorCode()), 3,
121       m_Code_TypeID, sqlError, element);
122
123     element = addElementWithData(
124       error.getLocalizedMessage(), 3,
125       m_Message_TypeID, sqlError, element);
126
127 // this.dumpDTM();
128
}
129
130
131   /**
132    * Build up an Error Exception with just the Standard Error Information
133    * @param mgr
134    * @param ident
135    * @param error
136    */

137   public SQLErrorDocument( DTMManager mgr, int ident, Exception JavaDoc error )
138   {
139     super(mgr, ident);
140     createExpandedNameTable();
141     buildBasicStructure(error);
142   }
143
144   /**
145    * Build up an Error Exception with just the Standard Error Information
146    * @param mgr
147    * @param ident
148    * @param error
149    */

150   public SQLErrorDocument(DTMManager mgr, int ident, Exception JavaDoc error, SQLWarning JavaDoc warning, boolean full)
151   {
152     super(mgr, ident);
153     createExpandedNameTable();
154     buildBasicStructure(error);
155
156     SQLException JavaDoc se = null;
157     int prev = m_MainMessageID;
158     boolean inWarnings = false;
159
160     if ( error != null && error instanceof SQLException JavaDoc )
161         se = (SQLException JavaDoc)error;
162     else if ( full && warning != null )
163     {
164         se = warning;
165         inWarnings = true;
166     }
167
168     while ( se != null )
169     {
170         int sqlError = addElement(2, inWarnings ? m_SQLWarning_TypeID : m_SQLError_TypeID, m_extErrorID, prev);
171         prev = sqlError;
172         int element = DTM.NULL;
173
174         element = addElementWithData(
175           new Integer JavaDoc(se.getErrorCode()), 3,
176           m_Code_TypeID, sqlError, element);
177
178         element = addElementWithData(
179           se.getLocalizedMessage(), 3,
180           m_Message_TypeID, sqlError, element);
181
182         if ( full )
183         {
184             String JavaDoc state = se.getSQLState();
185             if ( state != null && state.length() > 0 )
186                 element = addElementWithData(
187                   state, 3,
188                   m_State_TypeID, sqlError, element);
189
190             if ( inWarnings )
191                 se = ((SQLWarning JavaDoc)se).getNextWarning();
192             else
193                 se = se.getNextException();
194         }
195         else
196             se = null;
197     }
198   }
199
200   /**
201    * Build up the basic structure that is common for each error.
202    * @param e
203    * @return
204    */

205   private void buildBasicStructure( Exception JavaDoc e )
206   {
207     m_rootID = addElement(0, m_Document_TypeID, DTM.NULL, DTM.NULL);
208     m_extErrorID = addElement(1, m_ErrorExt_TypeID, m_rootID, DTM.NULL);
209     m_MainMessageID = addElementWithData
210       (e != null ? e.getLocalizedMessage() : "SQLWarning", 2, m_Message_TypeID, m_extErrorID, DTM.NULL);
211   }
212
213   /**
214    * Populate the Expanded Name Table with the Node that we will use.
215    * Keep a reference of each of the types for access speed.
216    * @return
217    */

218   protected void createExpandedNameTable( )
219   {
220
221     super.createExpandedNameTable();
222
223     m_ErrorExt_TypeID =
224       m_expandedNameTable.getExpandedTypeID(S_NAMESPACE, S_EXT_ERROR, DTM.ELEMENT_NODE);
225
226     m_SQLError_TypeID =
227       m_expandedNameTable.getExpandedTypeID(S_NAMESPACE, S_SQL_ERROR, DTM.ELEMENT_NODE);
228
229     m_Message_TypeID =
230       m_expandedNameTable.getExpandedTypeID(S_NAMESPACE, S_MESSAGE, DTM.ELEMENT_NODE);
231
232     m_Code_TypeID =
233       m_expandedNameTable.getExpandedTypeID(S_NAMESPACE, S_CODE, DTM.ELEMENT_NODE);
234
235     m_State_TypeID =
236       m_expandedNameTable.getExpandedTypeID(S_NAMESPACE, S_STATE, DTM.ELEMENT_NODE);
237
238     m_SQLWarning_TypeID =
239       m_expandedNameTable.getExpandedTypeID(S_NAMESPACE, S_SQL_WARNING, DTM.ELEMENT_NODE);
240   }
241
242 }
243
Popular Tags