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;
16 import net.sourceforge.phpeclipse.wiki.xml.Page;
17 import net.sourceforge.phpeclipse.wiki.xml.XStreamManager;
19 import org.eclipse.core.resources.IContainer;
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IFolder;
22 import org.eclipse.core.resources.ResourcesPlugin;
23 import org.eclipse.core.resources.WorkspaceJob;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.core.runtime.Path;
29 import org.eclipse.core.runtime.Status;
30 import org.eclipse.core.runtime.jobs.Job;
31 import org.eclipse.ui.progress.IProgressConstants;
33 public class RefreshJob extends WorkspaceJob {
36 IWikipedia configuration;
40 public RefreshJob(IWikipedia configuration, IFile[] files, String actionURL) {
43 this.configuration = configuration;
44 this.actionURL = actionURL;
47 public IStatus runInWorkspace(IProgressMonitor monitor) {
48 ProblemConsole console = new ProblemConsole();
52 int totalWork = files.length;
56 monitor.beginTask("Download Wiki Articles: ", totalWork);
57 // ArrayList wikiTitles = new ArrayList();
58 // for (int i = 0; i < files.length; i++) {
59 // wikiTitles.add( Util.getReadableWikiName(files[i]) );
61 StringBuffer buffer = new StringBuffer();
62 HashMap map = new HashMap();
63 MediaWikiConnector mwConnector = new MediaWikiConnector();
66 for (int i = 0; i < files.length; i++) {
68 wikiTitle = createWikiTitle(file, i);
69 buffer.append(wikiTitle);
71 map.put(wikiTitle, file);
72 if (i != files.length - 1) {
76 // read only 5 files at a time
77 IStatus status = readWikisFromBuffer(mwConnector, buffer, map, monitor, console);
78 if (status.equals(Status.CANCEL_STATUS)) {
79 return Status.CANCEL_STATUS;
81 buffer = new StringBuffer();
85 if (titleCounter > 0) {
86 IStatus status = readWikisFromBuffer(mwConnector, buffer, map, monitor, console);
87 if (status.equals(Status.CANCEL_STATUS)) {
88 return Status.CANCEL_STATUS;
90 buffer = new StringBuffer();
95 // The progress dialog is still open show the message
97 // setProperty(IProgressConstants.KEEP_PROPERTY, Boolean.TRUE);
98 // setProperty(IProgressConstants.ACTION_PROPERTY, getWikisCompletedAction());
100 return Status.OK_STATUS;
101 // } catch(CoreException e) {
102 // return e.getStatus();
103 } catch (UnexpectedAnswerException e) {
105 console.println("File: " + file.getLocation().toString() + "\n==>UnexpectedAnswerException: " + e.getMessage());
107 console.println("UnexpectedAnswerException: " + e.getMessage());
109 } catch (MethodException e) {
111 console.println("File: " + file.getLocation().toString() + "\n==>HTTP-MethodException: " + e.getMessage());
113 console.println("HTTP-MethodException: " + e.getMessage());
115 } catch (InterruptedException e) {
117 console.println("File: " + file.getLocation().toString() + "\n==>InterruptedException: " + e.getMessage());
119 console.println("InterruptedException: " + e.getMessage());
125 // The progress dialog is still open show the message
127 return Status.OK_STATUS;
137 * @throws UnexpectedAnswerException
138 * @throws MethodException
140 private IStatus readWikisFromBuffer(MediaWikiConnector mwConnector, StringBuffer buffer, HashMap map, IProgressMonitor monitor,
141 ProblemConsole console) throws UnexpectedAnswerException, InterruptedException, MethodException {
143 boolean showConsole = WikiEditorPlugin.getDefault().getPreferenceStore().getBoolean(WikiEditorPlugin.CONSOLE_OUTPUT);
145 String url = actionURL;
147 url = configuration.getActionUrl() + "/" + configuration.getSpecialNs() + ":Export";
149 // get a list of Parsed elements
150 monitor.subTask("Downloading (XML Import)");
152 console.println("Downloading (XML Import):\n" + buffer.toString());
154 ArrayList list = mwConnector.loadXML(configuration, url, buffer.toString());
157 for (int i = 0; i < list.size(); i++) {
158 Parsed parsed = (Parsed) list.get(i);
159 wikiTitle = parsed.getTitle();
160 if (wikiTitle != null) {
161 body = parsed.getBody();
163 file = (IFile) map.get(wikiTitle);
165 // rearrange parsed data into a page for XStream hamdling:
166 Page page = new Page(parsed.getDateTimestamp(), wikiTitle, body);
167 monitor.subTask("Modify file: " + file.getLocation().toString());
169 console.println("Update file: " + file.getLocation().toString());
171 updateFileContent(console, file, page, body, configuration, monitor);
176 if (monitor.isCanceled()) {
177 return Status.CANCEL_STATUS;
181 return Status.OK_STATUS;
190 private String createWikiTitle(IFile file, int i) {
191 String wikiTitle = null;
192 String srcBasePath = Util.getWikiTextsPath(file);
193 String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH);
195 String fileXMLName = Util.getXMLFileName(file, binBasePath, srcBasePath);
196 IPath path = new Path(fileXMLName);
197 IFile xmlFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
198 if (xmlFile.exists()) {
200 Page page = XStreamManager.fromXML(xmlFile.getContents());
202 wikiTitle = page.getTitle();
204 // timestamp = XMLReader.getDateTimestamp(xmlFile.getContents());
205 } catch (Exception e2) {
208 if (wikiTitle == null) {
209 // if no XML file exists we create the name from the filename
210 wikiTitle = Util.getReadableWikiName(files[i]);
215 public boolean isModal(Job job) {
216 Boolean isModal = (Boolean) job.getProperty(IProgressConstants.PROPERTY_IN_DIALOG);
217 if (isModal == null) {
220 return isModal.booleanValue();
223 private static void updateFileContent(ProblemConsole console, IFile file, Page page, String body, IWikipedia wp,
224 IProgressMonitor monitor) {
228 file.setContents(new ByteArrayInputStream(body.getBytes()), true, false, null);
230 file.setContents(new ByteArrayInputStream(body.getBytes(wp.getCharSet())), true, false, null);
231 file.setCharset(wp.getCharSet(), monitor);
235 file.create(new ByteArrayInputStream(body.getBytes()), false, null);
237 file.create(new ByteArrayInputStream(body.getBytes(wp.getCharSet())), false, null);
238 file.setCharset(wp.getCharSet(), monitor);
241 String srcBasePath = Util.getWikiTextsPath(file);
242 String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH);
244 String filename = Util.getXMLFileName(file, binBasePath, srcBasePath);
245 IPath path = new Path(filename);
246 IFile xmlFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
247 IContainer parent = xmlFile.getParent();
248 if (parent instanceof IFolder && (!((IFolder) parent).exists())) {
249 Util.createFolder((IFolder) parent, monitor);
252 // String xmlData = parsed.getXmlData();
253 // String charSet = "UTF-8";
254 // StringBuffer buf = new StringBuffer();
255 // int index = xmlData.indexOf("<page>");
257 // console.println("File: " + xmlFile.getLocation().toString() + "\n==>Couldn't create xml file - <page> tag not found");
260 // xmlData = xmlData.substring(index);
261 // buf.append(WikiEditorPlugin.XML_START_1);
262 // buf.append(charSet);
263 // buf.append(WikiEditorPlugin.XML_START_2);
264 // buf.append(xmlData);
265 // buf.append(WikiEditorPlugin.XML_END);
267 // byte[] buffer = buf.toString().getBytes();
268 byte[] buffer = XStreamManager.toXML(page).getBytes();
269 ByteArrayInputStream source = new ByteArrayInputStream(buffer);
270 if (!xmlFile.exists()) {
271 xmlFile.create(source, true, monitor);
273 xmlFile.setContents(source, true, true, monitor);
275 } catch (CoreException e) {
277 console.println("File: " + xmlFile.getLocation().toString() + "\n==>CoreException: " + e.getMessage());
280 } catch (UnsupportedEncodingException e) {
281 console.println("File: " + file.getLocation().toString() + "\n==>UnsupportedEncodingException: " + e.getMessage());
282 } catch (Exception e) {
283 console.println("File: " + file.getLocation().toString() + "\n==>Exception: " + e.getMessage());