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("Refresh Wikis", 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 ArrayList list = mwc.loadXML(configuration, url, buffer.toString());
74 for (int i = 0; i < list.size(); i++) {
75 Parsed parsed = (Parsed) list.get(i);
76 wikiTitle = parsed.getTitle();
77 if (wikiTitle != null) {
78 body = parsed.getBody();
80 file = (IFile) map.get(wikiTitle);
82 updateFileContent(console, file, parsed, body, configuration, monitor);
86 if (monitor.isCanceled()) {
87 return Status.CANCEL_STATUS;
91 // The progress dialog is still open show the message
92 console.reportError();
94 // setProperty(IProgressConstants.KEEP_PROPERTY, Boolean.TRUE);
95 // setProperty(IProgressConstants.ACTION_PROPERTY, getWikisCompletedAction());
97 return Status.OK_STATUS;
98 // } catch(CoreException e) {
99 // return e.getStatus();
100 } catch (UnexpectedAnswerException e) {
102 console.println("File: " + file.getLocation().toString() + "\n==>UnexpectedAnswerException: " + e.getMessage());
104 console.println("UnexpectedAnswerException: " + e.getMessage());
106 } catch (MethodException e) {
108 console.println("File: " + file.getLocation().toString() + "\n==>HTTP-MethodException: " + e.getMessage());
110 console.println("HTTP-MethodException: " + e.getMessage());
116 // The progress dialog is still open show the message
117 console.reportError();
119 return Status.OK_STATUS;
122 public boolean isModal(Job job) {
123 Boolean isModal = (Boolean) job.getProperty(IProgressConstants.PROPERTY_IN_DIALOG);
124 if (isModal == null) {
127 return isModal.booleanValue();
130 private static void updateFileContent(ProblemConsole console, IFile file, Parsed parsed, String body, IWikipedia wp,
131 IProgressMonitor monitor) {
135 file.setContents(new ByteArrayInputStream(body.getBytes()), true, false, null);
137 file.setContents(new ByteArrayInputStream(body.getBytes(wp.getCharSet())), true, false, null);
138 file.setCharset(wp.getCharSet(), monitor);
142 file.create(new ByteArrayInputStream(body.getBytes()), false, null);
144 file.create(new ByteArrayInputStream(body.getBytes(wp.getCharSet())), false, null);
145 file.setCharset(wp.getCharSet(), monitor);
148 String srcBasePath = Util.getWikiTextsPath(file);
149 String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH);
151 String filename = Util.getXMLFileName(file, binBasePath, srcBasePath);
152 IPath path = new Path(filename);
153 IFile xmlFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
154 IContainer parent = xmlFile.getParent();
155 if (parent instanceof IFolder && (!((IFolder) parent).exists())) {
156 Util.createFolder((IFolder) parent, monitor);
159 String xmlData = parsed.getXmlData();
160 String charSet = "UTF-8";
161 StringBuffer buf = new StringBuffer();
162 int index = xmlData.indexOf("<page>");
164 console.println("File: " + xmlFile.getLocation().toString() + "\n==>Couldn't create xml file - <page> tag not found");
167 xmlData = xmlData.substring(index);
168 // buf.setLength(xmlData.length() + WikiEditorPlugin.XML_START_1.length() + WikiEditorPlugin.XML_START_2.length()
169 // + WikiEditorPlugin.XML_END.length() + charSet.length());
171 buf.append(WikiEditorPlugin.XML_START_1);
173 buf.append(WikiEditorPlugin.XML_START_2);
175 buf.append(WikiEditorPlugin.XML_END);
177 byte[] buffer = buf.toString().getBytes();
178 ByteArrayInputStream source = new ByteArrayInputStream(buffer);
179 if (!xmlFile.exists()) {
180 xmlFile.create(source, true, monitor);
182 xmlFile.setContents(source, true, true, monitor);
184 } catch (CoreException e) {
186 console.println("File: " + xmlFile.getLocation().toString() + "\n==>CoreException: " + e.getMessage());
189 } catch (UnsupportedEncodingException e) {
190 console.println("File: " + file.getLocation().toString() + "\n==>UnsupportedEncodingException: " + e.getMessage());
191 } catch (CoreException e) {
192 console.println("File: " + file.getLocation().toString() + "\n==>CoreException: " + e.getMessage());