From: axelcl Date: Mon, 31 Jan 2005 19:57:11 +0000 (+0000) Subject: RefreshJob loads max 10 articles at a time; there's a delay of 1 second for the next... X-Git-Url: http://git.phpeclipse.com RefreshJob loads max 10 articles at a time; there's a delay of 1 second for the next 10 articles. --- diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/CreateFilesFromLinksEditorAction.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/CreateFilesFromLinksEditorAction.java index 5f6739b..0fa5c0d 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/CreateFilesFromLinksEditorAction.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/CreateFilesFromLinksEditorAction.java @@ -12,48 +12,19 @@ public final class CreateFilesFromLinksEditorAction extends OpenWikiLinkEditorAc public void openWikiLinkOnSelection() { IDocument doc = getDocument(); - // ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection(); - // int pos = selection.getOffset(); - ArrayList startPositionList = new ArrayList(); - char[] text = doc.get().toCharArray(); - try { - char ch = ' '; - int i = 0; - int startPos = -1; - while (true) { - ch = text[i++]; - switch (ch) { - case '[': - ch = text[i++]; - if (ch == '[') { - startPos = i; - } - break; - case ']': - ch = text[i++]; - if (ch == ']' && startPos != (-1)) { - startPositionList.add(new Integer(startPos)); - } - break; - case '\r': - case '\n': - startPos = -1; - break; - } - } - } catch (IndexOutOfBoundsException e) { - // ignore it - } + ArrayList startPositionList = getLinksStartingPosition(doc); + HashSet wikiNames = new HashSet(); ArrayList filesList = new ArrayList(); ArrayList wikiList = new ArrayList(); String wikiTitle; Integer posInteger; IFile currentFile = ((IFileEditorInput) editor.getEditorInput()).getFile(); + for (int i = 0; i < startPositionList.size(); i++) { posInteger = (Integer) startPositionList.get(i); wikiTitle = getWikiTitle(editor, doc, posInteger.intValue()); - + if (wikiTitle != null && !wikiTitle.equals("")) { if (!wikiNames.contains(wikiTitle)) { IFile file = getWikiFile(currentFile, wikiTitle); @@ -61,35 +32,60 @@ public final class CreateFilesFromLinksEditorAction extends OpenWikiLinkEditorAc wikiList.add(wikiTitle); wikiNames.add(wikiTitle); } - } + } } - + if (filesList.size() > 0) { IFile[] files = new IFile[filesList.size()]; String[] wikiTitles = new String[filesList.size()]; filesList.toArray(files); wikiList.toArray(wikiTitles); + Job job = new CreateFilesJob(files, wikiTitles); // job.setRule(createRule(files)); job.setRule(null); job.setUser(true); job.schedule(); + } -// -// -// Iterator iter = wikiNames.iterator(); -// while (iter.hasNext()) { -// openWikiFile(file, (String) iter.next(), false); -// } } -// public void openWikiFile(IFile cfile, String word, boolean openEditor) { -// if (word != null && !word.equals("")) { -// IFile file = getWikiFile(cfile, word); -// try { -// createNewFileIfNeeded(file, word); -// } catch (Exception e) { -// } -// } -// } + /** + * @param doc + * @return + */ + private ArrayList getLinksStartingPosition(IDocument doc) { + ArrayList startPositionList = new ArrayList(); + char[] text = doc.get().toCharArray(); + try { + char ch = ' '; + int i = 0; + int startPos = -1; + while (true) { + ch = text[i++]; + switch (ch) { + case '[': + ch = text[i++]; + if (ch == '[') { + startPos = i; + } + break; + case ']': + ch = text[i++]; + if (ch == ']' && startPos != (-1)) { + startPositionList.add(new Integer(startPos)); + startPos = -1; + } + break; + case '\r': + case '\n': + startPos = -1; + break; + } + } + } catch (IndexOutOfBoundsException e) { + // ignore it + } + return startPositionList; + } } \ No newline at end of file diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/CreateFilesJob.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/CreateFilesJob.java index c947d56..095f77e 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/CreateFilesJob.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/CreateFilesJob.java @@ -37,6 +37,8 @@ public class CreateFilesJob extends WorkspaceJob { byte[] buffer = newText.getBytes(); ByteArrayInputStream source = new ByteArrayInputStream(buffer); ProblemConsole console = new ProblemConsole(); + boolean showConsole = WikiEditorPlugin.getDefault().getPreferenceStore().getBoolean(WikiEditorPlugin.CONSOLE_OUTPUT); + for (int i = 0; i < files.length; i++) { file = files[i]; wikiTitle = wikiTitles[i]; @@ -51,6 +53,9 @@ public class CreateFilesJob extends WorkspaceJob { file.create(source, true, monitor); Page page = new Page("", wikiTitle, ""); page.createXMLFile(file, false); + if (showConsole) { + console.println("Title: "+wikiTitle+" =>File: " + file.getLocation().toString() + "created\n"); + } } else { String message = "File: " + file.getLocation().toString() + "\n==>file already exists!"; monitor.subTask(message); diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/OpenWikiLinkEditorAction.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/OpenWikiLinkEditorAction.java index c09b190..0a01234 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/OpenWikiLinkEditorAction.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/OpenWikiLinkEditorAction.java @@ -128,10 +128,10 @@ public class OpenWikiLinkEditorAction implements IEditorActionDelegate { if (wikiLinkStart != (-1) && wikiLinkEnd != (-1) && wikiLinkStart < wikiLinkEnd) { return new String(word.toCharArray(), wikiLinkStart, wikiLinkEnd - wikiLinkStart); } - } catch (BadLocationException e) { + } catch (Exception e) { } - return ""; + return null; } public IDocument getDocument() { @@ -145,8 +145,10 @@ public class OpenWikiLinkEditorAction implements IEditorActionDelegate { int pos = selection.getOffset(); String wikiTitle = getWikiTitle(editor, doc, pos); - IFileEditorInput ei = (IFileEditorInput) editor.getEditorInput(); - openWikiFile(ei.getFile(), wikiTitle, true); + if (wikiTitle != null && !wikiTitle.equals("")) { + IFileEditorInput ei = (IFileEditorInput) editor.getEditorInput(); + openWikiFile(ei.getFile(), wikiTitle, true); + } } // public void openWikiUrl(IProject project, String wikiTitle) { @@ -173,7 +175,7 @@ public class OpenWikiLinkEditorAction implements IEditorActionDelegate { Page page = new Page("", wikiTitle, ""); page.createXMLFile(file, false); -// createXMLFile(wikiTitle, file, false); + // createXMLFile(wikiTitle, file, false); IDE.openEditor(WikiEditorPlugin.getDefault().getActivePage(), file, true); } catch (Exception e) { diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/ProblemConsole.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/ProblemConsole.java index d258d6a..04161fd 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/ProblemConsole.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/ProblemConsole.java @@ -19,7 +19,6 @@ public class ProblemConsole { hasMessages = false; myConsole = new MessageConsole("Wikipedia Editor Problems", null); ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[] { myConsole }); - ; ConsolePlugin.getDefault().getConsoleManager().showConsoleView(myConsole); // layout.addView(IConsoleConstants.ID_CONSOLE_VIEW, IPageLayout.BOTTOM, .5f,IPageLayout.ID_EDITOR_AREA); stream = myConsole.newMessageStream(); diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/RefreshJob.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/RefreshJob.java index a50202d..9758384 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/RefreshJob.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/RefreshJob.java @@ -47,6 +47,7 @@ public class RefreshJob extends WorkspaceJob { public IStatus runInWorkspace(IProgressMonitor monitor) { ProblemConsole console = new ProblemConsole(); IFile file = null; + try { monitor.beginTask("Download Wiki Articles: ", 100); // ArrayList wikiTitles = new ArrayList(); @@ -56,51 +57,35 @@ public class RefreshJob extends WorkspaceJob { StringBuffer buffer = new StringBuffer(); HashMap map = new HashMap(); String wikiTitle; + int titleCounter = 0; for (int i = 0; i < files.length; i++) { file = files[i]; wikiTitle = createWikiTitle(file, i); buffer.append(wikiTitle); + titleCounter++; map.put(wikiTitle, file); if (i != files.length - 1) { buffer.append("\n"); } - } - MediaWikiConnector mwConnector = new MediaWikiConnector(); - String url = actionURL; - if (url == null) { - url = configuration.getActionUrl() + "/" + configuration.getSpecialNs() + ":Export"; - } - // get a list of Parsed elements - monitor.subTask("Downloading (XML Import)"); - ArrayList list = mwConnector.loadXML(configuration, url, buffer.toString()); - String body; - - if (list.size() == 0&&files.length>0) { - console.println("File: " + file.getLocation().toString() + " not available on the server."); - } else { - if (list.size() < files.length) { - console.println("Not all requested files are available on the server."); - } - for (int i = 0; i < list.size(); i++) { - Parsed parsed = (Parsed) list.get(i); - wikiTitle = parsed.getTitle(); - if (wikiTitle != null) { - body = parsed.getBody(); - if (body != null) { - file = (IFile) map.get(wikiTitle); - if (file != null) { - // rearrange parsed data into a page for XStream hamdling: - Page page = new Page(parsed.getDateTimestamp(), wikiTitle, body); - monitor.subTask("Modify file: " + file.getLocation().toString()); - updateFileContent(console, file, page, body, configuration, monitor); - } - } - } - if (monitor.isCanceled()) { + if (i % 10 == 0) { + // read only 10 files at a time + IStatus status = readWikisFromBuffer(buffer, map, monitor, console); + if (status.equals(Status.CANCEL_STATUS)) { return Status.CANCEL_STATUS; } + buffer = new StringBuffer(); + titleCounter = 0; } } + if (titleCounter > 0) { + IStatus status = readWikisFromBuffer(buffer, map, monitor, console); + if (status.equals(Status.CANCEL_STATUS)) { + return Status.CANCEL_STATUS; + } + buffer = new StringBuffer(); + titleCounter = 0; + } + if (isModal(this)) { // The progress dialog is still open show the message console.reportError(); @@ -123,6 +108,12 @@ public class RefreshJob extends WorkspaceJob { } else { console.println("HTTP-MethodException: " + e.getMessage()); } + } catch (InterruptedException e) { + if (file != null) { + console.println("File: " + file.getLocation().toString() + "\n==>InterruptedException: " + e.getMessage()); + } else { + console.println("InterruptedException: " + e.getMessage()); + } } finally { monitor.done(); } @@ -134,6 +125,59 @@ public class RefreshJob extends WorkspaceJob { } /** + * @param buffer + * @param map + * @param monitor + * @param console + * @param file + * @return + * @throws UnexpectedAnswerException + * @throws MethodException + */ + private IStatus readWikisFromBuffer(StringBuffer buffer, HashMap map, IProgressMonitor monitor, ProblemConsole console) + throws UnexpectedAnswerException, InterruptedException, MethodException { + String wikiTitle; + boolean showConsole = WikiEditorPlugin.getDefault().getPreferenceStore().getBoolean(WikiEditorPlugin.CONSOLE_OUTPUT); + MediaWikiConnector mwConnector = new MediaWikiConnector(); + String url = actionURL; + if (url == null) { + url = configuration.getActionUrl() + "/" + configuration.getSpecialNs() + ":Export"; + } + // get a list of Parsed elements + monitor.subTask("Downloading (XML Import)"); + if (showConsole) { + console.println("Downloading (XML Import):\n" + buffer.toString()); + } + ArrayList list = mwConnector.loadXML(configuration, url, buffer.toString()); + String body; + IFile file = null; + for (int i = 0; i < list.size(); i++) { + Parsed parsed = (Parsed) list.get(i); + wikiTitle = parsed.getTitle(); + if (wikiTitle != null) { + body = parsed.getBody(); + if (body != null) { + file = (IFile) map.get(wikiTitle); + if (file != null) { + // rearrange parsed data into a page for XStream hamdling: + Page page = new Page(parsed.getDateTimestamp(), wikiTitle, body); + monitor.subTask("Modify file: " + file.getLocation().toString()); + if (showConsole) { + console.println("Update file: " + file.getLocation().toString()); + } + updateFileContent(console, file, page, body, configuration, monitor); + } + } + } + if (monitor.isCanceled()) { + return Status.CANCEL_STATUS; + } + } + + return Status.OK_STATUS; + } + + /** * @param file * @param wikiTitle * @param i diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/MediaWikiConnector.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/MediaWikiConnector.java index 7d42d9b..07cc568 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/MediaWikiConnector.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/MediaWikiConnector.java @@ -15,7 +15,6 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia; -import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.WikipediaDE; import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.MethodException; import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.PageNotEditableException; import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.UnexpectedAnswerException; @@ -252,7 +251,8 @@ public class MediaWikiConnector { return result; } - public ArrayList loadXML(IWikipedia config, String actionURL, String pages) throws UnexpectedAnswerException, MethodException { + public ArrayList loadXML(IWikipedia config, String actionURL, String pages) throws UnexpectedAnswerException, MethodException , InterruptedException{ + storeThrottle.delay(); PostMethod method = new PostMethod(actionURL); method.setFollowRedirects(false); method.addRequestHeader("User-Agent", userAgent); @@ -503,7 +503,7 @@ public class MediaWikiConnector { } catch (UnexpectedAnswerException e) { // TODO Auto-generated catch block e.printStackTrace(); - } catch (MethodException e) { + } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/editor/WikiEditorPlugin.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/editor/WikiEditorPlugin.java index fa131a4..099922a 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/editor/WikiEditorPlugin.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/editor/WikiEditorPlugin.java @@ -131,6 +131,8 @@ public class WikiEditorPlugin extends AbstractUIPlugin { public final static String EXPORT_CSS_URL = "__export_css_url"; public final static String PREF_STRING_CONFIGURATIONS = "__configurations4"; + + public final static String CONSOLE_OUTPUT = "__console_output"; public final static String CONFIG_MEMENTO = "" + "" + "" @@ -324,6 +326,7 @@ public class WikiEditorPlugin extends AbstractUIPlugin { */ protected void initializeDefaultPreferences(IPreferenceStore store) { store.setDefault(PREF_STRING_CONFIGURATIONS, CONFIG_MEMENTO); + store.setDefault(CONSOLE_OUTPUT, "true"); } /* @@ -378,8 +381,8 @@ public class WikiEditorPlugin extends AbstractUIPlugin { public void reportError(String title, String message) { try { - Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); - MessageDialog.openError(shell, title, message); +// Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); + MessageDialog.openError(null, title, message); } catch (RuntimeException e) { log(e.getLocalizedMessage(), e); }