Quantum version 2.4.1
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / model / BookmarkCollection.java
index 882f111..2f66b2e 100644 (file)
@@ -20,8 +20,10 @@ import org.w3c.dom.NodeList;
 
 import com.quantum.IQuantumConstants;
 import com.quantum.Messages;
+import com.quantum.adapters.AdapterFactory;
 import com.quantum.model.xml.ModelToXMLConverter;
 import com.quantum.sql.metadata.MetaDataXMLInterface;
+import com.quantum.util.JarUtil;
 
 /**
  * The collection of database bookmarks that the Quantum plug-in knows about.
@@ -32,7 +34,9 @@ import com.quantum.sql.metadata.MetaDataXMLInterface;
  */
 public class BookmarkCollection {
     
-    private static BookmarkCollection instance = new BookmarkCollection();
+    
+private static final String SUN_JDBC_ODBC_DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
+       private static BookmarkCollection instance = new BookmarkCollection();
     private List bookmarks = new Vector();
     private boolean changed = false;
     private PropertyChangeSupport support = new PropertyChangeSupport(this);
@@ -86,7 +90,7 @@ public class BookmarkCollection {
             String driver = props.getProperty(i + ".driver"); //$NON-NLS-1$
             String driverFile = props.getProperty(i + ".driverLocation"); //$NON-NLS-1$
             String type = props.getProperty(i + ".type"); //$NON-NLS-1$
-            this.drivers.add(new JDBCDriver(driver, driverFile, type));
+            this.drivers.add(new JDBCDriver(driver, new String[] { driverFile }, type));
         }
         if (overwrite) {
             this.bookmarks = newBookmarks;
@@ -154,15 +158,49 @@ public class BookmarkCollection {
         for (int i = 0; i < nodes.getLength(); i++) {
                Element driver = (Element) nodes.item(i);
 
-               if (!"".equals(driver.getAttribute("type"))) {
+               List jarFileNames = new ArrayList();
+               String jarFileName = driver.getAttribute("jarFileName");
+               if (jarFileName != null && jarFileName.trim().length() > 0) {
+                       jarFileNames.add(jarFileName);
+               }
+               NodeList jars = driver.getElementsByTagName("jar");
+               for (int j = 0; j < jars.getLength(); j++) {
+                       String fileName = ((Element) jars.item(j)).getAttribute("fileName");
+                       if (fileName != null && fileName.trim().length() > 0) {
+                               jarFileNames.add(fileName);
+                       }
+               }
+               
+               if (!isQuantum232Notation(driver)) {
                        addDriver(new JDBCDriver(
                                driver.getAttribute("className"),
-                               driver.getAttribute("jarFileName"),
+                               (String[]) jarFileNames.toArray(new String[jarFileNames.size()]),
                                driver.getAttribute("type"),
                                driver.getAttribute("name"),
                                driver.getAttribute("version")));
                }
-               
+        }
+        addStandardDrivers();
+       }
+
+       /**
+        * An earlier version of the Quantum XML format omitted the
+        * type.
+        * 
+        * @param driver
+        * @return
+        */
+       private boolean isQuantum232Notation(Element driver) {
+               return "".equals(driver.getAttribute("type"));
+       }
+
+       /**
+        * 
+        */
+       private void addStandardDrivers() {
+               if (JarUtil.loadDriver(null, SUN_JDBC_ODBC_DRIVER) != null) {
+               addDriver(new JDBCDriver(SUN_JDBC_ODBC_DRIVER, new String[0], 
+                               AdapterFactory.JDBC_ODBC_BRIDGE));
         }
        }
 
@@ -204,10 +242,21 @@ public class BookmarkCollection {
                        backwardCompatibility(bookmark, column);
 
             String driverClassName = MetaDataXMLInterface.getElementText(column,"driver"); //$NON-NLS-1$
-            String driverFile = MetaDataXMLInterface.getElementText(column,"driverLocation"); //$NON-NLS-1$
+            
+            List jarFiles = new ArrayList();
+            NodeList driverLocations = column.getElementsByTagName("driverLocation");
+            for (int j = 0, length = driverLocations == null 
+                       ? 0 : driverLocations.getLength(); j < length; j++) {
+               String jarFileName = MetaDataXMLInterface.extractText((Element) driverLocations.item(j), "");
+               if (jarFileName != null && jarFileName.trim().length() > 0) {
+                       jarFiles.add(jarFileName);
+               }
+                       }
             String type = MetaDataXMLInterface.getElementText(column,"type"); //$NON-NLS-1$
             
-            bookmark.setJDBCDriver(new JDBCDriver(driverClassName, driverFile, type));
+            bookmark.setJDBCDriver(new JDBCDriver(driverClassName, 
+                       (String[]) jarFiles.toArray(new String[jarFiles.size()]), 
+                                       type));
             
             NodeList children = column.getElementsByTagName("Other_Schemas");
             if (children.getLength() > 0) {
@@ -336,7 +385,13 @@ public class BookmarkCollection {
     }
     
     public JDBCDriver[] getJDBCDrivers() {
-       return (JDBCDriver[]) this.drivers.toArray(new JDBCDriver[this.drivers.size()]);
+       if (this.drivers.isEmpty()) {
+            addStandardDrivers();
+       }
+       
+       List list = new ArrayList(this.drivers);
+       Collections.sort(list);
+       return (JDBCDriver[]) list.toArray(new JDBCDriver[list.size()]);
     }
     /**
      * @return
@@ -396,8 +451,8 @@ public class BookmarkCollection {
         * @param driver
         * @param driverFile
         */
-       public JDBCDriver findDriver(String driverClassName, String driverFile, String type) {
-               JDBCDriver temp = new JDBCDriver(driverClassName, driverFile, type);
+       public JDBCDriver findDriver(String driverClassName, String[] driverFiles, String type) {
+               JDBCDriver temp = new JDBCDriver(driverClassName, driverFiles, type);
                return findDriver(temp);
        }
 
@@ -420,5 +475,25 @@ public class BookmarkCollection {
                return result;
        }
 
-
+       /**
+        * @param driver
+        */
+       public boolean removeDriver(JDBCDriver driver) {
+               boolean found = false;
+               for (Iterator i = this.bookmarks.iterator(); 
+                               !found && driver != null && i.hasNext();) {
+                       Bookmark bookmark = (Bookmark) i.next();
+                       found |= driver.equals(bookmark.getJDBCDriver());
+               }
+               
+               if (!found && driver != null && !SUN_JDBC_ODBC_DRIVER.equals(driver.getClassName())) {
+                       boolean deleted = this.drivers.remove(driver);
+                       if (deleted) {
+                               this.support.firePropertyChange("drivers", null, null);
+                       }
+                       return deleted;
+               } else {
+                       return false;
+               }
+       }
 }