2
* Licensed to the Apache Software Foundation (ASF) under one
3
* or more contributor license agreements. See the NOTICE file
4
* distributed with this work for additional information
5
* regarding copyright ownership. The ASF licenses this file
6
* to you under the Apache License, Version 2.0 (the "License");
7
* you may not use this file except in compliance with the License.
8
* You may obtain a copy of the License at
10
* http://www.apache.org/licenses/LICENSE-2.0
12
* Unless required by applicable law or agreed to in writing, software
13
* distributed under the License is distributed on an "AS IS" BASIS,
14
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
* See the License for the specific language governing permissions and
16
* limitations under the License.
19
* $Id: SQLErrorDocument.java,v 1.2 2009/12/10 03:18:45 matthewoliver Exp $
22
package org.apache.xalan.lib.sql;
24
import java.sql.SQLException;
25
import java.sql.SQLWarning;
27
import org.apache.xml.dtm.DTM;
28
import org.apache.xml.dtm.DTMManager;
32
* A base class that will convert an exception into an XML stream
33
* that can be returned in place of the standard result. The XML
34
* format returned is a follows.
37
* <message> The Message for a generic error </message>
39
* <message> SQL Message from the Exception thrown </message>
40
* <code> SQL Error Code </stack>
47
* The SQL Document is the main controlling class the executesa SQL Query
49
public class SQLErrorDocument extends DTMDocument
53
private static final String S_EXT_ERROR = "ext-error";
56
private static final String S_SQL_ERROR = "sql-error";
59
private static final String S_MESSAGE = "message";
62
private static final String S_CODE = "code";
66
private static final String S_STATE = "state";
70
private static final String S_SQL_WARNING = "sql-warning";
74
private int m_ErrorExt_TypeID = DTM.NULL;
77
private int m_Message_TypeID = DTM.NULL;
80
private int m_Code_TypeID = DTM.NULL;
84
private int m_State_TypeID = DTM.NULL;
88
private int m_SQLWarning_TypeID = DTM.NULL;
92
private int m_SQLError_TypeID = DTM.NULL;
96
private int m_rootID = DTM.NULL;
99
private int m_extErrorID = DTM.NULL;
102
private int m_MainMessageID = DTM.NULL;
105
* Build up an SQLErrorDocument that includes the basic error information
106
* along with the Extended SQL Error information.
111
public SQLErrorDocument( DTMManager mgr, int ident, SQLException error )
115
createExpandedNameTable();
116
buildBasicStructure(error);
118
int sqlError = addElement(2, m_SQLError_TypeID, m_extErrorID, m_MainMessageID);
119
int element = DTM.NULL;
121
element = addElementWithData(
122
new Integer(error.getErrorCode()), 3,
123
m_Code_TypeID, sqlError, element);
125
element = addElementWithData(
126
error.getLocalizedMessage(), 3,
127
m_Message_TypeID, sqlError, element);
134
* Build up an Error Exception with just the Standard Error Information
139
public SQLErrorDocument( DTMManager mgr, int ident, Exception error )
142
createExpandedNameTable();
143
buildBasicStructure(error);
147
* Build up an Error Exception with just the Standard Error Information
152
public SQLErrorDocument(DTMManager mgr, int ident, Exception error, SQLWarning warning, boolean full)
155
createExpandedNameTable();
156
buildBasicStructure(error);
158
SQLException se = null;
159
int prev = m_MainMessageID;
160
boolean inWarnings = false;
162
if ( error != null && error instanceof SQLException )
163
se = (SQLException)error;
164
else if ( full && warning != null )
172
int sqlError = addElement(2, inWarnings ? m_SQLWarning_TypeID : m_SQLError_TypeID, m_extErrorID, prev);
174
int element = DTM.NULL;
176
element = addElementWithData(
177
new Integer(se.getErrorCode()), 3,
178
m_Code_TypeID, sqlError, element);
180
element = addElementWithData(
181
se.getLocalizedMessage(), 3,
182
m_Message_TypeID, sqlError, element);
186
String state = se.getSQLState();
187
if ( state != null && state.length() > 0 )
188
element = addElementWithData(
190
m_State_TypeID, sqlError, element);
193
se = ((SQLWarning)se).getNextWarning();
195
se = se.getNextException();
203
* Build up the basic structure that is common for each error.
207
private void buildBasicStructure( Exception e )
209
m_rootID = addElement(0, m_Document_TypeID, DTM.NULL, DTM.NULL);
210
m_extErrorID = addElement(1, m_ErrorExt_TypeID, m_rootID, DTM.NULL);
211
m_MainMessageID = addElementWithData
212
(e != null ? e.getLocalizedMessage() : "SQLWarning", 2, m_Message_TypeID, m_extErrorID, DTM.NULL);
216
* Populate the Expanded Name Table with the Node that we will use.
217
* Keep a reference of each of the types for access speed.
220
protected void createExpandedNameTable( )
223
super.createExpandedNameTable();
226
m_expandedNameTable.getExpandedTypeID(S_NAMESPACE, S_EXT_ERROR, DTM.ELEMENT_NODE);
229
m_expandedNameTable.getExpandedTypeID(S_NAMESPACE, S_SQL_ERROR, DTM.ELEMENT_NODE);
232
m_expandedNameTable.getExpandedTypeID(S_NAMESPACE, S_MESSAGE, DTM.ELEMENT_NODE);
235
m_expandedNameTable.getExpandedTypeID(S_NAMESPACE, S_CODE, DTM.ELEMENT_NODE);
238
m_expandedNameTable.getExpandedTypeID(S_NAMESPACE, S_STATE, DTM.ELEMENT_NODE);
240
m_SQLWarning_TypeID =
241
m_expandedNameTable.getExpandedTypeID(S_NAMESPACE, S_SQL_WARNING, DTM.ELEMENT_NODE);