KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejen > ext > db > MetaDataNodeBuilder


1 //
2
// Ejen (code generation system)
3
// Copyright (C) 2001, 2002 François Wolff (ejen@noos.fr).
4
//
5
// This file is part of Ejen.
6
//
7
// Ejen is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License as published by
9
// the Free Software Foundation; either version 2 of the License, or
10
// (at your option) any later version.
11
//
12
// Ejen is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License for more details.
16
//
17
// You should have received a copy of the GNU General Public License
18
// along with Ejen; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
//
21
package org.ejen.ext.db;
22
23 import org.ejen.util.DOMUtil;
24 import org.ejen.util.XSLUtil;
25 import java.sql.Statement JavaDoc;
26 import java.sql.ResultSetMetaData JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.Connection JavaDoc;
29 import java.sql.DatabaseMetaData JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import org.w3c.dom.Document JavaDoc;
32 import org.w3c.dom.Element JavaDoc;
33 import org.w3c.dom.Node JavaDoc;
34 import org.apache.xpath.NodeSet;
35 import org.apache.xml.utils.WrappedRuntimeException;
36 import org.apache.xalan.extensions.ExpressionContext;
37
38 /**
39  * Database/table metadata <b>abstract</b> class.
40  * (see {@link org.ejen.ext.db.BasicMetaDataConnection} for usage).
41  * @author F. Wolff
42  * @version 1.0
43  */

44 public abstract class MetaDataNodeBuilder {
45     // DatabaseMetaData.*
46
public static final String JavaDoc S_DI_NODE_NAME = "database-information";
47     public static final String JavaDoc S_DI_DATABASE_PRODUCT_NAME = "database-product-name";
48     public static final String JavaDoc S_DI_DATABASE_PRODUCT_VERSION = "database-product-version";
49     // ResultSetMetaData.*
50
public static final String JavaDoc S_RS_COLUMN_NODE_NAME = "column";
51     public static final String JavaDoc S_RS_CATALOG_NAME = "catalog-name";
52     public static final String JavaDoc S_RS_COLUMN_CLASS_NAME = "column-class-name";
53     public static final String JavaDoc S_RS_COLUMN_DISPLAY_SIZE = "column-display-size";
54     public static final String JavaDoc S_RS_COLUMN_LABEL = "column-label";
55     public static final String JavaDoc S_RS_COLUMN_NAME = "column-name";
56     public static final String JavaDoc S_RS_COLUMN_TYPE = "column-type";
57     public static final String JavaDoc S_RS_COLUMN_TYPE_NAME = "column-type-name";
58     public static final String JavaDoc S_RS_PRECISION = "precision";
59     public static final String JavaDoc S_RS_SCALE = "scale";
60     public static final String JavaDoc S_RS_SCHEMA_NAME = "schema-name";
61     public static final String JavaDoc S_RS_TABLE_NAME = "table-name";
62     public static final String JavaDoc S_RS_AUTO_INCREMENT = "auto-increment";
63     public static final String JavaDoc S_RS_CASE_SENSITIVE = "case-sensitive";
64     public static final String JavaDoc S_RS_CURRENCY = "currency";
65     public static final String JavaDoc S_RS_DEFINITELY_WRITABLE = "definitely-writable";
66     public static final String JavaDoc S_RS_NULLABLE = "nullable";
67     public static final String JavaDoc S_RS_READ_ONLY = "read-only";
68     public static final String JavaDoc S_RS_SEARCHABLE = "searchable";
69     public static final String JavaDoc S_RS_SIGNED = "signed";
70     public static final String JavaDoc S_RS_WRITABLE = "writable";
71     public static final String JavaDoc S_RS_COLUMN_NO_NULLS = "column-no-nulls";
72     public static final String JavaDoc S_RS_COLUMN_NULLABLE = "column-nullable";
73     public static final String JavaDoc S_RS_COLUMN_NULLABLE_UNKNOWN = "column-nullable-unknown";
74     // DatabaseMetaData.getPrimaryKeys
75
public static final String JavaDoc S_PK_NODE_NAME = "primary-key";
76     public static final String JavaDoc S_PK_TABLE_CAT = "table-cat";
77     public static final String JavaDoc S_PK_TABLE_SCHEM = "table-shem";
78     public static final String JavaDoc S_PK_TABLE_NAME = "table-name";
79     public static final String JavaDoc S_PK_COLUMN_NAME = "column-name";
80     public static final String JavaDoc S_PK_KEY_SEQ = "key-seq";
81     public static final String JavaDoc S_PK_PK_NAME = "pk-name";
82     // DatabaseMetaData.getExportedKeys / DatabaseMetaData.getImportedKeys
83
public static final String JavaDoc S_EIK_EK_NODE_NAME = "exported-key";
84     public static final String JavaDoc S_EIK_IK_NODE_NAME = "imported-key";
85     public static final String JavaDoc S_EIK_PKTABLE_CAT = "pktable-cat";
86     public static final String JavaDoc S_EIK_PKTABLE_SCHEM = "pktable-shem";
87     public static final String JavaDoc S_EIK_PKTABLE_NAME = "pktable-name";
88     public static final String JavaDoc S_EIK_PKCOLUMN_NAME = "pkcolumn-name";
89     public static final String JavaDoc S_EIK_FKTABLE_CAT = "fktable-cat";
90     public static final String JavaDoc S_EIK_FKTABLE_SCHEM = "fktable-shem";
91     public static final String JavaDoc S_EIK_FKTABLE_NAME = "fktable-name";
92     public static final String JavaDoc S_EIK_FKCOLUMN_NAME = "fkcolumn-name";
93     public static final String JavaDoc S_EIK_KEY_SEQ = "key-seq";
94     public static final String JavaDoc S_EIK_UPDATE_RULE = "update-rule";
95     public static final String JavaDoc S_EIK_DELETE_RULE = "delete-rule";
96     public static final String JavaDoc S_EIK_FK_NAME = "fk-name";
97     public static final String JavaDoc S_EIK_PK_NAME = "pk-name";
98     public static final String JavaDoc S_EIK_DEFERRABILITY = "deferrability";
99     // DatabaseMetaData.getIndexInfo
100
public static final String JavaDoc S_II_NODE_NAME = "index";
101     public static final String JavaDoc S_II_TABLE_CAT = "table-cat";
102     public static final String JavaDoc S_II_TABLE_SCHEM = "table-shem";
103     public static final String JavaDoc S_II_TABLE_NAME = "table-name";
104     public static final String JavaDoc S_II_NON_UNIQUE = "non-unique";
105     public static final String JavaDoc S_II_INDEX_QUALIFIER = "index-qualifier";
106     public static final String JavaDoc S_II_INDEX_NAME = "index-name";
107     public static final String JavaDoc S_II_TYPE = "type";
108     public static final String JavaDoc S_II_ORDINAL_POSITION = "ordinal-position";
109     public static final String JavaDoc S_II_COLUMN_NAME = "column-name";
110     public static final String JavaDoc S_II_ASC_OR_DESC = "asc-or-desc";
111     public static final String JavaDoc S_II_CARDINALITY = "cardinality";
112     public static final String JavaDoc S_II_PAGES = "pages";
113     public static final String JavaDoc S_II_FILTER_CONDITION = "filter-condition";
114     // Errors
115
public static final String JavaDoc S_EXCEPTION_NODE_NAME = "exception";
116     public static final String JavaDoc S_EXCEPTION_CLASS_NAME = "class-name";
117     public static final String JavaDoc S_EXCEPTION_CODE = "code";
118     // Default values
119
public static final String JavaDoc S_NOT_SUPPORTED = "#NOT_SUPPORTED";
120     public static final String JavaDoc S_NULL = "#NULL";
121
122     /** <code>NodeSet</code> containing errors informations */
123     protected static NodeSet _errors = null;
124
125     /** Current active connection */
126     protected static Connection JavaDoc _activeConn = null;
127
128     /**
129      * Gets the current active JDBC connection.
130      * @return the connection.
131      * @throws java.sql.SQLException if there is no active connection
132      * (ie: no connections at all).
133      */

134     protected static Connection JavaDoc getConnection() throws SQLException JavaDoc {
135         if (_activeConn == null) {
136             throw new SQLException JavaDoc("Not connected");
137         }
138         return _activeConn;
139     }
140
141     /**
142      * Returns a <code>NodeSet</code> that contains errors informations.
143      * <p>
144      * <table class="usage"><tr><td class="usage"><pre>
145      *
146      * &lt;xsl:variable name="errors" select="mta:getErrors()"/&gt;
147      * </pre></td></tr></table>
148      * <p>
149      * The returned <code>NodeSet</code> has the following format:
150      * <p>
151      * <table class="usage"><tr><td class="usage"><pre>
152      *
153      * [... other 'exception' nodes with earlier errors]
154      * &lt;exception class-name="java.sql.SQLException" code="0"&gt;
155      * Table not found: ADRESS in statement [SELECT * FROM ADRESS]
156      * &lt;/exception&gt;
157      * </pre></td></tr></table>
158      * <p>
159      * @param context automatically passed by the xalan extension mechanism.
160      * @return the errors <code>NodeSet</code>.
161      */

162     public static NodeSet getErrors(ExpressionContext context) {
163         return _errors;
164     }
165
166     /**
167      * Returns a <code>Node</code> that contains basic database informations.
168      * <p>
169      * <table class="usage"><tr><td class="usage"><pre>
170      *
171      * &lt;xsl:copy-of select="mta:getDatabaseInformation()/@*"/&gt;
172      * </pre></td></tr></table>
173      * <p>
174      * The returned <code>Node</code> has the following format:
175      * <p>
176      * <table class="usage"><tr><td class="usage"><pre>
177      *
178      * &lt;database-information database-product-name="..."
179      * database-product-version="..."/&gt;
180      * </pre></td></tr></table>
181      * <p>
182      * Database errors (SQLException) are not thrown. Instead, an errors
183      * <code>NodeSet</code> is built that can be retreived by the
184      * {@link #getErrors(ExpressionContext)} method.
185      * <p>
186      * See {@link java.sql.DatabaseMetaData#getDatabaseProductName()} and
187      * {@link java.sql.DatabaseMetaData#getDatabaseProductVersion()}.
188      * <p>
189      * @param context automatically passed by the xalan extension mechanism.
190      * @return the database informations <code>Node</code>.
191      * @throws org.apache.xml.utils.WrappedRuntimeException DOM error.
192      */

193     public static Node JavaDoc getDatabaseInformation(ExpressionContext context) {
194         Document JavaDoc doc = XSLUtil.getContextDocument(context);
195
196         try {
197             DatabaseMetaData JavaDoc dbmd = getConnection().getMetaData();
198             Element JavaDoc elt = doc.createElement(S_DI_NODE_NAME);
199
200             elt.setAttribute(S_DI_DATABASE_PRODUCT_NAME,
201                     noNull(dbmd.getDatabaseProductName()));
202             elt.setAttribute(S_DI_DATABASE_PRODUCT_VERSION,
203                     noNull(dbmd.getDatabaseProductVersion()));
204             return elt;
205         } catch (SQLException JavaDoc e) {
206             appendErrorNode(e);
207         } catch (Exception JavaDoc e) {
208             throw new WrappedRuntimeException(e);
209         }
210         return null;
211     }
212
213     /**
214      * Returns a <code>NodeSet</code> that contains <code>ResultSet</code>
215      * metadata (for all columns).
216      * <p>
217      * <table class="usage"><tr><td class="usage"><pre>
218      *
219      * &lt;xsl:copy-of select="mta:getResultSetMetaData('ADDRESS')"/&gt;
220      * </pre></td></tr></table>
221      * <p>
222      * The returned <code>NodeSet</code> has the following format
223      * <p>
224      * <table class="usage"><tr><td class="usage"><pre>
225      *
226      * &lt;column auto-increment="false"
227      * case-sensitive="true"
228      * catalog-name=""
229      * column-class-name="#NOT_SUPPORTED"
230      * column-display-size="0"
231      * column-label="ID"
232      * column-name="ID"
233      * column-no-nulls="0"
234      * column-nullable="1"
235      * column-nullable-unknown="2"
236      * column-type="4"
237      * column-type-name="INTEGER"
238      * currency="false"
239      * definitely-writable="true"
240      * nullable="1"
241      * precision="0"
242      * read-only="false"
243      * scale="0"
244      * schema-name=""
245      * searchable="true"
246      * sequence="true"
247      * signed="true"
248      * table-name="ADDRESS"
249      * writable="true"/>
250      * [... Other column nodes]
251      * </pre></td></tr></table>
252      * <p>
253      * Note: <code>column-no-nulls</code>, <code>column-nullable</code> and
254      * <code>column-nullable-unknown</code> are just constant values used by
255      * <code>nullable</code> attribute.
256      * <p>
257      * Any <code>null</code> value returned by a
258      * {@link java.sql.ResultSetMetaData}.get*()
259      * or a {@link java.sql.ResultSet}.get*(int columnIndex) is marked
260      * as <code>#NULL</code>.
261      * <p>
262      * Any exception thrown by a {@link java.sql.ResultSetMetaData}.get*() method
263      * is marked as <code>#NOT_SUPPORTED</code>.
264      * <p>
265      * Important database errors (table does not exists, ...) are not thrown. Instead,
266      * an errors <code>NodeSet</code> is built and can be retreived by the
267      * {@link MetaDataNodeBuilder#getErrors(ExpressionContext)} method.
268      * <p>
269      * See {@link java.sql.ResultSetMetaData}.
270      * <p>
271      * <dd><dl><dt><b>XSLT parameters:</b>
272      * <dd><b>[Mandatory/AVT]</b> name of the table.
273      * </dl></dd>
274      * <p>
275      * @param context automatically passed by the xalan extension mechanism.
276      * @param table name of the table.
277      * @return the <code>ResultSet</code> metadata <code>NodeSet</code>.
278      * @throws org.apache.xml.utils.WrappedRuntimeException DOM error.
279      */

280     public static NodeSet getResultSetMetaData(ExpressionContext context,
281             String JavaDoc table) {
282         table = XSLUtil.evaluate(context, table);
283         _errors = null;
284         return getResultSetMetaData(table, "*", S_RS_COLUMN_NODE_NAME,
285                 XSLUtil.getContextDocument(context));
286     }
287
288     /**
289      * Returns a <code>NodeSet</code> that contains <code>ResultSet</code> metadata
290      * (for the given column names).
291      * <p>
292      * <table class="usage"><tr><td class="usage"><pre>
293      *
294      * &lt;xsl:copy-of select="mta:getResultSetMetaData('ADDRESS','COL1,COL2')"/&gt;
295      * </pre></td></tr></table>
296      * <p>
297      * See {@link #getResultSetMetaData(ExpressionContext,String)} method.
298      * <p>
299      * <dd><dl><dt><b>XSLT parameters:</b>
300      * <dd><b>[Mandatory/AVT]</b> name of the table.
301      * <dd><b>[Mandatory/AVT]</b> comma separated list column names.
302      * </dl></dd>
303      * <p>
304      * @param context automatically passed by the xalan extension mechanism.
305      * @param table name of the table.
306      * @param columns comma separated list of columns.
307      * @return the <code>ResultSet</code> metadata <code>NodeSet</code>.
308      * @throws org.apache.xml.utils.WrappedRuntimeException DOM error.
309      */

310     public static NodeSet getResultSetMetaData(ExpressionContext context,
311             String JavaDoc table,
312             String JavaDoc columns) {
313         table = XSLUtil.evaluate(context, table);
314         columns = XSLUtil.evaluate(context, columns);
315         _errors = null;
316         return getResultSetMetaData(table, columns, S_RS_COLUMN_NODE_NAME,
317                 XSLUtil.getContextDocument(context));
318     }
319
320     /**
321      * Returns a <code>NodeSet</code> that contains <code>ResultSet</code> metadata
322      * (for the given column names).
323      * <p>
324      * See {@link #getResultSetMetaData(ExpressionContext,String)} method.
325      * <p>
326      * @param table name of the table.
327      * @param columns comma separated list of columns.
328      * @param eltName name of each columns node name.
329      * @param doc a <code>Document</code>.
330      * @return the <code>ResultSet</code> metadata <code>NodeSet</code>.
331      * @throws org.apache.xml.utils.WrappedRuntimeException DOM error.
332      */

333     protected static NodeSet getResultSetMetaData(String JavaDoc table,
334             String JavaDoc columns,
335             String JavaDoc eltName,
336             Document JavaDoc doc) {
337         Statement JavaDoc stmt = null;
338
339         try {
340             stmt = getConnection().createStatement();
341             ResultSetMetaData JavaDoc rsmd
342                     = stmt.executeQuery("SELECT " + columns + " FROM " + table).getMetaData();
343             String JavaDoc columnNoNulls = String.valueOf(rsmd.columnNoNulls);
344             String JavaDoc columnNullable = String.valueOf(rsmd.columnNullable);
345             String JavaDoc columnNullableUnknown = String.valueOf(rsmd.columnNullableUnknown);
346             int count = rsmd.getColumnCount();
347             NodeSet ns = new NodeSet();
348
349             ns.setShouldCacheNodes(true);
350             for (int i = 1; i <= count; i++) {
351                 Element JavaDoc elt = doc.createElement(eltName);
352
353                 try {
354                     elt.setAttribute(S_RS_CATALOG_NAME,
355                             noNull(rsmd.getCatalogName(i)));
356                 } catch (SQLException JavaDoc e) {
357                     elt.setAttribute(S_RS_CATALOG_NAME, S_NOT_SUPPORTED);
358                 }
359                 try {
360                     elt.setAttribute(S_RS_COLUMN_CLASS_NAME,
361                             noNull(rsmd.getColumnClassName(i)));
362                 } catch (SQLException JavaDoc e) {
363                     elt.setAttribute(S_RS_COLUMN_CLASS_NAME, S_NOT_SUPPORTED);
364                 }
365                 try {
366                     elt.setAttribute(S_RS_COLUMN_DISPLAY_SIZE,
367                             String.valueOf(rsmd.getColumnDisplaySize(i)));
368                 } catch (SQLException JavaDoc e) {
369                     elt.setAttribute(S_RS_COLUMN_DISPLAY_SIZE, S_NOT_SUPPORTED);
370                 }
371                 try {
372                     elt.setAttribute(S_RS_COLUMN_LABEL,
373                             noNull(rsmd.getColumnLabel(i)));
374                 } catch (SQLException JavaDoc e) {
375                     elt.setAttribute(S_RS_COLUMN_LABEL, S_NOT_SUPPORTED);
376                 }
377                 try {
378                     elt.setAttribute(S_RS_COLUMN_NAME,
379                             noNull(rsmd.getColumnName(i)));
380                 } catch (SQLException JavaDoc e) {
381                     elt.setAttribute(S_RS_COLUMN_NAME, S_NOT_SUPPORTED);
382                 }
383                 try {
384                     elt.setAttribute(S_RS_COLUMN_TYPE,
385                             String.valueOf(rsmd.getColumnType(i)));
386                 } catch (SQLException JavaDoc e) {
387                     elt.setAttribute(S_RS_COLUMN_TYPE, S_NOT_SUPPORTED);
388                 }
389                 try {
390                     elt.setAttribute(S_RS_COLUMN_TYPE_NAME,
391                             noNull(rsmd.getColumnTypeName(i)));
392                 } catch (SQLException JavaDoc e) {
393                     elt.setAttribute(S_RS_COLUMN_TYPE_NAME, S_NOT_SUPPORTED);
394                 }
395                 try {
396                     elt.setAttribute(S_RS_PRECISION,
397                             String.valueOf(rsmd.getPrecision(i)));
398                 } catch (SQLException JavaDoc e) {
399                     elt.setAttribute(S_RS_PRECISION, S_NOT_SUPPORTED);
400                 }
401                 try {
402                     elt.setAttribute(S_RS_SCALE,
403                             String.valueOf(rsmd.getScale(i)));
404                 } catch (SQLException JavaDoc e) {
405                     elt.setAttribute(S_RS_SCALE, S_NOT_SUPPORTED);
406                 }
407                 try {
408                     elt.setAttribute(S_RS_SCHEMA_NAME,
409                             noNull(rsmd.getSchemaName(i)));
410                 } catch (SQLException JavaDoc e) {
411                     elt.setAttribute(S_RS_SCHEMA_NAME, S_NOT_SUPPORTED);
412                 }
413                 try {
414                     elt.setAttribute(S_RS_TABLE_NAME,
415                             noNull(rsmd.getTableName(i)));
416                 } catch (SQLException JavaDoc e) {
417                     elt.setAttribute(S_RS_TABLE_NAME, S_NOT_SUPPORTED);
418                 }
419                 try {
420                     elt.setAttribute(S_RS_AUTO_INCREMENT,
421                             String.valueOf(rsmd.isAutoIncrement(i)));
422                 } catch (SQLException JavaDoc e) {
423                     elt.setAttribute(S_RS_AUTO_INCREMENT, S_NOT_SUPPORTED);
424                 }
425                 try {
426                     elt.setAttribute(S_RS_CASE_SENSITIVE,
427                             String.valueOf(rsmd.isCaseSensitive(i)));
428                 } catch (SQLException JavaDoc e) {
429                     elt.setAttribute(S_RS_CASE_SENSITIVE, S_NOT_SUPPORTED);
430                 }
431                 try {
432                     elt.setAttribute(S_RS_CURRENCY,
433                             String.valueOf(rsmd.isCurrency(i)));
434                 } catch (SQLException JavaDoc e) {
435                     elt.setAttribute(S_RS_CURRENCY, S_NOT_SUPPORTED);
436                 }
437                 try {
438                     elt.setAttribute(S_RS_DEFINITELY_WRITABLE,
439                             String.valueOf(rsmd.isDefinitelyWritable(i)));
440                 } catch (SQLException JavaDoc e) {
441                     elt.setAttribute(S_RS_DEFINITELY_WRITABLE, S_NOT_SUPPORTED);
442                 }
443                 try {
444                     elt.setAttribute(S_RS_NULLABLE,
445                             String.valueOf(rsmd.isNullable(i)));
446                 } catch (SQLException JavaDoc e) {
447                     elt.setAttribute(S_RS_NULLABLE, S_NOT_SUPPORTED);
448                 }
449                 try {
450                     elt.setAttribute(S_RS_READ_ONLY,
451                             String.valueOf(rsmd.isReadOnly(i)));
452                 } catch (SQLException JavaDoc e) {
453                     elt.setAttribute(S_RS_READ_ONLY, S_NOT_SUPPORTED);
454                 }
455                 try {
456                     elt.setAttribute(S_RS_SEARCHABLE,
457                             String.valueOf(rsmd.isSearchable(i)));
458                 } catch (SQLException JavaDoc e) {
459                     elt.setAttribute(S_RS_SEARCHABLE, S_NOT_SUPPORTED);
460                 }
461                 try {
462                     elt.setAttribute(S_RS_SIGNED,
463                             String.valueOf(rsmd.isSigned(i)));
464                 } catch (SQLException JavaDoc e) {
465                     elt.setAttribute(S_RS_SIGNED, S_NOT_SUPPORTED);
466                 }
467                 try {
468                     elt.setAttribute(S_RS_WRITABLE,
469                             String.valueOf(rsmd.isWritable(i)));
470                 } catch (SQLException JavaDoc e) {
471                     elt.setAttribute(S_RS_WRITABLE, S_NOT_SUPPORTED);
472                 }
473                 elt.setAttribute(S_RS_COLUMN_NO_NULLS, columnNoNulls);
474                 elt.setAttribute(S_RS_COLUMN_NULLABLE, columnNullable);
475                 elt.setAttribute(S_RS_COLUMN_NULLABLE_UNKNOWN,
476                         columnNullableUnknown);
477                 ns.addElement(elt);
478             }
479             return ns;
480         } catch (SQLException JavaDoc e) {
481             appendErrorNode(e);
482         } catch (Exception JavaDoc e) {
483             throw new WrappedRuntimeException(e);
484         }
485         finally {
486             if (stmt != null) {
487                 try {
488                     stmt.close();
489                 } catch (SQLException JavaDoc e) {
490                     appendErrorNode(e);
491                 }
492             }
493         }
494         return null;
495     }
496
497     /**
498      * Returns a <code>NodeSet</code> that contains primary keys metadata.
499      * <p>
500      * <table class="usage"><tr><td class="usage"><pre>
501      *
502      * &lt;xsl:copy-of select="mta:getPrimaryKeys('ADDRESS')"/&gt;
503      * </pre></td></tr></table>
504      * <p>
505      * The returned <code>NodeSet</code> has the following format:
506      * <p>
507      * <table class="usage"><tr><td class="usage"><pre>
508      *
509      * &lt;primary-key column-name="ID"
510      * key-seq="1"
511      * pk-name="SYSTEM_PK"
512      * table-cat="#NULL"
513      * table-name="ADDRESS"
514      * table-shem="#NULL"/>
515      * </pre></td></tr></table>
516      * <p>
517      * Any <code>null</code> value returned by a
518      * {@link java.sql.ResultSetMetaData}.get*()
519      * or a {@link java.sql.ResultSet}.get*(int columnIndex) is marked
520      * as <code>#NULL</code>.
521      * <p>
522      * Any exception thrown by a {@link java.sql.ResultSetMetaData}.get*() method
523      * is marked as <code>#NOT_SUPPORTED</code>.
524      * <p>
525      * Important database errors (table does not exists, ...) are not thrown.
526      * Instead, an errors <code>NodeSet</code> is built and can be retreived by the
527      * {@link MetaDataNodeBuilder#getErrors(ExpressionContext)} method.
528      * <p>
529      * See {@link java.sql.DatabaseMetaData#getPrimaryKeys(String,String,String)}.
530      * <p>
531      * <dd><dl><dt><b>XSLT parameters:</b>
532      * <dd><b>[Mandatory/AVT]</b> name of the table.
533      * </dl></dd>
534      * <p>
535      * @param context automatically passed by the xalan extension mechanism.
536      * @param table name of the table.
537      * @return the primary keys metadata <code>NodeSet</code>.
538      * @throws org.apache.xml.utils.WrappedRuntimeException DOM error.
539      */

540     public static NodeSet getPrimaryKeys(ExpressionContext context, String JavaDoc table) {
541         table = XSLUtil.evaluate(context, table);
542         _errors = null;
543         return getPrimaryKeys(null, null, table,
544                 XSLUtil.getContextDocument(context));
545     }
546
547     /**
548      * Returns a <code>NodeSet</code> that contains primary keys metadata.
549      * <p>
550      * <table class="usage"><tr><td class="usage"><pre>
551      *
552      * &lt;xsl:copy-of select="mta:getPrimaryKeys('CAT','SCH','ADDRESS')"/&gt;
553      * </pre></td></tr></table>
554      * <p>
555      * See {@link #getPrimaryKeys(ExpressionContext,String)}.
556      * <p>
557      * <dd><dl><dt><b>XSLT parameters:</b>
558      * <dd><b>[Mandatory/AVT]</b> name of the catalog.
559      * <dd><b>[Mandatory/AVT]</b> name of the schema.
560      * <dd><b>[Mandatory/AVT]</b> name of the table.
561      * </dl></dd>
562      * <p>
563      * @param context automatically passed by the xalan extension mechanism.
564      * @param catalog name of the catalog.
565      * @param schema name of the schema.
566      * @param table name of the table.
567      * @return the primary keys metadata <code>NodeSet</code>.
568      * @throws org.apache.xml.utils.WrappedRuntimeException DOM error.
569      */

570     public static NodeSet getPrimaryKeys(ExpressionContext context,
571             String JavaDoc catalog,
572             String JavaDoc schema,
573             String JavaDoc table) {
574         catalog = XSLUtil.evaluate(context, catalog);
575         schema = XSLUtil.evaluate(context, schema);
576         table = XSLUtil.evaluate(context, table);
577         _errors = null;
578         return getPrimaryKeys(catalog, schema, table,
579                 XSLUtil.getContextDocument(context));
580     }
581
582     /**
583      * Returns a <code>NodeSet</code> that contains primary keys metadata.
584      * <p>
585      * See {@link #getPrimaryKeys(ExpressionContext,String)}.
586      * <p>
587      * @param catalog name of the catalog.
588      * @param schema name of the schema.
589      * @param table name of the table.
590      * @param doc a <code>Document</code>.
591      * @return the <code>ResultSet</code> metadata <code>NodeSet</code>.
592      * @throws org.apache.xml.utils.WrappedRuntimeException DOM error.
593      */

594     protected static NodeSet getPrimaryKeys(String JavaDoc catalog,
595             String JavaDoc schema,
596             String JavaDoc table,
597             Document JavaDoc doc) {
598         ResultSet JavaDoc rs = null;
599
600         try {
601             rs = getConnection().getMetaData().getPrimaryKeys(catalog, schema,
602                     table);
603             NodeSet ns = new NodeSet();
604
605             ns.setShouldCacheNodes(true);
606             while (rs.next()) {
607                 Element JavaDoc elt = doc.createElement(S_PK_NODE_NAME);
608
609                 elt.setAttribute(S_PK_TABLE_CAT, noNull(rs.getString(1)));
610                 elt.setAttribute(S_PK_TABLE_SCHEM, noNull(rs.getString(2)));
611                 elt.setAttribute(S_PK_TABLE_NAME, noNull(rs.getString(3)));
612                 elt.setAttribute(S_PK_COLUMN_NAME, noNull(rs.getString(4)));
613                 elt.setAttribute(S_PK_KEY_SEQ, String.valueOf(rs.getShort(5)));
614                 elt.setAttribute(S_PK_PK_NAME, noNull(rs.getString(6)));
615                 ns.addElement(elt);
616             }
617             return ns;
618         } catch (SQLException JavaDoc e) {
619             appendErrorNode(e);
620         } catch (Exception JavaDoc e) {
621             throw new WrappedRuntimeException(e);
622         }
623         finally {
624             if (rs != null) {
625                 try {
626                     rs.close();
627                 } catch (SQLException JavaDoc e) {
628                     appendErrorNode(e);
629                 }
630             }
631         }
632         return null;
633     }
634
635     /**
636      * Returns a <code>NodeSet</code> that contains imported keys metadata.
637      * <p>
638      * <table class="usage"><tr><td class="usage"><pre>
639      *
640      * &lt;xsl:copy-of select="mta:getImportedKeys('ADDRESS')"/&gt;
641      * </pre></td></tr></table>
642      * <p>
643      * The returned <code>NodeSet</code> has the following format:
644      * <p>
645      * <table class="usage"><tr><td class="usage"><pre>
646      *
647      * &lt;imported-key pktable-cat="..."
648      * pktable-shem="..."
649      * pktable-name="..."
650      * pkcolumn-name="..."
651      * fktable-cat="..."
652      * fktable-shem="..."
653      * fktable-name="..."
654      * fkcolumn-name="..."
655      * key-seq="..."
656      * update-rule="..."
657      * delete-rule="..."
658      * fk-name="..."
659      * pk-name="..."
660      * deferrability="..."/>
661      * [... Other imported-key nodes]
662      * </pre></td></tr></table>
663      * <p>
664      * Any <code>null</code> value is marked as <code>#NULL</code>.
665      * <p>
666      * Important database errors (table does not exists, ...) are not thrown.
667      * Instead, an errors <code>NodeSet</code> is built and can be retreived by the
668      * {@link MetaDataNodeBuilder#getErrors(ExpressionContext)} method.
669      * <p>
670      * See {@link java.sql.DatabaseMetaData#getImportedKeys(String,String,String)}.
671      * <p>
672      * <dd><dl><dt><b>XSLT parameters:</b>
673      * <dd><b>[Mandatory/AVT]</b> name of the table.
674      * </dl></dd>
675      * <p>
676      * @param context automatically passed by the xalan extension mechanism.
677      * @param table name of the table.
678      * @return the primary keys metadata <code>NodeSet</code>.
679      * @throws org.apache.xml.utils.WrappedRuntimeException DOM error.
680      */

681     public static NodeSet getImportedKeys(ExpressionContext context, String JavaDoc table) {
682         table = XSLUtil.evaluate(context, table);
683         _errors = null;
684         return getImportedKeys(null, null, table,
685                 XSLUtil.getContextDocument(context));
686     }
687
688     /**
689      * Returns a <code>NodeSet</code> that contains imported keys metadata.
690      * <p>
691      * <table class="usage"><tr><td class="usage"><pre>
692      *
693      * &lt;xsl:copy-of select="mta:getImportedKeys('CAT','SCH','ADDRESS')"/&gt;
694      * </pre></td></tr></table>
695      * <p>
696      * See {@link #getImportedKeys(ExpressionContext,String)}.
697      * <p>
698      * <dd><dl><dt><b>XSLT parameters:</b>
699      * <dd><b>[Mandatory/AVT]</b> name of the catalog.
700      * <dd><b>[Mandatory/AVT]</b> name of the schema.
701      * <dd><b>[Mandatory/AVT]</b> name of the table.
702      * </dl></dd>
703      * <p>
704      * @param context automatically passed by the xalan extension mechanism.
705      * @param catalog name of the catalog.
706      * @param schema name of the schema.
707      * @param table name of the table.
708      * @return the primary keys metadata <code>NodeSet</code>.
709      * @throws org.apache.xml.utils.WrappedRuntimeException DOM error.
710      */

711     public static NodeSet getImportedKeys(ExpressionContext context,
712             String JavaDoc catalog,
713             String JavaDoc schema,
714             String JavaDoc table) {
715         catalog = XSLUtil.evaluate(context, catalog);
716         schema = XSLUtil.evaluate(context, schema);
717         table = XSLUtil.evaluate(context, table);
718         _errors = null;
719         return getImportedKeys(catalog, schema, table,
720                 XSLUtil.getContextDocument(context));
721     }
722
723     /**
724      * Returns a <code>NodeSet</code> that contains imported keys metadata.
725      * <p>
726      * See {@link #getImportedKeys(ExpressionContext,String)}.
727      * <p>
728      * @param catalog name of the catalog.
729      * @param schema name of the schema.
730      * @param table name of the table.
731      * @param doc a <code>Document</code>.
732      * @return the <code>ResultSet</code> metadata <code>NodeSet</code>.
733      * @throws org.apache.xml.utils.WrappedRuntimeException DOM error.
734      */

735     protected static NodeSet getImportedKeys(String JavaDoc catalog,
736             String JavaDoc schema,
737             String JavaDoc table,
738             Document JavaDoc doc) {
739         ResultSet JavaDoc rs = null;
740
741         try {
742             rs = getConnection().getMetaData().getImportedKeys(catalog, schema,
743                     table);
744             NodeSet ns = new NodeSet();
745
746             ns.setShouldCacheNodes(true);
747             getImportedOrExportedKeys(rs, S_EIK_IK_NODE_NAME, ns, doc);
748             return ns;
749         } catch (SQLException JavaDoc e) {
750             appendErrorNode(e);
751         } catch (Exception JavaDoc e) {
752             throw new WrappedRuntimeException(e);
753         }
754         finally {
755             if (rs != null) {
756                 try {
757                     rs.close();
758                 } catch (SQLException JavaDoc e) {
759                     appendErrorNode(e);
760                 }
761             }
762         }
763         return null;
764     }
765
766     /**
767      * Returns a <code>NodeSet</code> that contains exported keys metadata.
768      * <p>
769      * <table class="usage"><tr><td class="usage"><pre>
770      *
771      * &lt;xsl:copy-of select="mta:getExportedKeys('ADDRESS')"/&gt;
772      * </pre></td></tr></table>
773      * <p>
774      * The returned <code>NodeSet</code> has the following format:
775      * <p>
776      * <table class="usage"><tr><td class="usage"><pre>
777      *
778      * &lt;exported-key pktable-cat="..."
779      * pktable-shem="..."
780      * pktable-name="..."
781      * pkcolumn-name="..."
782      * fktable-cat="..."
783      * fktable-shem="..."
784      * fktable-name="..."
785      * fkcolumn-name="..."
786      * key-seq="..."
787      * update-rule="..."
788      * delete-rule="..."
789      * fk-name="..."
790      * pk-name="..."
791      * deferrability="..."/>
792      * [... Other exported-key nodes]
793      * </pre></td></tr></table>
794      * <p>
795      * Any <code>null</code> value is marked as <code>#NULL</code>.
796      * <p>
797      * Important database errors (table does not exists, ...) are not thrown.
798      * Instead, an errors <code>NodeSet</code> is built and can be retreived by the
799      * {@link MetaDataNodeBuilder#getErrors(ExpressionContext)} method.
800      * <p>
801      * See {@link java.sql.DatabaseMetaData#getExportedKeys(String,String,String)}.
802      * <p>
803      * <dd><dl><dt><b>XSLT parameters:</b>
804      * <dd><b>[Mandatory/AVT]</b> name of the table.
805      * </dl></dd>
806      * <p>
807      * @param context automatically passed by the xalan extension mechanism.
808      * @param table name of the table.
809      * @return the primary keys metadata <code>NodeSet</code>.
810      * @throws org.apache.xml.utils.WrappedRuntimeException DOM error.
811      */

812     public static NodeSet getExportedKeys(ExpressionContext context, String JavaDoc table) {
813         table = XSLUtil.evaluate(context, table);
814         _errors = null;
815         return getExportedKeys(null, null, table,
816                 XSLUtil.getContextDocument(context));
817     }
818
819     /**
820      * Returns a <code>NodeSet</code> that contains exported keys metadata.
821      * <p>
822      * <table class="usage"><tr><td class="usage"><pre>
823      *
824      * &lt;xsl:copy-of select="mta:getExportedKeys('CAT','SCH','ADDRESS')"/&gt;
825      * </pre></td></tr></table>
826      * <p>
827      * See {@link #getExportedKeys(ExpressionContext,String)}.
828      * <p>
829      * <dd><dl><dt><b>XSLT parameters:</b>
830      * <dd><b>[Mandatory/AVT]</b> name of the catalog.
831      * <dd><b>[Mandatory/AVT]</b> name of the schema.
832      * <dd><b>[Mandatory/AVT]</b> name of the table.
833      * </dl></dd>
834      * <p>
835      * @param context automatically passed by the xalan extension mechanism.
836      * @param catalog name of the catalog.
837      * @param schema name of the schema.
838      * @param table name of the table.
839      * @return the primary keys metadata <code>NodeSet</code>.
840      * @throws org.apache.xml.utils.WrappedRuntimeException DOM error.
841      */

842     public static NodeSet getExportedKeys(ExpressionContext context,
843             String JavaDoc catalog,
844             String JavaDoc schema,
845             String JavaDoc table) {
846         catalog = XSLUtil.evaluate(context, catalog);
847         schema = XSLUtil.evaluate(context, schema);
848         table = XSLUtil.evaluate(context, table);
849         _errors = null;
850         return getExportedKeys(catalog, schema, table,
851                 XSLUtil.getContextDocument(context));
852     }
853
854     /**
855      * Returns a <code>NodeSet</code> that contains exported keys metadata.
856      * <p>
857      * See {@link #getExportedKeys(ExpressionContext,String)}.
858      * <p>
859      * @param catalog name of the catalog.
860      * @param schema name of the schema.
861      * @param table name of the table.
862      * @param doc a <code>Document</code>.
863      * @return the <code>ResultSet</code> metadata <code>NodeSet</code>.
864      * @throws org.apache.xml.utils.WrappedRuntimeException DOM error.
865      */

866     protected static NodeSet getExportedKeys(String JavaDoc catalog,
867             String JavaDoc schema,
868             String JavaDoc table,
869             Document JavaDoc doc) {
870         ResultSet JavaDoc rs = null;
871
872         try {
873             rs = getConnection().getMetaData().getExportedKeys(catalog, schema,
874                     table);
875             NodeSet ns = new NodeSet();
876
877             ns.setShouldCacheNodes(true);
878             getImportedOrExportedKeys(rs, S_EIK_EK_NODE_NAME, ns, doc);
879             return ns;
880         } catch (SQLException JavaDoc e) {
881             appendErrorNode(e);
882         } catch (Exception JavaDoc e) {
883             throw new WrappedRuntimeException(e);
884         }
885         finally {
886             if (rs != null) {
887                 try {
888                     rs.close();
889                 } catch (SQLException JavaDoc e) {
890                     appendErrorNode(e);
891                 }
892             }
893         }
894         return null;
895     }
896
897     /**
898      * Returns a <code>NodeSet</code> that contains exported or imported
899      * keys metadata.
900      * <p>
901      * See {@link #getExportedKeys(ExpressionContext,String)} and
902      * {@link #getImportedKeys(ExpressionContext,String)}.
903      * <p>
904      * @param rs a ResutSet with imported or exported keys metadata.
905      * @param eltName name of the Nodes to be built.
906      * @param ns NodeSet to fill with Nodes.
907      * @param doc a <code>Document</code>.
908      * @throws java.lang.Exception any...
909      */

910     protected static void getImportedOrExportedKeys(ResultSet JavaDoc rs,
911             String JavaDoc eltName,
912             NodeSet ns,
913             Document JavaDoc doc)
914         throws Exception JavaDoc {
915         while (rs.next()) {
916             Element JavaDoc elt = doc.createElement(eltName);
917
918             elt.setAttribute(S_EIK_PKTABLE_CAT, noNull(rs.getString(1)));
919             elt.setAttribute(S_EIK_PKTABLE_SCHEM, noNull(rs.getString(2)));
920             elt.setAttribute(S_EIK_PKTABLE_NAME, noNull(rs.getString(3)));
921             elt.setAttribute(S_EIK_PKCOLUMN_NAME, noNull(rs.getString(4)));
922             elt.setAttribute(S_EIK_FKTABLE_CAT, noNull(rs.getString(5)));
923             elt.setAttribute(S_EIK_FKTABLE_SCHEM, noNull(rs.getString(6)));
924             elt.setAttribute(S_EIK_FKTABLE_NAME, noNull(rs.getString(7)));
925             elt.setAttribute(S_EIK_FKCOLUMN_NAME, noNull(rs.getString(8)));
926             elt.setAttribute(S_EIK_KEY_SEQ, String.valueOf(rs.getShort(9)));
927             elt.setAttribute(S_EIK_UPDATE_RULE, String.valueOf(rs.getShort(10)));
928             elt.setAttribute(S_EIK_DELETE_RULE, String.valueOf(rs.getShort(11)));
929             elt.setAttribute(S_EIK_FK_NAME, noNull(rs.getString(12)));
930             elt.setAttribute(S_EIK_PK_NAME, noNull(rs.getString(13)));
931             elt.setAttribute(S_EIK_DEFERRABILITY,
932                     String.valueOf(rs.getShort(14)));
933             ns.addElement(elt);
934         }
935     }
936
937     /**
938      * Returns a <code>NodeSet</code> that contains indexes metadata.
939      * <p>
940      * <table class="usage"><tr><td class="usage"><pre>
941      *
942      * &lt;xsl:copy-of select="mta:getIndexInfo('ADDRESS')"/&gt;
943      * </pre></td></tr></table>
944      * <p>
945      * The returned <code>NodeSet</code> has the following format:
946      * <p>
947      * <table class="usage"><tr><td class="usage"><pre>
948      *
949      * &lt;index asc-or-desc="A"
950      * cardinality="#NULL"
951      * column-name="ID"
952      * filter-condition="#NULL"
953      * index-name="SYSTEM_PK"
954      * index-qualifier="#NULL"
955      * non-unique="false"
956      * ordinal-position="1"
957      * pages="#NULL"
958      * table-cat="#NULL"
959      * table-name="ADDRESS"
960      * table-shem="#NULL" type="3"/>
961      * [... Other index nodes]
962      * </pre></td></tr></table>
963      * <p>
964      * Any <code>null</code> value is marked as <code>#NULL</code>.
965      * <p>
966      * Important database errors (table does not exists, ...) are not thrown.
967      * Instead, an errors <code>NodeSet</code> is built and can be retreived by the
968      * {@link MetaDataNodeBuilder#getErrors(ExpressionContext)} method.
969      * <p>
970      * See {@link java.sql.DatabaseMetaData#getIndexInfo(String,String,String,boolean,boolean)}.
971      * <p>
972      * <dd><dl><dt><b>XSLT parameters:</b>
973      * <dd><b>[Mandatory/AVT]</b> name of the table.
974      * </dl></dd>
975      * <p>
976      * @param context automatically passed by the xalan extension mechanism.
977      * @param table name of the table.
978      * @return the primary keys metadata <code>NodeSet</code>.
979      * @throws org.apache.xml.utils.WrappedRuntimeException DOM error.
980      */

981     public static NodeSet getIndexInfo(ExpressionContext context, String JavaDoc table) {
982         table = XSLUtil.evaluate(context, table);
983         _errors = null;
984         return getIndexInfo(null, null, table,
985                 XSLUtil.getContextDocument(context));
986     }
987
988     /**
989      * Returns a <code>NodeSet</code> that contains indexes metadata.
990      * <p>
991      * <table class="usage"><tr><td class="usage"><pre>
992      *
993      * &lt;xsl:copy-of select="mta:getIndexInfo('CAT','SCH','ADDRESS')"/&gt;
994      * </pre></td></tr></table>
995      * <p>
996      * See {@link #getIndexInfo(ExpressionContext,String)}.
997      * <p>
998      * <dd><dl><dt><b>XSLT parameters:</b>
999      * <dd><b>[Mandatory/AVT]</b> name of the catalog.
1000     * <dd><b>[Mandatory/AVT]</b> name of the schema.
1001     * <dd><b>[Mandatory/AVT]</b> name of the table.
1002     * </dl></dd>
1003     * <p>
1004     * @param context automatically passed by the xalan extension mechanism.
1005     * @param catalog name of the catalog.
1006     * @param schema name of the schema.
1007     * @param table name of the table.
1008     * @return the primary keys metadata <code>NodeSet</code>.
1009     * @throws org.apache.xml.utils.WrappedRuntimeException DOM error.
1010     */

1011    public static NodeSet getIndexInfo(ExpressionContext context,
1012            String JavaDoc catalog,
1013            String JavaDoc schema,
1014            String JavaDoc table) {
1015        catalog = XSLUtil.evaluate(context, catalog);
1016        schema = XSLUtil.evaluate(context, schema);
1017        table = XSLUtil.evaluate(context, table);
1018        _errors = null;
1019        return getIndexInfo(catalog, schema, table,
1020                XSLUtil.getContextDocument(context));
1021    }
1022
1023    /**
1024     * Returns a <code>NodeSet</code> that contains indexes metadata.
1025     * <p>
1026     * See {@link #getIndexInfo(ExpressionContext,String)}.
1027     * <p>
1028     * @param catalog name of the catalog.
1029     * @param schema name of the schema.
1030     * @param table name of the table.
1031     * @param doc a <code>Document</code>.
1032     * @return the <code>ResultSet</code> metadata <code>NodeSet</code>.
1033     * @throws org.apache.xml.utils.WrappedRuntimeException DOM error.
1034     */

1035    protected static NodeSet getIndexInfo(String JavaDoc catalog,
1036            String JavaDoc schema,
1037            String JavaDoc table,
1038            Document JavaDoc doc) {
1039        ResultSet JavaDoc rs = null;
1040
1041        try {
1042            rs = getConnection().getMetaData().getIndexInfo(catalog, schema,
1043                    table, false, false);
1044            NodeSet ns = new NodeSet();
1045
1046            ns.setShouldCacheNodes(true);
1047            while (rs.next()) {
1048                Element JavaDoc elt = doc.createElement(S_II_NODE_NAME);
1049
1050                elt.setAttribute(S_II_TABLE_CAT, noNull(rs.getString(1)));
1051                elt.setAttribute(S_II_TABLE_SCHEM, noNull(rs.getString(2)));
1052                elt.setAttribute(S_II_TABLE_NAME, noNull(rs.getString(3)));
1053                elt.setAttribute(S_II_NON_UNIQUE,
1054                        String.valueOf(rs.getBoolean(4)));
1055                elt.setAttribute(S_II_INDEX_QUALIFIER, noNull(rs.getString(5)));
1056                elt.setAttribute(S_II_INDEX_NAME, noNull(rs.getString(6)));
1057                elt.setAttribute(S_II_TYPE, String.valueOf(rs.getShort(7)));
1058                elt.setAttribute(S_II_ORDINAL_POSITION,
1059                        String.valueOf(rs.getShort(8)));
1060                elt.setAttribute(S_II_COLUMN_NAME, noNull(rs.getString(9)));
1061                elt.setAttribute(S_II_ASC_OR_DESC, noNull(rs.getString(10)));
1062                elt.setAttribute(S_II_CARDINALITY, noNull(rs.getString(11)));
1063                elt.setAttribute(S_II_PAGES, noNull(rs.getString(12)));
1064                elt.setAttribute(S_II_FILTER_CONDITION, noNull(rs.getString(13)));
1065                ns.addElement(elt);
1066            }
1067            return ns;
1068        } catch (SQLException JavaDoc e) {
1069            appendErrorNode(e);
1070        } catch (Exception JavaDoc e) {
1071            throw new WrappedRuntimeException(e);
1072        }
1073        finally {
1074            if (rs != null) {
1075                try {
1076                    rs.close();
1077                } catch (SQLException JavaDoc e) {
1078                    appendErrorNode(e);
1079                }
1080            }
1081        }
1082        return null;
1083    }
1084
1085    /**
1086     * Appends an error Node to the current _errors NodeSet.
1087     * <p>
1088     * @param e the exception that must be add as an error Node.
1089     * @throws org.apache.xml.utils.WrappedRuntimeException DOM error.
1090     */

1091    protected static void appendErrorNode(Exception JavaDoc e) {
1092        if (e == null) {
1093            return;
1094        }
1095        int errCode = 0;
1096
1097        if (e instanceof SQLException JavaDoc) {
1098            errCode = ((SQLException JavaDoc) e).getErrorCode();
1099        }
1100        Document JavaDoc doc = null;
1101
1102        if (_errors == null) {
1103            _errors = new NodeSet();
1104            _errors.setShouldCacheNodes(true);
1105            doc = DOMUtil.newDocument();
1106        } else {
1107            doc = _errors.getRoot() != null
1108                    ? _errors.getRoot().getOwnerDocument()
1109                    : DOMUtil.newDocument();
1110        }
1111        try {
1112            Element JavaDoc elt = doc.createElement(S_EXCEPTION_NODE_NAME);
1113
1114            elt.setAttribute(S_EXCEPTION_CLASS_NAME, e.getClass().getName());
1115            elt.setAttribute(S_EXCEPTION_CODE, String.valueOf(errCode));
1116            elt.appendChild(doc.createTextNode(e.getMessage()));
1117            _errors.addElement(elt);
1118        } catch (Exception JavaDoc f) {
1119            throw new WrappedRuntimeException(f);
1120        }
1121    }
1122
1123    /**
1124     * Returns s if s is not <code>null</code>, "#NULL" otherwise.
1125     * <p>
1126     * @param s the String to test for nullity.
1127     * @return the s if s is not <code>null</code>, "#NULL" otherwise.
1128     */

1129    protected static String JavaDoc noNull(String JavaDoc s) {
1130        return (s == null) ? S_NULL : s;
1131    }
1132}
1133
Popular Tags