KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > xquery > metadata > MetaDataImpl


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.xquery.metadata;
24
25 import java.io.Serializable JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.*;
28
29 import org.xml.sax.InputSource JavaDoc;
30 import org.xml.sax.SAXException JavaDoc;
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 /**
44  * Implementation of XDBC DataSourceMetadata offering other internal metadata
45  * interfaces. Used by extractor and mediator.
46  */

47 public class MetaDataImpl extends AbstractXMLDataSourceMetaData implements MetadataAccess, Serializable JavaDoc {
48     // **********************************************************************
49
// * Constant
50
// **********************************************************************
51
private static final String JavaDoc RCSRevision = "$Revision: 1.9 $";
52     private static final String JavaDoc RCSName = "$Name: $";
53     private static final String JavaDoc 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 JavaDoc url = MetaDataImpl.class.getResource(FILE_MD_XSD);
62             InputSource JavaDoc source = new InputSource JavaDoc(url.toString());
63             loadingManager.loadSchema(source);
64         } catch (SAXException JavaDoc e) {
65             // do nothing !!!
66
}
67         
68     }
69
70     // **********************************************************************
71
// * Fields
72
// **********************************************************************
73
private HashMap metawrappers;
74     private SchemaManager schemaManager;
75     private HashMap strschemas = null;
76
77     // **********************************************************************
78
// * Constructor
79
// **********************************************************************
80

81     /**
82      * Construct the class MetaData
83      *
84      * @param
85      */

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     // **********************************************************************
101
// * Methods
102
// **********************************************************************
103

104     /**
105      *
106      *
107      * @param
108      *
109      * @return
110      */

111     public void addMetaWrapper(MetaWrapper metawrapper) throws MetadataException {
112         // incorrect argument
113
if (metawrapper == null)
114             throw new MetadataException("MetaData.addMetaWrapper : metawrapper to add is null");
115         // get name of metawrapper
116
String JavaDoc key = metawrapper.getSourceName();
117         // incorrect name
118
if (key == null)
119             throw new MetadataException("MetaData.addMetaWrapper : metawrapper sourcename is null");
120         // initialize metawrappers if necessary
121
if (metawrappers == null)
122             metawrappers = new HashMap();
123         // verify metawrapper of same name not present
124
if (metawrappers.get(key) != null) {
125             System.err.println(key + " already loaded");
126             return;
127             // throw new MetadataException ("MetaData.addMetaWrapper : metawrapper already added") ;
128
}
129         // add metawrapper
130
metawrappers.put(key, metawrapper);
131     }
132
133     // used exclusivily by the mediator (and mediator shouldn't use the other
134
// method, because in some cases, wrapper.getSourceName() can be null
135
// case of getURL of connection are null (eg. extractor with jdbc string)
136
public void addMetaWrapper(MetaWrapper metawrapper, String JavaDoc wrappername) throws MetadataException {
137         // incorrect argument
138
if (metawrapper == null)
139             throw new MetadataException("MetaData.addMetaWrapper : metawrapper to add is null");
140         // get name of metawrapper
141
String JavaDoc key = wrappername;
142         // incorrect name
143
if (key == null)
144             throw new MetadataException("MetaData.addMetaWrapper : metawrapper sourcename is null");
145         // initialize metawrappers if necessary
146
if (metawrappers == null)
147             metawrappers = new HashMap();
148         // verify metawrapper of same name not present
149
if (metawrappers.get(key) != null) {
150             System.err.println(key + " already loaded");
151             return;
152             // throw new MetadataException ("MetaData.addMetaWrapper : metawrapper already added") ;
153
}
154         // add metawrapper
155
metawrapper.setSourceName(key);
156         metawrappers.put(key, metawrapper);
157     }
158
159     public MetaWrapper getMetaWrapper(String JavaDoc 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 JavaDoc strschema) throws SAXException JavaDoc {
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 JavaDoc schemaname) {
182         return schemaManager.getSchema(schemaname);
183     }
184
185     // returns a Collection of SourceMetadata
186
public Collection getSources() {
187         if (metawrappers == null)
188             return null;
189         return metawrappers.values();
190     }
191
192     public SourceMetadata getSourceMetadata(String JavaDoc 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 JavaDoc getStringSchema(String JavaDoc targetNamespace) {
211         return (String JavaDoc) strschemas.get(targetNamespace);
212     }
213
214     // **********************************************************************
215
/**
216      * Get the property with the specified name
217      * @return the value of the property
218      * @throws XMLDBCNotRecognizedException if the property is not known
219      */

220     public Object JavaDoc getProperty(String JavaDoc propertyId) throws XMLDBCNotRecognizedException {
221         if (propertyId.equalsIgnoreCase("xmldbc.vendor.version"))
222             return new String JavaDoc("$Revision: 1.9 $");
223         //throw new XMLDBCNotSupportedException("getProperty not yet supported");
224
throw new XMLDBCNotRecognizedException("Property \"" + propertyId + "\" not defined.");
225     }
226
227     /**
228      * Retrieves the name of all XML collections in this data source.
229      * @return a list of all XML collection names as String objects.
230      * @throws XMLDBCException if a data source access error occurs.
231      * @throws XMLDBCNotSupportedException if the data source does not support this operation (typically, when the data source is read-only)
232      */

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     /**
245      * Retrieves the available schema namespaces in the data source.
246      * @return the target namespaces of all schemas available in the data source
247      * in a list of String objects.
248      * @throws XMLDBCException if a data source access error occurs.
249      */

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 JavaDoc 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     /**
272      * Retrieves the XML document describing all existing paths in all documents
273      * of the specified XML collection.
274      * @param the name of the XML collection.
275      * @return an XML document containing the path set.
276      * @throws XMLDBCException if a data source access error occurs.
277      */

278     public XMLDocument getPathSet(String JavaDoc 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 JavaDoc collectionName = (String JavaDoc) it.next();
285                 if (collectionName.equals(colName)) {
286                     MetaCollection mc = (MetaCollection) ai.get(collectionName);
287                     // beware, XMLCollection is null !
288
PathSetXMLDocument doc = new PathSetXMLDocument(collectionName, mc.getXTree());
289                     return doc;
290                 }
291             }
292         }
293         throw new XMLDBCException("Collection " + colName + " not found.");
294     }
295
296     /**
297      * Retrieves the XML document describing the XML schema for the given namespace.
298      * @param the target namespace of the schema.
299      * @return an XML document containing the XML schema.
300      * @throws XMLDBCException if a data source access error occurs.
301      */

302     public XMLDocument getSchema(String JavaDoc targetNamespace) throws XMLDBCException {
303         //Schema schema = getSchemaSchema (targetNamespace) ;
304
// ici, si on peut faire un truc du style
305
// SchemaXMLDocument xmldoc = new SchemaXMLDocument (schema) ;
306
// return xmldoc
307
String JavaDoc strschema = getStringSchema(targetNamespace);
308         // when it's null (i.e. it isn't in set of schema), it's not
309
// necessary because of an error. It happens, because definition
310
// namespace as "http://www.w3.org/2001/XMLSchema" are also defined,
311
// but aren't considered as useful schema.
312
if (strschema == null)
313             return null;
314
315         XMLDocument doc = new StringDocument(strschema, true);
316         return doc;
317     }
318
319 }
320
Popular Tags