From 1e2bd4a3db1c9628222028c0474017ab545ee63a Mon Sep 17 00:00:00 2001 From: axelcl Date: Sat, 22 Jan 2005 13:23:18 +0000 Subject: [PATCH 1/1] Added some more console messages --- .../phpeclipse/wiki/actions/CreateFilesJob.java | 5 +- .../actions/mediawiki/LoadWikipediaSQLAction.java | 53 ---- .../wiki/actions/mediawiki/RefreshJob.java | 4 +- .../mediawiki/connect/MediaWikiConnector.java | 8 + .../wiki/actions/mediawiki/connect/XMLReader.java | 83 +++++- .../wiki/actions/mediawiki/post/PostJob.java | 121 ++++++-- .../phpeclipse/wiki/editor/WikiEditorPlugin.java | 8 +- .../phpeclipse/wiki/internal/Configuration.java | 312 ++++++++++++++++++-- 8 files changed, 467 insertions(+), 127 deletions(-) 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 82f7769..1b384c7 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 @@ -42,9 +42,12 @@ public class CreateFilesJob extends WorkspaceJob { } try { if (!file.exists()) { + monitor.subTask("Create File: "+file.getLocation().toString()); file.create(source, true, monitor); } else { - console.println("File: " + file.getLocation().toString() + "\n==>file already exists!"); + String message = "File: " + file.getLocation().toString() + "\n==>file already exists!"; + monitor.subTask(message); + console.println(message); } } catch (CoreException e) { if (file != null) { diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/LoadWikipediaSQLAction.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/LoadWikipediaSQLAction.java index 1d0b92c..3344736 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/LoadWikipediaSQLAction.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/LoadWikipediaSQLAction.java @@ -50,56 +50,6 @@ public class LoadWikipediaSQLAction implements IEditorActionDelegate { private IWorkbenchWindow window; - private void createFolder(IFolder folderHandle, IProgressMonitor monitor) throws CoreException { - try { - // Create the folder resource in the workspace - // Recursive to create any folders which do not exist already - if (!folderHandle.exists()) { - IContainer parent = folderHandle.getParent(); - if (parent instanceof IFolder && (!((IFolder) parent).exists())) { - createFolder((IFolder) parent, monitor); - } - folderHandle.create(false, true, monitor); - } - } catch (CoreException e) { - // If the folder already existed locally, just refresh to get contents - if (e.getStatus().getCode() == IResourceStatus.PATH_OCCUPIED) - folderHandle.refreshLocal(IResource.DEPTH_INFINITE, new SubProgressMonitor(monitor, 500)); - else - throw e; - } - } - - /** - * Creates a folder resource handle for the folder with the given workspace path. This method does not create the folder resource; - * this is the responsibility of createFolder. - * - * @param folderPath - * the path of the folder resource to create a handle for - * @return the new folder resource handle - * @see #createFolder - */ - private IFolder createFolderHandle(IPath folderPath) { - return IDEWorkbenchPlugin.getPluginWorkspace().getRoot().getFolder(folderPath); - } - - private void createNewFileIfNeeded(IFile file, String word) throws CoreException { - if (!file.exists()) { - createWikiFile(file, word); - } - } - - private void createWikiFile(IFile file, String word) throws CoreException { - IContainer parent = file.getParent(); - if (parent instanceof IFolder && (!((IFolder) parent).exists())) { - createFolder((IFolder) parent, null); - } - String newText = ""; - byte[] buffer = newText.getBytes(); - ByteArrayInputStream source = new ByteArrayInputStream(buffer); - file.create(source, true, null); - } - public void dispose() { } @@ -155,9 +105,6 @@ public class LoadWikipediaSQLAction implements IEditorActionDelegate { } public void openWikiLinkOnSelection() { - IDocument doc = getDocument(); - ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection(); - int pos = selection.getOffset(); IFileEditorInput ei = (IFileEditorInput) fEditor.getEditorInput(); openWikiFile(ei.getFile()); } 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 49dec5b..321c369 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 @@ -46,7 +46,7 @@ public class RefreshJob extends WorkspaceJob { ProblemConsole console = new ProblemConsole(); IFile file = null; try { - monitor.beginTask("Refresh Wikis", 100); + monitor.beginTask("Download Wiki Articles: ", 100); // ArrayList wikiTitles = new ArrayList(); // for (int i = 0; i < files.length; i++) { // wikiTitles.add( Util.getReadableWikiName(files[i]) ); @@ -68,6 +68,7 @@ public class RefreshJob extends WorkspaceJob { url = configuration.getActionUrl() + "/" + configuration.getSpecialNs() + ":Export"; } // get a list of Parsed elements + monitor.subTask("Downloading (XML Import)"); ArrayList list = mwc.loadXML(configuration, url, buffer.toString()); String body; @@ -79,6 +80,7 @@ public class RefreshJob extends WorkspaceJob { if (body != null) { file = (IFile) map.get(wikiTitle); if (file != null) { + monitor.subTask("Modify file: "+file.getLocation().toString()); updateFileContent(console, file, parsed, body, configuration, monitor); } } 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 4c1abb6..7d42d9b 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 @@ -131,6 +131,13 @@ public class MediaWikiConnector { } else if (responseCode == 200 && responseBody.matches(config.getLoginWrongPw()) || responseCode == 200 && responseBody.matches(config.getLoginNoUser())) { result = false; + if (responseBody.matches(config.getLoginNoUser())) { + throw new UnexpectedAnswerException("login not successful: wrong user name: "+user); + } else if (responseBody.matches(config.getLoginWrongPw())) { + throw new UnexpectedAnswerException("login not successful: wrong password for user: "+user); + } else { + throw new UnexpectedAnswerException("logout not successful: responseCode == 200"); + } } else { throw new UnexpectedAnswerException("login not successful: " + method.getStatusLine()); } @@ -177,6 +184,7 @@ public class MediaWikiConnector { } else if (responseCode == 200) { //### should check for a failure message result = false; + throw new UnexpectedAnswerException("logout not successful: responseCode == 200"); } else { throw new UnexpectedAnswerException("logout not successful: " + method.getStatusLine()); } diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/XMLReader.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/XMLReader.java index 8cdfc20..013ff3b 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/XMLReader.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/XMLReader.java @@ -30,8 +30,11 @@ import org.xml.sax.SAXParseException; */ public class XMLReader { private static final String TITLE_TAG = "title"; //$NON-NLS-1$ + private static final String TIMESTAMP_TAG = "timestamp"; //$NON-NLS-1$ + private static final String TEXT_TAG = "text"; //$NON-NLS-1$ + private static final String PAGE_TAG = "page"; //$NON-NLS-1$ public XMLReader() { @@ -86,20 +89,20 @@ public class XMLReader { private static void traverse(String eleName, Node cNode, Parsed parsed) { switch (cNode.getNodeType()) { case Node.DOCUMENT_NODE: -// System.out.println("DOCUMENT_NODE " + cNode.getNodeName()); + // System.out.println("DOCUMENT_NODE " + cNode.getNodeName()); processChildren(eleName, cNode.getChildNodes(), parsed); break; case Node.ELEMENT_NODE: eleName = cNode.getNodeName(); -// System.out.println("ELEMENT_NODE " + eleName); -// NamedNodeMap attributeMap = cNode.getAttributes(); -// int numAttrs = attributeMap.getLength(); -// for (int i = 0; i < attributeMap.getLength(); i++) { -// Attr attribute = (Attr) attributeMap.item(i); -// String attrName = attribute.getNodeName(); -// String attrValue = attribute.getNodeValue(); -// } + // System.out.println("ELEMENT_NODE " + eleName); + // NamedNodeMap attributeMap = cNode.getAttributes(); + // int numAttrs = attributeMap.getLength(); + // for (int i = 0; i < attributeMap.getLength(); i++) { + // Attr attribute = (Attr) attributeMap.item(i); + // String attrName = attribute.getNodeName(); + // String attrValue = attribute.getNodeValue(); + // } processChildren(eleName, cNode.getChildNodes(), parsed); break; case Node.CDATA_SECTION_NODE: @@ -127,10 +130,59 @@ public class XMLReader { } /** + * Read the first timestamp found in the Wikipedia xml stream + * + * @param stream + * @return + * @throws Exception + */ + public static String getTimestamp(InputStream stream) throws Exception { + // Create a factory object for creating DOM parsers + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + // Now use the factory to create a DOM parser (a.k.a. a DocumentBuilder) + DocumentBuilder parser = factory.newDocumentBuilder(); + // Parse the file and build a Document tree to represent its content + Document document = parser.parse(stream); + // Ask the document for a list of all tags it contains + NodeList timestamps = document.getElementsByTagName(TIMESTAMP_TAG); + // Loop through those elements one at a time, and extract the + // content of their tags. + int numPages = timestamps.getLength(); + for (int i = 0; i < numPages; i++) { + ElementNode page = (ElementNode) timestamps.item(i); // A + return page.getChildNodes().item(0).getNodeValue(); + } + return null; + } + + /** + * Get the timestamp as java Date Format String + * + * @param stream + * @return + * @throws Exception + */ + public static String getDateTimestamp(InputStream stream) throws Exception { + String timestamp = getTimestamp(stream); + if (timestamp!=null) { + StringBuffer buffer = new StringBuffer(); + // 2004-11-22T12:41:10Z + buffer.append(timestamp.substring(0,4)); //year + buffer.append(timestamp.substring(5,7)); //month + buffer.append(timestamp.substring(8,10)); //day + buffer.append(timestamp.substring(11,13));//hour + buffer.append(timestamp.substring(14,16));//minute + buffer.append(timestamp.substring(17,19));//second + return buffer.toString(); + } + return null; + } + + /** * Reads the wikipedia xml data from the given stream * * @param stream - * @return + * @return * @throws CoreException */ public static ArrayList readFromStream(Reader stream) throws CoreException { @@ -144,8 +196,7 @@ public class XMLReader { Document document = parser.parse(new InputSource(stream)); // Ask the document for a list of all tags it contains NodeList pages = document.getElementsByTagName(PAGE_TAG); - // Loop through those elements one at a time, and extract the - // content of their tags. + // Loop through those elements one at a time int numPages = pages.getLength(); for (int i = 0; i < numPages; i++) { @@ -162,7 +213,7 @@ public class XMLReader { } catch (IOException e) { throwReadException(e); } catch (SAXParseException e) { -// System.out.println("SAXParseException in line:" + e.getLineNumber() + " column:" + e.getColumnNumber()); + // System.out.println("SAXParseException in line:" + e.getLineNumber() + " column:" + e.getColumnNumber()); throwReadException(e); } catch (SAXException e) { throwReadException(e); @@ -174,7 +225,7 @@ public class XMLReader { Node node = attributes.getNamedItem(name); return node == null ? null : node.getNodeValue(); } - + // public static void saveToFile(File file) throws CoreException { // OutputStream stream = null; // try { @@ -347,9 +398,9 @@ public class XMLReader { + "\r\n" + "[[Kategorie:Rhetorischer Begriff]]\r\n" + "[[en:Synaesthesia]] [[es:Sinestesia]] [[sv:Synestesi]] [[tr:Sinestezi]]\r\n" + " \r\n" + " \r\n" + ""; - StringReader st = new StringReader(test2); + StringReader st = new StringReader(test); - readFromStream(st); + System.out.println(readFromStream(st)); } catch (CoreException e) { // TODO Auto-generated catch block e.printStackTrace(); diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/post/PostJob.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/post/PostJob.java index 5767954..24f807e 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/post/PostJob.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/post/PostJob.java @@ -3,14 +3,15 @@ package net.sourceforge.phpeclipse.wiki.actions.mediawiki.post; import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; -import java.util.Date; import java.util.HashMap; import net.sourceforge.phpeclipse.wiki.actions.ProblemConsole; import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia; import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.Content; import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.MediaWikiConnector; +import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.XMLReader; 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; import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin; import net.sourceforge.phpeclipse.wiki.internal.Configuration; @@ -20,10 +21,13 @@ import net.sourceforge.phpeclipse.wiki.velocity.EditorText; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.resources.WorkspaceJob; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.jobs.Job; import org.eclipse.ui.progress.IProgressConstants; @@ -77,25 +81,20 @@ public class PostJob extends WorkspaceJob { return template; } - private void selectWiki(String body, MediaWikiConnector connector, String actionUrl, String wikiName) { + private void selectWiki(String timestamp, String body, MediaWikiConnector connector, String actionUrl, String wikiName) + throws UnexpectedAnswerException, MethodException, PageNotEditableException, InterruptedException { - try { - String url = generateUrl(configuration.getURL(), wikiName); - - Date d = new Date(); - Content content = new Content(String.valueOf(d.getTime()), body); + String url = generateUrl(configuration.getURL(), wikiName); + // System.out.println(timestamp); + Content content = new Content(timestamp, body); - connector.store(wikipedia, actionUrl, wikiName, content, "", false, false); + connector.store(wikipedia, actionUrl, wikiName, content, "", false, false); - } catch (Exception e) { - e.printStackTrace(); - WikiEditorPlugin.getDefault() - .reportError("Exception occured: ", e.getMessage() + "\nSee stacktrace in /.metadata/.log file."); - } } public IStatus runInWorkspace(IProgressMonitor monitor) { boolean success = false; + IFile file = null; MediaWikiConnector connector = null; InputStream is = null; ProblemConsole console = new ProblemConsole(); @@ -105,15 +104,18 @@ public class PostJob extends WorkspaceJob { actionUrl = wikipedia.getActionUrl(); } try { - monitor.beginTask("Refresh Wikis", 100); + if (files.length>0) { + // prefetch for error messages + file = files[0]; + } + monitor.beginTask("Upload Wiki Articles: ", 100); int partWork = 100 / files.length; int work = 0; - int autoCreateIndex = -1; StringBuffer buffer = new StringBuffer(); HashMap map = new HashMap(); String wikiTitle; - IFile file = null; + monitor.subTask("Login user:"+user); connector = new MediaWikiConnector(); success = connector.login(wikipedia, actionUrl, user, password, false); if (success) { @@ -123,18 +125,37 @@ public class PostJob extends WorkspaceJob { is = file.getContents(); String wikiName = Util.getFileWikiName(file); String body = StoreWikipediaAction.getInputStreamAsString(is, wikipedia.getCharSet()); - autoCreateIndex = body.indexOf(WikiEditorPlugin.AUTOMATICALLY_CREATED); - if (autoCreateIndex < 0) { - selectWiki(body, connector, actionUrl, wikiName); + char ch; + boolean noContent = checkNoContent(body); + String srcBasePath = Util.getWikiTextsPath(file); + String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH); + + String filename = Util.getXMLFileName(file, binBasePath, srcBasePath); + IPath path = new Path(filename); + IFile xmlFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path); + String timestamp = null; + if (xmlFile.exists()) { + try { + timestamp = XMLReader.getDateTimestamp(xmlFile.getContents()); + } catch (Exception e2) { + } + } + if (timestamp == null) { + // Date d = new Date(); + // timestamp = String.valueOf(d.getTime()); + timestamp = ""; + } + + if (noContent) { + console.println("File: " + file.getLocation().toString() + "\n==>upload not allowed; Wiki text contains no content"); } else { - console.println("File: " + file.getLocation().toString() + "\n==>no upload allowed; Wiki text contains string: " - + WikiEditorPlugin.AUTOMATICALLY_CREATED); + monitor.subTask("Upload: " + file.getLocation().toString()); + selectWiki(timestamp, body, connector, actionUrl, wikiName); } } catch (CoreException e1) { if (file != null) { - console.println("File: " + file.getLocation().toString() + "\n==>CoreException: " - + e1.getMessage()); + console.println("File: " + file.getLocation().toString() + "\n==>CoreException: " + e1.getMessage()); } } if (monitor.isCanceled()) { @@ -156,15 +177,32 @@ public class PostJob extends WorkspaceJob { // } catch(CoreException e) { // return e.getStatus(); } catch (IOException e) { + if (file != null) { + console.println("File: " + file.getLocation().toString()); + } console.println("==>IOException: " + e.getMessage()); } catch (UnexpectedAnswerException e) { console.println("==>UnexpectedAnswerException: " + e.getMessage()); + } catch (InterruptedException e) { + if (file != null) { + console.println("File: " + file.getLocation().toString()); + } + console.println("==>InterruptedException: " + e.getMessage()); + } catch (PageNotEditableException e) { + if (file != null) { + console.println("File: " + file.getLocation().toString()); + } + console.println("==>PageNotEditableException: " + e.getMessage()); } catch (MethodException e) { + if (file != null) { + console.println("File: " + file.getLocation().toString()); + } console.println("==>HTTP-MethodException: " + e.getMessage()); } finally { monitor.done(); if (success && connector != null) { try { + monitor.subTask("Logout!"); connector.logout(wikipedia, actionUrl); } catch (UnexpectedAnswerException e1) { console.println("==>UnexpectedAnswerException: " + e1.getMessage()); @@ -186,6 +224,43 @@ public class PostJob extends WorkspaceJob { return Status.CANCEL_STATUS; } + /** + * @param body + * @param j + * @return + */ + private boolean checkNoContent(String body) { + char ch; + boolean noContent = true; + int j = 0; + try { + while (true) { + ch = body.charAt(j++); + if (!Character.isWhitespace(ch)) { + if (ch == '<' && body.charAt(j) == '!' && body.charAt(j + 1) == '-' && body.charAt(j + 2) == '-') { + // + j += 3; + while (true) { + ch = body.charAt(j++); + if (ch == '-' && body.charAt(j) == '-' && body.charAt(j + 1) == '>') { + j += 2; + break; + } + } + } else if (ch == '<' && body.charAt(j) == 'b' && body.charAt(j + 1) == 'r' && body.charAt(j + 2) == '>') { + //
+ } else { + noContent = false; + break; + } + } + } + } catch (IndexOutOfBoundsException e) { + + } + return noContent; + } + public boolean isModal(Job job) { Boolean isModal = (Boolean) job.getProperty(IProgressConstants.PROPERTY_IN_DIALOG); if (isModal == null) { 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 eebe5e1..fa131a4 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 @@ -129,8 +129,8 @@ public class WikiEditorPlugin extends AbstractUIPlugin { public final static String LOCAL_CSS_URL = "__local_css_url"; public final static String EXPORT_CSS_URL = "__export_css_url"; - - public final static String PREF_STRING_CONFIGURATIONS = "__configurations3"; + + public final static String PREF_STRING_CONFIGURATIONS = "__configurations4"; public final static String CONFIG_MEMENTO = "" + "" + "" @@ -140,8 +140,8 @@ public class WikiEditorPlugin extends AbstractUIPlugin { + "" + "" - + "" - + "" + + "" + + "" + "" + "