1 package net.sourceforge.phpeclipse.wiki.editor;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.lang.reflect.InvocationTargetException;
6 import java.lang.reflect.Method;
7 import java.net.MalformedURLException;
9 import java.sql.SQLException;
10 import java.text.MessageFormat;
11 import java.util.ArrayList;
12 import java.util.Enumeration;
13 import java.util.Hashtable;
14 import java.util.List;
15 import java.util.MissingResourceException;
16 import java.util.PropertyResourceBundle;
17 import java.util.ResourceBundle;
19 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia;
20 import net.sourceforge.phpeclipse.wiki.internal.ConfigurationManager;
21 import net.sourceforge.phpeclipse.wiki.internal.IConfigurationWorkingCopy;
22 import net.sourceforge.phpeclipse.wiki.sql.WikipediaDB;
24 import org.eclipse.core.resources.IWorkspace;
25 import org.eclipse.core.resources.ResourcesPlugin;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IExtension;
28 import org.eclipse.core.runtime.IPluginDescriptor;
29 import org.eclipse.core.runtime.IStatus;
30 import org.eclipse.core.runtime.Path;
31 import org.eclipse.core.runtime.Platform;
32 import org.eclipse.core.runtime.Status;
33 import org.eclipse.jface.dialogs.MessageDialog;
34 import org.eclipse.jface.preference.IPreferenceStore;
35 import org.eclipse.jface.resource.ImageDescriptor;
36 import org.eclipse.jface.text.templates.ContextTypeRegistry;
37 import org.eclipse.jface.text.templates.persistence.TemplateStore;
38 import org.eclipse.swt.graphics.Image;
39 import org.eclipse.swt.widgets.Display;
40 import org.eclipse.swt.widgets.Shell;
41 import org.eclipse.ui.IWorkbenchPage;
42 import org.eclipse.ui.IWorkbenchWindow;
43 import org.eclipse.ui.PlatformUI;
44 import org.eclipse.ui.editors.text.templates.ContributionContextTypeRegistry;
45 import org.eclipse.ui.editors.text.templates.ContributionTemplateStore;
46 import org.eclipse.ui.plugin.AbstractUIPlugin;
48 public class WikiEditorPlugin extends AbstractUIPlugin {
50 private static WikiEditorPlugin fgPlugin;
52 public final static String AUTOMATICALLY_CREATED = "<!-- automatically created -->";
54 public final static String WP_EXTENSION = "wp";
56 public final static String XML_START_1 = "<?xml version=\"1.0\" encoding=\"";
58 public final static String XML_START_2 = "\" ?>\n" + "<mediawiki version=\"0.1\">\n";
60 public final static String XML_END = "\n</mediawiki>";
62 public static final String PREFIX_LOAD = "Load ";
64 public static final String PREFIX_STORE = "Store ";
66 public static final String HTTP_QUERY = "HTTP Query";
68 // public static final String WIKIPEDIA_GET_TEXT = "Load WikipediaEN";
70 // public static final String WIKIPEDIA_SET_TEXT = "Store WikipediaEN";
71 public static final String WIKIPEDIA_SQL = "Wikipedia SQL access";
73 public static final String BLOG_A_WIKI = "Blog as Wiki Text";
75 public static final String BLOG_A_HTML = "Blog as HTML Text";
77 public static final String PLOG4U_DE_LOAD = PREFIX_LOAD + "Plog4UDE";
79 public static final String PLOG4U_DE_STORE = PREFIX_STORE + "Plog4UDE";
81 public static final String PLOG4U_ORG_LOAD = PREFIX_LOAD + "Plog4UORG";
83 public static final String PLOG4U_ORG_STORE = PREFIX_STORE + "Plog4UORG";
85 public static final String[] PREDEFINED_TYPES = {
95 public static final String[] PREDEFINED_WIKIS = {
121 public static final ArrayList CONFIGURATION_TYPES = new ArrayList();
124 public static final String ICON_PATH = "icons/full/"; //$NON-NLS-1$
126 public final static String PLUGIN_ID = "net.sourceforge.phpeclipse.wiki";
128 public final static String HTML_OUTPUT_PATH = "__static_wiki_folder";
130 public final static String WIKI_TEXTS_BASE_PATH = "__wiki_texts_base_path";
132 public final static String LOCAL_TEMPLATE_FILE_NAME = "__local_template_file_name";
134 public final static String EXPORT_TEMPLATE_FILE_NAME = "__export_template_file_name";
136 public final static String LOCAL_CSS_URL = "__local_css_url";
138 public final static String EXPORT_CSS_URL = "__export_css_url";
140 public final static String PREF_STRING_CONFIGURATIONS = "__configurations4";
142 public final static String CONSOLE_OUTPUT = "__console_output";
144 public final static String HTTP_TIMEOUT = "http.timeout";
146 public final static String HTTP_PROXYHOST = "http.proxyHost";
148 public final static String HTTP_PROXYPORT = "http.proxyPort";
150 public final static String HTTP_PROXYUSERNAME = "http.proxyUserName";
152 public final static String HTTP_PROXYPASSWORD = "http.proxyPassword";
154 public final static String CONFIG_MEMENTO = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<configurations>"
155 + "<config name=\"Google Search\" type-id=\"HTTP Query\" url=\"http://www.google.com/search?q=$text.selection\"/>"
156 + "<config name=\"Koders.com Search\" type-id=\"HTTP Query\" url=\"http://koders.com/?s=$text.selection\"/>"
157 + "<config name=\"Leo.org Translation\" type-id=\"HTTP Query\" url=\"http://dict.leo.org/?search=$text.selection\"/>"
159 + "<config name=\"Plog4U.de Download\" type-id=\"" + PLOG4U_DE_LOAD
160 + "\" url=\"http://www.plog4u.de/index.php/Spezial:Export\"/>" + "<config name=\"Plog4U.org Download\" type-id=\""
161 + PLOG4U_ORG_LOAD + "\" url=\"http://www.plog4u.org/index.php/Special:Export\"/>"
162 + "<config name=\"Plog4U.de Upload\" type-id=\"" + PLOG4U_DE_STORE + "\" url=\"http://www.plog4u.de/index.php\"/>"
163 + "<config name=\"Plog4U.org Upload\" type-id=\"" + PLOG4U_ORG_STORE + "\" url=\"http://www.plog4u.org/index.php\"/>"
165 + "<config name=\"WikipediaEN Download\" type-id=\"" + PREFIX_LOAD
166 + "WikipediaEN\" url=\"http://en.wikipedia.org/wiki/Special:Export\"/>" + "<config name=\"WikibooksEN Download\" type-id=\""
167 + PREFIX_LOAD + "WikibooksEN\" url=\"http://en.wikibooks.org/wiki/Special:Export\"/>"
168 + "<config name=\"WikipediaEN Upload\" type-id=\"" + PREFIX_STORE
169 + "WikipediaEN\" url=\"http://en.wikipedia.org/w/index.php\"/>" + "<config name=\"WikibooksEN Upload\" type-id=\""
170 + PREFIX_STORE + "WikibooksEN\" url=\"http://en.wikibooks.org/w/index.php\"/>"
171 + "<config name=\"Wikipedia-SQL\" type-id=\"Wikipedia SQL access\" user=\"root\" url=\"jdbc:mysql://localhost/wikidb\"/>"
172 + "</configurations>";
174 public static WikipediaDB fWikiDB = null;
176 private static ConfigurationManager manager;
179 // public static final String IMG_MONITOR_ON = "monitorOn";
181 // public static final String IMG_MONITOR_OFF = "monitorOff";
184 * Creates an image and places it in the image registry.
187 * the identifier for the image
189 * the base URL for the image
191 protected static void createImageDescriptor(WikiEditorPlugin plugin, String id, URL baseURL) {
192 // Delegate to the plugin instance to avoid concurrent class loading problems
193 plugin.privateCreateImageDescriptor(id, baseURL);
196 public static WikiEditorPlugin getDefault() {
201 * Returns the image descriptor for the given image ID. Returns null if there is no such image.
204 * the identifier for the image to retrieve
205 * @return the image associated with the given ID
207 public static ImageDescriptor getImageDescriptor(String id) {
208 // Delegate to the plugin instance to avoid concurrent class loading problems
209 return getDefault().privateGetImageDescriptor(id);
213 * Convenience method to get an image descriptor for an extension
216 * the extension declaring the image
217 * @param subdirectoryAndFilename
218 * the path to the image
221 public static ImageDescriptor getImageDescriptorFromExtension(IExtension extension, String subdirectoryAndFilename) {
222 IPluginDescriptor pluginDescriptor = extension.getDeclaringPluginDescriptor();
223 URL path = pluginDescriptor.getInstallURL();
224 URL fullPathString = null;
226 fullPathString = new URL(path, subdirectoryAndFilename);
227 return ImageDescriptor.createFromURL(fullPathString);
228 } catch (MalformedURLException e) {
233 public static String getResourceString(String key) {
234 ResourceBundle bundle = WikiEditorPlugin.getDefault().getResourceBundle();
236 return (bundle != null) ? bundle.getString(key) : key;
237 } catch (MissingResourceException e) {
243 * Returns the standard display to be used. The method first checks, if the thread calling this method has an associated display.
244 * If so, this display is returned. Otherwise the method returns the default display.
246 public static Display getStandardDisplay() {
247 Display display = Display.getCurrent();
248 if (display == null) {
249 display = Display.getDefault();
254 private ContributionContextTypeRegistry fContextTypeRegistry;
256 private ResourceBundle fResourceBundle;
258 private ContributionTemplateStore fTemplateStore;
260 private Hashtable imageDescriptors = new Hashtable(20);
262 public WikiEditorPlugin(IPluginDescriptor descriptor) {
266 for (int i = 0; i < PREDEFINED_TYPES.length; i++) {
267 CONFIGURATION_TYPES.add(PREDEFINED_TYPES[i]);
270 for (int i = 0; i < PREDEFINED_WIKIS.length; i++) {
271 CONFIGURATION_TYPES.add(PREFIX_LOAD + PREDEFINED_WIKIS[i]);
273 for (int i = 0; i < PREDEFINED_WIKIS.length; i++) {
274 CONFIGURATION_TYPES.add(PREFIX_STORE + PREDEFINED_WIKIS[i]);
276 manager = ConfigurationManager.getInstance();
278 fResourceBundle = ResourceBundle.getBundle("net.sourceforge.phpeclipse.wiki.editor.WikiEditorMessages");
279 } catch (MissingResourceException x) {
280 fResourceBundle = null;
285 * Creates an image and places it in the image registry.
287 protected void createImageDescriptor(String id, URL baseURL) {
290 url = new URL(baseURL, ICON_PATH + id);
291 } catch (MalformedURLException e) {
293 ImageDescriptor desc = ImageDescriptor.createFromURL(url);
294 imageDescriptors.put(id, desc);
297 public IWorkbenchPage getActivePage() {
298 IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow();
300 return window.getActivePage();
304 public ContextTypeRegistry getContextTypeRegistry() {
305 if (fContextTypeRegistry == null) {
306 fContextTypeRegistry = new ContributionContextTypeRegistry();
307 fContextTypeRegistry.addContextType("net.sourceforge.phpeclipse.wiki.editor.templates");
309 return fContextTypeRegistry;
312 public Image getImage(String key) {
313 Image image = getImageRegistry().get(key);
315 ImageDescriptor d = getImageDescriptor(key);
316 image = d.createImage();
317 getImageRegistry().put(key, image);
322 public ResourceBundle getResourceBundle() {
323 return fResourceBundle;
326 public TemplateStore getTemplateStore() {
327 if (fTemplateStore == null) {
328 fTemplateStore = new ContributionTemplateStore(getContextTypeRegistry(), getDefault().getPreferenceStore(), "templates");
330 fTemplateStore.load();
331 } catch (IOException e) {
336 return fTemplateStore;
339 private static boolean isDefined(String test) {
340 if (test != null && test.length() > 0) {
341 for (int i = 0; i < test.length(); i++) {
342 if (!Character.isWhitespace(test.charAt(i))) {
353 * @see org.eclipse.ui.plugin.AbstractUIPlugin#initializeDefaultPreferences(org.eclipse.jface.preference.IPreferenceStore)
355 protected void initializeDefaultPreferences(IPreferenceStore store) {
356 store.setDefault(PREF_STRING_CONFIGURATIONS, CONFIG_MEMENTO);
357 store.setDefault(CONSOLE_OUTPUT, "true");
359 String operatingSystem = Platform.getOS();
360 // operatingSystem ==> maxosx, linux, solaris, win32,...
362 InputStream is = getDefault().openStream(new Path("prefs/default_" + operatingSystem + ".properties"));
363 PropertyResourceBundle resourceBundle = new PropertyResourceBundle(is);
365 String key = HTTP_TIMEOUT;
367 String timeout = resourceBundle.getString(key);
368 int val = Integer.parseInt(timeout);
374 store.setDefault(key, Integer.toString(val));
375 } catch (Exception e) {
376 store.setDefault(key, "30000"); // 30 seconds timeout
379 key = HTTP_PROXYHOST;
381 String proxyHost = resourceBundle.getString(key);
382 if (isDefined(proxyHost)) {
383 store.setDefault(key, proxyHost);
385 store.setDefault(key, System.getProperty(key, ""));
387 } catch (Exception e) {
388 store.setDefault(key, System.getProperty(key, ""));
391 key = HTTP_PROXYPORT;
393 String proxyPort = resourceBundle.getString(key);
394 if (isDefined(proxyPort)) {
395 // check for integer parse exception:
396 Integer.parseInt(proxyPort);
397 store.setDefault(key, proxyPort);
399 store.setDefault(key, System.getProperty(key, "80"));
401 } catch (Exception e) {
402 store.setDefault(key, System.getProperty(key, "80"));
405 key = HTTP_PROXYUSERNAME;
407 String proxyUserName = resourceBundle.getString(key);
408 if (isDefined(proxyUserName)) {
409 store.setDefault(key, proxyUserName);
411 store.setDefault(key, System.getProperty(key, ""));
413 } catch (Exception e) {
414 store.setDefault(key, System.getProperty(key, ""));
417 key = HTTP_PROXYPASSWORD;
419 String proxyPassWord = resourceBundle.getString(key);
420 if (isDefined(proxyPassWord)) {
421 store.setDefault(key, proxyPassWord);
423 store.setDefault(key, System.getProperty(key, ""));
425 } catch (Exception e) {
426 store.setDefault(key, System.getProperty(key, ""));
429 } catch (Exception e) {
435 * Initializes the table of images used in this plugin. The plugin is provided because this method is called before the plugin
436 * staic variable has been set. See the comment on the getPlugin() method for a description of why this is required.
438 private void initializeImages() {
439 URL baseURL = getDescriptor().getInstallURL();
442 createImageDescriptor("glyphs/glyph1.gif", baseURL); //$NON-NLS-1$
443 createImageDescriptor("glyphs/glyph2.gif", baseURL); //$NON-NLS-1$
444 createImageDescriptor("glyphs/glyph3.gif", baseURL); //$NON-NLS-1$
445 createImageDescriptor("glyphs/glyph4.gif", baseURL); //$NON-NLS-1$
446 createImageDescriptor("glyphs/glyph5.gif", baseURL); //$NON-NLS-1$
447 createImageDescriptor("glyphs/glyph6.gif", baseURL); //$NON-NLS-1$
448 createImageDescriptor("glyphs/glyph7.gif", baseURL); //$NON-NLS-1$
449 createImageDescriptor("glyphs/glyph8.gif", baseURL); //$NON-NLS-1$
453 public void log(String message) {
454 getDefault().getLog().log(new Status(IStatus.OK, PLUGIN_ID, IStatus.OK, message, null));
457 public void log(String message, Exception e) {
458 getDefault().getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK, "Caught exception", e));
461 public void logAndReport(String title, String message, Exception e) {
463 reportError(title, message);
466 private void privateCreateImageDescriptor(String id, URL baseURL) {
469 url = new URL(baseURL, ICON_PATH + id);
470 } catch (MalformedURLException e) {
472 ImageDescriptor desc = ImageDescriptor.createFromURL(url);
473 imageDescriptors.put(id, desc);
476 private ImageDescriptor privateGetImageDescriptor(String id) {
477 if (!imageDescriptors.containsKey(id)) {
478 URL baseURL = WikiEditorPlugin.getDefault().getDescriptor().getInstallURL();
479 createImageDescriptor(getDefault(), id, baseURL);
481 return (ImageDescriptor) imageDescriptors.get(id);
484 public void reportError(String title, String message) {
486 // Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
487 MessageDialog.openError(null, title, message);
488 } catch (RuntimeException e) {
489 log(e.getLocalizedMessage(), e);
493 public void startup() throws CoreException {
498 * Returns the translated String found with the given key.
500 * @return java.lang.String
504 public static String getResource(String key) {
506 return Platform.getResourceString(getDefault().getBundle(), key);
507 } catch (Exception e) {
513 * Returns the translated String found with the given key, and formatted with the given object.
519 * @return java.lang.String
521 public static String getResource(String key, Object[] obj) {
523 return MessageFormat.format(getResource(key), obj);
524 } catch (Exception e) {
530 * Returns the translated String found with the given key, and formatted with the given object.
536 * @return java.lang.String
538 public static String getResource(String key, String s) {
540 return MessageFormat.format(getResource(key), new String[] { s });
541 } catch (Exception e) {
547 * Return a list of all the existing configurations.
549 * @return java.util.List
551 public static List getConfigurations() {
552 return manager.getConfigurations();
556 * Create a new monitor.
558 * @return working copy
560 public static IConfigurationWorkingCopy createConfiguration() {
561 return manager.createConfiguration();
564 public static ArrayList getTypes() {
565 return CONFIGURATION_TYPES;
568 public static void addType(String type) {
569 CONFIGURATION_TYPES.add(type);
575 * @see org.eclipse.core.runtime.Plugin#shutdown()
577 public void shutdown() throws CoreException {
578 if (fWikiDB != null) {
581 } catch (SQLException e) {
587 public static void log(int severity, String message) {
588 Status status = new Status(severity, PLUGIN_ID, IStatus.OK, message, null);
592 public static void log(IStatus status) {
593 getDefault().getLog().log(status);
596 public static void log(Throwable e) {
597 log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, "PHPeclipsePlugin.internalErrorOccurred", e)); //$NON-NLS-1$
601 * Get a IWikpedia singleton instance through reflection (i.e. Method#invoke() )
605 * @throws NoSuchMethodException
606 * @throws IllegalAccessException
607 * @throws ClassNotFoundException
608 * @throws InvocationTargetException
610 public static IWikipedia getWikiInstance(String wikiLocale) throws NoSuchMethodException, IllegalAccessException,
611 ClassNotFoundException, InvocationTargetException {
612 String className = "net.sourceforge.phpeclipse.wiki.actions.mediawiki.config." + wikiLocale;
613 Class cls = Class.forName(className);
614 Method method = cls.getMethod("getInstance", new Class[0]);
615 return (IWikipedia) method.invoke(null, new Object[0]);
619 * Returns the workspace instance.
621 public static IWorkspace getWorkspace() {
622 return ResourcesPlugin.getWorkspace();
626 // public static void main(String[] args) {
628 // getWikiInstance("WikipediaEN");
629 // } catch (NoSuchMethodException e) {
630 // // TODO Auto-generated catch block
631 // e.printStackTrace();
632 // } catch (IllegalAccessException e) {
633 // // TODO Auto-generated catch block
634 // e.printStackTrace();
635 // } catch (ClassNotFoundException e) {
636 // // TODO Auto-generated catch block
637 // e.printStackTrace();
638 // } catch (InvocationTargetException e) {
639 // // TODO Auto-generated catch block
640 // e.printStackTrace();