Upload/Download of multiple files now possible
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / actions / mediawiki / RefreshJob.java
1 package net.sourceforge.phpeclipse.wiki.actions.mediawiki;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.UnsupportedEncodingException;
5 import java.util.ArrayList;
6 import java.util.HashMap;
7
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
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;
30
31 public class RefreshJob extends WorkspaceJob {
32   IFile[] files;
33
34   IWikipedia configuration;
35
36   String actionURL;
37
38   public RefreshJob(IWikipedia configuration, IFile[] files, String actionURL) {
39     super("Refresh Job");
40     this.files = files;
41     this.configuration = configuration;
42     this.actionURL = actionURL;
43   }
44
45   public IStatus runInWorkspace(IProgressMonitor monitor) {
46     ProblemConsole console = new ProblemConsole();
47     IFile file = null;
48     try {
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]) );
53       //      }
54       StringBuffer buffer = new StringBuffer();
55       HashMap map = new HashMap();
56       String wikiTitle;
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) {
62           buffer.append("\n");
63         }
64       }
65       MediaWikiConnector mwc = new MediaWikiConnector();
66       String url = actionURL;
67       if (url == null) {
68         url = configuration.getActionUrl() + "/" + configuration.getSpecialNs() + ":Export";
69       }
70       // get a list of Parsed elements
71       ArrayList list = mwc.loadXML(configuration, url, buffer.toString());
72       String body;
73
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();
79           if (body != null) {
80             file = (IFile) map.get(wikiTitle);
81             if (file != null) {
82               updateFileContent(console, file, parsed, body, configuration, monitor);
83             }
84           }
85         }
86         if (monitor.isCanceled()) {
87           return Status.CANCEL_STATUS;
88         }
89       }
90       if (isModal(this)) {
91         // The progress dialog is still open show the message
92         console.reportError();
93       } else {
94         //        setProperty(IProgressConstants.KEEP_PROPERTY, Boolean.TRUE);
95         //            setProperty(IProgressConstants.ACTION_PROPERTY, getWikisCompletedAction());
96       }
97       return Status.OK_STATUS;
98       //        } catch(CoreException e) {
99       //          return e.getStatus();
100     } catch (UnexpectedAnswerException e) {
101       if (file != null) {
102         console.println("File: " + file.getLocation().toString() + "\n==>UnexpectedAnswerException: " + e.getMessage());
103       } else {
104         console.println("UnexpectedAnswerException: " + e.getMessage());
105       }
106     } catch (MethodException e) {
107       if (file != null) {
108         console.println("File: " + file.getLocation().toString() + "\n==>HTTP-MethodException: " + e.getMessage());
109       } else {
110         console.println("HTTP-MethodException: " + e.getMessage());
111       }
112     } finally {
113       monitor.done();
114     }
115     if (isModal(this)) {
116       // The progress dialog is still open show the message
117       console.reportError();
118     }
119     return Status.OK_STATUS;
120   }
121
122   public boolean isModal(Job job) {
123     Boolean isModal = (Boolean) job.getProperty(IProgressConstants.PROPERTY_IN_DIALOG);
124     if (isModal == null) {
125       return false;
126     }
127     return isModal.booleanValue();
128   }
129
130   private static void updateFileContent(ProblemConsole console, IFile file, Parsed parsed, String body, IWikipedia wp,
131       IProgressMonitor monitor) {
132     try {
133       if (file.exists()) {
134         if (wp == null) {
135           file.setContents(new ByteArrayInputStream(body.getBytes()), true, false, null);
136         } else {
137           file.setContents(new ByteArrayInputStream(body.getBytes(wp.getCharSet())), true, false, null);
138           file.setCharset(wp.getCharSet(), monitor);
139         }
140       } else {
141         if (wp == null) {
142           file.create(new ByteArrayInputStream(body.getBytes()), false, null);
143         } else {
144           file.create(new ByteArrayInputStream(body.getBytes(wp.getCharSet())), false, null);
145           file.setCharset(wp.getCharSet(), monitor);
146         }
147       }
148       String srcBasePath = Util.getWikiTextsPath(file);
149       String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH);
150
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);
157       }
158       try {
159         String xmlData = parsed.getXmlData();
160         String charSet = "UTF-8";  
161         StringBuffer buf = new StringBuffer();
162         int index = xmlData.indexOf("<page>");
163         if (index<0) {
164           console.println("File: " + xmlFile.getLocation().toString() + "\n==>Couldn't create xml file - <page> tag not found");
165           return;
166         }
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());
170 //        
171         buf.append(WikiEditorPlugin.XML_START_1);
172         buf.append(charSet);
173         buf.append(WikiEditorPlugin.XML_START_2);
174         buf.append(xmlData);
175         buf.append(WikiEditorPlugin.XML_END);
176
177         byte[] buffer = buf.toString().getBytes();
178         ByteArrayInputStream source = new ByteArrayInputStream(buffer);
179         if (!xmlFile.exists()) {
180           xmlFile.create(source, true, monitor);
181         } else {
182           xmlFile.setContents(source, true, true, monitor);
183         }
184       } catch (CoreException e) {
185         if (file != null) {
186           console.println("File: " + xmlFile.getLocation().toString() + "\n==>CoreException: " + e.getMessage());
187         }
188       }
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());
193     }
194   }
195 }