Added the PHP wizards again
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / builder / ParserBuilder.java
1 package net.sourceforge.phpeclipse.builder;
2
3 import java.util.Map;
4
5 import net.sourceforge.phpdt.internal.ui.util.PHPFileUtil;
6 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
7 import net.sourceforge.phpeclipse.phpeditor.PHPParserAction;
8 import net.sourceforge.phpeclipse.resourcesview.PHPProject;
9
10 import org.eclipse.core.resources.IFile;
11 import org.eclipse.core.resources.IProject;
12 import org.eclipse.core.resources.IResource;
13 import org.eclipse.core.resources.IResourceDelta;
14 import org.eclipse.core.resources.IResourceVisitor;
15 import org.eclipse.core.resources.IncrementalProjectBuilder;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IConfigurationElement;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.OperationCanceledException;
20
21 /**
22  * Builder for .php files. 
23  * 
24  * 
25  * @see org.eclipse.core.resources.IncrementalProjectBuilder
26  * @see org.eclipse.core.resources.IResourceDelta
27  */
28 public class ParserBuilder extends IncrementalProjectBuilder {
29   private final static int TOTAL_WORK = 100;
30
31   /**
32    * Constructor
33    */
34   public ParserBuilder() {
35   }
36
37   /**
38    * 
39    */
40   protected IProject[] build(int kind, Map args, IProgressMonitor monitor) throws CoreException {
41     monitor.beginTask("Parsing files", TOTAL_WORK);
42
43     if (kind == IncrementalProjectBuilder.FULL_BUILD) {
44       IResourceDelta delta = getDelta(getProject());
45
46       processFull(getProject(), monitor);
47
48     } else { // INCREMENTAL_BUILD or AUTO_BUILD
49
50       IResourceDelta delta = getDelta(getProject());
51       if (delta != null) {
52         delta.accept(new ParserVisitor(getProject(), monitor));
53       }
54
55     }
56     monitor.done();
57     return null;
58   }
59
60   /**
61    * Performs a <code>FULL_BUILD</code> by visiting all nodes in the resource
62    * tree under the specified project.  
63    * 
64    * @param iProject
65    */
66   public void processFull(final IProject iProject, final IProgressMonitor monitor) {
67                 final IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(iProject);
68     // Create resource visitor logic
69     IResourceVisitor myVisitor = new IResourceVisitor() {
70       public boolean visit(IResource resource) throws CoreException {
71         if (resource.getType() == IResource.FILE) {
72           if (monitor.isCanceled()) {
73             throw new OperationCanceledException();
74           }
75           if ((resource.getFileExtension() != null) && PHPFileUtil.isPHPFile((IFile) resource)) {
76             monitor.worked(1);
77             monitor.subTask("Parsing: " + resource.getFullPath());
78             // check for parsing errors
79             PHPParserAction.parseFile((IFile) resource);
80             // update indexfile for the project:
81             PHPProject nature = (PHPProject) iProject.getNature(PHPeclipsePlugin.PHP_NATURE_ID);
82                                                 indexManager.addFile((IFile) resource);
83           }
84         }
85
86         return true;
87       }
88     };
89
90     // Process the project using the visitor just created
91     try {
92
93 //      if (iProject.hasNature(PHPeclipsePlugin.PHP_NATURE_ID)) {
94 //        thePHPProject = new PHPProject();
95 //        thePHPProject.setProject(iProject);
96 //      }
97                         indexManager.initialize();
98       iProject.accept(myVisitor);
99                         indexManager.writeFile();
100     } catch (CoreException e) {
101       e.printStackTrace();
102     }
103
104   }
105
106   /** 
107    * Sets initialization data for this builder.
108    * <p>
109    * This method is part of the <code>IExecutableExtension</code>
110    * interface.
111    * </p>
112    * <p>
113    * Subclasses are free to extend this method to pick up 
114    * initialization parameters from the plug-in plug-in manifest 
115    * (<code>plugin.xml</code>) file,
116    * but should be sure to invoke this method on their superclass.
117    * <p>
118    * For example, the following method looks for a boolean-valued 
119    * parameter named "trace":
120    * <pre>
121    *     public void setInitializationData(IConfigurationElement cfig, 
122    *             String propertyName, Object data) 
123    *                    throws CoreException {
124    *         super.setInitializationData(cfig, propertyName, data);
125    *         if (data instanceof Hashtable) { 
126    *             Hashtable args = (Hashtable) data; 
127    *             String traceValue = (String) args.get("trace"); 
128    *             TRACING = (traceValue!=null && traceValue.equals("true"));
129    *         }
130    *     }
131    * </pre>
132    * </p>
133    */
134   public void setInitializationData(IConfigurationElement config, String propertyName, Object data) throws CoreException {
135     super.setInitializationData(config, propertyName, data);
136
137   }
138
139   /**
140    * Informs this builder that it is being started by the build management
141    * infrastructure.  By the time this method is run, the builder's project
142    * is available and <code>setInitializationData</code> has been called.
143    * The default implementation should be called by all overriding methods.
144    *
145    * @see #setInitializationData
146    */
147   protected void startupOnInitialize() {
148     //   traceMsg("Parse Builder Initialize - startupOnInitialize()");
149   }
150
151   /**
152   * Write trace statements.  
153   * System.out.println with prefix tagging used for simplicity.
154   */
155   //  private void traceMsg(String msg) {
156   //    if (PHPeclipsePlugin.DEBUG | traceEnabled)
157   //      System.out.println(
158   //        buildMode
159   //          + "<"
160   //          + getProject()
161   //          + "> "
162   //          + "\t\t\t"
163   //          + buildMark
164   //          + msg);
165   //  }
166 }