initial contribution
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / builder / WikiBuilder.java
diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/builder/WikiBuilder.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/builder/WikiBuilder.java
new file mode 100644 (file)
index 0000000..f20fddb
--- /dev/null
@@ -0,0 +1,156 @@
+package net.sourceforge.phpeclipse.wiki.builder;
+
+import java.io.IOException;
+import java.util.Map;
+
+import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
+import net.sourceforge.phpeclipse.wiki.export.WikiExporter;
+import net.sourceforge.phpeclipse.wiki.preferences.Util;
+
+import org.eclipse.core.internal.resources.Folder;
+import org.eclipse.core.resources.ICommand;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IFolder;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.IResourceDelta;
+import org.eclipse.core.resources.IResourceDeltaVisitor;
+import org.eclipse.core.resources.IncrementalProjectBuilder;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.PlatformUI;
+
+public class WikiBuilder extends IncrementalProjectBuilder {
+
+  class WikiVisitor implements IResourceDeltaVisitor {
+    String fBinBasePath;
+
+    String fSrcBasePath;
+
+    public WikiVisitor(String binBasePath, String srcBasePath) {
+      fBinBasePath = binBasePath;
+      fSrcBasePath = srcBasePath;
+    }
+
+    public boolean visit(IResourceDelta delta) {
+      IResource resource = delta.getResource();
+      if (DEBUG) {
+        System.out.println(resource.toString());
+      }
+      if (delta.getKind() == IResourceDelta.REMOVED && resource.getType() == IResource.FILE) {
+        // remove this file from the wiki publishing directory
+        String htmlName = Util.getHTMLFileName((IFile) resource, fBinBasePath, fSrcBasePath);
+        if (htmlName != null) {
+          java.io.File file = new java.io.File(htmlName);
+          if (file.exists()) {
+            file.delete();
+          }
+        }
+        return true;
+      }
+      //only interested in changed resources at this point (not added or removed)
+      if (delta.getKind() != IResourceDelta.CHANGED)
+        return true;
+      //only interested in content changes
+      if ((delta.getFlags() & IResourceDelta.CONTENT) == 0)
+        return true;
+      if (resource.getType() == IResource.FILE) {
+        CreatePageAction.createPage((IFile) resource);
+      }
+      return true;
+    }
+  }
+
+  public static final String BUILDER_ID = "net.sourceforge.phpeclipse.wiki.wikibuilder";
+
+  private static final boolean DEBUG = false;
+
+  private IProject fProject;
+
+  private IWorkbench workbench;
+
+  public WikiBuilder() {
+    workbench = PlatformUI.getWorkbench();
+    fProject = null;
+  }
+
+  protected IProject[] build(int kind, Map args, IProgressMonitor _monitor) {
+    try {
+      fProject = getProject();
+      ICommand[] commands = fProject.getDescription().getBuildSpec();
+      boolean found = false;
+      for (int i = 0; i < commands.length; i++) {
+        if (commands[i].getBuilderName().equals(BUILDER_ID)) {
+          found = true;
+          break;
+        }
+      }
+      // fProject.hasNature()
+      if (found && fProject != null && fProject.isAccessible()) {
+
+        if (_monitor == null)
+          _monitor = new NullProgressMonitor();
+        switch (kind) {
+        case INCREMENTAL_BUILD:
+          if (DEBUG) {
+            System.out.println("INCREMENTAL_BUILD requested");
+          }
+          incrementalBuild();
+          break;
+        // we don't need auto build, because on every save we create a new *.html file ?
+        case AUTO_BUILD:
+          if (DEBUG) {
+            System.out.println("AUTO_BUILD requested");
+          }
+          incrementalBuild();
+          break;
+        case FULL_BUILD:
+          if (DEBUG) {
+            System.out.println("FULL_BUILD requested");
+          }
+          fullBuild(_monitor);
+          break;
+        default:
+        // unknown build kind requested;
+        }
+        //        fProject.refreshLocal(1,_monitor);
+      }
+    } catch (Exception x) {
+
+    }
+    return new IProject[0];
+  }
+
+  private void fullBuild(IProgressMonitor monitor) throws CoreException, IOException {
+    try {
+      WikiExporter wikiExporter = new WikiExporter();
+      String srcBasePath = Util.getProjectsWikiTextsPath(fProject);
+      String basePath = Util.getProjectsWikiOutputPath(fProject, WikiEditorPlugin.HTML_OUTPUT_PATH);
+      wikiExporter.export(fProject, basePath, srcBasePath, monitor);
+    } catch (IOException e) {
+      e.printStackTrace();
+    } catch (CoreException e) {
+      e.printStackTrace();
+    } catch (InstantiationException e) {
+      e.printStackTrace();
+    } catch (IllegalAccessException e) {
+      e.printStackTrace();
+    } catch (ClassNotFoundException e) {
+      e.printStackTrace();
+    }
+  }
+
+  private void incrementalBuild() throws CoreException, IOException {
+    IResourceDelta delta = getDelta(fProject);
+    String srcBasePath = Util.getProjectsWikiTextsPath(fProject);
+    String basePath = Util.getProjectsWikiOutputPath(fProject, WikiEditorPlugin.HTML_OUTPUT_PATH);
+    IResourceDeltaVisitor visitor = new WikiVisitor(basePath, srcBasePath);
+    if (delta != null)
+      delta.accept(visitor);
+  }
+
+}
\ No newline at end of file