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