1 22 23 package org.xquark.extractor.metadata; 24 25 import java.io.IOException ; 26 import java.io.StringReader ; 27 import java.io.StringWriter ; 28 import java.util.*; 29 30 import org.xml.sax.InputSource ; 31 import org.xml.sax.SAXException ; 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 50 public class MetaDataManager { 51 private static final String RCSRevision = "$Revision: 1.17 $"; 52 private static final String RCSName = "$Name: $"; 53 54 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 List retVal = findTables(colName.getCatalog(), colName.getSchema(), colName.getTable()); 74 return retVal; 79 } 80 81 private List findTables(String catalogName, String schemaName, String tableName) { 82 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 return retVal; 104 } 105 106 private List findTables(Catalog catalog, String schemaName, String tableName) { 107 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 return retVal; 132 } 133 134 private List findTables(Schema schema, String tableName) { 135 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 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 originalNameOf(String fullEncodedName) { 163 return _tableMap.getProperty(fullEncodedName); 164 } 165 166 public String pprint() { 167 String retVal = null; 168 return retVal; 169 } 170 171 178 public MetaDataImpl buildMetaDataForParser(String name) throws XMLDBCException { 179 180 if (_site == null) 181 return null; 182 StringBuffer bufSchema = new StringBuffer (); 183 StringBuffer bufCollections = new StringBuffer (); 184 185 List catalogs = this._site.getChildren(); 186 String 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 195 if (schemas != null) { 196 for (int i = 0; i < schemas.size(); i++) { 197 Schema schema = (Schema) schemas.get(i); 198 200 String targetNamespace = schema.getTargetNamespace(); 201 203 int elementFormDefault = schema.getElementFormDefault(); 204 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 218 } else { 219 226 bufSchema.append(" targetNamespace=\""); 227 bufSchema.append(targetNamespace); 228 bufSchema.append("\">"); 229 } 230 231 List tables = schema.getChildren(); 232 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 collectionName = table.getAlias(); 242 String 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 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("\">"); 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 buf = new StringBuffer (); 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 339 341 try { 342 MetaParser metaparser = new MetaParser(metadata); 343 MetaWrapper metawrapper = metaparser.createWrapper(new InputSource (new StringReader (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 XQueryParser parser = null; 354 StringWriter typestring = new StringWriter (); 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 key = (String ) it.next(); 362 Object value = _viewMap.get(key); 363 if (value instanceof String ) { 364 try { 365 String viewDefinition = (String ) value; 366 if (parser == null) 367 parser = new XQueryParser(new StringReader ("dummy")); 368 parser.setFactory("org.xquark.xml.xqueryparser.common.ParserFactory"); 369 parser.ReInit(new StringReader (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 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 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 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 e) { 423 throw new XMLDBCException("Could not parse view definition", e); 424 } catch (IOException 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 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 452 metadata = new MetaDataImpl(null); 453 try { 454 MetaParser metaparser = new MetaParser(metadata); 455 MetaWrapper metawrapper = metaparser.createWrapper(new InputSource (new StringReader (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 |