1 package net.sourceforge.phpeclipse.wiki.actions.mediawiki;
3 import java.io.ByteArrayInputStream;
4 import java.io.UnsupportedEncodingException;
5 import java.util.ArrayList;
6 import java.util.HashMap;
8 import net.sourceforge.phpeclipse.wiki.actions.ProblemConsole;
9 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia;
10 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.MediaWikiConnector;
11 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.Parsed;
12 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.MethodException;
13 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.UnexpectedAnswerException;
14 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
15 import net.sourceforge.phpeclipse.wiki.preferences.Util;
17 import org.eclipse.core.resources.IContainer;
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IFolder;
20 import org.eclipse.core.resources.ResourcesPlugin;
21 import org.eclipse.core.resources.WorkspaceJob;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IPath;
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.eclipse.core.runtime.IStatus;
26 import org.eclipse.core.runtime.Path;
27 import org.eclipse.core.runtime.Status;
28 import org.eclipse.core.runtime.jobs.Job;
29 import org.eclipse.ui.progress.IProgressConstants;
31 public class RefreshJob extends WorkspaceJob {
34 IWikipedia configuration;
38 public RefreshJob(IWikipedia configuration, IFile[] files, String actionURL) {
41 this.configuration = configuration;
42 this.actionURL = actionURL;
45 public IStatus runInWorkspace(IProgressMonitor monitor) {
46 ProblemConsole console = new ProblemConsole();
49 monitor.beginTask("Download Wiki Articles: ", 100);
50 // ArrayList wikiTitles = new ArrayList();
51 // for (int i = 0; i < files.length; i++) {
52 // wikiTitles.add( Util.getReadableWikiName(files[i]) );
54 StringBuffer buffer = new StringBuffer();
55 HashMap map = new HashMap();
57 for (int i = 0; i < files.length; i++) {
58 wikiTitle = Util.getReadableWikiName(files[i]);
59 buffer.append(wikiTitle);
60 map.put(wikiTitle, files[i]);
61 if (i != files.length - 1) {
65 MediaWikiConnector mwc = new MediaWikiConnector();
66 String url = actionURL;
68 url = configuration.getActionUrl() + "/" + configuration.getSpecialNs() + ":Export";
70 // get a list of Parsed elements
71 monitor.subTask("Downloading (XML Import)");
72 ArrayList list = mwc.loadXML(configuration, url, buffer.toString());
75 for (int i = 0; i < list.size(); i++) {
76 Parsed parsed = (Parsed) list.get(i);
77 wikiTitle = parsed.getTitle();
78 if (wikiTitle != null) {
79 body = parsed.getBody();
81 file = (IFile) map.get(wikiTitle);
83 monitor.subTask("Modify file: "+file.getLocation().toString());
84 updateFileContent(console, file, parsed, body, configuration, monitor);
88 if (monitor.isCanceled()) {
89 return Status.CANCEL_STATUS;
93 // The progress dialog is still open show the message
94 console.reportError();
96 // setProperty(IProgressConstants.KEEP_PROPERTY, Boolean.TRUE);
97 // setProperty(IProgressConstants.ACTION_PROPERTY, getWikisCompletedAction());
99 return Status.OK_STATUS;
100 // } catch(CoreException e) {
101 // return e.getStatus();
102 } catch (UnexpectedAnswerException e) {
104 console.println("File: " + file.getLocation().toString() + "\n==>UnexpectedAnswerException: " + e.getMessage());
106 console.println("UnexpectedAnswerException: " + e.getMessage());
108 } catch (MethodException e) {
110 console.println("File: " + file.getLocation().toString() + "\n==>HTTP-MethodException: " + e.getMessage());
112 console.println("HTTP-MethodException: " + e.getMessage());
118 // The progress dialog is still open show the message
119 console.reportError();
121 return Status.OK_STATUS;
124 public boolean isModal(Job job) {
125 Boolean isModal = (Boolean) job.getProperty(IProgressConstants.PROPERTY_IN_DIALOG);
126 if (isModal == null) {
129 return isModal.booleanValue();
132 private static void updateFileContent(ProblemConsole console, IFile file, Parsed parsed, String body, IWikipedia wp,
133 IProgressMonitor monitor) {
137 file.setContents(new ByteArrayInputStream(body.getBytes()), true, false, null);
139 file.setContents(new ByteArrayInputStream(body.getBytes(wp.getCharSet())), true, false, null);
140 file.setCharset(wp.getCharSet(), monitor);
144 file.create(new ByteArrayInputStream(body.getBytes()), false, null);
146 file.create(new ByteArrayInputStream(body.getBytes(wp.getCharSet())), false, null);
147 file.setCharset(wp.getCharSet(), monitor);
150 String srcBasePath = Util.getWikiTextsPath(file);
151 String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH);
153 String filename = Util.getXMLFileName(file, binBasePath, srcBasePath);
154 IPath path = new Path(filename);
155 IFile xmlFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
156 IContainer parent = xmlFile.getParent();
157 if (parent instanceof IFolder && (!((IFolder) parent).exists())) {
158 Util.createFolder((IFolder) parent, monitor);
161 String xmlData = parsed.getXmlData();
162 String charSet = "UTF-8";
163 StringBuffer buf = new StringBuffer();
164 int index = xmlData.indexOf("<page>");
166 console.println("File: " + xmlFile.getLocation().toString() + "\n==>Couldn't create xml file - <page> tag not found");
169 xmlData = xmlData.substring(index);
170 // buf.setLength(xmlData.length() + WikiEditorPlugin.XML_START_1.length() + WikiEditorPlugin.XML_START_2.length()
171 // + WikiEditorPlugin.XML_END.length() + charSet.length());
173 buf.append(WikiEditorPlugin.XML_START_1);
175 buf.append(WikiEditorPlugin.XML_START_2);
177 buf.append(WikiEditorPlugin.XML_END);
179 byte[] buffer = buf.toString().getBytes();
180 ByteArrayInputStream source = new ByteArrayInputStream(buffer);
181 if (!xmlFile.exists()) {
182 xmlFile.create(source, true, monitor);
184 xmlFile.setContents(source, true, true, monitor);
186 } catch (CoreException e) {
188 console.println("File: " + xmlFile.getLocation().toString() + "\n==>CoreException: " + e.getMessage());
191 } catch (UnsupportedEncodingException e) {
192 console.println("File: " + file.getLocation().toString() + "\n==>UnsupportedEncodingException: " + e.getMessage());
193 } catch (CoreException e) {
194 console.println("File: " + file.getLocation().toString() + "\n==>CoreException: " + e.getMessage());