KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > extractor > metadata > MetaDataManager


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.extractor.metadata;
24
25 import java.io.IOException JavaDoc;
26 import java.io.StringReader JavaDoc;
27 import java.io.StringWriter JavaDoc;
28 import java.util.*;
29
30 import org.xml.sax.InputSource JavaDoc;
31 import org.xml.sax.SAXException JavaDoc;
32 import org.xquark.extractor.algebra.CollectionName;
33 import org.xquark.extractor.runtime.Selection;
34 import org.xquark.jdbc.typing.DBMSInfo;
35 import org.xquark.schema.SchemaReader;
36 import org.xquark.schema.Type;
37 import org.xquark.serialize.XMLSerializer;
38 import org.xquark.xml.xdbc.XMLDBCException;
39 import org.xquark.xquery.metadata.*;
40 import org.xquark.xquery.parser.*;
41 import org.xquark.xquery.typing.QType;
42 import org.xquark.xquery.typing.QTypeElement;
43
44 /**
45  * Object that encapsulates relational metadata information for a data source (
46  * a relational database. This object is also used, basing on this information,
47  * to build medatata used by XQuery parser and the XDBC API
48  * (@see org.xquark.xquery.metadata.MetaDataImpl}).
49  */

50 public class MetaDataManager {
51     private static final String JavaDoc RCSRevision = "$Revision: 1.17 $";
52     private static final String JavaDoc RCSName = "$Name: $";
53
54     /** root NamedNode of the tree repressenting the relational metadata (site,
55      * catalogs, schema, tables, views. A site is another term for data source.
56      * It is loaded by @link Loader basing on @link Selection.
57      */

58     private Site _site = null;
59     private Properties _tableMap = null;
60     private Map _viewMap = null;
61     private DBMSInfo dbmsInfo = null;
62     
63     public MetaDataManager(DBMSInfo dbmsInfo) {
64         this.dbmsInfo = dbmsInfo;
65     }
66
67     public void setMetaData(Site site) {
68         _site = site;
69     }
70
71     public List findTables(CollectionName colName) {
72         //Trace.enter(this, "findTables(Collection_Name colName)", colName.pprint());
73
List retVal = findTables(colName.getCatalog(), colName.getSchema(), colName.getTable());
74         // if (null == retVal || retVal.isEmpty()) {
75
// retVal = findTables(colName.getCatalog(), colName.getSchema(), _encoder.decode(colName.getTable()));
76
// }
77
//Trace.enter(this, "findTables(Collection_Name colName)", colName.pprint());
78
return retVal;
79     }
80
81     private List findTables(String JavaDoc catalogName, String JavaDoc schemaName, String JavaDoc tableName) {
82         //Trace.enter(this, "findTables(String catalog String schema,String table)");
83

84         List retVal = new ArrayList();
85
86         List nodeList = null;
87         Catalog node = null;
88
89         if (null == catalogName || catalogName.equals("*")) {
90             nodeList = _site.getChildren();
91             for (int i = 0; i < nodeList.size(); i++) {
92                 node = (Catalog) nodeList.get(i);
93                 retVal.addAll(findTables(node, schemaName, tableName));
94             }
95         } else {
96             node = (Catalog) _site.findChild(catalogName);
97             if (null != node) {
98                 retVal.addAll(findTables(node, schemaName, tableName));
99             }
100         }
101
102         //Trace.exit(this, "findTables(String catalog String schema,String table)");
103
return retVal;
104     }
105
106     private List findTables(Catalog catalog, String JavaDoc schemaName, String JavaDoc tableName) {
107         //Trace.enter(this, "findTables(Catalog catalog, String schemaName, String tableName)", catalog.getName());
108

109         List retVal = new ArrayList();
110
111         List nodeList = null;
112         Schema node = null;
113
114         if (null == schemaName || schemaName.equals("*")) {
115             nodeList = catalog.getChildren();
116             for (int i = 0; i < nodeList.size(); i++) {
117                 node = (Schema) nodeList.get(i);
118                 List tables = findTables(node, tableName);
119                 if (null != tables) {
120                     retVal.addAll(findTables(node, tableName));
121                 }
122             }
123         } else {
124             node = (Schema) catalog.findChild(schemaName);
125             if (null != node) {
126                 retVal.addAll(findTables(node, tableName));
127             }
128         }
129
130         //Trace.exit(this, "findTables(Catalog catalog, String schemaName, String tableName)", catalog.getName());
131
return retVal;
132     }
133
134     private List findTables(Schema schema, String JavaDoc tableName) {
135         //Trace.enter(this, "findTables(Schema schema, String tableName)", schema.getName());
136

137         List retVal = null;
138         Table node = null;
139
140         if (null == tableName || tableName.equals("*")) {
141             retVal = schema.getChildren();
142         } else {
143             node = (Table) schema.findChildByAlias(tableName);
144             if (null != node) {
145                 retVal = new ArrayList();
146                 retVal.add(node);
147             }
148         }
149
150         //Trace.exit(this, "findTables(Schema schema, String tableName)", schema.getName());
151
return retVal;
152     }
153
154     public void setTableMap(Properties tableMap) {
155         _tableMap = tableMap;
156     }
157
158     public void setViewMap(Map viewMap) {
159         _viewMap = viewMap;
160     }
161
162     public String JavaDoc originalNameOf(String JavaDoc fullEncodedName) {
163         return _tableMap.getProperty(fullEncodedName);
164     }
165
166     public String JavaDoc pprint() {
167         String JavaDoc retVal = null;
168         return retVal;
169     }
170
171     /**
172      * Builds metadata used by XQuery parser and for XDBC API.
173      * @param name datasource symbolic name read in the configuration file or a
174      * default one.
175      * @return
176      * @throws XMLDBCException
177      */

178     public MetaDataImpl buildMetaDataForParser(String JavaDoc name) throws XMLDBCException {
179
180         if (_site == null)
181             return null;
182         StringBuffer JavaDoc bufSchema = new StringBuffer JavaDoc();
183         StringBuffer JavaDoc bufCollections = new StringBuffer JavaDoc();
184
185         List catalogs = this._site.getChildren();
186         //org.xquark.xml.misc.Debug.print("Catalog list = " + catalogs);
187
String JavaDoc nameSpace = null;
188         if (catalogs != null)
189             for (int cat = 0; cat < catalogs.size(); cat++) {
190                 Catalog catalog = (Catalog) catalogs.get(cat);
191
192                 List schemas = catalog.getChildren();
193                 //org.xquark.xml.misc.Debug.print("schemas = " + schemas);
194

195                 if (schemas != null) {
196                     for (int i = 0; i < schemas.size(); i++) {
197                         Schema schema = (Schema) schemas.get(i);
198                         //org.xquark.xml.misc.Debug.print("schema = " + schema);
199

200                         String JavaDoc targetNamespace = schema.getTargetNamespace();
201                         //org.xquark.xml.misc.Debug.print("targetNamespace = " + targetNamespace);
202

203                         int elementFormDefault = schema.getElementFormDefault();
204                         //org.xquark.xml.misc.Debug.print("elementFormDefault = " + elementFormDefault);
205

206                         bufSchema.append("\t\t<schema");
207                         bufSchema.append("\n\t\t\txmlns=\"http://www.w3.org/2001/XMLSchema\"");
208                         bufSchema.append("\n\t\t\telementFormDefault=\"" + Selection.ELEMENT_FORM_DEFAULT[elementFormDefault] + "\"");
209                         if (targetNamespace == null) {
210                             bufSchema.append(">");
211                             /*
212                             bufSchema.append("targetNamespace=\"");
213                             bufSchema.append("http://");
214                             if (catalog.getName() != null) bufSchema.append(catalog.getName());
215                             bufSchema.append(schema.getName());
216                             bufSchema.append("\">");
217                              */

218                         } else {
219                             /*
220                             bufSchema.append("\n\t\t\t\txmlns:");
221                             bufSchema.append(localns);
222                             bufSchema.append("=\"http://www.xquark.org/");
223                             bufSchema.append(name);
224                             bufSchema.append("Schema\"");
225                              */

226                             bufSchema.append(" targetNamespace=\"");
227                             bufSchema.append(targetNamespace);
228                             bufSchema.append("\">");
229                         }
230
231                         List tables = schema.getChildren();
232                         //org.xquark.xml.misc.Debug.print("tables = " + tables);
233
if (tables != null) {
234                             for (int j = 0; j < tables.size(); j++) {
235                                 Table table = (Table) tables.get(j);
236
237                                 List attributes = table.getChildren();
238                                 if (attributes == null)
239                                     continue;
240
241                                 String JavaDoc collectionName = table.getAlias();
242                                 String JavaDoc fullCollectionName = table.getAlias();
243                                 if (schema.getName() != null && schema.getName().length() != 0)
244                                     fullCollectionName = schema.getName() + "." + fullCollectionName;
245                                 if (catalog.getName() != null && catalog.getName().length() != 0)
246                                     fullCollectionName = catalog.getName() + "." + fullCollectionName;
247
248                                 bufSchema.append("\n\t\t\t<element name=\"");
249                                 //bufTable.append(fullCollectionName);
250
bufSchema.append(collectionName);
251                                 bufSchema.append("\">");
252                                 bufSchema.append("\n\t\t\t\t<complexType>");
253
254                                 bufSchema.append("\n\t\t\t\t\t<sequence>");
255                                 bufCollections.append("\n\t\t<collection name=\"");
256                                 bufCollections.append(fullCollectionName);
257                                 //bufCollections.append(collectionName);
258
bufCollections.append("\">");
259                                 if (targetNamespace == null) {
260                                     bufCollections.append("\n\t\t\t<step name=\"");
261                                     bufCollections.append(collectionName);
262                                     bufCollections.append("\">");
263                                 } else {
264                                     bufCollections.append("\n\t\t\t<step ns=\"");
265                                     bufCollections.append(targetNamespace);
266                                     bufCollections.append("\" name=\"");
267                                     bufCollections.append(collectionName);
268                                     bufCollections.append("\">");
269                                 }
270
271                                 for (int k = 0; k < attributes.size(); k++) {
272                                     Attribute attribute = (Attribute) attributes.get(k);
273                                     ExtractorMappingInfo mappingInfo = attribute.getMappingInfo();
274
275                                     bufSchema.append("\n\t\t\t\t\t\t<element name=\"");
276                                     bufSchema.append(attribute.getAlias());
277                                     if (mappingInfo.getColumnMetaData().isNullable()) {
278                                         bufSchema.append("\" minOccurs=\"0");
279                                     }
280                                     if (mappingInfo.isBuiltInXMLType()) {
281                                         bufSchema.append("\" type=\"");
282                                         bufSchema.append(mappingInfo.getStringXMLType());
283                                         bufSchema.append("\"/>");
284                                     }
285                                     else {
286                                         bufSchema.append("\">");
287                                         bufSchema.append("\n\t\t\t\t\t\t\t");
288                                         bufSchema.append(mappingInfo.getStringXMLType());
289                                         bufSchema.append("\n\t\t\t\t\t\t</element>");
290                                     }
291
292                                     if (Selection.QUALIFIED != elementFormDefault || targetNamespace == null) {
293                                         bufCollections.append("\n\t\t\t\t<step name=\"");
294                                         bufCollections.append(attribute.getAlias());
295                                         bufCollections.append("\"/>");
296                                     } else {
297                                         bufCollections.append("\n\t\t\t\t<step ns=\"");
298                                         bufCollections.append(targetNamespace);
299                                         bufCollections.append("\" name=\"");
300                                         bufCollections.append(attribute.getAlias());
301                                         bufCollections.append("\"/>");
302                                     }
303                                 }
304
305                                 bufSchema.append("\n\t\t\t\t\t</sequence>");
306                                 bufSchema.append("\n\t\t\t\t</complexType>");
307                                 bufSchema.append("\n\t\t\t</element>");
308
309                                 bufCollections.append("\n\t\t\t</step>");
310                                 bufCollections.append("\n\t\t</collection>");
311                             }
312                         }
313                         bufSchema.append("\n\t\t</schema>");
314                     }
315                 }
316             }
317
318         MetaDataImpl metadata = new MetaDataImpl(null);
319
320         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
321         buf.append("<?xml version='1.0'?>");
322         buf.append("\n<xm:metadata");
323         buf.append("\n\txmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
324         buf.append("\n\txmlns:xm=\"http://www.xquark.org/XMLDBC/Metadata\"");
325         buf.append("\n\tsource=\"");
326         buf.append(name);
327         buf.append("\">");
328         buf.append("\n\t<schemas>\n");
329         buf.append(bufSchema.toString());
330         buf.append("\n\t</schemas>");
331         buf.append("\n\t<collections>");
332         buf.append(bufCollections.toString());
333         buf.append("\n\t</collections>");
334         buf.append("\n</xm:metadata>");
335
336         //Trace.log(this, "buildMetaDataForParser(String name)", "metadata schema :");
337
//Trace.log(this, "buildMetaDataForParser(String name)", buf.toString());
338

339         //System.out.println(buf.toString());
340

341         try {
342             MetaParser metaparser = new MetaParser(metadata);
343             MetaWrapper metawrapper = metaparser.createWrapper(new InputSource JavaDoc(new StringReader JavaDoc(buf.toString())), true);
344             metadata.addMetaWrapper(metawrapper);
345         } catch (MetadataException e) {
346             throw new XMLDBCException("build metadata failed", e);
347         }
348
349         if (_viewMap == null || _viewMap.isEmpty()) return metadata;
350
351         // metadata of views
352
// add schemas and collections
353
XQueryParser parser = null;
354         StringWriter JavaDoc typestring = new StringWriter JavaDoc();
355         XMLSerializer serializer = new XMLSerializer(typestring);
356         serializer.setIndent(true);
357         SchemaReader schemareader = new SchemaReader(metadata.getSchemaManager());
358         schemareader.setContentHandler(serializer);
359         Collection keys = _viewMap.keySet();
360         for (Iterator it = keys.iterator(); it.hasNext();) {
361             String JavaDoc key = (String JavaDoc) it.next();
362             Object JavaDoc value = _viewMap.get(key);
363             if (value instanceof String JavaDoc) {
364                 try {
365                     String JavaDoc viewDefinition = (String JavaDoc) value;
366                     if (parser == null)
367                         parser = new XQueryParser(new StringReader JavaDoc("dummy"));
368                     parser.setFactory("org.xquark.xml.xqueryparser.common.ParserFactory");
369                     parser.ReInit(new StringReader JavaDoc(viewDefinition));
370                     XQueryModule module = parser.Start(metadata,metadata.getSchemaManager(),metadata.getModuleManager(),new VarCounter());
371                     XQueryExpression expr = null;
372                     QType qtype = null;
373                     Type type = null;
374                     QName elementname = null;
375                     String JavaDoc targetNamespace = null;
376                     expr = (XQueryExpression)module.getExpressions().get(0);
377                     qtype = expr.getQType();
378                     type = qtype.getType();
379                     elementname = (QName)((QTypeElement)qtype).getName();
380                     targetNamespace = elementname.getNameSpace();
381                     if (type == null || !(qtype instanceof QTypeElement)) {
382                         this._viewMap.put(key, null);
383                         continue;
384                     }
385                     this._viewMap.put(key, module);
386                     
387                     // add string to corresponding strings
388
// add to schemas
389
if (targetNamespace != null) {
390                         bufSchema.append("\n\t\t<schema");
391                         bufSchema.append("\n\t\t\txmlns=\"http://www.w3.org/2001/XMLSchema\"");
392                         bufSchema.append("\n\t\t\telementFormDefault=\"unqualified\"");
393                         if (targetNamespace == null) {
394                             bufSchema.append(">");
395                         } else {
396                             bufSchema.append(" targetNamespace=\"");
397                             bufSchema.append(targetNamespace);
398                             bufSchema.append("\">");
399                         }
400                         bufSchema.append("\n\t\t\t<element name=\"");
401                         bufSchema.append(elementname.getName());
402                         bufSchema.append("\">");
403                         
404                         typestring.flush();
405                         schemareader.parse(type);
406                         bufSchema.append(typestring.toString());
407                         
408                         bufSchema.append("\n\t\t\t</element>");
409                         bufSchema.append("\n\t\t</schema>");
410                     }
411                     // add to collections
412
bufCollections.append("\n\t\t<collection name=\"");
413                     bufCollections.append(key);
414                     bufCollections.append("\">");
415                     bufCollections.append("\n\t\t\t<step name=\"");
416                     bufCollections.append(elementname.getName());
417                     bufCollections.append("\"/>");
418                     bufCollections.append("\n\t\t</collection>");
419                     
420                 } catch (XQueryException e) {
421                     throw new XMLDBCException("Could not parse view definition", e);
422                 } catch (SAXException JavaDoc e) {
423                     throw new XMLDBCException("Could not parse view definition", e);
424                 } catch (IOException JavaDoc e) {
425                     throw new XMLDBCException("Could not parse view definition", e);
426                 } catch (ParseException e) {
427                     throw new XMLDBCException("Could not parse view definition", e);
428                 }
429             }
430         }
431         // create metadata string
432
buf.setLength(0);
433         buf.append("<?xml version='1.0'?>");
434         buf.append("\n<xm:metadata");
435         buf.append("\n\txmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
436         buf.append("\n\txmlns:xm=\"http://www.xquark.org/XMLDBC/Metadata\"");
437         buf.append("\n\tsource=\"");
438         buf.append(name);
439         buf.append("\">");
440         buf.append("\n\t<schemas>\n");
441         buf.append(bufSchema.toString());
442         buf.append("\n\t</schemas>");
443         buf.append("\n\t<collections>");
444         buf.append(bufCollections.toString());
445         buf.append("\n\t</collections>");
446         buf.append("\n</xm:metadata>");
447
448         //Trace.log(this, "buildMetaDataForParser(String name)", "metadata schema :");
449
//Trace.log(this, "buildMetaDataForParser(String name)", buf.toString());
450
//System.out.println(buf.toString());
451

452         metadata = new MetaDataImpl(null);
453         try {
454             MetaParser metaparser = new MetaParser(metadata);
455             MetaWrapper metawrapper = metaparser.createWrapper(new InputSource JavaDoc(new StringReader JavaDoc(buf.toString())), true);
456             metadata.addMetaWrapper(metawrapper);
457         } catch (MetadataException e) {
458             throw new XMLDBCException("build metadata failed", e);
459         }
460         return metadata;
461     }
462     public DBMSInfo getDbmsInfo() {
463         return dbmsInfo;
464     }
465
466 }
467
Popular Tags