1. /**
  2. * @(#) SQLErrorDocument.java
  3. *
  4. * The Apache Software License, Version 1.1
  5. *
  6. *
  7. * Copyright (c) 1999 The Apache Software Foundation. All rights
  8. * reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * 3. The end-user documentation included with the redistribution,
  23. * if any, must include the following acknowledgment:
  24. * "This product includes software developed by the
  25. * Apache Software Foundation (http://www.apache.org/)."
  26. * Alternately, this acknowledgment may appear in the software itself,
  27. * if and wherever such third-party acknowledgments normally appear.
  28. *
  29. * 4. The names "Xalan" and "Apache Software Foundation" must
  30. * not be used to endorse or promote products derived from this
  31. * software without prior written permission. For written
  32. * permission, please contact apache@apache.org.
  33. *
  34. * 5. Products derived from this software may not be called "Apache",
  35. * nor may "Apache" appear in their name, without prior written
  36. * permission of the Apache Software Foundation.
  37. *
  38. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  39. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  40. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  41. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  44. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  45. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  46. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  47. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  48. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  49. * SUCH DAMAGE.
  50. * ====================================================================
  51. *
  52. * This software consists of voluntary contributions made by many
  53. * individuals on behalf of the Apache Software Foundation and was
  54. * originally based on software copyright (c) 1999, Lotus
  55. * Development Corporation., http://www.lotus.com. For more
  56. * information on the Apache Software Foundation, please see
  57. * <http://www.apache.org/>.
  58. *
  59. */
  60. package org.apache.xalan.lib.sql;
  61. import org.apache.xml.dtm.DTMManager;
  62. import org.apache.xml.dtm.DTM;
  63. import java.sql.SQLException;
  64. /**
  65. *
  66. * A base class that will convert an exception into an XML stream
  67. * that can be returned in place of the standard result. The XML
  68. * format returned is a follows.
  69. *
  70. * <ext-error>
  71. * <message> The Message for a generic error </message>
  72. * <sql-error>
  73. * <message> SQL Message from the Exception thrown </message>
  74. * <code> SQL Error Code </stack>
  75. * </sql-error>
  76. * <ext-error>
  77. *
  78. */
  79. /**
  80. * The SQL Document is the main controlling class the executesa SQL Query
  81. */
  82. public class SQLErrorDocument extends DTMDocument
  83. {
  84. /**
  85. */
  86. private static final String S_EXT_ERROR = "ext-error";
  87. /**
  88. */
  89. private static final String S_SQL_ERROR = "sql-error";
  90. /**
  91. */
  92. private static final String S_MESSAGE = "message";
  93. /**
  94. */
  95. private static final String S_CODE = "code";
  96. /**
  97. */
  98. private int m_ErrorExt_TypeID = DTM.NULL;
  99. /**
  100. */
  101. private int m_Message_TypeID = DTM.NULL;
  102. /**
  103. */
  104. private int m_Code_TypeID = DTM.NULL;
  105. /**
  106. */
  107. private int m_SQLError_TypeID = DTM.NULL;
  108. /**
  109. */
  110. private int m_rootID = DTM.NULL;
  111. /**
  112. */
  113. private int m_extErrorID = DTM.NULL;
  114. /**
  115. */
  116. private int m_MainMessageID = DTM.NULL;
  117. /**
  118. * Build up an SQLErrorDocument that includes the basic error information
  119. * along with the Extended SQL Error information.
  120. * @param mgr
  121. * @param ident
  122. * @param error
  123. */
  124. public SQLErrorDocument( DTMManager mgr, int ident, SQLException error )
  125. {
  126. super(mgr, ident);
  127. createExpandedNameTable();
  128. buildBasicStructure(error);
  129. int sqlError = addElement(2, m_SQLError_TypeID, m_extErrorID, m_MainMessageID);
  130. int element = DTM.NULL;
  131. element = addElementWithData(
  132. new Integer(error.getErrorCode()), 3,
  133. m_Code_TypeID, sqlError, element);
  134. element = addElementWithData(
  135. error.getLocalizedMessage(), 3,
  136. m_Message_TypeID, sqlError, element);
  137. // this.dumpDTM();
  138. }
  139. /**
  140. * Build up an Error Exception with just the Standard Error Information
  141. * @param mgr
  142. * @param ident
  143. * @param error
  144. */
  145. public SQLErrorDocument( DTMManager mgr, int ident, Exception error )
  146. {
  147. super(mgr, ident);
  148. createExpandedNameTable();
  149. buildBasicStructure(error);
  150. }
  151. /**
  152. * Build up the basic structure that is common for each error.
  153. * @param e
  154. * @return
  155. */
  156. private void buildBasicStructure( Exception e )
  157. {
  158. m_rootID = addElement(0, m_Document_TypeID, DTM.NULL, DTM.NULL);
  159. m_extErrorID = addElement(1, m_ErrorExt_TypeID, m_rootID, DTM.NULL);
  160. m_MainMessageID = addElementWithData
  161. (e.getLocalizedMessage(), 2, m_Message_TypeID, m_extErrorID, DTM.NULL);
  162. }
  163. /**
  164. * Populate the Expanded Name Table with the Node that we will use.
  165. * Keep a reference of each of the types for access speed.
  166. * @return
  167. */
  168. protected void createExpandedNameTable( )
  169. {
  170. super.createExpandedNameTable();
  171. m_ErrorExt_TypeID =
  172. m_expandedNameTable.getExpandedTypeID(S_NAMESPACE, S_EXT_ERROR, DTM.ELEMENT_NODE);
  173. m_SQLError_TypeID =
  174. m_expandedNameTable.getExpandedTypeID(S_NAMESPACE, S_SQL_ERROR, DTM.ELEMENT_NODE);
  175. m_Message_TypeID =
  176. m_expandedNameTable.getExpandedTypeID(S_NAMESPACE, S_MESSAGE, DTM.ELEMENT_NODE);
  177. m_Code_TypeID =
  178. m_expandedNameTable.getExpandedTypeID(S_NAMESPACE, S_CODE, DTM.ELEMENT_NODE);
  179. }
  180. }