''Open Wiki link'' and ''Create Files for Wiki link'' every new created file creates...
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / actions / mediawiki / RefreshJob.java
index 49dec5b..a50202d 100644 (file)
@@ -13,6 +13,8 @@ import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.MethodExcept
 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.UnexpectedAnswerException;
 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
 import net.sourceforge.phpeclipse.wiki.preferences.Util;
+import net.sourceforge.phpeclipse.wiki.xml.Page;
+import net.sourceforge.phpeclipse.wiki.xml.XStreamManager;
 
 import org.eclipse.core.resources.IContainer;
 import org.eclipse.core.resources.IFile;
@@ -46,7 +48,7 @@ public class RefreshJob extends WorkspaceJob {
     ProblemConsole console = new ProblemConsole();
     IFile file = null;
     try {
-      monitor.beginTask("Refresh Wikis", 100);
+      monitor.beginTask("Download Wiki Articles: ", 100);
       //      ArrayList wikiTitles = new ArrayList();
       //      for (int i = 0; i < files.length; i++) {
       //        wikiTitles.add( Util.getReadableWikiName(files[i]) );
@@ -55,36 +57,48 @@ public class RefreshJob extends WorkspaceJob {
       HashMap map = new HashMap();
       String wikiTitle;
       for (int i = 0; i < files.length; i++) {
-        wikiTitle = Util.getReadableWikiName(files[i]);
+        file = files[i];
+        wikiTitle = createWikiTitle(file, i);
         buffer.append(wikiTitle);
-        map.put(wikiTitle, files[i]);
+        map.put(wikiTitle, file);
         if (i != files.length - 1) {
           buffer.append("\n");
         }
       }
-      MediaWikiConnector mwc = new MediaWikiConnector();
+      MediaWikiConnector mwConnector = new MediaWikiConnector();
       String url = actionURL;
       if (url == null) {
         url = configuration.getActionUrl() + "/" + configuration.getSpecialNs() + ":Export";
       }
       // get a list of Parsed elements
-      ArrayList list = mwc.loadXML(configuration, url, buffer.toString());
+      monitor.subTask("Downloading (XML Import)");
+      ArrayList list = mwConnector.loadXML(configuration, url, buffer.toString());
       String body;
 
-      for (int i = 0; i < list.size(); i++) {
-        Parsed parsed = (Parsed) list.get(i);
-        wikiTitle = parsed.getTitle();
-        if (wikiTitle != null) {
-          body = parsed.getBody();
-          if (body != null) {
-            file = (IFile) map.get(wikiTitle);
-            if (file != null) {
-              updateFileContent(console, file, parsed, body, configuration, monitor);
+      if (list.size() == 0&&files.length>0) {
+        console.println("File: " + file.getLocation().toString() + " not available on the server.");
+      } else {
+        if (list.size() < files.length) {
+          console.println("Not all requested files are available on the server.");
+        }
+        for (int i = 0; i < list.size(); i++) {
+          Parsed parsed = (Parsed) list.get(i);
+          wikiTitle = parsed.getTitle();
+          if (wikiTitle != null) {
+            body = parsed.getBody();
+            if (body != null) {
+              file = (IFile) map.get(wikiTitle);
+              if (file != null) {
+                // rearrange parsed data into a page for XStream hamdling:
+                Page page = new Page(parsed.getDateTimestamp(), wikiTitle, body);
+                monitor.subTask("Modify file: " + file.getLocation().toString());
+                updateFileContent(console, file, page, body, configuration, monitor);
+              }
             }
           }
-        }
-        if (monitor.isCanceled()) {
-          return Status.CANCEL_STATUS;
+          if (monitor.isCanceled()) {
+            return Status.CANCEL_STATUS;
+          }
         }
       }
       if (isModal(this)) {
@@ -119,6 +133,37 @@ public class RefreshJob extends WorkspaceJob {
     return Status.OK_STATUS;
   }
 
+  /**
+   * @param file
+   * @param wikiTitle
+   * @param i
+   * @return
+   */
+  private String createWikiTitle(IFile file, int i) {
+    String wikiTitle = null;
+    String srcBasePath = Util.getWikiTextsPath(file);
+    String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH);
+
+    String fileXMLName = Util.getXMLFileName(file, binBasePath, srcBasePath);
+    IPath path = new Path(fileXMLName);
+    IFile xmlFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
+    if (xmlFile.exists()) {
+      try {
+        Page page = XStreamManager.fromXML(xmlFile.getContents());
+        if (page != null) {
+          wikiTitle = page.getTitle();
+        }
+        //                timestamp = XMLReader.getDateTimestamp(xmlFile.getContents());
+      } catch (Exception e2) {
+      }
+    }
+    if (wikiTitle == null) {
+      // if no XML file exists we create the name from the filename
+      wikiTitle = Util.getReadableWikiName(files[i]);
+    }
+    return wikiTitle;
+  }
+
   public boolean isModal(Job job) {
     Boolean isModal = (Boolean) job.getProperty(IProgressConstants.PROPERTY_IN_DIALOG);
     if (isModal == null) {
@@ -127,7 +172,7 @@ public class RefreshJob extends WorkspaceJob {
     return isModal.booleanValue();
   }
 
-  private static void updateFileContent(ProblemConsole console, IFile file, Parsed parsed, String body, IWikipedia wp,
+  private static void updateFileContent(ProblemConsole console, IFile file, Page page, String body, IWikipedia wp,
       IProgressMonitor monitor) {
     try {
       if (file.exists()) {
@@ -156,25 +201,23 @@ public class RefreshJob extends WorkspaceJob {
         Util.createFolder((IFolder) parent, monitor);
       }
       try {
-        String xmlData = parsed.getXmlData();
-        String charSet = "UTF-8";  
-        StringBuffer buf = new StringBuffer();
-        int index = xmlData.indexOf("<page>");
-        if (index<0) {
-          console.println("File: " + xmlFile.getLocation().toString() + "\n==>Couldn't create xml file - <page> tag not found");
-          return;
-        }
-        xmlData = xmlData.substring(index);
-//        buf.setLength(xmlData.length() + WikiEditorPlugin.XML_START_1.length() + WikiEditorPlugin.XML_START_2.length()
-//            + WikiEditorPlugin.XML_END.length() + charSet.length());
-//        
-        buf.append(WikiEditorPlugin.XML_START_1);
-        buf.append(charSet);
-        buf.append(WikiEditorPlugin.XML_START_2);
-        buf.append(xmlData);
-        buf.append(WikiEditorPlugin.XML_END);
+        //        String xmlData = parsed.getXmlData();
+        //        String charSet = "UTF-8";
+        //        StringBuffer buf = new StringBuffer();
+        //        int index = xmlData.indexOf("<page>");
+        //        if (index<0) {
+        //          console.println("File: " + xmlFile.getLocation().toString() + "\n==>Couldn't create xml file - <page> tag not found");
+        //          return;
+        //        }
+        //        xmlData = xmlData.substring(index);
+        //        buf.append(WikiEditorPlugin.XML_START_1);
+        //        buf.append(charSet);
+        //        buf.append(WikiEditorPlugin.XML_START_2);
+        //        buf.append(xmlData);
+        //        buf.append(WikiEditorPlugin.XML_END);
 
-        byte[] buffer = buf.toString().getBytes();
+        //        byte[] buffer = buf.toString().getBytes();
+        byte[] buffer = XStreamManager.toXML(page).getBytes();
         ByteArrayInputStream source = new ByteArrayInputStream(buffer);
         if (!xmlFile.exists()) {
           xmlFile.create(source, true, monitor);
@@ -188,8 +231,8 @@ public class RefreshJob extends WorkspaceJob {
       }
     } catch (UnsupportedEncodingException e) {
       console.println("File: " + file.getLocation().toString() + "\n==>UnsupportedEncodingException: " + e.getMessage());
-    } catch (CoreException e) {
-      console.println("File: " + file.getLocation().toString() + "\n==>CoreException: " + e.getMessage());
+    } catch (Exception e) {
+      console.println("File: " + file.getLocation().toString() + "\n==>Exception: " + e.getMessage());
     }
   }
 }
\ No newline at end of file