KickJava   Java API By Example, From Geeks To Geeks.

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


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.XSLUtil;
24 import org.w3c.dom.Document JavaDoc;
25 import org.w3c.dom.Element JavaDoc;
26 import org.w3c.dom.Node JavaDoc;
27 import org.apache.xalan.extensions.ExpressionContext;
28 import org.apache.xpath.NodeSet;
29 import org.apache.xml.utils.WrappedRuntimeException;
30
31 /**
32  * Table metadata <b>abstract</b> class
33  * (see {@link org.ejen.ext.db.BasicMetaDataConnection} for usage).
34  * @author F. Wolff
35  * @version 1.0
36  */

37 public abstract class TableMetaDataNodeBuilder extends MetaDataNodeBuilder {
38     public static final String JavaDoc S_TABLE_METADATA_NODE_NAME = "metadata";
39
40     /**
41      * Gets table metadata.
42      * <p>
43      * <table class="usage"><tr><td class="usage"><pre>
44      *
45      * &lt;xsl:copy-of select="mta:getTableMetaData('ADDRESS')"/&gt;
46      * </pre></td></tr></table>
47      * <p>
48      * The returned <code>Node</code> has the following format:
49      * <p>
50      * <table class="usage"><tr><td class="usage"><pre>
51      *
52      * &lt;metadata&gt;
53      * &lt;primary-key column-name="ID"
54      * key-seq="1"
55      * pk-name="SYSTEM_PK"
56      * table-cat="#NULL"
57      * table-name="ADDRESS"
58      * table-shem="#NULL"/>
59      * &lt;index asc-or-desc="A"
60      * cardinality="#NULL"
61      * column-name="ID"
62      * filter-condition="#NULL"
63      * index-name="SYSTEM_PK"
64      * index-qualifier="#NULL"
65      * non-unique="false"
66      * ordinal-position="1"
67      * pages="#NULL"
68      * table-cat="#NULL"
69      * table-name="ADDRESS"
70      * table-shem="#NULL" type="3"/>
71      * [... Other index nodes]
72      * &lt;column auto-increment="false"
73      * case-sensitive="true"
74      * catalog-name=""
75      * column-class-name="#NOT_SUPPORTED"
76      * column-display-size="0"
77      * column-label="ID"
78      * column-name="ID"
79      * column-no-nulls="0"
80      * column-nullable="1"
81      * column-nullable-unknown="2"
82      * column-type="4"
83      * column-type-name="INTEGER"
84      * currency="false"
85      * definitely-writable="true"
86      * nullable="1"
87      * precision="0"
88      * read-only="false"
89      * scale="0"
90      * schema-name=""
91      * searchable="true"
92      * sequence="true"
93      * signed="true"
94      * table-name="ADDRESS"
95      * writable="true"/>
96      * [... Other column nodes]
97      * &lt;/metadata&gt;
98      * </pre></td></tr></table>
99      * <p>
100      * Any <code>null</code> value returned by a
101      * {@link java.sql.ResultSetMetaData}.get*() or a
102      * {@link java.sql.ResultSet}.get*(int columnIndex) is marked as
103      * <code>#NULL</code>.
104      * <p>
105      * Any exception thrown by a {@link java.sql.ResultSetMetaData}.get*() method
106      * is marked as <code>#NOT_SUPPORTED</code>.
107      * <p>
108      * Important database errors (table does not exists, ...) are not thrown. Instead,
109      * an errors <code>NodeSet</code> is built and can be retreived by the
110      * {@link MetaDataNodeBuilder#getErrors(ExpressionContext)} method.
111      * <p>
112      * See {@link MetaDataNodeBuilder#getPrimaryKeys(ExpressionContext,String)},
113      * {@link MetaDataNodeBuilder#getIndexInfo(ExpressionContext,String)} and
114      * {@link MetaDataNodeBuilder#getResultSetMetaData(ExpressionContext,String)}.
115      * <p>
116      * <dd><dl><dt><b>XSLT parameters:</b>
117      * <dd><b>[Mandatory/AVT]</b> name of the table.
118      * </dl></dd>
119      * <p>
120      * @param context automatically passed by the xalan extension mechanism.
121      * @param table table name from which metadata are to be retreived.
122      * @return a <code>Node</code> containing the table metadata informations.
123      */

124     public static Node JavaDoc getTableMetaData(ExpressionContext context, String JavaDoc table) {
125         table = XSLUtil.evaluate(context, table);
126         _errors = null;
127         return getTableMetaData(null, null, table,
128                 XSLUtil.getContextDocument(context));
129     }
130
131     /**
132      * Gets table metadata.
133      * <p>
134      * <table class="usage"><tr><td class="usage"><pre>
135      *
136      * &lt;xsl:copy-of select="mta:getTableMetaData('CAT','SCH','ADDRESS')"/&gt;
137      * </pre></td></tr></table>
138      * <p>
139      * See {@link #getTableMetaData(ExpressionContext,String)}.
140      * <p>
141      * <dd><dl><dt><b>XSLT parameters:</b>
142      * <dd><b>[Mandatory/AVT]</b> name of the catalog.
143      * <dd><b>[Mandatory/AVT]</b> name of the schema.
144      * <dd><b>[Mandatory/AVT]</b> name of the table.
145      * </dl></dd>
146      * <p>
147      * @param context automatically passed by the xalan extension mechanism.
148      * @param catalog the table catalog name.
149      * @param schema the table schema name.
150      * @param table the table name from which metadata are to be retreived.
151      * @return a <code>Node</code> containing the table metadata informations.
152      */

153     public static Node JavaDoc getTableMetaData(ExpressionContext context,
154             String JavaDoc catalog,
155             String JavaDoc schema,
156             String JavaDoc table) {
157         catalog = XSLUtil.evaluate(context, catalog);
158         schema = XSLUtil.evaluate(context, schema);
159         table = XSLUtil.evaluate(context, table);
160         _errors = null;
161         return getTableMetaData(catalog, schema, table,
162                 XSLUtil.getContextDocument(context));
163     }
164
165     /**
166      * Gets table metadata.
167      * <p>
168      * See {@link #getTableMetaData(ExpressionContext,String)}.
169      * <p>
170      * @param catalog the table catalog name.
171      * @param schema the table schema name.
172      * @param table the table name from which metadata are to be retreived.
173      * @param doc a <code>Document</code> object used to build the
174      * <code>metadata Node</code>.
175      * @return a <code>Node</code> containing the table metadata informations.
176      * @throws org.apache.xml.utils.WrappedRuntimeException if something goes
177      * wrong (except database errors).
178      */

179     protected static Node JavaDoc getTableMetaData(String JavaDoc catalog,
180             String JavaDoc schema,
181             String JavaDoc table,
182             Document JavaDoc doc) {
183         NodeSet pks = getPrimaryKeys(catalog, schema, table, doc);
184         NodeSet ii = getIndexInfo(catalog, schema, table, doc);
185         NodeSet cls = getResultSetMetaData(table, "*", S_RS_COLUMN_NODE_NAME,
186                 doc);
187
188         if (pks == null || ii == null || cls == null) {
189             return null;
190         }
191         try {
192             Element JavaDoc root = doc.createElement(S_TABLE_METADATA_NODE_NAME);
193
194             for (int i = 0; i < pks.getLength(); i++) {
195                 root.appendChild(pks.item(i));
196             }
197             for (int i = 0; i < ii.getLength(); i++) {
198                 root.appendChild(ii.item(i));
199             }
200             for (int i = 0; i < cls.getLength(); i++) {
201                 root.appendChild(cls.item(i));
202             }
203             return root;
204         } catch (Exception JavaDoc e) {
205             throw new WrappedRuntimeException(e);
206         }
207     }
208 }
209
Popular Tags