Wikipedia SQL access configurable through type ''Wikipedia SQL access''
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / sql / WikipediaDB.java
index 204d563..9bbad44 100644 (file)
@@ -12,8 +12,12 @@ import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
 import java.sql.Statement;
 import java.util.ArrayList;
+import java.util.List;
 
 import net.sourceforge.phpeclipse.wiki.editor.LineTokenizer;
+import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
+import net.sourceforge.phpeclipse.wiki.internal.ConfigurationManager;
+import net.sourceforge.phpeclipse.wiki.internal.IConfiguration;
 
 public class WikipediaDB {
 
@@ -95,36 +99,36 @@ public class WikipediaDB {
     }
   }
 
-//  private static void readFile(WikipediaDB db, String filename) {
-//    FileReader fileReader;
-//    try {
-//      BufferedReader bufferedReader = new BufferedReader(new FileReader(filename));
-//      //      String line;
-//      LineTokenizer lineTokenizer = new LineTokenizer();
-//      StringBuffer line = new StringBuffer(1024);
-//      while (lineTokenizer.getToken(line, bufferedReader)) {
-//        if (line.length() == 0) {
-//          // this should not happen
-//        } else {
-//          //            try {
-//          //              db.update("INSERT INTO wp_titles(title) VALUES('" + line + "')");
-//          System.out.println(line);
-//          line.delete(0, line.length());
-//          //        addLine(line);
-//          //            } catch (SQLException ex3) {
-//          //// ex3.printStackTrace();
-//          //            }
-//        }
-//      }
-//      bufferedReader.close();
-//    } catch (FileNotFoundException e) {
-//      // ignore this
-//      // TODO DialogBox which asks the user if she/he likes to build new index?
-//    } catch (IOException e) {
-//      // TODO Auto-generated catch block
-//      e.printStackTrace();
-//    }
-//  }
+  //  private static void readFile(WikipediaDB db, String filename) {
+  //    FileReader fileReader;
+  //    try {
+  //      BufferedReader bufferedReader = new BufferedReader(new FileReader(filename));
+  //      // String line;
+  //      LineTokenizer lineTokenizer = new LineTokenizer();
+  //      StringBuffer line = new StringBuffer(1024);
+  //      while (lineTokenizer.getToken(line, bufferedReader)) {
+  //        if (line.length() == 0) {
+  //          // this should not happen
+  //        } else {
+  //          // try {
+  //          // db.update("INSERT INTO wp_titles(title) VALUES('" + line + "')");
+  //          System.out.println(line);
+  //          line.delete(0, line.length());
+  //          // addLine(line);
+  //          // } catch (SQLException ex3) {
+  //          //// ex3.printStackTrace();
+  //          // }
+  //        }
+  //      }
+  //      bufferedReader.close();
+  //    } catch (FileNotFoundException e) {
+  //      // ignore this
+  //      // TODO DialogBox which asks the user if she/he likes to build new index?
+  //    } catch (IOException e) {
+  //      // TODO Auto-generated catch block
+  //      e.printStackTrace();
+  //    }
+  //  }
 
   Connection conn;
 
@@ -137,15 +141,32 @@ public class WikipediaDB {
     // mysql-connector.jar should be in the class path or made part of the current jar
     Class.forName("com.mysql.jdbc.Driver");
 
+    // determine the foirst SQL configuration
+    List allConfigsList = ConfigurationManager.getInstance().getConfigurations();
+    ArrayList configsList = new ArrayList();
+    IConfiguration configuration = null;
+    for (int i = 0; i < allConfigsList.size(); i++) {
+      configuration = (IConfiguration) allConfigsList.get(i);
+      if (configuration.getType().equals(WikiEditorPlugin.WIKIPEDIA_SQL)) {
+        break;
+      }
+      configuration = null;
+    }
+
     // connect to the database. This will load the db files and start the
     // database if it is not alread running.
     // db_file_name_prefix is used to open or create files that hold the state
     // of the db.
     // It can contain directory names relative to the
     // current working directory
-    conn = DriverManager.getConnection("jdbc:mysql://localhost/wikidb", // filenames
-        "root", // username
-        ""); // password
+    if (configuration != null) {
+      conn = DriverManager.getConnection(configuration.getURL(), configuration.getUser(), configuration.getPassword());
+    } else {
+      // default configuration for XAMPP distribution
+      conn = DriverManager.getConnection("jdbc:mysql://localhost/wikidb", // filenames
+          "root", // username
+          ""); // password
+    }
     fGetPrefixTitles = conn.prepareStatement("SELECT cur_title FROM cur WHERE LOWER( cur_title ) like ?");
   }