Simplified parsing for double quoted strings
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / model / BookmarkCollection.java
index 184f790..873e42e 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);
@@ -75,29 +79,18 @@ public class BookmarkCollection {
             bookmark.setUsername(props.getProperty(i + ".username")); //$NON-NLS-1$
             bookmark.setPassword(props.getProperty(i + ".password")); //$NON-NLS-1$
             bookmark.setConnect(props.getProperty(i + ".connect")); //$NON-NLS-1$
-            bookmark.setDriver(props.getProperty(i + ".driver")); //$NON-NLS-1$
             String schema = props.getProperty(i + ".schema"); //$NON-NLS-1$
             if (schema != null) {
                 bookmark.addSchema(schema);
             }
-            String type = props.getProperty(i + ".type"); //$NON-NLS-1$
-            if (type != null) {
-                bookmark.setType(type);
-            } else {
-                bookmark.setType(""); //$NON-NLS-1$
-            }
-            String driverFile = props.getProperty(i + ".driverLocation"); //$NON-NLS-1$
-            if (driverFile != null) {
-                bookmark.setDriverFile(driverFile);
-            } else {
-                bookmark.setDriverFile(""); //$NON-NLS-1$
-            }
-            System.out.println(bookmark.toString());
             if (!bookmark.isEmpty()) {
                 newBookmarks.add(bookmark);
             }
             i++;
-            this.drivers.add(new JDBCDriver(bookmark.getDriver(), driverFile));
+            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, new String[] { driverFile }, type));
         }
         if (overwrite) {
             this.bookmarks = newBookmarks;
@@ -164,11 +157,50 @@ public class BookmarkCollection {
         NodeList nodes = root.getElementsByTagName("jdbcDriver"); //$NON-NLS-1$
         for (int i = 0; i < nodes.getLength(); i++) {
                Element driver = (Element) nodes.item(i);
-               addDriver(new JDBCDriver(
-                       driver.getAttribute("className"),
-                       driver.getAttribute("jarFileName"),
-                       driver.getAttribute("name"),
-                       driver.getAttribute("version")));
+
+               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"),
+                               (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));
         }
        }
 
@@ -206,18 +238,32 @@ public class BookmarkCollection {
                        bookmark.setAutoCommit(Boolean.TRUE.toString().equalsIgnoreCase(
                                MetaDataXMLInterface.getElementText(column,"autoCommit", "True"))); //$NON-NLS-1$
                        bookmark.setAutoCommitPreference(MetaDataXMLInterface.getElementText(column,"autoCommitPreference", IQuantumConstants.autoCommitTrue)); //$NON-NLS-1$
-            bookmark.addSchema(MetaDataXMLInterface.getElementText(column,"schema")); //$NON-NLS-1$
-            bookmark.setType(MetaDataXMLInterface.getElementText(column,"type")); //$NON-NLS-1$
-            NodeList children = column.getElementsByTagName(Messages.getString("ExportXMLAction.OtherSchemas"));
+                       
+                       backwardCompatibility(bookmark, column);
 
             String driverClassName = MetaDataXMLInterface.getElementText(column,"driver"); //$NON-NLS-1$
-            String driverFile = MetaDataXMLInterface.getElementText(column,"driverLocation"); //$NON-NLS-1$
             
-            bookmark.setJDBCDriver(new JDBCDriver(driverClassName, driverFile));
+            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, 
+                       (String[]) jarFiles.toArray(new String[jarFiles.size()]), 
+                                       type));
             
+            NodeList children = column.getElementsByTagName("Other_Schemas");
             if (children.getLength() > 0) {
                 importSchemas((Element) children.item(0), bookmark);
             }
+            
+            
             System.out.println(bookmark.toString());
             if (!bookmark.isEmpty()) {
                 newBookmarks.addElement(bookmark);
@@ -228,17 +274,51 @@ public class BookmarkCollection {
                return newBookmarks;
        }
 
+       /**
+        * Earlier versions of the xml file expected one schema element under the 
+        * bookmark element.  This method sees if it exists.
+        * 
+        * @param bookmark
+        * @param element
+        */
+       private void backwardCompatibility(Bookmark bookmark, Element element) {
+               NodeList children = element.getChildNodes();
+               for (int i = 0, length = children.getLength(); i < length; i++) {
+                       Node node = children.item(i);
+                       if (node.getNodeType() == Node.ELEMENT_NODE && 
+                                       "schema".equals(((Element) node).getTagName())) {
+                               String schema = MetaDataXMLInterface.extractText(element,"");
+                               if (schema != null && schema.trim().length() > 0) {
+                                       bookmark.addSchema(schema);
+                               }
+                       }
+               }
+       }
+
        private void importSchemas(Element otherSchemas, Bookmark bookmark) {
-        Vector vector = MetaDataXMLInterface.getVectorText(otherSchemas, Messages.getString("ExportXMLAction.SchemaName"));
         List list = new ArrayList();
-        for (Iterator i = vector.iterator(); i.hasNext();) {
-            String schemaName = (String) i.next();
-            list.add(new Schema(schemaName));
+        NodeList children = otherSchemas.getChildNodes();
+        for (int i = 0, length = children.getLength(); i < length; i++) {
+               Node node = children.item(i);
+               if (node.getNodeType() == Node.ELEMENT_NODE
+                               && "schema".equalsIgnoreCase(((Element) node).getTagName())) {
+                   list.add(new Schema(
+                               MetaDataXMLInterface.extractText((Element) node, "")));
+               }
         }
-        bookmark.setSchemas((Schema[]) list.toArray(new Schema[list.size()]));
+        
+        String schemaRule = otherSchemas.getAttribute("schemaRule");
+        if ("useAll".equals(schemaRule)) {
+               bookmark.setSchemaRule(Bookmark.SCHEMA_RULE_USE_ALL);
+        } else if ("useDefault".equals(schemaRule)) {
+               bookmark.setSchemaRule(Bookmark.SCHEMA_RULE_USE_DEFAULT);
+        } else {
+               bookmark.setSchemaRule(Bookmark.SCHEMA_RULE_USE_SELECTED);
+        }
+        bookmark.setSchemaSelections((Schema[]) list.toArray(new Schema[list.size()]));
     }
 
-    private void importQuickList(Bookmark bookmark, Element bookmarkElement) {
+       private void importQuickList(Bookmark bookmark, Element bookmarkElement) {
         NodeList quickList = bookmarkElement.getElementsByTagName("quickList");
         for (int j = 0,
             length = (quickList == null) ? 0 : quickList.getLength();
@@ -255,7 +335,7 @@ public class BookmarkCollection {
                 if (Node.ELEMENT_NODE == childNodes.item(k).getNodeType()) {
                     Element entity = (Element) childNodes.item(k);
                     bookmark.addQuickListEntry(entity.getTagName(), 
-                        entity.getAttribute("schema"), entity.getAttribute("name"));
+                        entity.getAttribute("schema"), entity.getAttribute("name"), entity.getAttribute("isSynonym").equals("true") );
                 }
             }
         }
@@ -305,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
@@ -365,8 +451,8 @@ public class BookmarkCollection {
         * @param driver
         * @param driverFile
         */
-       public JDBCDriver findDriver(String driverClassName, String driverFile) {
-               JDBCDriver temp = new JDBCDriver(driverClassName, driverFile);
+       public JDBCDriver findDriver(String driverClassName, String[] driverFiles, String type) {
+               JDBCDriver temp = new JDBCDriver(driverClassName, driverFiles, type);
                return findDriver(temp);
        }
 
@@ -389,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;
+               }
+       }
 }