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);
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
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];
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);
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() {
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) {
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) {
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();
public IStatus runInWorkspace(IProgressMonitor monitor) {
ProblemConsole console = new ProblemConsole();
IFile file = null;
+
try {
monitor.beginTask("Download Wiki Articles: ", 100);
// ArrayList wikiTitles = new ArrayList();
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();
} 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();
}
}
/**
+ * @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
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;
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);
} catch (UnexpectedAnswerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
- } catch (MethodException e) {
+ } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<configurations>"
+ "<config name=\"Google Search\" type-id=\"HTTP Query\" url=\"http://www.google.com/search?q=$text.selection\"/>"
*/
protected void initializeDefaultPreferences(IPreferenceStore store) {
store.setDefault(PREF_STRING_CONFIGURATIONS, CONFIG_MEMENTO);
+ store.setDefault(CONSOLE_OUTPUT, "true");
}
/*
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);
}