KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > extractor > ExtractorDriver


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;
24
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.xquark.xml.xdbc.*;
31
32 public class ExtractorDriver implements XMLDriver {
33     private static final String JavaDoc RCSRevision = "$Revision: 1.12 $";
34     private static final String JavaDoc RCSName = "$Name: $";
35
36     public static final String JavaDoc EXTRACTOR_URL_PREFIX = "xdbc:xquark:extractor:";
37
38     static private ExtractorDriver instance = new ExtractorDriver();
39     
40     static private Map JavaDoc datasources = Collections.synchronizedMap(new HashMap JavaDoc());
41
42     private ExtractorDriver() {
43         XMLDriverManager.registerDriver(this);
44     }
45
46     public boolean acceptsURI(String JavaDoc uri) throws XMLDBCException {
47         return uri != null && uri.startsWith(EXTRACTOR_URL_PREFIX);
48     }
49
50     public String JavaDoc getSpecificPart(String JavaDoc uri) throws XMLDBCException {
51         if (acceptsURI(uri)) return uri.substring(EXTRACTOR_URL_PREFIX.length());
52         else throw new XMLDBCException("Unrecognized URI "+uri);
53     }
54     
55     public XMLDataSource getDataSource(String JavaDoc uri) throws XMLDBCException {
56         if (!acceptsURI(uri)) return null;
57
58         String JavaDoc uriContents = getSpecificPart(uri);
59         if (uriContents.startsWith("file:") || uriContents.startsWith("http:") || uriContents.startsWith("jndi:")) {
60             XMLDataSource source = (XMLDataSource) datasources.get(uriContents);
61             if (source != null) return source;
62             try {
63                 Class JavaDoc dsImpl = Class.forName("org.xquark.extractor.Extractor");
64                 Class JavaDoc[] paramTypes = new Class JavaDoc[] { String JavaDoc.class };
65                 Object JavaDoc[] params = new Object JavaDoc[] { uriContents };
66                 source = (XMLDataSource)dsImpl.getConstructor(paramTypes).newInstance(params);
67                 datasources.put(uriContents, source);
68                 return source;
69             } catch (ClassNotFoundException JavaDoc e) {
70                 return null;
71             } catch (IllegalArgumentException JavaDoc e) {
72                 return null;
73             } catch (SecurityException JavaDoc e) {
74                 return null;
75             } catch (InstantiationException JavaDoc e) {
76                 return null;
77             } catch (IllegalAccessException JavaDoc e) {
78                 return null;
79             } catch (InvocationTargetException JavaDoc e) {
80                 return null;
81             } catch (NoSuchMethodException JavaDoc e) {
82                 return null;
83             }
84         } else {
85             return null;
86         }
87     }
88
89     public XMLDataSource getDataSource(String JavaDoc uri, String JavaDoc user, String JavaDoc password) throws XMLDBCException {
90         if (!acceptsURI(uri)) return null;
91
92         String JavaDoc uriContents = getSpecificPart(uri);
93         if (uriContents.startsWith("jdbc:")) {
94             String JavaDoc sourceKey = user+"@"+uriContents;
95             XMLDataSource source = (XMLDataSource) datasources.get(sourceKey);
96             if (source != null) return source;
97             try {
98                 Class JavaDoc dsImpl = Class.forName("org.xquark.extractor.Extractor");
99                 Class JavaDoc[] paramTypes = new Class JavaDoc[] { String JavaDoc.class, String JavaDoc.class, String JavaDoc.class };
100                 Object JavaDoc[] params = new Object JavaDoc[] { uriContents, user, password };
101                 source = (XMLDataSource)dsImpl.getConstructor(paramTypes).newInstance(params);
102                 datasources.put(sourceKey, source);
103                 return source;
104             } catch (ClassNotFoundException JavaDoc e) {
105                 return null;
106             } catch (IllegalArgumentException JavaDoc e) {
107                 return null;
108             } catch (SecurityException JavaDoc e) {
109                 return null;
110             } catch (InstantiationException JavaDoc e) {
111                 return null;
112             } catch (IllegalAccessException JavaDoc e) {
113                 return null;
114             } catch (InvocationTargetException JavaDoc e) {
115                 return null;
116             } catch (NoSuchMethodException JavaDoc e) {
117                 return null;
118             }
119         } else {
120             return null;
121         }
122     }
123
124 }
125
Popular Tags