Added more Wikipedia configurations
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / actions / mediawiki / DownloadWikipediaAction.java
index cdbff14..051cce8 100644 (file)
@@ -1,4 +1,5 @@
 package net.sourceforge.phpeclipse.wiki.actions.mediawiki;
+
 //Parts of this sources are copied and modified from the jEdit Wikipedia plugin:
 //http://www.djini.de/software/wikipedia/index.html
 //
@@ -6,10 +7,12 @@ package net.sourceforge.phpeclipse.wiki.actions.mediawiki;
 //with permission from the original author: Daniel Wunsch
 
 import java.io.StringWriter;
+import java.lang.reflect.InvocationTargetException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
+import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia;
 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.MediaWikiConnector;
 import net.sourceforge.phpeclipse.wiki.editor.WikiEditor;
 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
@@ -45,18 +48,23 @@ public class DownloadWikipediaAction implements IEditorActionDelegate {
 
   private IWorkbenchWindow window;
 
-  
-
   public void dispose() {
   }
 
-  public String generateUrl(Configuration config, String template, String wikiname) {
+  public String generateUrl(IWikipedia wikipediaProperties, Configuration config, String template, String wikiname) {
 
     /* first, we init the runtime engine. Defaults are fine. */
 
     try {
       Velocity.init();
 
+      if (template == null || template.equals("")) {
+        // fall back to default settings
+        // Example:
+        // http://en.wikipedia.org/w/index.php?title=$text.wikiname&action=raw
+        template = wikipediaProperties.getActionUrl() + "?title=$text.wikiname&action=raw";
+      }
+
       /* lets make a Context and put data into it */
 
       VelocityContext context = new VelocityContext();
@@ -79,12 +87,12 @@ public class DownloadWikipediaAction implements IEditorActionDelegate {
     return template;
   }
 
-  protected Configuration getConfiguration(){
+  protected Configuration getConfiguration() {
     List allConfigsList = ConfigurationManager.getInstance().getConfigurations();
     ArrayList configsList = new ArrayList();
     for (int i = 0; i < allConfigsList.size(); i++) {
       IConfiguration temp = (IConfiguration) allConfigsList.get(i);
-      if (temp.getType().equals(WikiEditorPlugin.WIKIPEDIA_GET_TEXT)) {
+      if (temp.getType().startsWith(WikiEditorPlugin.PREFIX_LOAD)) {
         configsList.add(temp);
       }
     }
@@ -162,15 +170,24 @@ public class DownloadWikipediaAction implements IEditorActionDelegate {
       action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class));
     }
   }
-  
+
   private void selectWiki(String wikiName) {
     Configuration configuration = getConfiguration();
     if (configuration != null && !configuration.equals("")) {
-      String url = generateUrl(configuration, configuration.getURL(), wikiName);
-      String wikiContent = MediaWikiConnector.getWikiRawText(wikiName, url);
-      if (wikiContent != null) {
-        IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
-        doc.set(wikiContent);
+      try {
+        String wikiLocale = configuration.getType().substring(WikiEditorPlugin.PREFIX_LOAD.length());
+        IWikipedia wikipediaProperties = WikiEditorPlugin.getWikiInstance(wikiLocale);
+
+        String url = generateUrl(wikipediaProperties, configuration, configuration.getURL(), wikiName);
+        String wikiContent = MediaWikiConnector.getWikiRawText(wikiName, url);
+        if (wikiContent != null) {
+          IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
+          doc.set(wikiContent);
+        }
+      } catch (Exception e) {
+        e.printStackTrace();
+        WikiEditorPlugin.getDefault().reportError("Exception occured: ",
+            e.getMessage() + "\nSee stacktrace in /.metadata/.log file.");
       }
     }
   }