initial contribution
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / builder / WikiBuilder.java
1 package net.sourceforge.phpeclipse.wiki.builder;
2
3 import java.io.IOException;
4 import java.util.Map;
5
6 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
7 import net.sourceforge.phpeclipse.wiki.export.WikiExporter;
8 import net.sourceforge.phpeclipse.wiki.preferences.Util;
9
10 import org.eclipse.core.internal.resources.Folder;
11 import org.eclipse.core.resources.ICommand;
12 import org.eclipse.core.resources.IFile;
13 import org.eclipse.core.resources.IFolder;
14 import org.eclipse.core.resources.IProject;
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.resources.IResourceDelta;
17 import org.eclipse.core.resources.IResourceDeltaVisitor;
18 import org.eclipse.core.resources.IncrementalProjectBuilder;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.NullProgressMonitor;
23 import org.eclipse.core.runtime.Path;
24 import org.eclipse.ui.IWorkbench;
25 import org.eclipse.ui.PlatformUI;
26
27 public class WikiBuilder extends IncrementalProjectBuilder {
28
29   class WikiVisitor implements IResourceDeltaVisitor {
30     String fBinBasePath;
31
32     String fSrcBasePath;
33
34     public WikiVisitor(String binBasePath, String srcBasePath) {
35       fBinBasePath = binBasePath;
36       fSrcBasePath = srcBasePath;
37     }
38
39     public boolean visit(IResourceDelta delta) {
40       IResource resource = delta.getResource();
41       if (DEBUG) {
42         System.out.println(resource.toString());
43       }
44       if (delta.getKind() == IResourceDelta.REMOVED && resource.getType() == IResource.FILE) {
45         // remove this file from the wiki publishing directory
46         String htmlName = Util.getHTMLFileName((IFile) resource, fBinBasePath, fSrcBasePath);
47         if (htmlName != null) {
48           java.io.File file = new java.io.File(htmlName);
49           if (file.exists()) {
50             file.delete();
51           }
52         }
53         return true;
54       }
55       //only interested in changed resources at this point (not added or removed)
56       if (delta.getKind() != IResourceDelta.CHANGED)
57         return true;
58       //only interested in content changes
59       if ((delta.getFlags() & IResourceDelta.CONTENT) == 0)
60         return true;
61       if (resource.getType() == IResource.FILE) {
62         CreatePageAction.createPage((IFile) resource);
63       }
64       return true;
65     }
66   }
67
68   public static final String BUILDER_ID = "net.sourceforge.phpeclipse.wiki.wikibuilder";
69
70   private static final boolean DEBUG = false;
71
72   private IProject fProject;
73
74   private IWorkbench workbench;
75
76   public WikiBuilder() {
77     workbench = PlatformUI.getWorkbench();
78     fProject = null;
79   }
80
81   protected IProject[] build(int kind, Map args, IProgressMonitor _monitor) {
82     try {
83       fProject = getProject();
84       ICommand[] commands = fProject.getDescription().getBuildSpec();
85       boolean found = false;
86       for (int i = 0; i < commands.length; i++) {
87         if (commands[i].getBuilderName().equals(BUILDER_ID)) {
88           found = true;
89           break;
90         }
91       }
92       // fProject.hasNature()
93       if (found && fProject != null && fProject.isAccessible()) {
94
95         if (_monitor == null)
96           _monitor = new NullProgressMonitor();
97         switch (kind) {
98         case INCREMENTAL_BUILD:
99           if (DEBUG) {
100             System.out.println("INCREMENTAL_BUILD requested");
101           }
102           incrementalBuild();
103           break;
104         // we don't need auto build, because on every save we create a new *.html file ?
105         case AUTO_BUILD:
106           if (DEBUG) {
107             System.out.println("AUTO_BUILD requested");
108           }
109           incrementalBuild();
110           break;
111         case FULL_BUILD:
112           if (DEBUG) {
113             System.out.println("FULL_BUILD requested");
114           }
115           fullBuild(_monitor);
116           break;
117         default:
118         // unknown build kind requested;
119         }
120         //        fProject.refreshLocal(1,_monitor);
121       }
122     } catch (Exception x) {
123
124     }
125     return new IProject[0];
126   }
127
128   private void fullBuild(IProgressMonitor monitor) throws CoreException, IOException {
129     try {
130       WikiExporter wikiExporter = new WikiExporter();
131       String srcBasePath = Util.getProjectsWikiTextsPath(fProject);
132       String basePath = Util.getProjectsWikiOutputPath(fProject, WikiEditorPlugin.HTML_OUTPUT_PATH);
133       wikiExporter.export(fProject, basePath, srcBasePath, monitor);
134     } catch (IOException e) {
135       e.printStackTrace();
136     } catch (CoreException e) {
137       e.printStackTrace();
138     } catch (InstantiationException e) {
139       e.printStackTrace();
140     } catch (IllegalAccessException e) {
141       e.printStackTrace();
142     } catch (ClassNotFoundException e) {
143       e.printStackTrace();
144     }
145   }
146
147   private void incrementalBuild() throws CoreException, IOException {
148     IResourceDelta delta = getDelta(fProject);
149     String srcBasePath = Util.getProjectsWikiTextsPath(fProject);
150     String basePath = Util.getProjectsWikiOutputPath(fProject, WikiEditorPlugin.HTML_OUTPUT_PATH);
151     IResourceDeltaVisitor visitor = new WikiVisitor(basePath, srcBasePath);
152     if (delta != null)
153       delta.accept(visitor);
154   }
155
156 }