1 package net.sourceforge.phpeclipse.wiki.actions.mediawiki.post;
3 //Parts of this sources are copied and modified from the jEdit Wikipedia plugin:
4 //http://www.djini.de/software/wikipedia/index.html
6 //The modified sources are available under the "Common Public License"
7 //with permission from the original author: Daniel Wunsch
9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.io.InputStreamReader;
12 import java.io.StringWriter;
13 import java.lang.reflect.InvocationTargetException;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.Date;
17 import java.util.List;
19 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia;
20 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.Content;
21 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.MediaWikiConnector;
22 import net.sourceforge.phpeclipse.wiki.editor.WikiEditor;
23 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
24 import net.sourceforge.phpeclipse.wiki.internal.Configuration;
25 import net.sourceforge.phpeclipse.wiki.internal.ConfigurationManager;
26 import net.sourceforge.phpeclipse.wiki.internal.IConfiguration;
27 import net.sourceforge.phpeclipse.wiki.preferences.Util;
28 import net.sourceforge.phpeclipse.wiki.velocity.EditorText;
30 import org.apache.velocity.VelocityContext;
31 import org.apache.velocity.app.Velocity;
32 import org.eclipse.core.resources.IFile;
33 import org.eclipse.core.runtime.CoreException;
34 import org.eclipse.jface.action.IAction;
35 import org.eclipse.jface.text.IDocument;
36 import org.eclipse.jface.text.ITextSelection;
37 import org.eclipse.jface.text.TextSelection;
38 import org.eclipse.jface.viewers.ISelection;
39 import org.eclipse.jface.viewers.LabelProvider;
40 import org.eclipse.jface.window.Window;
41 import org.eclipse.ui.IEditorActionDelegate;
42 import org.eclipse.ui.IEditorPart;
43 import org.eclipse.ui.IFileEditorInput;
44 import org.eclipse.ui.IWorkbenchWindow;
45 import org.eclipse.ui.dialogs.ListSelectionDialog;
46 import org.eclipse.ui.internal.dialogs.ListContentProvider;
47 import org.eclipse.ui.texteditor.AbstractTextEditor;
49 public class StoreWikipediaAction implements IEditorActionDelegate {
52 * Constant for an empty char array
54 public static final char[] NO_CHAR = new char[0];
56 private static final int DEFAULT_READING_SIZE = 8192;
58 private AbstractTextEditor fEditor;
60 private EditorText text;
62 private IWorkbenchWindow window;
64 public void dispose() {
67 public String generateUrl(Configuration config, String template, String wikiname) {
69 /* first, we init the runtime engine. Defaults are fine. */
74 /* lets make a Context and put data into it */
76 VelocityContext context = new VelocityContext();
78 context.put("config", config);
80 text.setWikiname(wikiname);
81 context.put("text", text);
83 /* lets make our own string to render */
84 StringWriter w = new StringWriter();
85 w = new StringWriter();
86 Velocity.evaluate(context, w, "mystring", template);
89 } catch (Exception e) {
90 // TODO Auto-generated catch block
96 protected Configuration getConfigurationPrefix(String prefix) {
98 List allConfigsList = ConfigurationManager.getInstance().getConfigurations();
99 ArrayList configsList = new ArrayList();
100 for (int i = 0; i < allConfigsList.size(); i++) {
101 IConfiguration temp = (IConfiguration) allConfigsList.get(i);
102 if (temp.getType().startsWith(prefix)) {
103 configsList.add(temp);
106 if (configsList.size() == 1) {
107 return (Configuration) configsList.get(0);
109 Collections.sort(configsList);
110 Configuration configuration = null;
111 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(WikiEditorPlugin.getDefault().getWorkbench()
112 .getActiveWorkbenchWindow().getShell(), configsList, new ListContentProvider(), new LabelProvider(),
113 "Select the refresh URL.");
114 listSelectionDialog.setTitle("Multiple active configuration found");
115 if (listSelectionDialog.open() == Window.OK) {
116 Object[] locations = listSelectionDialog.getResult();
117 if (locations != null) {
118 for (int i = 0; i < locations.length; i++) {
119 configuration = (Configuration) locations[i];
124 return configuration;
127 protected Configuration getConfiguration() {
128 return getConfigurationPrefix(WikiEditorPlugin.PREFIX_STORE);
131 public IDocument getDocument() {
132 IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
136 private String getWikiFile(IFile file) {
137 return Util.getURLWikiName(file);
140 public void init(IWorkbenchWindow window) {
141 this.window = window;
144 // void postWikiFile(IFile currentFile) {
145 // String wikiName = getWikiFile(currentFile);
147 // if (fEditor != null) {
148 // selectWiki(currentFile.getContents(), wikiName);
150 // } catch (Exception e) {
155 public void run(IAction action) {
156 if (fEditor == null) {
157 IEditorPart targetEditor = window.getActivePage().getActiveEditor();
158 if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
159 fEditor = (AbstractTextEditor) targetEditor;
162 if (fEditor != null) {
163 Configuration configuration = getConfiguration();
164 if (configuration != null && !configuration.equals("")) {
166 String wikiLocale = configuration.getType().substring(WikiEditorPlugin.PREFIX_STORE.length());
167 IWikipedia wikipediaProperties = WikiEditorPlugin.getWikiInstance(wikiLocale);
168 String user = configuration.getUser();
169 String password = configuration.getPassword();
170 if (user == null || password == null || user.equals("") || password.equals("")) {
171 String[] result = new String[2];
172 boolean cache = configuration.promptForPassword(configuration.getUser(), "Set User/Password", true, result);
173 if (result[0] == null || result[1] == null) {
176 if (result[0].equals("") || result[1].equals("")) {
180 password = result[1];
182 // IDocument doc = getDocument();
183 // ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection();
184 // int pos = selection.getOffset();
185 if (fEditor.getEditorInput() instanceof IFileEditorInput) {
186 IFileEditorInput ei = (IFileEditorInput) fEditor.getEditorInput();
187 IFile currentFile = ei.getFile();
188 IFile[] files = { currentFile };
189 PostJob job = new PostJob(configuration, wikipediaProperties, user, password, files);
194 String wikiName = getWikiFile(currentFile);
195 if (fEditor != null) {
196 selectWiki(configuration, currentFile.getContents(), wikipediaProperties, user, password, wikiName);
199 } catch (CoreException e1) {
200 // TODO Auto-generated catch block
201 e1.printStackTrace();
202 } catch (NoSuchMethodException e) {
203 // TODO Auto-generated catch block
205 } catch (IllegalAccessException e) {
206 // TODO Auto-generated catch block
208 } catch (ClassNotFoundException e) {
209 // TODO Auto-generated catch block
211 } catch (InvocationTargetException e) {
212 // TODO Auto-generated catch block
219 public void selectionChanged(IAction action, ISelection selection) {
220 if (selection.isEmpty()) {
223 if (selection instanceof TextSelection) {
224 action.setEnabled(true);
227 if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) {
228 action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class));
232 private void selectWiki(Configuration configuration, InputStream is, IWikipedia wikipediaProperties, String user, String password, String wikiName) {
235 // IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
236 String url = generateUrl(configuration, configuration.getURL(), wikiName);
238 MediaWikiConnector connector = new MediaWikiConnector();
239 String actionUrl = configuration.getURL();
240 if (actionUrl == null || actionUrl.equals("")) {
241 // fall back to default settings
242 actionUrl = wikipediaProperties.getActionUrl();
246 Content content = new Content(String.valueOf(d.getTime()), getInputStreamAsString(is, wikipediaProperties.getCharSet()));
248 boolean success = connector.login(wikipediaProperties, actionUrl, user, password, false);
250 connector.store(wikipediaProperties, actionUrl, wikiName, content, "", false, false);
251 connector.logout(wikipediaProperties, actionUrl);
253 } catch (Exception e) {
255 WikiEditorPlugin.getDefault()
256 .reportError("Exception occured: ", e.getMessage() + "\nSee stacktrace in /.metadata/.log file.");
260 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
261 if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
262 fEditor = (AbstractTextEditor) targetEditor;
263 text = new EditorText(targetEditor);
268 * Returns the given input stream's contents as a character array. If a length is specified (ie. if length != -1), only length
269 * chars are returned. Otherwise all chars in the stream are returned. Note this doesn't close the stream.
271 * @throws IOException
272 * if a problem occured reading the stream.
274 public static String getInputStreamAsString(InputStream stream, String encoding) throws IOException {
275 char[] array = getInputStreamAsCharArray(stream, -1, encoding);
277 return new String(array);
283 * Returns the given input stream's contents as a character array. If a length is specified (ie. if length != -1), only length
284 * chars are returned. Otherwise all chars in the stream are returned. Note this doesn't close the stream.
286 * @throws IOException
287 * if a problem occured reading the stream.
289 public static char[] getInputStreamAsCharArray(InputStream stream, int length, String encoding) throws IOException {
290 InputStreamReader reader = null;
291 reader = encoding == null ? new InputStreamReader(stream) : new InputStreamReader(stream, encoding);
295 int contentsLength = 0;
298 int amountRequested = Math.max(stream.available(), DEFAULT_READING_SIZE); // read at least 8K
300 // resize contents if needed
301 if (contentsLength + amountRequested > contents.length) {
302 System.arraycopy(contents, 0, contents = new char[contentsLength + amountRequested], 0, contentsLength);
305 // read as many chars as possible
306 amountRead = reader.read(contents, contentsLength, amountRequested);
308 if (amountRead > 0) {
309 // remember length of contents
310 contentsLength += amountRead;
312 } while (amountRead != -1);
314 // resize contents if necessary
315 if (contentsLength < contents.length) {
316 System.arraycopy(contents, 0, contents = new char[contentsLength], 0, contentsLength);
319 contents = new char[length];
322 while ((readSize != -1) && (len != length)) {
324 // We record first the read size. In this case len is the actual read size.
326 readSize = reader.read(contents, len, length - len);
329 // Now we need to resize in case the default encoding used more than one byte for each
332 System.arraycopy(contents, 0, (contents = new char[len]), 0, len);