Added xstream handiling for Wikipedia upload/download
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / actions / mediawiki / DownloadWikipediaAction.java
1 package net.sourceforge.phpeclipse.wiki.actions.mediawiki;
2
3 //Parts of this sources are copied and modified from the jEdit Wikipedia plugin:
4 //http://www.djini.de/software/wikipedia/index.html
5 //
6 //The modified sources are available under the "Common Public License"
7 //with permission from the original author: Daniel Wunsch
8
9 import java.io.StringWriter;
10 import java.lang.reflect.InvocationTargetException;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
14
15 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia;
16 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.MediaWikiConnector;
17 import net.sourceforge.phpeclipse.wiki.editor.WikiEditor;
18 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
19 import net.sourceforge.phpeclipse.wiki.internal.Configuration;
20 import net.sourceforge.phpeclipse.wiki.internal.ConfigurationManager;
21 import net.sourceforge.phpeclipse.wiki.internal.IConfiguration;
22 import net.sourceforge.phpeclipse.wiki.preferences.Util;
23 import net.sourceforge.phpeclipse.wiki.velocity.EditorText;
24
25 import org.apache.velocity.VelocityContext;
26 import org.apache.velocity.app.Velocity;
27 import org.eclipse.core.resources.IFile;
28 import org.eclipse.core.runtime.jobs.Job;
29 import org.eclipse.jface.action.IAction;
30 import org.eclipse.jface.text.IDocument;
31 import org.eclipse.jface.text.ITextSelection;
32 import org.eclipse.jface.text.TextSelection;
33 import org.eclipse.jface.viewers.ISelection;
34 import org.eclipse.jface.viewers.LabelProvider;
35 import org.eclipse.jface.window.Window;
36 import org.eclipse.ui.IEditorActionDelegate;
37 import org.eclipse.ui.IEditorPart;
38 import org.eclipse.ui.IFileEditorInput;
39 import org.eclipse.ui.IWorkbenchWindow;
40 import org.eclipse.ui.dialogs.ListSelectionDialog;
41 import org.eclipse.ui.internal.dialogs.ListContentProvider;
42 import org.eclipse.ui.texteditor.AbstractTextEditor;
43
44 public class DownloadWikipediaAction implements IEditorActionDelegate {
45
46   private AbstractTextEditor fEditor;
47
48   private EditorText text;
49
50   private IWorkbenchWindow window;
51
52   public void dispose() {
53   }
54
55   public String generateUrl(IWikipedia wikipediaProperties, Configuration config, String template, String wikiname) {
56
57     /* first, we init the runtime engine. Defaults are fine. */
58
59     try {
60       Velocity.init();
61
62       if (template == null || template.equals("")) {
63         // fall back to default settings
64         // Example:
65         // http://en.wikipedia.org/w/index.php?title=$text.wikiname&action=raw
66         template = wikipediaProperties.getActionUrl() + "?title=$text.wikiname&action=raw";
67       }
68
69       /* lets make a Context and put data into it */
70
71       VelocityContext context = new VelocityContext();
72
73       context.put("config", config);
74       text.clear();
75       text.setWikiname(wikiname);
76       context.put("text", text);
77
78       /* lets make our own string to render */
79       StringWriter w = new StringWriter();
80       w = new StringWriter();
81       Velocity.evaluate(context, w, "mystring", template);
82       return w.toString();
83
84     } catch (Exception e) {
85       // TODO Auto-generated catch block
86       e.printStackTrace();
87     }
88     return template;
89   }
90
91   protected Configuration getConfigurationPrefix(String prefix) {
92     List allConfigsList = ConfigurationManager.getInstance().getConfigurations();
93     ArrayList configsList = new ArrayList();
94     for (int i = 0; i < allConfigsList.size(); i++) {
95       IConfiguration temp = (IConfiguration) allConfigsList.get(i);
96       if (temp.getType().startsWith(prefix)) {
97         configsList.add(temp);
98       }
99     }
100     Collections.sort(configsList);
101     Configuration configuration = null;
102     ListSelectionDialog listSelectionDialog = new ListSelectionDialog(WikiEditorPlugin.getDefault().getWorkbench()
103         .getActiveWorkbenchWindow().getShell(), configsList, new ListContentProvider(), new LabelProvider(),
104         "Select the refresh URL.");
105     listSelectionDialog.setTitle("Multiple active configuration found");
106     if (listSelectionDialog.open() == Window.OK) {
107       Object[] locations = listSelectionDialog.getResult();
108       if (locations != null) {
109         for (int i = 0; i < locations.length; i++) {
110           configuration = (Configuration) locations[i];
111           break;
112         }
113       }
114     }
115     return configuration;
116   }
117   protected Configuration getConfiguration( ) {
118     return getConfigurationPrefix(WikiEditorPlugin.PREFIX_LOAD);
119   }
120
121   public IDocument getDocument() {
122     IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
123     return doc;
124   }
125
126   private static String getWikiFile(IFile file) {
127     return Util.getURLWikiName(file);
128   }
129
130   public void init(IWorkbenchWindow window) {
131     this.window = window;
132   }
133
134   void openWikiFile(IFile cfile) {
135     String wikiName = getWikiFile(cfile);
136     try {
137       if (fEditor != null) {
138         selectWiki(cfile, wikiName);
139       }
140     } catch (Exception e) {
141     }
142
143   }
144
145   public void openWikiLinkOnSelection() {
146     IDocument doc = getDocument();
147     ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection();
148     int pos = selection.getOffset();
149     IFileEditorInput ei = (IFileEditorInput) fEditor.getEditorInput();
150     openWikiFile(ei.getFile());
151   }
152
153   public void run(IAction action) {
154     if (fEditor == null) {
155       IEditorPart targetEditor = window.getActivePage().getActiveEditor();
156       if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
157         fEditor = (AbstractTextEditor) targetEditor;
158       }
159     }
160     if (fEditor != null) {
161       openWikiLinkOnSelection();
162     }
163   }
164
165   public void selectionChanged(IAction action, ISelection selection) {
166     if (selection.isEmpty()) {
167       return;
168     }
169     if (selection instanceof TextSelection) {
170       action.setEnabled(true);
171       return;
172     }
173     if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) {
174       action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class));
175     }
176   }
177
178   private void selectWiki(IFile cfile, String wikiName) {
179     Configuration configuration = getConfiguration();
180     if (configuration != null && !configuration.equals("")) {
181       try {
182         String wikiLocale = configuration.getType().substring(WikiEditorPlugin.PREFIX_LOAD.length());
183         IWikipedia wikipediaProperties = WikiEditorPlugin.getWikiInstance(wikiLocale);
184
185         //        String url = generateUrl(wikipediaProperties, configuration, configuration.getURL(), wikiName);
186         //        MediaWikiConnector mc = new MediaWikiConnector();
187         //        String wikiContent = mc.getWikiRawText(wikiName, url);
188         //        if (wikiContent != null) {
189         //          IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
190         //          doc.set(wikiContent);
191         //        }
192         IFile[] files = new IFile[1];
193         files[0] = cfile;
194
195         Job job = new RefreshJob(wikipediaProperties, files, configuration.getURL());
196         job.setRule(null);
197         job.setUser(true);
198         job.setPriority(Job.SHORT);
199         job.schedule();
200       } catch (Exception e) {
201         e.printStackTrace();
202         WikiEditorPlugin.getDefault().reportError("Exception occured: ",
203             e.getMessage() + "\nSee stacktrace in /.metadata/.log file.");
204       }
205     }
206   }
207
208   public void setActiveEditor(IAction action, IEditorPart targetEditor) {
209     if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
210       fEditor = (AbstractTextEditor) targetEditor;
211       text = new EditorText(targetEditor);
212     }
213   }
214 }