1 22 23 package org.xquark.xquery.metadata; 24 25 import java.io.Serializable ; 26 import java.net.URL ; 27 import java.util.*; 28 29 import org.xml.sax.InputSource ; 30 import org.xml.sax.SAXException ; 31 import org.xquark.schema.Schema; 32 import org.xquark.schema.SchemaConstants; 33 import org.xquark.schema.SchemaManager; 34 import org.xquark.xml.xdbc.*; 35 import org.xquark.xquery.ModuleManager; 36 import org.xquark.xquery.metadata.resolver.MetadataAccess; 37 import org.xquark.xquery.metadata.resolver.SourceMetadata; 38 import org.xquark.xquery.xdbc.AbstractXMLDataSourceMetaData; 39 import org.xquark.xquery.xdbc.PathSetXMLDocument; 40 import org.xquark.xquery.xdbc.StringDocument; 41 42 43 47 public class MetaDataImpl extends AbstractXMLDataSourceMetaData implements MetadataAccess, Serializable { 48 private static final String RCSRevision = "$Revision: 1.9 $"; 52 private static final String RCSName = "$Name: $"; 53 private static final String FILE_MD_XSD = "/org/xquark/xml/xdbc/resources/XMLDBCMetaData.xsd"; 54 55 private static SchemaManager loadingManager = new SchemaManager(); 56 57 private ModuleManager moduleManager = new ModuleManager(); 58 59 static { 60 try { 61 URL url = MetaDataImpl.class.getResource(FILE_MD_XSD); 62 InputSource source = new InputSource (url.toString()); 63 loadingManager.loadSchema(source); 64 } catch (SAXException e) { 65 } 67 68 } 69 70 private HashMap metawrappers; 74 private SchemaManager schemaManager; 75 private HashMap strschemas = null; 76 77 81 86 public MetaDataImpl(XMLConnection connection) { 87 super(connection); 88 metawrappers = new HashMap(); 89 strschemas = new HashMap(); 90 schemaManager = new SchemaManager(); 91 } 92 93 public MetaDataImpl(XMLConnection connection, SchemaManager schemaManager) throws MetadataException { 94 super(connection); 95 this.schemaManager = schemaManager; 96 strschemas = new HashMap(); 97 metawrappers = new HashMap(); 98 } 99 100 104 111 public void addMetaWrapper(MetaWrapper metawrapper) throws MetadataException { 112 if (metawrapper == null) 114 throw new MetadataException("MetaData.addMetaWrapper : metawrapper to add is null"); 115 String key = metawrapper.getSourceName(); 117 if (key == null) 119 throw new MetadataException("MetaData.addMetaWrapper : metawrapper sourcename is null"); 120 if (metawrappers == null) 122 metawrappers = new HashMap(); 123 if (metawrappers.get(key) != null) { 125 System.err.println(key + " already loaded"); 126 return; 127 } 129 metawrappers.put(key, metawrapper); 131 } 132 133 public void addMetaWrapper(MetaWrapper metawrapper, String wrappername) throws MetadataException { 137 if (metawrapper == null) 139 throw new MetadataException("MetaData.addMetaWrapper : metawrapper to add is null"); 140 String key = wrappername; 142 if (key == null) 144 throw new MetadataException("MetaData.addMetaWrapper : metawrapper sourcename is null"); 145 if (metawrappers == null) 147 metawrappers = new HashMap(); 148 if (metawrappers.get(key) != null) { 150 System.err.println(key + " already loaded"); 151 return; 152 } 154 metawrapper.setSourceName(key); 156 metawrappers.put(key, metawrapper); 157 } 158 159 public MetaWrapper getMetaWrapper(String sourceName) { 160 if (metawrappers == null) 161 return null; 162 return (MetaWrapper) metawrappers.get(sourceName); 163 } 164 165 public HashMap getMetaWrappers() { 166 return metawrappers; 167 } 168 169 public void putSchema(Schema schema, String strschema) throws SAXException { 170 if (schema == null) 171 return; 172 173 boolean load = schemaManager.putSchema(schema); 174 strschemas.put(schema.getNamespace(), strschema); 175 } 176 177 public Collection getSchemas() { 178 return schemaManager.getSchemas(); 179 } 180 181 public Schema getSchemaSchema(String schemaname) { 182 return schemaManager.getSchema(schemaname); 183 } 184 185 public Collection getSources() { 187 if (metawrappers == null) 188 return null; 189 return metawrappers.values(); 190 } 191 192 public SourceMetadata getSourceMetadata(String sourceName) { 193 if (metawrappers == null) 194 return null; 195 return (SourceMetadata) metawrappers.get(sourceName); 196 } 197 198 public ModuleManager getModuleManager() { 199 return moduleManager; 200 } 201 202 public SchemaManager getSchemaManager() { 203 return schemaManager; 204 } 205 206 public SchemaManager getLoadingManager() { 207 return loadingManager; 208 } 209 210 public String getStringSchema(String targetNamespace) { 211 return (String ) strschemas.get(targetNamespace); 212 } 213 214 220 public Object getProperty(String propertyId) throws XMLDBCNotRecognizedException { 221 if (propertyId.equalsIgnoreCase("xmldbc.vendor.version")) 222 return new String ("$Revision: 1.9 $"); 223 throw new XMLDBCNotRecognizedException("Property \"" + propertyId + "\" not defined."); 225 } 226 227 233 public List getCollectionNames() throws XMLDBCException, XMLDBCNotSupportedException { 234 ArrayList al = new ArrayList(); 235 HashMap ht = getMetaWrappers(); 236 for (Iterator e = ht.values().iterator(); e.hasNext();) { 237 MetaWrapper mwi = (MetaWrapper) e.next(); 238 List ai = mwi.getCollectionsName(); 239 al.addAll(ai); 240 } 241 return al; 242 } 243 244 250 public List getSchemaNamespaces() throws XMLDBCException { 251 ArrayList al = new ArrayList(); 252 Collection cols = getSchemas(); 253 for (Iterator it = cols.iterator(); it.hasNext();) { 254 Schema schema = (Schema) it.next(); 255 if (schema == null) 256 continue; 257 String namespace = schema.getNamespace(); 258 if (namespace != null) { 259 if (namespace.equals(SchemaConstants.XMLSCHEMA_URI)) 260 continue; 261 if (namespace.equals(SchemaConstants.XML_URI)) 262 continue; 263 if (namespace.equals(SchemaConstants.XSI_URI)) 264 continue; 265 } 266 al.add(schema.getNamespace()); 267 } 268 return al; 269 } 270 271 278 public XMLDocument getPathSet(String colName) throws XMLDBCException { 279 HashMap ht = getMetaWrappers(); 280 for (Iterator e = ht.values().iterator(); e.hasNext();) { 281 MetaWrapper mwi = (MetaWrapper) e.next(); 282 HashMap ai = mwi.getMetaCollections(); 283 for (Iterator it = ai.keySet().iterator(); it.hasNext();) { 284 String collectionName = (String ) it.next(); 285 if (collectionName.equals(colName)) { 286 MetaCollection mc = (MetaCollection) ai.get(collectionName); 287 PathSetXMLDocument doc = new PathSetXMLDocument(collectionName, mc.getXTree()); 289 return doc; 290 } 291 } 292 } 293 throw new XMLDBCException("Collection " + colName + " not found."); 294 } 295 296 302 public XMLDocument getSchema(String targetNamespace) throws XMLDBCException { 303 String strschema = getStringSchema(targetNamespace); 308 if (strschema == null) 313 return null; 314 315 XMLDocument doc = new StringDocument(strschema, true); 316 return doc; 317 } 318 319 } 320 | Popular Tags |