321c3692f2d4896942cf5478b773bf9e40de79a4
[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("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]) );
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       monitor.subTask("Downloading (XML Import)");
72       ArrayList list = mwc.loadXML(configuration, url, buffer.toString());
73       String body;
74
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();
80           if (body != null) {
81             file = (IFile) map.get(wikiTitle);
82             if (file != null) {
83               monitor.subTask("Modify file: "+file.getLocation().toString());
84               updateFileContent(console, file, parsed, body, configuration, monitor);
85             }
86           }
87         }
88         if (monitor.isCanceled()) {
89           return Status.CANCEL_STATUS;
90         }
91       }
92       if (isModal(this)) {
93         // The progress dialog is still open show the message
94         console.reportError();
95       } else {
96         //        setProperty(IProgressConstants.KEEP_PROPERTY, Boolean.TRUE);
97         //            setProperty(IProgressConstants.ACTION_PROPERTY, getWikisCompletedAction());
98       }
99       return Status.OK_STATUS;
100       //        } catch(CoreException e) {
101       //          return e.getStatus();
102     } catch (UnexpectedAnswerException e) {
103       if (file != null) {
104         console.println("File: " + file.getLocation().toString() + "\n==>UnexpectedAnswerException: " + e.getMessage());
105       } else {
106         console.println("UnexpectedAnswerException: " + e.getMessage());
107       }
108     } catch (MethodException e) {
109       if (file != null) {
110         console.println("File: " + file.getLocation().toString() + "\n==>HTTP-MethodException: " + e.getMessage());
111       } else {
112         console.println("HTTP-MethodException: " + e.getMessage());
113       }
114     } finally {
115       monitor.done();
116     }
117     if (isModal(this)) {
118       // The progress dialog is still open show the message
119       console.reportError();
120     }
121     return Status.OK_STATUS;
122   }
123
124   public boolean isModal(Job job) {
125     Boolean isModal = (Boolean) job.getProperty(IProgressConstants.PROPERTY_IN_DIALOG);
126     if (isModal == null) {
127       return false;
128     }
129     return isModal.booleanValue();
130   }
131
132   private static void updateFileContent(ProblemConsole console, IFile file, Parsed parsed, String body, IWikipedia wp,
133       IProgressMonitor monitor) {
134     try {
135       if (file.exists()) {
136         if (wp == null) {
137           file.setContents(new ByteArrayInputStream(body.getBytes()), true, false, null);
138         } else {
139           file.setContents(new ByteArrayInputStream(body.getBytes(wp.getCharSet())), true, false, null);
140           file.setCharset(wp.getCharSet(), monitor);
141         }
142       } else {
143         if (wp == null) {
144           file.create(new ByteArrayInputStream(body.getBytes()), false, null);
145         } else {
146           file.create(new ByteArrayInputStream(body.getBytes(wp.getCharSet())), false, null);
147           file.setCharset(wp.getCharSet(), monitor);
148         }
149       }
150       String srcBasePath = Util.getWikiTextsPath(file);
151       String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH);
152
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);
159       }
160       try {
161         String xmlData = parsed.getXmlData();
162         String charSet = "UTF-8";  
163         StringBuffer buf = new StringBuffer();
164         int index = xmlData.indexOf("<page>");
165         if (index<0) {
166           console.println("File: " + xmlFile.getLocation().toString() + "\n==>Couldn't create xml file - <page> tag not found");
167           return;
168         }
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());
172 //        
173         buf.append(WikiEditorPlugin.XML_START_1);
174         buf.append(charSet);
175         buf.append(WikiEditorPlugin.XML_START_2);
176         buf.append(xmlData);
177         buf.append(WikiEditorPlugin.XML_END);
178
179         byte[] buffer = buf.toString().getBytes();
180         ByteArrayInputStream source = new ByteArrayInputStream(buffer);
181         if (!xmlFile.exists()) {
182           xmlFile.create(source, true, monitor);
183         } else {
184           xmlFile.setContents(source, true, true, monitor);
185         }
186       } catch (CoreException e) {
187         if (file != null) {
188           console.println("File: " + xmlFile.getLocation().toString() + "\n==>CoreException: " + e.getMessage());
189         }
190       }
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());
195     }
196   }
197 }